Creating a multi-agent application – Part 5 (final)

In part 4 of this series we created our final two agents. In this final part of the series we’ll review the workflow that we create with the StateGraph class of LangGraph.

In the following code we pass the ResearchState object to StateGraph and create a node for each agent and edges that represent transitions between the nodes. There is one conditional edge: if the reviewer rejects the draft, it goes back to the author

from langgraph.graph import StateGraph, END
workflow = StateGraph(ResearchState)

workflow.add_node("blogger", blogger_node)
workflow.add_node("researcher", research_node)
workflow.add_node("author", author_node)
workflow.add_node("reviewer", reviewer_node)

workflow.set_entry_point("blogger")

workflow.add_edge("blogger", "researcher")
workflow.add_edge("researcher", "author")
workflow.add_edge("author", "reviewer")

workflow.add_conditional_edges(
    "reviewer",
    lambda state: "author" if state.get("review_result") == "rejected" else "END",
    {
        "author": "author",
        "END": END
    }
)

# Compile the graph
app = workflow.compile()

The conditional edge says, “Start with reviewer. Now examine the state. If the review_result is rejected, go to the author; otherwise, go to END. Finally, the dictionary identifies what each node is.

That’s it. All that’s left is to take it out for a spin.

Unknown's avatar

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. Liberty is a Senior AI Engineer at the University of Pittsburgh Medical Center, and was a Team Lead and Senior Software Engineer for various corporations, a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a 21 year Microsoft MVP.
This entry was posted in AI, Programming, Python. Bookmark the permalink.