[Python] How do i use agents in langgraph workflow

Discussão em 'Python' iniciado por Stack, Fevereiro 3, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página