sultan aljahwary
sultan aljahwary

Reputation: 39

How do i use agents in langgraph workflow

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 = builder.compile(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.

Upvotes: -1

Views: 135

Answers (1)

Awais khan
Awais khan

Reputation: 330

1. 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_node.compile()

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 = builder.compile(checkpointer=memory)

This should allow you to extend your chatbot with multiple specialized agents while keeping it efficient

Upvotes: 0

Related Questions