Skip to content

Commit f8aef0e

Browse files
authored
feat: refine mcp prompt (#1426)
* feat: refine mcp prompt * feat: refine mcp prompt
1 parent a797253 commit f8aef0e

File tree

6 files changed

+34
-19
lines changed

6 files changed

+34
-19
lines changed

aperag/mcp/server.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ async def search_collection(
7878
topk: int = 5,
7979
query_keywords: list[str] = None,
8080
) -> Dict[str, Any]:
81-
"""Search for knowledge in a specific collection using vector, full-text, graph, and/or summary search.
81+
"""Search for knowledge in a persistent collection/knowledge base using vector, full-text, graph, and/or summary search.
82+
83+
PRIMARY USE CASE: This is the main tool for searching permanent knowledge repositories.
84+
Use this for general Q&A, knowledge retrieval, and accessing organized knowledge collections.
85+
86+
For temporary files uploaded in a chat session, use search_chat_files instead.
8287
8388
Args:
8489
collection_id: The ID of the collection to search in
@@ -215,7 +220,18 @@ async def search_chat_files(
215220
rerank: bool = True,
216221
topk: int = 5,
217222
) -> Dict[str, Any]:
218-
"""Search for knowledge in chat files using vector, full-text, graph, and/or summary search.
223+
"""Search ONLY within files temporarily uploaded by the user in THIS specific chat session.
224+
225+
IMPORTANT - When to Use This Tool:
226+
- ONLY when searching files that the user explicitly uploaded in THIS chat conversation
227+
- For temporary, session-specific document analysis (e.g., "analyze this PDF I just uploaded")
228+
- When the user references documents they shared in the current chat
229+
230+
DO NOT Use This Tool For:
231+
- Searching general knowledge bases or collections (use search_collection instead)
232+
- Accessing persistent/permanent knowledge repositories
233+
- General Q&A that doesn't involve chat-uploaded files
234+
- When no files have been uploaded in the current chat
219235
220236
Args:
221237
chat_id: The ID of the chat to search files in
@@ -230,10 +246,9 @@ async def search_chat_files(
230246
231247
Note:
232248
Uses SearchResult view model for type-safe response parsing and validation.
233-
This tool searches within files uploaded to the specified chat.
234249
235-
The search behavior is similar to search_collection, but limited to documents
236-
uploaded in the specified chat context.
250+
SCOPE: This tool ONLY searches temporary files uploaded in the current chat.
251+
It does NOT search permanent knowledge collections.
237252
238253
Return format follows the same structure as search_collection:
239254
- rank: Result rank

aperag/service/prompt_template_service.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
5151
### Knowledge Management
5252
- `list_collections()`: Discover available knowledge sources
53-
- `search_collection(collection_id, query, ...)`: Hybrid search within collections
54-
- `search_chat_files(chat_id, query, ...)`: Search files uploaded in specific chat sessions
53+
- `search_collection(collection_id, query, ...)`: **[PRIMARY TOOL]** Hybrid search within persistent knowledge collections/repositories
54+
- `search_chat_files(chat_id, query, ...)`: **[CHAT-ONLY]** Search ONLY temporary files uploaded by user in THIS chat session (NOT for general knowledge bases)
5555
- `create_diagram(content)`: Create Mermaid diagrams for knowledge graph visualization
5656
5757
### Web Intelligence
@@ -151,8 +151,8 @@
151151
152152
### 知识管理
153153
- `list_collections()`:发现可用知识源
154-
- `search_collection(collection_id, query, ...)`:知识库内混合搜索
155-
- `search_chat_files(chat_id, query, ...)`:搜索特定聊天会话中上传的文件
154+
- `search_collection(collection_id, query, ...)`: **[主要工具]** 在持久化知识库/仓库中进行混合搜索
155+
- `search_chat_files(chat_id, query, ...)`: **[仅限聊天]** 仅搜索用户在本次聊天会话中临时上传的文件(不用于常规知识库)
156156
- `create_diagram(content)`:创建Mermaid图表进行知识图谱可视化
157157
158158
### 网络智能
@@ -235,7 +235,7 @@
235235
{% set web_status = "enabled" if web_search_enabled else "disabled" %}
236236
{% set web_instruction = "Use web search strategically for current information, verification, or gap-filling" if web_search_enabled else "Rely entirely on knowledge collections; inform user if web search would be helpful" %}
237237
{% set chat_context = "Chat ID: " + chat_id if chat_id else "No chat files" %}
238-
{% set chat_instruction = "Use search_chat_files tool to search files uploaded in this chat" if chat_id else "" %}
238+
{% set chat_instruction = "ONLY use search_chat_files tool when searching files that user explicitly uploaded in THIS chat. Do NOT use it for general knowledge base queries." if chat_id else "" %}
239239
240240
**User Query**: {{ query }}
241241
@@ -272,7 +272,7 @@
272272
{% set web_status = "已启用" if web_search_enabled else "已禁用" %}
273273
{% set web_instruction = "战略性地使用网络搜索获取当前信息、验证或填补空白" if web_search_enabled else "完全依赖知识库;如果网络搜索有帮助请告知用户" %}
274274
{% set chat_context = "聊天ID: " + chat_id if chat_id else "无" %}
275-
{% set chat_instruction = "可使用 search_chat_files 工具搜索此聊天中上传的文件" if chat_id else "" %}
275+
{% set chat_instruction = "仅在搜索用户明确在本次聊天中上传的文件时使用 search_chat_files 工具。不要将其用于常规知识库查询。" if chat_id else "" %}
276276
277277
**用户查询**: {{ query }}
278278

web/src/lib/prompt-template/query-prompt.en-US.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% set collection_list = [] %} {% if collections %} {% for c in collections %} {% set title = c.title or "Collection " + c.id %} {% set _ = collection_list.append("- " + title + " (ID: " + c.id + ")") %} {% endfor %} {% set collection_context = collection_list | join("\n") %} {% set collection_instruction = "PRIORITY: Search these user-specified collections first" %} {% else %} {% set collection_context = "None specified by user" %} {% set collection_instruction = "discover and select relevant collections automatically" %} {% endif %} {% set web_status = "enabled" if web_search_enabled else "disabled" %} {% set web_instruction = "Use web search strategically for current information, verification, or gap-filling" if web_search_enabled else "Rely entirely on knowledge collections; inform user if web search would be helpful" %} {% set chat_context = "Chat ID: " + chat_id if chat_id else "No chat files" %} {% set chat_instruction = "Use search_chat_files tool to search files uploaded in this chat" if chat_id else "" %}
1+
{% set collection_list = [] %} {% if collections %} {% for c in collections %} {% set title = c.title or "Collection " + c.id %} {% set _ = collection_list.append("- " + title + " (ID: " + c.id + ")") %} {% endfor %} {% set collection_context = collection_list | join("\n") %} {% set collection_instruction = "PRIORITY: Search these user-specified collections first" %} {% else %} {% set collection_context = "None specified by user" %} {% set collection_instruction = "discover and select relevant collections automatically" %} {% endif %} {% set web_status = "enabled" if web_search_enabled else "disabled" %} {% set web_instruction = "Use web search strategically for current information, verification, or gap-filling" if web_search_enabled else "Rely entirely on knowledge collections; inform user if web search would be helpful" %} {% set chat_context = "Chat ID: " + chat_id if chat_id else "No chat files" %} {% set chat_instruction = "ONLY use search_chat_files tool when searching files that user explicitly uploaded in THIS chat. Do NOT use it for general knowledge base queries." if chat_id else "" %}
22

33
**User Query**: {{ query }}
44

@@ -12,7 +12,7 @@
1212

1313
1. LANGUAGE PRIORITY: Respond in the language the user is asking in, not the language of the content
1414
2. If user specified collections (@mentions), search those first (REQUIRED)
15-
3. If chat files are available, search files uploaded in this chat when relevant
15+
3. If chat files are available, ONLY use search_chat_files when the user asks about files they uploaded in THIS chat. Use search_collection for general knowledge queries.
1616
4. Use appropriate search keywords in multiple languages when beneficial
1717
5. Assess result quality and decide if additional collections are needed
1818
6. Use web search strategically if enabled and relevant

web/src/lib/prompt-template/query-prompt.zh-CN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% set collection_list = [] %} {% if collections %} {% for c in collections %} {% set title = c.title or "知识库" + c.id %} {% set _ = collection_list.append("- " + title + " (ID: " + c.id + ")") %} {% endfor %} {% set collection_context = collection_list | join("\n") %} {% set collection_instruction = "优先级:首先搜索这些用户指定的知识库" %} {% else %} {% set collection_context = "用户未指定" %} {% set collection_instruction = "自动发现并选择相关的知识库" %} {% endif %} {% set web_status = "已启用" if web_search_enabled else "已禁用" %} {% set web_instruction = "战略性地使用网络搜索获取当前信息、验证或填补空白" if web_search_enabled else "完全依赖知识库;如果网络搜索有帮助请告知用户" %} {% set chat_context = "聊天ID: " + chat_id if chat_id else "无" %} {% set chat_instruction = "可使用 search_chat_files 工具搜索此聊天中上传的文件" if chat_id else "" %}
1+
{% set collection_list = [] %} {% if collections %} {% for c in collections %} {% set title = c.title or "知识库" + c.id %} {% set _ = collection_list.append("- " + title + " (ID: " + c.id + ")") %} {% endfor %} {% set collection_context = collection_list | join("\n") %} {% set collection_instruction = "优先级:首先搜索这些用户指定的知识库" %} {% else %} {% set collection_context = "用户未指定" %} {% set collection_instruction = "自动发现并选择相关的知识库" %} {% endif %} {% set web_status = "已启用" if web_search_enabled else "已禁用" %} {% set web_instruction = "战略性地使用网络搜索获取当前信息、验证或填补空白" if web_search_enabled else "完全依赖知识库;如果网络搜索有帮助请告知用户" %} {% set chat_context = "聊天ID: " + chat_id if chat_id else "无" %} {% set chat_instruction = "仅在搜索用户明确在本次聊天中上传的文件时使用 search_chat_files 工具。不要将其用于常规知识库查询。" if chat_id else "" %}
22

