feat: 为agent封装为tool调用添加了runtimeconfig,可以传递记忆
This commit is contained in:
parent
68d654458c
commit
cb516b4986
@ -9,9 +9,11 @@ from typing import Annotated
|
||||
from langchain.messages import AnyMessage
|
||||
from langgraph.graph import add_messages
|
||||
|
||||
from src.agents.common.state import BaseState
|
||||
|
||||
|
||||
@dataclass
|
||||
class State:
|
||||
class State(BaseState):
|
||||
"""Defines the input state for the agent, representing a narrower interface to the outside world.
|
||||
|
||||
This class is used to define the initial state and structure of incoming data.
|
||||
|
||||
@ -2,6 +2,7 @@ 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
|
||||
@ -11,19 +12,25 @@ from src.utils import logger
|
||||
# TODO[修改建议]:能不能通过前端直接指定子智能体?
|
||||
# 调用子智能体后的日志是输出到tool_calls的
|
||||
@tool(name_or_callable="对话聊天智能体", description="调用指定智能体进行对话聊天的功能")
|
||||
async def call_chatbot(query: str) -> str:
|
||||
async def call_chatbot(query: str, config: RunnableConfig) -> str:
|
||||
"""
|
||||
调用指定chatbot智能体进行对话聊天的功能
|
||||
|
||||
Args:
|
||||
query: 根据需要构造的提问
|
||||
config: LangGraph运行时配置(自动注入)
|
||||
Returns:
|
||||
str: 最终的回答结果
|
||||
"""
|
||||
try:
|
||||
input = [{"role": "user", "content": query}]
|
||||
chatbot = agent_manager.get_agent("ChatbotAgent")
|
||||
message = await chatbot.invoke_messages(input)
|
||||
configurable = config.get("configurable",{})
|
||||
input_context = {
|
||||
"thread_id":configurable.get("thread_id"),
|
||||
"user_id": configurable.get("user_id"),
|
||||
}
|
||||
message = await chatbot.invoke_messages(input,input_context=input_context)
|
||||
# 直接获取最后一个消息的内容
|
||||
final_answer = message.get('messages', [])[-1].content
|
||||
logger.info(f"ChatbotAgent: {final_answer}")
|
||||
@ -33,19 +40,25 @@ async def call_chatbot(query: str) -> str:
|
||||
raise
|
||||
|
||||
@tool(name_or_callable="加密计算智能体", description="调用指定智能体进行加密计算的功能")
|
||||
async def call_react_agent(query: str) -> str:
|
||||
async def call_react_agent(query: str, config: RunnableConfig) -> str:
|
||||
"""
|
||||
调用指定智能体进行加密计算的功能
|
||||
|
||||
Args:
|
||||
query: 根据需要构造的提问
|
||||
config: LangGraph运行时配置(自动注入)
|
||||
Returns:
|
||||
str: 最终的回答结果
|
||||
"""
|
||||
try:
|
||||
input = [{"role": "user", "content": query}]
|
||||
chatbot = agent_manager.get_agent("ReActAgent")
|
||||
message = await chatbot.invoke_messages(input)
|
||||
configurable = config.get("configurable",{})
|
||||
input_context = {
|
||||
"thread_id":configurable.get("thread_id"),
|
||||
"user_id": configurable.get("user_id"),
|
||||
}
|
||||
message = await chatbot.invoke_messages(input,input_context=input_context)
|
||||
# 直接获取最后一个消息的内容
|
||||
final_answer = message.get('messages', [])[-1].content
|
||||
logger.info(f"ReActAgent: {final_answer}")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user