|
1 | 1 | """Deep Agent - 基于create_deep_agent的深度分析智能体""" |
2 | 2 |
|
3 | | -from deepagents import create_deep_agent |
4 | | -from langchain.agents.middleware import ModelRequest, dynamic_prompt |
| 3 | +from langchain.agents.middleware import ModelRequest, dynamic_prompt, SummarizationMiddleware |
| 4 | + |
| 5 | +from langchain.agents import create_agent |
| 6 | +from langchain.agents.middleware import TodoListMiddleware |
| 7 | +from langchain_anthropic.middleware import AnthropicPromptCachingMiddleware |
| 8 | + |
| 9 | +from deepagents.middleware.filesystem import FilesystemMiddleware |
| 10 | +from deepagents.middleware.patch_tool_calls import PatchToolCallsMiddleware |
| 11 | +from deepagents.middleware.subagents import SubAgentMiddleware |
5 | 12 |
|
6 | 13 | from src.agents.common import BaseAgent, load_chat_model |
7 | 14 | from src.agents.common.middlewares import context_based_model, inject_attachment_context |
|
15 | 22 | research_sub_agent = { |
16 | 23 | "name": "research-agent", |
17 | 24 | "description": ( |
18 | | - "用于研究更深入的问题。一次只给这个研究员一个主题。不要向这个研究员传递多个子问题。" |
19 | | - "相反,你应该将一个大主题分解成必要的组成部分,然后并行调用多个研究代理,每个子问题一个。" |
| 25 | + "利用搜索工具,用于研究更深入的问题。" |
20 | 26 | ), |
21 | 27 | "system_prompt": ( |
22 | 28 | "你是一位专注的研究员。你的工作是根据用户的问题进行研究。" |
@@ -81,15 +87,57 @@ async def get_graph(self, **kwargs): |
81 | 87 | # 获取上下文配置 |
82 | 88 | context = self.context_schema.from_file(module_name=self.module_name) |
83 | 89 |
|
| 90 | + model = load_chat_model(context.model) |
| 91 | + tools = await self.get_tools() |
| 92 | + |
| 93 | + if ( |
| 94 | + model.profile is not None |
| 95 | + and isinstance(model.profile, dict) |
| 96 | + and "max_input_tokens" in model.profile |
| 97 | + and isinstance(model.profile["max_input_tokens"], int) |
| 98 | + ): # 此处参考 model.dev 中的 max_input_tokens |
| 99 | + trigger = ("fraction", 0.85) |
| 100 | + keep = ("fraction", 0.10) |
| 101 | + else: |
| 102 | + trigger = ("tokens", 110000) |
| 103 | + keep = ("messages", 10) |
| 104 | + |
84 | 105 | # 使用 create_deep_agent 创建深度智能体 |
85 | | - graph = create_deep_agent( |
86 | | - model=load_chat_model(context.model), |
87 | | - tools=await self.get_tools(), |
88 | | - subagents=[critique_sub_agent, research_sub_agent], |
| 106 | + graph = create_agent( |
| 107 | + model=model, |
| 108 | + tools=tools, |
89 | 109 | middleware=[ |
90 | 110 | context_based_model, # 动态模型选择 |
91 | 111 | context_aware_prompt, # 动态系统提示词 |
92 | 112 | inject_attachment_context, # 附件上下文注入 |
| 113 | + TodoListMiddleware(), |
| 114 | + FilesystemMiddleware(), |
| 115 | + SubAgentMiddleware( |
| 116 | + default_model=load_chat_model(context.model), |
| 117 | + default_tools=tools, |
| 118 | + subagents=[critique_sub_agent, research_sub_agent], |
| 119 | + default_middleware=[ |
| 120 | + TodoListMiddleware(), |
| 121 | + FilesystemMiddleware(), |
| 122 | + SummarizationMiddleware( |
| 123 | + model=model, |
| 124 | + trigger=trigger, |
| 125 | + keep=keep, |
| 126 | + trim_tokens_to_summarize=None, |
| 127 | + ), |
| 128 | + AnthropicPromptCachingMiddleware(unsupported_model_behavior="ignore"), |
| 129 | + PatchToolCallsMiddleware(), |
| 130 | + ], |
| 131 | + general_purpose_agent=True, |
| 132 | + ), |
| 133 | + SummarizationMiddleware( |
| 134 | + model=model, |
| 135 | + trigger=trigger, |
| 136 | + keep=keep, |
| 137 | + trim_tokens_to_summarize=None, |
| 138 | + ), |
| 139 | + AnthropicPromptCachingMiddleware(unsupported_model_behavior="ignore"), |
| 140 | + PatchToolCallsMiddleware(), |
93 | 141 | ], |
94 | 142 | checkpointer=await self._get_checkpointer(), |
95 | 143 | ) |
|
0 commit comments