Langchain

Building a Multi-Agent System with LangChain

This guide walks you through creating a simple multi-agent architecture in LangChain. You’ll see how to:

  • Define individual agents (chains) with distinct roles
  • Use tools and memory in each agent
  • Orchestrate them via a router/orchestrator chain

1. Install and Import Dependencies

1
pip install langchain openai
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from langchain import LLMChain, OpenAI, PromptTemplate
from langchain.agents import AgentExecutor, Tool, ZeroShotAgent
from langchain.chains.router import RouterChain
from langchain.schema import AgentAction, AgentFinish
```

---

## 2. Define Your Agents

### 2.1 Research Agent
Fetches facts or external data.

```python
# A placeholder "web_search" tool
def web_search(query: str) -> str:
return f"Results for '{query}'..."

search_tool = Tool(
name="WebSearch",
func=web_search,
description="Use this to look up factual data."
)

research_prompt = PromptTemplate(
input_variables=["topic"],
template="Research key facts about: {topic}"
)

research_chain = LLMChain(
llm=OpenAI(temperature=0.2),
prompt=research_prompt
)

research_agent = ZeroShotAgent(
tools=[search_tool],
llm_chain=research_chain,
verbose=True,
)
```

### 2.2 Summarization Agent
Condenses raw text into crisp summaries.

```python
summarize_prompt = PromptTemplate(
input_variables=["text"],
template="Summarize the following text in 3 bullet points:\n\n{text}"
)

summarizer_chain = LLMChain(
llm=OpenAI(temperature=0.3),
prompt=summarize_prompt
)

summarizer_agent = ZeroShotAgent(
tools=[],
llm_chain=summarizer_chain,
verbose=True,
)
```

### 2.3 Creative Rewrite Agent
Rewrites summaries in an engaging style.

```python
rewrite_prompt = PromptTemplate(
input_variables=["summary"],
template="Rewrite the following in a lively tone:\n\n{summary}"
)

rewriter_chain = LLMChain(
llm=OpenAI(temperature=0.8),
prompt=rewrite_prompt
)

rewriter_agent = ZeroShotAgent(
tools=[],
llm_chain=rewriter_chain,
verbose=True,
)
```

---

## 3. Orchestrating with a RouterChain

Use a router to direct the user’s query to the right agent in sequence:

```python
# Map labels to executors
executors = {
"research": AgentExecutor.from_agent_and_tools(research_agent, [search_tool], verbose=True),
"summarize": AgentExecutor.from_agent_and_tools(summarizer_agent, [], verbose=True),
"rewrite": AgentExecutor.from_agent_and_tools(rewriter_agent, [], verbose=True),
}

router_prompt = PromptTemplate(
input_variables=["input", "agent_scratchpad"],
template=(
"You have three agents:\n"
" - research: fetch facts\n"
" - summarize: condense text\n"
" - rewrite: make it lively\n\n"
"Decide which agent to call next for the user request:\n"
"{input}\n\n"
"Use this format:\n"
"Agent: <agent_name>\n"
"Input: <what to send>\n"
"{agent_scratchpad}"
)
)

router_chain = RouterChain.from_chains(
chains=[research_chain, summarizer_chain, rewriter_chain],
router_prompt=router_prompt,
default_chain=research_chain,
verbose=True
)

4. Putting It All Together

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def run_multi_agent_system(user_input: str):
# Step 1: Router decides “research” first
agent_name, call_input = router_chain.predict_and_parse(input=user_input)

# Step 2: Execute the chosen agent
result1 = executors[agent_name].run(call_input)

# Step 3: Feed result back into router until “rewrite”
history = [(agent_name, call_input, result1)]
while agent_name != "rewrite":
agent_name, call_input = router_chain.predict_and_parse(
input=user_input, agent_scratchpad=str(history)
)
result = executors[agent_name].run(call_input)
history.append((agent_name, call_input, result))

return history[-1][2] # final rewritten text

# Example
final_output = run_multi_agent_system("Tell me about the Great Wall of China")
print(final_output)

Next Steps & Advanced Ideas

  • Introduce memory per agent to track past queries.
  • Swap in a PlannerChain for high-level step planning.
  • Use Toolkits: DB lookup, file I/O, math solver, etc.
  • Scale with async execution and long-term vector DBs.

This setup gives you a flexible foundation—tailor it by adding more agents, richer tools, or dynamic routing logic.

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2023-2025 John Doe
  • Visitors: | Views:

请我喝杯茶吧~

支付宝
微信