2025-11-05 02:04:34 +08:00
|
|
|
from langchain.agents import create_agent
|
2025-12-17 22:27:46 +08:00
|
|
|
from langchain.agents.middleware import ModelRetryMiddleware
|
2025-03-24 19:07:51 +08:00
|
|
|
|
2025-11-05 02:04:34 +08:00
|
|
|
from src.agents.common import BaseAgent, load_chat_model
|
2025-11-08 10:51:30 +08:00
|
|
|
from src.agents.common.middlewares import (
|
|
|
|
|
inject_attachment_context,
|
|
|
|
|
)
|
2026-01-15 16:04:19 +08:00
|
|
|
from src.agents.common.tools import get_tools_from_context
|
2025-08-31 00:34:26 +08:00
|
|
|
|
|
|
|
|
|
2025-03-24 19:07:51 +08:00
|
|
|
class ChatbotAgent(BaseAgent):
|
2025-08-11 21:29:33 +08:00
|
|
|
name = "智能体助手"
|
2026-01-15 16:04:19 +08:00
|
|
|
description = "基础的对话机器人,可以回答问题,可在配置中启用需要的工具。"
|
2025-12-30 19:19:49 +08:00
|
|
|
capabilities = ["file_upload"] # 支持文件上传功能
|
2025-03-24 23:00:14 +08:00
|
|
|
|
2025-03-28 11:40:46 +08:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
super().__init__(**kwargs)
|
2025-03-24 23:00:14 +08:00
|
|
|
|
2025-08-31 00:34:26 +08:00
|
|
|
async def get_graph(self, **kwargs):
|
2025-03-24 23:00:14 +08:00
|
|
|
"""构建图"""
|
2025-12-30 19:01:45 +08:00
|
|
|
# 获取上下文配置
|
|
|
|
|
context = self.context_schema.from_file(module_name=self.module_name)
|
2025-11-05 02:04:34 +08:00
|
|
|
|
2025-12-30 19:01:45 +08:00
|
|
|
# 使用 create_agent 创建智能体
|
2025-11-05 02:04:34 +08:00
|
|
|
graph = create_agent(
|
2025-12-30 19:01:45 +08:00
|
|
|
model=load_chat_model(context.model), # 使用 context 中的模型配置
|
2026-01-15 16:04:19 +08:00
|
|
|
tools=await get_tools_from_context(context),
|
2025-12-30 19:19:49 +08:00
|
|
|
system_prompt=context.system_prompt,
|
2025-11-05 02:04:34 +08:00
|
|
|
middleware=[
|
2025-12-30 19:01:45 +08:00
|
|
|
inject_attachment_context, # 附件上下文注入
|
2025-12-17 22:27:46 +08:00
|
|
|
ModelRetryMiddleware(), # 模型重试中间件
|
2025-11-05 02:04:34 +08:00
|
|
|
],
|
|
|
|
|
checkpointer=await self._get_checkpointer(),
|
2025-03-25 05:40:07 +08:00
|
|
|
)
|
|
|
|
|
|
2025-10-11 21:10:39 +08:00
|
|
|
return graph
|
2025-03-24 23:00:14 +08:00
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
2025-03-24 23:00:14 +08:00
|
|
|
def main():
|
2025-10-12 16:04:46 +08:00
|
|
|
pass
|
2025-03-24 19:07:51 +08:00
|
|
|
|
2025-10-12 23:45:17 +08:00
|
|
|
|
2025-03-24 23:00:14 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|
2025-12-30 19:19:49 +08:00
|
|
|
# asyncio.run(main())
|