引言
在过去一年中,LangChain 成为了市场上备受欢迎的 AI 框架之一。这个轻量级的协调工具使开发者能够轻松打造出依托大型语言模型(LLMs)的应用程序及其所有相关组件,如向量数据库(vectorDB)、内存、提示语、工具和代理。LangChain 的亮点在于它能够方便地构建所谓的“链条”。一个“链条”是一系列协作组件的序列,通过大型语言模型处理用户的输入和输出。链条可以包含不同类型的组件,比如提示语、检索器、处理器和工具,并且可以在一个链条内嵌套另一个链条,以构建更复杂的应用程序。
然而,这些链条缺少在运行时引入循环的功能,这意味着没有现成的框架能让大型语言模型在类似 for-loop 的情境中推断出下一个最佳动作。随着多代理应用程序——展现不同具有特定个性和工具的代理——变得越来越实用和主流(参见像 AutoGen 这样的库项目的兴起),LangChain 的开发人员引入了一个新的库,以简化这类代理应用程序的管理。这个在 2024 年 1 月推出的新库名为 LangGraph,正如名字所示,它基于图这一数学概念,作为大型语言模型驱动应用的框架。
在深入了解之前,让我们先简单介绍一下图的数学概念,以及它为何适合循环链条的框架。
图的概念
在模拟和分析复杂系统时,使用能够捕捉代理间关系的模型至关重要。这些关系及其权重和分布可以通过图的数学表示进行建模。
图是一组对象的集合,其中一些对象对通过连接相连。这些连接通常被称作边或弧,它们所连接的对象被称为顶点或节点。图用于建模对象之间的关系,如社交网络、道路网络或化学中的分子连接。
通常,当一个系统能够用图来建模时,我们称这个系统为一个网络。网络科学是一门旨在理解其基础结构为图的现象的学科。例如,在流行病学领域,网络用于模拟疾病的传播,其中节点代表个体,连接代表个体间的接触(你可以在这里找到我关于网络科学的系列文章)。
在 LangGraph 中,图框架用于表示构建大型语言模型驱动应用所需的典型组件。更具体地说:
- 节点 → 这些是游戏中的“参与者”或代理。每个人可以专长于不同的活动。例如,我们可以有一个图,包含三个节点,分别代表心理学家、社会学家和经济学家,他们正在进行一项关于社交媒体对心理健康影响的大规模调查(我们还可以增加一个代表这三位专家的监督者的第四个节点)。
- 边 → 这些代表节点之间的连接和关系。它们可以是节点间传递信息的连接,也可以是确定激活节点及其执行顺序的条件边。
更多关于 LangGraph 的详细介绍,你可以阅读官方博客(https://blog.langchain.dev/langgraph/)。
接下来,我们将看到如何设计你的第一个多代理图的示例。
构建多代理图
我们将构建一个包含三个角色及其特性(使用大型语言模型的词汇,即他们的“系统消息”)的多代理应用程序:
- 心理学家:“一个能够无缝将神经科学理论融入对话的专家心理学家,揭示人类行为和情感的复杂性。”
- 社会学家:“一位敏锐的社会学家,擅长剖析社会模式,探究社交媒体与我们的集体心理之间的相互作用。”
- 经济学家:“一位务实的经济学家,以精准的方式量化无形资产,将社交媒体趋势与经济影响联系起来。”
我们的研究问题将探讨社交媒体与心理健康之间的关联。为了构建图,我使用了来自 langgraph 仓库的示例(https://github.com/langchain-ai/langgraph/tree/main),并调整它来设计上述三位代理的个性。在这篇文章中,我仅覆盖修改过的脚本:如果你想自己尝试笔记本,请参考这个链接(https://github.com/langchain-ai/langgraph/blob/main/examples/multi_agent/multi-agent-collaboration.ipynb)。
在上面我们说过,要构建图,我们将需要两个主要成分:节点和边。让我们了解背后的逻辑。
节点
节点可以是具有特定任务或个性的代理,也可以是我们的应用程序可以执行的操作,给定提供的工具。在我们的示例中,我们将有 3 个代理 — 心理学家、社会学家和经济学家 — 和一个工具 — 搜索引擎 Tavily。
为了初始化图,我将首先设置我的模型(我将利用 Azure OpenAI GPT-4)和我的工具如下:
from langchain.schema import HumanMessage, AIMessage
from langchain_openai import AzureChatOpenAI
from dotenv import load_dotenv
load_dotenv()
os.environ["AZURE_OPENAI_API_KEY"]
os.environ["AZURE_OPENAI_ENDPOINT"]
os.environ["TAVILY_API_KEY"]
llm = AzureChatOpenAI(
openai_api_version="2023-07-01-preview",
azure_deployment="gpt-4",
)
from langchain_community.tools.tavily_search import TavilySearchResults
tavily_tool = TavilySearchResults(max_results=5)
tools = [tavily_tool]
tool_executor = ToolExecutor(tools)
接下来初始化代理:
import functools
# Helper function to create a node for a given agent
def agent_node(state, agent, name):
result = agent.invoke(state)
# We convert the agent output into a format that is suitable to append to the global state
if isinstance(result, FunctionMessage):
pass
else:
result = HumanMessage(**result.dict(exclude={"type", "name"}), name=name)
return {
"messages": [result],
# Since we have a strict workflow, we can
# track the sender so we know who to pass to next.
"sender": name,
}
# Psychologist agent and node
psychologist_agent = create_agent(
llm,
[tavily_tool],
system_message="An expert psychologist who seamlessly weaves neuroscientific theories into conversations, unraveling the complexities of human behavior and emotions.",
)
psychologist_node = functools.partial(agent_node, agent=psychologist_agent, name="Psychologist")
# Socioligist
sociologist_agent = create_agent(
llm,
[tavily_tool],
system_message="A keen-eyed sociologist adept at dissecting societal patterns, investigating the collective psyche. Focus on group effects rather than individual effects.",
)
sociologist_node = functools.partial(agent_node, agent=sociologist_agent, name="Sociologist")
# Socioligist
economist_agent = create_agent(
llm,
[tavily_tool],
system_message="A pragmatic economist who quantifies intangibles, connecting trends to economic implications with precision.",
)
economist_node = functools.partial(agent_node, agent=economist_agent, name="Economist")
边
现在我们需要一个逻辑来应用于我们的边,以确定循环的执行模式。为此,我们将创建一个具有以下结构的路由:

# Either agent can decide to end
def router(state):
# This is the router
messages = state["messages"]
last_message = messages[-1]
if "function_call" in last_message.additional_kwargs:
# The previus agent is invoking a tool
return "call_tool"
if "FINAL ANSWER" in last_message.content:
# Any agent decided the work is done
return "end"
return "continue"
我们说,给定一个周期中的状态,有三种可能的下一步动作:
- 如果前一个动作(-1)产生了调用工具的需要(输出为“function call”),则将调用一个可用工具;
- 如果前一个动作产生了最终输出(“FINAL ANSWER”),则周期结束;
- 否则,周期将按照我们在初始化图时定义的顺序继续。
现在我们有了设置图框架的所有要素。我们可以用路由初始化节点(3个代理+1个工具)和边,设置如下循环:心理学家 → 社会学家 → 经济学家。
workflow = StateGraph(AgentState)
workflow.add_node("Psychologist", psychologist_node)
workflow.add_node("Sociologist", sociologist_node)
workflow.add_node("Economist", economist_node)
workflow.add_node("call_tool", tool_node)
workflow.add_conditional_edges(
"Psychologist",
router,
{"continue": "Sociologist", "call_tool": "call_tool", "end": END},
)
workflow.add_conditional_edges(
"Sociologist",
router,
{"continue": "Economist", "call_tool": "call_tool", "end": END},
)
workflow.add_conditional_edges(
"Economist",
router,
{"continue": "Psychologist", "call_tool": "call_tool", "end": END},
)
workflow.add_conditional_edges(
"call_tool",
lambda x: x["sender"],
{
"Psychologist": "Psychologist",
"Sociologist": "Sociologist",
"Economist": "Economist"
},
)
workflow.set_entry_point("Psychologist")
graph = workflow.compile()
现在初始化图。我们将把整个对话存储到一个名为 outputs 的列表中,然后按照如下方式打印每个代理的推理:
outputs = []
for s in graph.stream(
{
"messages": [
HumanMessage(
content="Provide me with meaningful insights about the effects of social media on mental health, covering individual effects, collective effects and macroeconomics effects. Once you provided me with these three perpectives, finish."
)
],
},
# Maximum number of steps to take in the graph
{"recursion_limit": 150},
):
print(s)
outputs.append(s)
print("----")
输出:
{'心理学家':{'消息':[HumanMessage(content ='',additional_kwargs = {
----
{'call_tool': {'messages': [FunctionMessage(content=
----
{'社会学家': {'消息': [HumanMessage(content="基于先前的信息
{'__end__': {'messages': [HumanMessage(content='
.....
你可以只打印最终答案,如下所示:
print(outputs[-1]['__end__']['messages'][-1].content)
FINAL ANSWER:
Based on the search results, here are some insights about the effects of social media on mental health from three perspectives: individual, collective, and macroeconomic.
Individual effects: Social media platforms are popular venues for sharing personal experiences, seeking information, and offering peer-to-peer support among individuals living with mental illness. Many individuals living with mental disorders have expressed interest in using social media platforms for seeking mental health information, connecting with mental health providers, and accessing evidence-based mental health services delivered over social media specifically for coping with mental health symptoms or for promoting overall health and wellbeing. However, excessive use or problematic use of social media can have negative effects on mental health [source](<https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7785056/>).
Collective effects: Some studies have explored the relationship between how individuals perceive themselves on social media versus offline across different generations. Such research can help us understand the collective mental health impact of social media on different demographic groups. The perception of oneself on social media versus offline can have implications for psychological well-being [source](<https://www.psypost.org/2024/01/gen-zs-mental-health-and-authenticity-on-social-media-fascinating-insights-from-new-research-220725>).
Macroeconomic effects: Much of the research to date has focused on populations within individual countries, typically the high-income nations of the global north. Therefore, there's a need to further understand the impact of social media on mental health in diverse settings, including lower-income countries. As social media platforms have a global reach, understanding their impact on mental health across different economic contexts is crucial for developing effective mental health strategies and policies [source](<https://www.frontiersin.org/articles/10.3389/fpsyt.2024.1367335>).
The role of social media in mental health is complex, as it can both support and hinder mental health, depending on the usage patterns, individual factors, and the sociocultural and economic context. It's important for researchers, clinicians, policymakers, and social media users themselves to carefully weigh the potential benefits against the potential risks of social media use for mental health.
正如你所见,所有代理的输出都是对最终答案的宝贵输入,该答案涵盖了来自所有三个角度(个体、集体和宏观经济)的研究问题,并提供了通过工具检索的 3 个来源链接:
- https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7785056/
- https://www.psypost.org/2024/01/gen-zs-mental-health-and-authenticity-on-social-media-fascinating-insights-from-new-research-220725
- https://www.frontiersin.org/articles/10.3389/fpsyt.2024.1367335
结论
LangGraph 作为开发多代理应用程序的强大工具而崭露头角,其基于图的方法提供了独特的优势,使得可以创建动态工作流程,其中多个独立代理无缝协作。这些由大语言模型驱动的代理可以共享状态或独立工作,将最终响应传递给彼此。
此外,通过分组工具和职责,LangGraph 提高了效率。每个代理专注于特定任务,从而获得更好的结果。允许定制单独的提示词,代理甚至可以由单独的微调语言模型驱动。
未经允许不得转载:AI技术交流 » LangGraph 入门(1)-介绍