[Python] Langraph - tools_condition - conditional edge

Discussão em 'Python' iniciado por Stack, Setembro 24, 2024.

  1. Stack

    Stack Membro Participativo

    I am new to langgraph (python) and trying to go through some examples found in Langgraph pages.

    My code looks like below:

    class State(TypedDict):
    summary: str
    messages: list

    def summarize_conversation(state: State):
    # First, we summarize the conversation
    summary = state.get("summary", "")
    if summary:
    # If a summary already exists, we use a different system prompt
    # to summarize it than if one didn't
    summary_message = (
    f"This is summary of the conversation to date: {summary}\n\n"
    "Extend the summary by taking into account the new messages above:"
    )
    else:
    summary_message = "Create a summary of the conversation above:"

    messages = state["messages"] + [HumanMessage(content=summary_message)]
    response = model.invoke(messages)
    return {"summary": response.content, "messages": messages}

    graph_builder = StateGraph(State)
    graph_builder.add_node("tools", ToolNode(tool_set))
    graph_builder.add_node("chatbot", lambda state: {"messages":assistant_runnable.invoke(state)})
    graph_builder.add_node("summarize_conversation", summarize_conversation)
    graph_builder.add_edge("tools", "summarize_conversation")
    graph_builder.add_edge("summarize_conversation", "chatbot")
    graph_builder.add_conditional_edges(
    "chatbot", tools_condition
    )
    graph_builder.set_entry_point("chatbot")
    graph = graph_builder.compile()


    The trouble is I get this extra conditional edge from Chatbot node to "summarise_conversation" node. Why is this and how do i remove this ?

    [​IMG]

    PS : For brevity I have not included the full code.

    Continue reading...

Compartilhe esta Página