Skip to content

Commit 50ec5fe

Browse files
committed
refactor(agent): 移除 rag_summary_tools,改为路由层注入 RAG 上下文
- 从 agent 默认工具中移除 rag_summary_tools - get_agent_stream_response 新增 rag_context 参数 - 更新 system prompt:有 RAG 上下文时注入参考资料,无则使用默认 prompt - 更新 main_prompt.txt:从'优先检索'改为'基于资料回答'
1 parent 1e6a8df commit 50ec5fe

3 files changed

Lines changed: 21 additions & 35 deletions

File tree

backend/app/agent/agent.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
get_today_reviews_tool,
1919
get_user_info_tools,
2020
mark_reviewed_tool,
21-
rag_summary_tools,
2221
search_notes_tool,
2322
set_current_user_id,
2423
set_thinking_callback,
@@ -63,7 +62,6 @@ def __init__(
6362
def _get_default_tools() -> list[BaseTool]:
6463
"""获取默认工具列表"""
6564
return [
66-
rag_summary_tools,
6765
what_time_is_now,
6866
get_user_info_tools,
6967
search_notes_tool,
@@ -253,6 +251,7 @@ async def get_agent_stream_response(
253251
session_id: str,
254252
user_id: str,
255253
custom_tools: list[BaseTool] | None = None,
254+
rag_context: str = "",
256255
**kwargs
257256
) -> AsyncGenerator[str, None]:
258257
"""
@@ -261,6 +260,7 @@ async def get_agent_stream_response(
261260
:param session_id: 会话 ID
262261
:param user_id: 用户 ID
263262
:param custom_tools: 自定义工具(可选)
263+
:param rag_context: 预检索的 RAG 上下文(由路由层注入,为空则跳过)
264264
:param kwargs: 其他参数
265265
:return: 流式响应生成器
266266
"""
@@ -292,12 +292,23 @@ async def run_agent():
292292

293293
agent_executor = agent_factory.create_agent_executor(custom_tools=custom_tools, **kwargs)
294294

295+
# 根据是否有 RAG 上下文决定 system prompt 内容
296+
if rag_context:
297+
system_prompt = f"""你是用户的智能助手。
298+
299+
以下是与用户问题相关的参考资料:
300+
{rag_context}
301+
302+
请基于以上资料回答用户的问题。如果资料中没有相关信息,请如实告知。"""
303+
else:
304+
system_prompt = agent_factory.default_system_prompt
305+
295306
full_response = []
296307

297308
async for chunk in agent_executor.astream({
298309
"input": query,
299310
"chat_history": chat_history,
300-
"system_prompt": agent_factory.default_system_prompt
311+
"system_prompt": system_prompt
301312
}):
302313
if "output" in chunk:
303314
full_response.append(chunk["output"])

backend/app/agent/agent_tools.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from app.core.background_init import init_manager
88
from app.core.logger_handler import logger
99
from app.db.db_config import AsyncSessionLocal
10-
from app.rag.rag_service import RagService
1110
from app.services.review_service import review_service
1211
from app.utils.auth_utils import decode_django_jwt
1312

@@ -30,29 +29,6 @@ def get_thinking_callback_from_context():
3029
"""从上下文获取思考过程回调"""
3130
return thinking_callback_var.get()
3231

33-
@tool(description=(
34-
"用于从向量数据库里检索文档并生成摘要,返回包含文档列表和摘要的结果。"
35-
"返回格式为:'摘要: [摘要内容]\n\n检索到的文档列表:\n1. [文档1内容]\n2. [文档2内容]\n...'。"
36-
"注意:文档已经过自动重排序,无需再调用重排序工具"
37-
))
38-
async def rag_summary_tools(query: str, user_id: str = None) -> str:
39-
"""RAG 摘要工具"""
40-
effective_user_id = user_id or get_current_user_id_from_context()
41-
if not effective_user_id:
42-
return "错误: 无法确定用户身份,请提供有效的user_id"
43-
44-
thinking_callback = get_thinking_callback_from_context()
45-
result = await RagService(effective_user_id, thinking_callback=thinking_callback).get_documents_and_summary(query)
46-
documents = result.get("documents", [])
47-
summary = result.get("summary", "")
48-
49-
formatted_result = f"摘要: {summary}\n\n"
50-
formatted_result += "检索到的文档列表(已重排序):\n"
51-
for i, doc in enumerate(documents, 1):
52-
formatted_result += f"{i}. {doc}\n"
53-
54-
return formatted_result
55-
5632
@tool(description="当用户明确问自己的ID和用户名时,从JWT中获取当前用户ID和用户名,参数为完整的JWT token字符串")
5733
async def get_user_info_tools(token: str) -> str:
5834
"""获取用户信息工具"""

backend/app/prompt/main_prompt.txt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
你是一个智能笔记助手,核心能力是帮助用户管理笔记并**优先根据笔记内容和知识库内容**来回答问题。你具备RAG能力(可以检索用户上传的文档或用户写的笔记),也具备笔记管理能力(搜索、统计、创建、回顾)。
1+
你是一个智能笔记助手,核心能力是帮助用户管理笔记并回答问题。你具备笔记管理能力(搜索、统计、创建、回顾),也具备知识问答能力
22

33
你说话简单直接,不说废话。
44

55
## 核心任务
6-
1. **优先检索原则**:回答任何问题前,**必须优先**使用 `rag_summary_tools` 从知识库和笔记中检索相关内容,基于检索结果进行回答
7-
2. **笔记相关**:用户询问笔记时,使用 `search_notes_tool` 搜索笔记,或用 `get_note_stats_tool` 查看笔记统计
8-
3. **RAG检索**:需要从知识库或笔记中获取详细知识时,使用 `rag_summary_tools` 检索并生成摘要
9-
4. **每日回顾**:用户问"今天要回顾什么"时,使用 `get_today_reviews_tool`;回顾完成后用 `mark_reviewed_tool` 标记
10-
5. **创建笔记**:用户想让AI帮忙写笔记时,使用 `create_note_tool`
11-
6. **关联推荐**:用户想找相似笔记或相关资料时,使用 `get_related_notes_tool`
12-
7. **直接回答**:只有在确认知识库和笔记中完全没有相关内容时,才对常识性问题直接回答
6+
1. **基于资料回答**:如果系统提供了参考资料,请基于资料内容回答,并引用来源(如「根据你的笔记…」「根据知识库…」)。资料中没有的信息,如实告知用户
7+
2. **笔记管理**:搜索笔记用 `search_notes_tool`,查看统计用 `get_note_stats_tool`
8+
3. **每日回顾**:用户问"今天要回顾什么"时,使用 `get_today_reviews_tool`;回顾完成后用 `mark_reviewed_tool` 标记
9+
4. **创建笔记**:用户想让AI帮忙写笔记时,使用 `create_note_tool`
10+
5. **关联推荐**:用户想找相似笔记或相关资料时,使用 `get_related_notes_tool`
11+
6. **直接回答**:当没有提供参考资料时,基于你的知识直接回答用户问题
1312

1413
## 工具使用规则
1514
1. 每次调用工具前,必须输出真实的自然语言思考过程

0 commit comments

Comments
 (0)