0% found this document useful (0 votes)
315 views13 pages

Building A Dynamic Multi-Agent Workflow - Harnessing AI Collaboration With LangChain & LangGraph - by Rohit Kumar - Oct, 2024 - Medium

Uploaded by

陳賢明
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
315 views13 pages

Building A Dynamic Multi-Agent Workflow - Harnessing AI Collaboration With LangChain & LangGraph - by Rohit Kumar - Oct, 2024 - Medium

Uploaded by

陳賢明
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…

Open in app

44
Search

Get unlimited access to the best of Medium for less than $1/week. Become a member

Building a Dynamic Multi-Agent Workflow:


Harnessing AI Collaboration with LangChain &
LangGraph
Rohit Kumar · Follow
4 min read · Oct 12, 2024

Listen Share More

This article utilizes LangChain and LangGraph to create a simple, multi-agent


system. The agents work together to fulfill a task. The first agent generates a
sequence of random numbers, and the second agent multiplies them by 10. Each
agent uses OpenAI’s GPT-4o API to perform these tasks.

The article follows a workflow-based architecture where agents interact based on


assigned tasks. In this post, we’ll break down each part of the script and how it
contributes to the overall flow.

https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 1/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…

Fig-1

Prerequisites
Before diving into the code, ensure you have the following installed:

Python 3.7+

OpenAI API access (you’ll need an API key)

LangChain and LangGraph libraries installed. You can install them via pip:

pip install langchain langgraph

Setting up the Environment


In the script, you must set your OpenAI API key as an environment variable. This
ensures that the agents can interact with the GPT-4 model. You can set the API key in
your terminal:

import os
os.environ["OPENAI_API_KEY"] = "your_openai_api_key"

Creating an AI Agent
https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 2/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…

The function create_agent is responsible for setting up an agent using the


ChatPromptTemplate from LangChain. Each agent is initialized with a system message
that specifies the task it will perform. Here’s how it works:

def create_agent(llm, system_message: str):


"""Create an agent."""
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful AI assistant, collaborating with other assis
"Work on the assigned task and provide useful outputs. "
"Prefix your response with FINAL ANSWER if you have completed y
" Here is the task: {system_message}",
),
MessagesPlaceholder(variable_name="messages"),
]
)
prompt = prompt.partial(system_message=system_message)
return prompt | llm

The system message explains the role of the agent and how it should behave. For
example, one agent is instructed to generate random numbers, and another is asked
to multiply them.

The Agent State


To keep track of the messages exchanged between agents, the script defines a
structure for the agent’s state using TypedDict . This helps in managing the messages
and identifying which agent sent the last message:

class AgentState(TypedDict):
messages: Sequence[BaseMessage]
sender: str

Each agent sends and receives messages, and the state keeps track of the current
agent that is responsible for the next action.

Defining the Workflow


https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 3/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…

The workflow is implemented using LangGraph’s StateGraph . Here, the agents are
added as nodes in the workflow, and the transitions between them are defined
based on the router logic.

The router function helps in controlling the flow of messages between agents:

def router(state):
messages = state["messages"]
last_message = messages[-1]
if "FINAL ANSWER" in last_message.content:
if state["sender"] == "Agent_1":
return "Agent_2"
return END
return "continue"

The workflow defines how the agents interact and the conditions under which the
control moves from one agent to another.

Adding Agents to the Workflow


Agents are added as nodes in the workflow using workflow.add_node . For instance,
Agent_1 is responsible for generating random numbers:

workflow.add_node("Agent_1", agent_1_node)
workflow.add_node("Agent_2", agent_2_node)

Conditional edges are added to move the process from one agent to another based
on the router logic.

Main Execution
The main part of the script is responsible for initializing the workflow and executing
it based on a user’s initial input. The input message instructs the system to generate
random numbers and multiply them by 10:

if __name__ == "__main__":
initial_state = {
"messages": [

https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 4/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…

HumanMessage(content="Generate 10 random numbers and multiply each


],
"sender": "Agent_1",
}

events = graph.stream(initial_state, {"recursion_limit": 150})


for event in events:
print(event)
print("----")

Here, the workflow is executed with an initial message, and the system streams the
events through each agent. The recursion limit ensures that the workflow does not
run indefinitely.

Output

Fig-2

Fig-3

Conclusion
This Python script demonstrates how to build a simple, multi-agent workflow using
LangChain and LangGraph. The process involves defining agents, setting up their
states, and routing the messages between them to achieve a specific task. This
https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 5/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…

architecture can be extended to more complex workflows with multiple agents


collaborating on various tasks.

Thanks for visiting this blog. Stay tuned for more!

Follow

Written by Rohit Kumar


15 Followers

Tech-enthusiast | IIT KGP Alumnus | Senior ML Engineer

More from Rohit Kumar

Rohit Kumar

Multithreading in Python: Running 2 Scripts in Parallel

https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 6/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…

Sometimes we need to run two Python scripts simultaneously to optimize Hybrid CPU-GPU
Applications, Parallelism in Data Preparation…

Sep 30, 2023 58 2

Rohit Kumar

The LLMs Landscape: A Comprehensive Comparison of Leading Models


from OpenAI, Anthropic, Meta…
a. Let’s take a step back to understand how LLMs emerged:

Sep 17 5

https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 7/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…

Rohit Kumar

Distributed Training(Multi-gpu or Multi-CPU) using Accelerate


What is distributed training?

May 18 5

Rohit Kumar

Fine Tuning GPT 3.5: A step by step Guide


A. Introduction

Jun 23

See all from Rohit Kumar

Recommended from Medium

https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 8/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…

Pankaj

How to Build a Local RAG Agent with LLaMA3 Using Graph-Based


Workflows
Discover how to create a local Retrieval-Augmented Generation (RAG) agent with adaptive
routing, fallback mechanisms, and self-correction…

6d ago 58

Mohammed Lubbad

Top 4 Agentic AI Architecture Design Patterns

https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 9/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…

Artificial intelligence (AI) is evolving rapidly, reshaping how we approach problem-solving and
system design. But what if we could empower…

Oct 11 67

Lists

Staff Picks
756 stories · 1421 saves

Stories to Help You Level-Up at Work


19 stories · 854 saves

Self-Improvement 101
20 stories · 2969 saves

Productivity 101
20 stories · 2516 saves

https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 10/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…

Ben Selleslagh in Vectrix

How to implement a ReAct flow using LangGraph (Studio)


This post will explain how you can implement complex agentic ReAct flows using LangGraph
and LangGraph Studio. Basic Python knowledge is…

Sep 17 55 2

Okan Yenigün in Dev Genius

LangChain in Chains #45: Web Scraping with LLMs


Integrating LLMs into Web Scraping Workflows Using LangChain

https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 11/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…

4d ago 4

Donato_TH in Donato Story

Unpacking LLMs with LangChain


Data Mastery Series — Episode 32: LangChain Website (Part 7)

Oct 27

Bella Belgarokova

Effortless AI Prompt Generation: Leveraging Langchain and Langgraph


for Optimal Performance
https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 12/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…

In this tutorial, we will build a sophisticated tool for generating prompt templates tailored for AI
language models. This project is…

Jul 24

See more recommendations

https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 13/13

You might also like