refactor: 智能体重命名并添加格式检查

This commit is contained in:
Wenjie Zhang 2025-10-31 14:16:07 +08:00
parent ad8e26c654
commit 127c73e180
13 changed files with 58 additions and 14 deletions

View File

@ -84,4 +84,4 @@ class ToolAgent(BaseAgent):
# Execute the tool node
result = await tool_node.ainvoke(state)
return cast(dict[str, list[ToolMessage]], result)
return cast(dict[str, list[ToolMessage]], result)

View File

@ -31,7 +31,7 @@ def get_approved_user_goal(
"""
# 构建详细的中断信息
interrupt_info = {
"question": f"是否批准以下操作?",
"question": "是否批准以下操作?",
"operation": operation_description,
}

View File

@ -0,0 +1,3 @@
from .graph import MiniAgent
__all__ = ["MiniAgent"]

View File

@ -0,0 +1,49 @@
from langchain.agents import create_agent
from langchain.agents.middleware import ModelRequest, ModelResponse, dynamic_prompt, wrap_model_call
from src.agents.common.base import BaseAgent
from src.agents.common.models import load_chat_model
from src.agents.common.tools import get_buildin_tools
@dynamic_prompt
def context_aware_prompt(request: ModelRequest) -> str:
runtime = request.runtime
return runtime.context.system_prompt
@wrap_model_call
async def context_based_model(request: ModelRequest, handler) -> ModelResponse:
# 从 runtime context 读取配置
model_spec = request.runtime.context.model
model = load_chat_model(model_spec)
request = request.override(model=model)
return await handler(request)
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(
model=load_chat_model("siliconflow/Qwen/Qwen3-235B-A22B-Instruct-2507"), # 实际会被覆盖
tools=self.get_tools(),
middleware=[context_aware_prompt, context_based_model],
checkpointer=await self._get_checkpointer(),
)
self.graph = graph
return graph

View File

@ -1,11 +1,9 @@
import os
from typing import Any
from langchain.tools import tool
from langchain_core.runnables import RunnableConfig
from src.agents import agent_manager
from src.agents.common.toolkits.mysql import get_mysql_tools
from src.agents.common.tools import get_buildin_tools
from src.utils import logger

View File

@ -1,4 +1,4 @@
from langgraph.constants import START, END
from langgraph.constants import END
from langgraph.graph import StateGraph
from src.agents.common.toolagent import ToolAgent
@ -18,10 +18,9 @@ def tools_branch_continue(state: State):
class ReActAgent(ToolAgent):
name = "智能体 Demo"
description = "A react agent that can answer questions and help with tasks."
name = "ReActAgent"
description = "符合 ReAct 范式的智能体,可以通过调用工具来完成复杂任务。"
# TODO:[已完成] React智能体
'''
提示词示例
你是一个智能体助手

View File

@ -1,12 +1,9 @@
import os
from typing import Any
import requests
from langchain.tools import tool
from src.agents.common.toolkits.mysql import get_mysql_tools
from src.agents.common.tools import get_buildin_tools
from src.storage.minio import upload_image_to_minio
from src.utils import logger
@tool(name_or_callable="加密计算器", description="可以对给定的2个数字选择进行加减乘除四种加密计算")

View File

@ -1,5 +1,4 @@
import textwrap
from pathlib import Path
from langchain.agents import create_agent
from langchain.agents.middleware import ModelRequest, ModelResponse, dynamic_prompt, wrap_model_call
@ -64,4 +63,4 @@ class SqlReporterAgent(BaseAgent):
self.graph = graph
logger.info("SqlReporterAgent 构建成功")
return graph
return graph

View File

@ -136,7 +136,6 @@ class KnowledgeBase(ABC):
"""
from src.utils import hashstr
from src.utils import hashstr
# 从 kwargs 中获取 is_private 配置
is_private = kwargs.get('is_private', False)