2025-10-31 14:16:07 +08:00
|
|
|
from langchain.agents import create_agent
|
|
|
|
|
|
2025-11-05 16:22:51 +08:00
|
|
|
from src import config
|
2025-11-06 19:47:22 +08:00
|
|
|
from src.agents.common import BaseAgent, get_buildin_tools, load_chat_model
|
2025-11-05 02:04:34 +08:00
|
|
|
from src.agents.common.middlewares import context_aware_prompt, context_based_model
|
2025-10-31 14:16:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MiniAgent(BaseAgent):
|
|
|
|
|
name = "智能体 Demo"
|
|
|
|
|
description = "一个基于内置工具的智能体示例"
|
|
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
|
|
def get_tools(self):
|
|
|
|
|
return get_buildin_tools()
|
|
|
|
|
|
|
|
|
|
async def get_graph(self, **kwargs):
|
|
|
|
|
if self.graph:
|
|
|
|
|
return self.graph
|
|
|
|
|
|
|
|
|
|
# 创建 MiniAgent
|
|
|
|
|
graph = create_agent(
|
2025-11-05 16:22:51 +08:00
|
|
|
model=load_chat_model(config.default_model), # 实际会被覆盖
|
2025-10-31 14:16:07 +08:00
|
|
|
tools=self.get_tools(),
|
|
|
|
|
middleware=[context_aware_prompt, context_based_model],
|
|
|
|
|
checkpointer=await self._get_checkpointer(),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.graph = graph
|
|
|
|
|
return graph
|