admin管理员组文章数量:1315316
tools = [check_user_promotion, get_local_data, get_kpi_info, list_all_kpis]
llm_with_tools = model.bind_tools(tools)
def reasoner(state: MessagesState):
message_history = [
msg for msg in state["messages"][:-1]
if isinstance(msg, (HumanMessage, AIMessage))
and not hasattr(msg, 'tool_calls')
and not hasattr(msg, 'tool_call_id')
]
if len(message_history) >= 4:
last_human_message = state["messages"][-1]
summary_prompt = (
"Summarize the conversation between the human and AI, "
"focusing only on the key points of their dialogue. "
"Ignore any tool interactions or technical details."
)
summary_message = model.invoke(
message_history + [HumanMessage(content=summary_prompt)]
)
delete_messages = [RemoveMessage(id=m.id) for m in state["messages"]]
human_message = HumanMessage(content=last_human_message.content)
response = llm_with_tools.invoke([sys_msg, summary_message, human_message])
message_updates = [summary_message, human_message, response] + delete_messages
else:
message_updates = llm_with_tools.invoke([sys_msg] + state["messages"])
return {"messages": message_updates}
builder = StateGraph(MessagesState)
builder.add_node("reasoner", reasoner)
builder.add_node("tools", ToolNode(tools))
builder.add_edge(START, "reasoner")
builder.add_conditional_edges("reasoner", tools_condition)
builder.add_edge("tools", "reasoner")
memory = MemorySaver()
react_graph = builderpile(checkpointer=memory)
i am developing a chatbot, its working fine but i wanted to convert some of the tools into a separate agent, why? because it will have its own workflow, and more tools ,for example if the question is related to a 'kpi' the agent will process the output where there will be several tools + human in the loop, i have searched and looked into the documentation , its just that every section is explained separately and i am struggling to put it all to work within the current implementation with memory and routing.
So if anyone could guide me to the correct path please.
tools = [check_user_promotion, get_local_data, get_kpi_info, list_all_kpis]
llm_with_tools = model.bind_tools(tools)
def reasoner(state: MessagesState):
message_history = [
msg for msg in state["messages"][:-1]
if isinstance(msg, (HumanMessage, AIMessage))
and not hasattr(msg, 'tool_calls')
and not hasattr(msg, 'tool_call_id')
]
if len(message_history) >= 4:
last_human_message = state["messages"][-1]
summary_prompt = (
"Summarize the conversation between the human and AI, "
"focusing only on the key points of their dialogue. "
"Ignore any tool interactions or technical details."
)
summary_message = model.invoke(
message_history + [HumanMessage(content=summary_prompt)]
)
delete_messages = [RemoveMessage(id=m.id) for m in state["messages"]]
human_message = HumanMessage(content=last_human_message.content)
response = llm_with_tools.invoke([sys_msg, summary_message, human_message])
message_updates = [summary_message, human_message, response] + delete_messages
else:
message_updates = llm_with_tools.invoke([sys_msg] + state["messages"])
return {"messages": message_updates}
builder = StateGraph(MessagesState)
builder.add_node("reasoner", reasoner)
builder.add_node("tools", ToolNode(tools))
builder.add_edge(START, "reasoner")
builder.add_conditional_edges("reasoner", tools_condition)
builder.add_edge("tools", "reasoner")
memory = MemorySaver()
react_graph = builderpile(checkpointer=memory)
i am developing a chatbot, its working fine but i wanted to convert some of the tools into a separate agent, why? because it will have its own workflow, and more tools ,for example if the question is related to a 'kpi' the agent will process the output where there will be several tools + human in the loop, i have searched and looked into the documentation , its just that every section is explained separately and i am struggling to put it all to work within the current implementation with memory and routing.
So if anyone could guide me to the correct path please.
Share Improve this question asked Jan 30 at 8:51 sultan aljahwarysultan aljahwary 391 silver badge9 bronze badges1 Answer
Reset to default 01. Define the KPI Agent
You need to create an agent that specifically handles KPI-related questions. This agent will have its own toolset and workflow.
kpi_tools = [get_kpi_info, list_all_kpis]
# Bind the model to these tools
kpi_agent_llm = model.bind_tools(kpi_tools)
# Define the agent workflow
def kpi_agent(state: MessagesState):
kpi_memory = ConversationBufferMemory(return_messages=True)
kpi_message_history = [
msg for msg in state["messages"] if isinstance(msg, (HumanMessage, AIMessage))
]
last_human_message = state["messages"][-1]
# Process the message through the KPI agent
response = kpi_agent_llm.invoke([sys_msg] + kpi_message_history + [last_human_message])
return {"messages": state["messages"] + [response]}
# Create an agent node
kpi_agent_node = StateGraph(MessagesState)
kpi_agent_node.add_node("kpi_agent", kpi_agent)
kpi_agent_node.add_edge(START, "kpi_agent")
kpi_agent_graph = kpi_agent_nodepile()
2. Modify the Main Graph to Include Routing You need to modify your main workflow to route KPI-related queries to the kpi_agent.
def route_query(state: MessagesState):
last_human_message = state["messages"][-1].content.lower()
if "kpi" in last_human_message or "performance" in last_human_message:
return "kpi_agent"
return "reasoner"
# Modify the main graph
builder = StateGraph(MessagesState)
# Add nodes
builder.add_node("reasoner", reasoner)
builder.add_node("kpi_agent", kpi_agent)
builder.add_node("tools", ToolNode(tools))
# Routing logic
builder.add_edge(START, route_query)
builder.add_edge("kpi_agent", "reasoner") # KPI agent hands off control after processing
builder.add_edge("reasoner", "tools")
builder.add_edge("tools", "reasoner")
# Memory persistence
memory = MemorySaver()
# Compile the final graph
react_graph = builderpile(checkpointer=memory)
This should allow you to extend your chatbot with multiple specialized agents while keeping it efficient
本文标签: pythonHow do i use agents in langgraph workflowStack Overflow
版权声明:本文标题:python - How do i use agents in langgraph workflow - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741977877a2408228.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论