33
**用户查询**: {{ query }}
44

@@ -12,7 +12,7 @@
1212

1313
1. 语言优先级: 使用用户提问的语言回应,而不是内容的语言
1414
2. 如果用户指定了知识库(@提及),首先搜索这些(必需)
15-
3. 如果有聊天文件,可以搜索聊天中上传的文件
15+
3. 如果有聊天文件,仅在用户询问他们在本次聊天中上传的文件时使用 search_chat_files。对于常规知识查询使用 search_collection。
1616
4. 在有益时使用多种语言的适当搜索关键词
1717
5. 评估结果质量并决定是否需要额外的知识库
1818
6. 如果启用且相关,战略性地使用网络搜索

web/src/lib/prompt-template/system-prompt.en-US.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ You are an advanced AI research assistant powered by ApeRAG's hybrid search capa
3131
### Knowledge Management
3232

3333
- `list_collections()`: Discover available knowledge sources
34-
- `search_collection(collection_id, query, ...)`: Hybrid search within collections
35-
- `search_chat_files(chat_id, query, ...)`: Search files uploaded in specific chat sessions
34+
- `search_collection(collection_id, query, ...)`: **[PRIMARY TOOL]** Hybrid search within persistent knowledge collections/repositories
35+
- `search_chat_files(chat_id, query, ...)`: **[CHAT-ONLY]** Search ONLY temporary files uploaded by user in THIS chat session (NOT for general knowledge bases)
3636
- `create_diagram(content)`: Create Mermaid diagrams for knowledge graph visualization
3737

3838
### Web Intelligence

web/src/lib/prompt-template/system-prompt.zh-CN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
### 知识管理
3232

3333
- `list_collections()`:发现可用知识源
34-
- `search_collection(collection_id, query, ...)`:知识库内混合搜索
35-
- `search_chat_files(chat_id, query, ...)`:搜索特定聊天会话中上传的文件
34+
- `search_collection(collection_id, query, ...)`: **[主要工具]** 在持久化知识库/仓库中进行混合搜索
35+
- `search_chat_files(chat_id, query, ...)`: **[仅限聊天]** 仅搜索用户在本次聊天会话中临时上传的文件(不用于常规知识库)
3636
- `create_diagram(content)`:创建Mermaid图表进行知识图谱可视化
3737

3838
### 网络智能

0 commit comments

Comments
 (0)