Langgraph Finish When a Path is Repeated #21567
-
Checked other resources
Commit to Help
Example Codeprompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
MessagesPlaceholder(variable_name="messages"),
(
"system",
"Given the conversation above, who should act next?"
" Or should we FINISH? Select one of: {options}",
),
]
).partial(options=str(options), members=", ".join(members))
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", api_key="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")
supervisor_chain = (
prompt
| llm.bind_functions(functions=[function_def], function_call="route")
| JsonOutputFunctionsParser()
)
#added edged in this part
for member in members:
# We want our workers to ALWAYS "report back" to the supervisor when done
workflow.add_edge(member, "supervisor")
# The supervisor populates the "next" field in the graph state
# which routes to a node or finishes
conditional_map = {k: k for k in members}
conditional_map["FINISH"] = END #"chart"
workflow.add_conditional_edges("supervisor", lambda x: x["next"], conditional_map)
#workflow.add_edge("chart",END)
workflow.set_entry_point("supervisor")
graph = workflow.compile() DescriptionIn langgraph, I want to finish the graph traversal of agents when supervisor agent is repeated itself. for example when System Infolangchain==0.1.17 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
To prevent your langgraph traversal from looping back to previously visited agents, you can implement a mechanism to track the sequence of agents and supervisors visited. This involves maintaining a history or stack of visited nodes and checking this history before transitioning to the next node. If the next node would result in a repeated sequence that has already been encountered, you can terminate the traversal or skip to the next possible node. Here's a conceptual approach to integrate into your existing code:
By incorporating this mechanism, your traversal logic will be aware of the history of visited nodes, allowing it to make informed decisions to avoid infinite loops or redundant traversals. This approach ensures efficient and effective graph traversal by preventing the traversal from revisiting the same sequence of supervisor and agent nodes.
|
Beta Was this translation helpful? Give feedback.
-
I wrote a work around for this:
so basically i created a list for used agent memory. when an agent other than the supervisor is repeated, stream is broken to call the final answer manually |
Beta Was this translation helpful? Give feedback.
I wrote a work around for this:
so basically i created a list for used agent memory. when an agent other than the supervisor is repeated, stream is broken to call the final answer manually