前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >多AI Agent代理:使用LangGraph和LangChain创建多代理工作流

多AI Agent代理:使用LangGraph和LangChain创建多代理工作流

作者头像
山行AI
发布于 2024-06-06 07:51:17
发布于 2024-06-06 07:51:17
2.6K00
代码可运行
举报
文章被收录于专栏:山行AI山行AI
运行总次数:0
代码可运行

前言

在AI时代,每个人都是一个超级个体,AI Agent智能体的出现,为我们打造超级个体提供了可能。如果说2024年将是AI应用的元年,那么AI Agent将是这个AI应用元年里最为闪亮的那颗星。之前有读者留言,希望多分享一些AI Agent智能体的搭建方法,在上一篇推文中也从实战案例角度分享了怎么用天工AI快速搭建一套属于我们自己的AI Agent智能体,天工AI的多模态和AI搜索能力相信已经能满足大多数人的使用需求。今天就从代码实战上来分享如何使用LangGraph和LangChain创建多代理工作流。

自我反省

自我反省的AI代理使用一个生成器来产生输出,并用一个反射器来审查生成器的工作。生成器和反射器都使用相同的大型语言模型(LLM),但各自有不同的提示,从而形成了一个自我反省的AI代理。

这种在循环方式中以两种不同角色使用同一LLM的方法,是通过LangChain的LangGraph框架 facilitated 的。LangGraph 框架也可以用来创建多代理工作流。就像在自我反思的 AI 代理中一样,LLM 可以扮演多个角色,每个角色都充当一个不同的 AI 代理。这就是多代理的概念。

多代理

一个多代理系统涉及到将独立的行动者连接起来,每个行动者都由一个大型语言模型提供支持,按照特定的排列组合。

每个代理可以有自己的提示、LLM、工具和其他自定义代码来与其他代理协作。然而,同一个 LLM 也可以基于提供的提示扮演不同的角色。

使用LangGraph进行多代理工作流

LangGraph非常适合创建多代理工作流,因为它允许将两个或更多代理连接成一个图。每个代理都是一个独立的行动者,代理之间的连接由边缘表示。每个连接边缘都可以有一个控制条件,指导信息从一个代理流向另一个代理。每个代理都有一个状态,可以在每次流动过程中用信息更新。

协作多代理

就像人类合作一样,协作多代理工作流中的不同AI代理通过共享的留言板进行沟通。这使得每个代理都可以查看其他代理的工作并观察所有独立步骤。

这些不同的AI代理可能基于同一个大型语言模型,但扮演不同的角色。

我们现在将创建一个多代理工作流,用于生成过去五年马来西亚GDP的图表。此工作流要求研究人员在互联网上搜索并找出过去五年马来西亚GDP的数据,以及一个图表生成器,该生成器使用Python代码根据研究人员获得的数据生成图表。

因此,参与者包括:

1.研究人员 — 使用互联网搜索工具来研究问题。2.图表生成器 — 根据研究人员提供的数据执行代码创建图表。

图表蓝图

我们将构造一个图表蓝图,展示代理节点及其之间的连接。

图中的信息流动

A. 用户将输入发送给研究人员。

B. 研究人员将消息发送给路由器

C. 根据消息的内容,路由器要么决定调用一个工具,要么发送消息给图表生成器。

D. 工具完成其工作并将输出发送给研究人员。研究人员可能再次调用该工具或发送消息给图表生成器。相应地,路由器调用工具或将消息发送给图表生成器。

E. 图表生成器将回复发送给路由器,路由器可能调用工具或将回复发送回研究人员。F. 最终,当研究者或图表生成器发送消息作为最终答案时,路由器将最终答案路由给用户。

现在,根据上述图表,让我们创建一个多代理工作流程。

安装包

首先,我们需要安装所有langchain包。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
pip install langchain langchain_openai langgraph langchain_core langsmith langchain_experimental

我们还需要安装一些包用于存储数据(pandas)和创建图表(matplotlib)。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
pip install pandas matplotlib

导入库

接下来,我们将导入创建多代理工作流所需的库。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import json

from langchain_core.messages import (
 AIMessage,
 BaseMessage,
 ChatMessage,
 FunctionMessage,
 HumanMessage,
)
from langchain.tools.render import format_tool_to_openai_function
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langgraph.graph import END, StateGraph
from langgraph.prebuilt.tool_executor import ToolExecutor, ToolInvocation

创建工具

Tavily搜索工具

接下来,我们将设置工具。我们需要一个互联网搜索工具。我们将使用tavily来实现这一点。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import os
os.environ['TAVILY_API_KEY'] = "<Your Tavily API Key here>"

from langchain_core.tools import tool
from langchain_community.tools.tavily_search import TavilySearchResults
tavily_tool = TavilySearchResults(max_results=5)

Python 工具

我们还需要一个工具来执行 Python 代码。我们将使用来自 langchain_experimental.utilities 的一个名为 PythonREPL 的工具。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
from langchain_experimental.utilities import PythonREPL
from typing import Annotated

repl = PythonREPL()
@tool
def python_repl(code: Annotated[str, "The python code to execute to generate your chart."]):
 """Use this to execute python code. If you want to see the output of a value, you should print it out with `print(...)`. This is visible to the user."""
 try:
 result = repl.run(code)
 except BaseException as e:
 return f"Failed to execute. Error: {repr(e)}"
 return f"Succesfully executed:\\\\n`python\\\\\\\\n{code}\\\\\\\\n`\\\\nStdout: {result}"

我们将工具列入一个列表中。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tools = [tavily_tool, python_repl]

创建状态

接下来,我们将为状态创建对象。状态将是从一个节点传递到另一个节点的消息列表。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import operator
from typing import Annotated, List, Sequence, Tuple, TypedDict, Union

from langchain.agents import create_openai_functions_agent
from langchain.tools.render import format_tool_to_openai_function
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholderfrom langchain_openai import ChatOpenAI
from typing_extensions import TypedDict# This defines the object that is passed between each node

在图中。我们将为每个代理和工具创建不同的节点

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class AgentState(TypedDict):
 messages: Annotated[Sequence[BaseMessage], operator.add]
 sender: str

创建工具节点

我们将根据消息的内容使用每个工具。我们将创建一个 tool_node 函数,该函数将根据消息执行一个工具。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tool_executor = ToolExecutor(tools)

def tool_node(state):
 """This runs tools in the graph It takes in an agent action and calls that tool and returns the result."""
 messages = state["messages"]
 # Based on the continue condition
 # we know the last message involves a function call
 last_message = messages[-1]
 # We construct an ToolInvocation from the function_call
 tool_input = json.loads(
 last_message.additional_kwargs["function_call"]["arguments"]
 )
 # We can pass single-arg inputs by value
 if len(tool_input) == 1 and "__arg1" in tool_input:
 tool_input = next(iter(tool_input.values()))
 tool_name = last_message.additional_kwargs["function_call"]["name"]
 action = ToolInvocation(
 tool=tool_name,
 tool_input=tool_input,
 )
 # We call the tool_executor and get back a response
 response = tool_executor.invoke(action)
 # We use the response to create a FunctionMessage
 function_message = FunctionMessage(
 content=f"{tool_name} response: {str(response)}", name=action.tool
 )
 # We return a list, because this will get added to the existing list
 return {"messages": [function_message]}

创建路由器

现在我们已经设置好了工具节点,接下来让我们创建一个路由器,它将根据来自状态的消息内容决定将信息发送到哪里。

任一代理都可以决定结束

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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"

接下来,我们要设置AI代理研究员和图表生成器。

创建LLM

首先,让我们创建llm。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(api_key="<Your API Key here>")

创建代理

首先,我们创建一个 create_agent 函数,该函数将创建一个代理并追加函数提供的系统消息。我们稍后将使用此函数来创建研究员和图表生成器。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import json

from langchain_core.messages import (
 AIMessage,
 BaseMessage,
 ChatMessage,
 FunctionMessage,
 HumanMessage,
)
from langchain_core.utils.function_calling import convert_to_openai_function
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langgraph.graph import END, StateGraph
from langgraph.prebuilt.tool_executor import ToolExecutor, ToolInvocationdef create_agent(llm, tools, system_message: str):
 """Create an agent."""
 functions = [convert_to_openai_function(t) for t in tools] prompt = ChatPromptTemplate.from_messages(
 [
 (
 "system",
 "You are a helpful AI assistant, collaborating with other assistants."
 " Use the provided tools to progress towards answering the question."
 " If you are unable to fully answer, that's OK, another assistant with different tools "
 " will help where you left off. Execute what you can to make progress."
 " If you or any of the other assistants have the final answer or deliverable,"
 " prefix your response with FINAL ANSWER so the team knows to stop."
 " You have access to the following tools: {tool_names}.\\\\n{system_message}",
 ),
 MessagesPlaceholder(variable_name="messages"),
 ]
 )
 prompt = prompt.partial(system_message=system_message)
 prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools]))
 return prompt | llm.bind_functions(functions)

接下来,我们创建一个名为agent_node的函数,该函数负责添加消息和发送者的名称。这将用于路由目的。

创建给定代理的节点的辅助函数

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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,
 }

Now, we create the researcher agent and the chart generator agent.

research_agent= create_agent(
 llm,
 [tavily_tool],
 system_message="You should provide accurate data for the chart generator to use.",
)

chart_agent= create_agent(
 llm,
 [python_repl],
 system_message="Any charts you display will be visible by the user.",
)

从代理节点创建研究者节点和图表节点

然后,我们从 research_agent 和 chart_agent 函数创建 research_node 函数和 chart_node 函数。我们使用 functools 库从另一个函数 agent_node 创建函数。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import functools
research_node= functools.partial(agent_node, agent=research_agent, name="Researcher")

chart_node= functools.partial(agent_node, agent=chart_agent, name="Chart Generator")

创建图表

我们现在可以将所有节点放入名为workflow的图中,这包括research_node(研究节点)、chart_node(图表节点)和tool_node(工具节点)。我们还添加了条件边缘,这些边缘将根据满足的条件路由消息。最后,我们将研究者节点设置为入口点。因此,用户输入将首先进入研究者节点。最后,我们编译图表。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
workflow= StateGraph(AgentState)

workflow.add_node("Researcher", research_node)
workflow.add_node("Chart Generator", chart_node)
workflow.add_node("call_tool", tool_node)workflow.add_conditional_edges(
 "Researcher",
 router,
 {"continue": "Chart Generator", "call_tool": "call_tool", "end": END},
)
workflow.add_conditional_edges(
 "Chart Generator",
 router,
 {"continue": "Researcher", "call_tool": "call_tool", "end": END},
)workflow.add_conditional_edges(
 "call_tool",#每个代理节点更新'sender'字段
#调用工具的节点不会更新,意味着
#这条边将回路到最初调用工具的代理节点
  lambda x: x["sender"],
{
    "Researcher": "Researcher",
    "Chart Generator": "Chart Generator",
},
)
workflow.set_entry_point("Researcher")
graph= workflow.compile()

调用多代理工作流程

我们现在准备好调用多代理工作流程了。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
for s in graph.stream(
 {
 "messages": [
 HumanMessage(
 content="Fetch the Malaysia's GDP over the past 5 years,"
 " then draw a line graph of it."
 " Once you code it up, finish."
 )
 ],
 },
 # Maximum number of steps to take in the graph
 {"recursion_limit": 150},
):
 print(s)
 print("----")

输出为:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
{'Researcher': {'messages': [HumanMessage(content='', additional_kwargs={'function_call': {'arguments': '{"query":"Malaysia GDP by year"}', 'name': 'tavily_search_results_json'}}, response_metadata={'token_usage': {'completion_tokens': 22, 'prompt_tokens': 221, 'total_tokens': 243}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'function_call', 'logprobs': None}, name='Researcher', id='run-313ac64c-fed0-4505-b00d-715c7ff0aa59-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Researcher'}}

{'call_tool': {'messages': [FunctionMessage(content="tavily_search_results_json response: [{'url': '<https://data.worldbank.org/indicator/NY.GDP.MKTP.KD.ZG?locations=MY>', 'content': 'GDP growth (annual %) - Malaysia from The World Bank: Data.'}, {'url': '<https://tradingeconomics.com/malaysia/gdp>', 'content': 'GDP in Malaysia averaged 109.79 USD Billion from 1960 until 2022, reaching an all time high of 407.03 USD Billion in 2022 and a record low of 1.90 USD Billion\\\\xa0...'}, {'url': '<https://www.macrotrends.net/global-metrics/countries/MYS/malaysia/gdp-gross-domestic-product>', 'content': 'Malaysia gdp for 2019 was $365.18B, a 1.78% increase from 2018. Download Historical Data Save as Image\\\\xa0...'}, {'url': '<https://data.worldbank.org/indicator/NY.GDP.MKTP.CD?locations=MY>', 'content': '1960 1980 2000 2020 0.00 225b 450b Malaysia. 1960 - 2022. 2022, 2021, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007\\\\xa0...'}, {'url': '<https://www.worldometers.info/gdp/malaysia-gdp/>', 'content': 'Nominal (current) Gross Domestic Product (GDP) of Malaysia is $406,306,000,000 (USD) as of 2022. · Real GDP (constant, inflation adjusted) of Malaysia reached\\\\xa0...'}]", name='tavily_search_results_json')]}}

{'Researcher': {'messages': [HumanMessage(content='', additional_kwargs={'function_call': {'arguments': '{"query":"Malaysia GDP historical data"}', 'name': 'tavily_search_results_json'}}, response_metadata={'token_usage': {'completion_tokens': 22, 'prompt_tokens': 632, 'total_tokens': 654}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'function_call', 'logprobs': None}, name='Researcher', id='run-442c7d99-8b3c-4a32-99ec-e0739ac059dd-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Researcher'}}

{'call_tool': {'messages': [FunctionMessage(content='tavily_search_results_json response: [{\\'url\\': \\'<https://data.worldbank.org/indicator/NY.GDP.MKTP.KD.ZG?locations=MY&view=chart\\>', \\'content\\': \\'GDP growth (annual %) - Malaysia. World Bank national accounts data, and OECD National Accounts data files. License : CC BY-4.0. LineBarMap. Also Show Share Details. Label. 1961 - 2022.\\'}, {\\'url\\': \\'<https://tradingeconomics.com/malaysia/gdp\\>', \\'content\\': \\'The Gross Domestic Product (GDP) in Malaysia was worth 407.03 billion US dollars in 2022, according to official data from the World Bank. The GDP value of Malaysia represents 0.17 percent of the world economy. This page provides - Malaysia GDP - actual values, historical data, forecast, chart, statistics, economic calendar and news.\\'}, {\\'url\\': \\'<https://www.macrotrends.net/global-metrics/countries/MYS/malaysia/gdp-growth-rate\\>', \\'content\\': \\'Malaysia gdp growth rate for 2022 was 8.65%, a 5.35% increase from 2021. Malaysia gdp growth rate for 2021 was 3.30%, a 8.75% increase from 2020. Malaysia gdp growth rate for 2020 was -5.46%, a 9.87% decline from 2019. Malaysia gdp growth rate for 2019 was 4.41%, a 0.43% decline from 2018. Download Historical Data. Save as Image.\\'}, {\\'url\\': \\'<https://data.worldbank.org/indicator/NY.GDP.MKTP.CD?locations=MY&view=chart\\>', \\'content\\': \\'All Countries and Economies. Country. Most Recent Year. Most Recent Value(Millions) GDP (current US$) - Malaysia from The World Bank: Data.\\'}, {\\'url\\': \\'<https://www.macrotrends.net/global-metrics/countries/MYS/malaysia/gdp-gross-domestic-product\\>', \\'content\\': "Malaysia gdp for 2022 was $407.03B, a 8.88% increase from 2021. Malaysia gdp for 2021 was $373.83B, a 10.78% increase from 2020. Malaysia gdp for 2020 was $337.46B, a 7.59% decline from 2019. Malaysia gdp for 2019 was $365.18B, a 1.78% increase from 2018. GDP at purchaser\\'s prices is the sum of gross value added by all resident producers in the ..."}]', name='tavily_search_results_json')]}}

{'Researcher': {'messages': [HumanMessage(content="Based on the search results, I found the GDP data for Malaysia for the past 5 years as follows:\\n\\n- 2018: $365.18 billion\\n- 2019: $337.46 billion\\n- 2020: $373.83 billion\\n- 2021: $407.03 billion\\n- 2022: $407.03 billion\\n\\nNow, I will proceed to create a line graph to visualize this data.\\nI will now generate a line graph to visualize Malaysia's GDP over the past 5 years.", response_metadata={'token_usage': {'completion_tokens': 113, 'prompt_tokens': 1210, 'total_tokens': 1323}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Researcher', id='run-e498d73b-c688-4e38-aa9b-b4cfa39c34c7-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Researcher'}}

{'Chart Generator': {'messages': [HumanMessage(content='', additional_kwargs={'function_call': {'arguments': '{"code":"import matplotlib.pyplot as plt\\\\n\\\\ndata = {\\\\n \\'Year\\': [2018, 2019, 2020, 2021, 2022],\\\\n \\'GDP (in billion USD)\\': [365.18, 337.46, 373.83, 407.03, 407.03]\\\\n}\\\\n\\\\nplt.figure(figsize=(10, 6))\\\\nplt.plot(data[\\'Year\\'], data[\\'GDP (in billion USD)\\'], marker=\\'o\\', color=\\'b\\', linestyle=\\'-\\', linewidth=2)\\\\nplt.title(\\'Malaysia GDP Over the Past 5 Years\\')\\\\nplt.xlabel(\\'Year\\')\\\\nplt.ylabel(\\'GDP (in billion USD)\\')\\\\nplt.grid(True)\\\\nplt.show()"}', 'name': 'python_repl'}}, response_metadata={'token_usage': {'completion_tokens': 175, 'prompt_tokens': 1338, 'total_tokens': 1513}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'function_call', 'logprobs': None}, name='Chart Generator', id='run-11c4756f-097b-4733-8ecf-6091f5be5bd4-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Chart Generator'}}

{'call_tool': {'messages': [FunctionMessage(content="python_repl response: Succesfully executed:\\\\n`python\\\\\\\\nimport matplotlib.pyplot as plt\\n\\ndata = {\\n 'Year': [2018, 2019, 2020, 2021, 2022],\\n 'GDP (in billion USD)': [365.18, 337.46, 373.83, 407.03, 407.03]\\n}\\n\\nplt.figure(figsize=(10, 6))\\nplt.plot(data['Year'], data['GDP (in billion USD)'], marker='o', color='b', linestyle='-', linewidth=2)\\nplt.title('Malaysia GDP Over the Past 5 Years')\\nplt.xlabel('Year')\\nplt.ylabel('GDP (in billion USD)')\\nplt.grid(True)\\nplt.show()\\\\\\\\n`\\\\nStdout: ", name='python_repl')]}}

{'Chart Generator': {'messages': [HumanMessage(content="The line graph showing Malaysia's GDP over the past 5 years has been successfully generated. The graph displays the GDP (in billion USD) on the y-axis and the years from 2018 to 2022 on the x-axis. The GDP values are plotted as points connected by lines.\\n\\nIs there anything else you would like to analyze or any other assistance needed?", response_metadata={'token_usage': {'completion_tokens': 75, 'prompt_tokens': 1514, 'total_tokens': 1589}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Chart Generator', id='run-a6d15c89-6cb8-4f98-9cbb-67bb92214dc5-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Chart Generator'}}

{'Researcher': {'messages': [HumanMessage(content="I have successfully generated the line graph showing Malaysia's GDP over the past 5 years. If you have any more data to analyze or need further assistance, feel free to let me know!", response_metadata={'token_usage': {'completion_tokens': 39, 'prompt_tokens': 1579, 'total_tokens': 1618}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Researcher', id='run-9c1709e6-f6ee-4a8c-b0ae-f363d65a5a2f-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Researcher'}}

{'Chart Generator': {'messages': [HumanMessage(content="Great! If you have any more data to analyze or any other tasks you'd like assistance with, feel free to ask. I'm here to help!", response_metadata={'token_usage': {'completion_tokens': 32, 'prompt_tokens': 1634, 'total_tokens': 1666}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Chart Generator', id='run-1f869abb-930c-4b19-ab7e-c068a407f134-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Chart Generator'}}

{'Researcher': {'messages': [HumanMessage(content="I'm glad the line graph for Malaysia's GDP over the past 5 years was successfully generated. If you have any more data to analyze or any other tasks you'd like assistance with, feel free to ask. I'm here to help!", response_metadata={'token_usage': {'completion_tokens': 50, 'prompt_tokens': 1656, 'total_tokens': 1706}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Researcher', id='run-4c614614-5fc6-4d6b-abd8-5b1b85607f5a-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Researcher'}}

{'Chart Generator': {'messages': [HumanMessage(content='Thank you! If you have any more data to analyze or if you need assistance with any other tasks in the future, feel free to reach out. Have a great day!', response_metadata={'token_usage': {'completion_tokens': 36, 'prompt_tokens': 1722, 'total_tokens': 1758}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Chart Generator', id='run-659e7259-1906-4c07-8bb5-53c8d6ac7dd2-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Chart Generator'}}

{'Researcher': {'messages': [HumanMessage(content='Thank you! If you have any more data to analyze or if you need assistance with any other tasks in the future, feel free to reach out. Have a great day!', response_metadata={'token_usage': {'completion_tokens': 36, 'prompt_tokens': 1748, 'total_tokens': 1784}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Researcher', id='run-f87114d6-08aa-4a54-8490-1a1145a30263-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Researcher'}}

{'Chart Generator': {'messages': [HumanMessage(content='Great collaboration! If you have any more questions or need assistance in the future, feel free to ask. Have a wonderful day!', response_metadata={'token_usage': {'completion_tokens': 27, 'prompt_tokens': 1800, 'total_tokens': 1827}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Chart Generator', id='run-b12ec2b6-61bb-485c-81cf-279ade302e02-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Chart Generator'}}

{'Researcher': {'messages': [HumanMessage(content="Thank you! It was a pleasure collaborating with you. If you have any more questions or need assistance in the future, don't hesitate to ask. Have a wonderful day!", response_metadata={'token_usage': {'completion_tokens': 36, 'prompt_tokens': 1817, 'total_tokens': 1853}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Researcher', id='run-3c7cf207-ce49-416f-bb02-81a2a3410d5a-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Researcher'}}

{'Chart Generator': {'messages': [HumanMessage(content='It was a pleasure collaborating with you! If you have any more questions or need assistance in the future, feel free to reach out. Have a great day!', response_metadata={'token_usage': {'completion_tokens': 33, 'prompt_tokens': 1869, 'total_tokens': 1902}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Chart Generator', id='run-1d5d0ed0-c20a-4745-a16f-6adcaa8c2c81-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Chart Generator'}}

{'Researcher': {'messages': [HumanMessage(content='Likewise! If you ever have more questions or need help in the future, feel free to ask. Have a fantastic day!', response_metadata={'token_usage': {'completion_tokens': 27, 'prompt_tokens': 1892, 'total_tokens': 1919}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Researcher', id='run-bb88218f-ab2f-4ee5-b381-d75e1bd1d1cf-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Researcher'}}

{'Chart Generator': {'messages': [HumanMessage(content='Likewise! If you ever have more questions or need help in the future, feel free to ask. Have a fantastic day!', response_metadata={'token_usage': {'completion_tokens': 27, 'prompt_tokens': 1935, 'total_tokens': 1962}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Chart Generator', id='run-50293d69-34ff-455d-8b34-3a20a0a1bb91-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Chart Generator'}}

{'Researcher': {'messages': [HumanMessage(content="I'm glad we were able to collaborate effectively! If you have any more questions or need assistance in the future, feel free to reach out. Have a wonderful day!", response_metadata={'token_usage': {'completion_tokens': 35, 'prompt_tokens': 1952, 'total_tokens': 1987}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Researcher', id='run-67208ed1-0d31-4b43-b2e8-92619b806deb-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Researcher'}}

{'Researcher': {'messages': [HumanMessage(content="Thank you! It was a pleasure collaborating with you. If you have any more questions or need assistance in the future, don't hesitate to ask. Have a wonderful day!", response_metadata={'token_usage': {'completion_tokens': 36, 'prompt_tokens': 2087, 'total_tokens': 2123}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Researcher', id='run-bc8186f2-1f7e-477e-b1ff-0020c7171aa3-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Researcher'}}

{'Chart Generator': {'messages': [HumanMessage(content='FINAL ANSWER', response_metadata={'token_usage': {'completion_tokens': 4, 'prompt_tokens': 2139, 'total_tokens': 2143}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, name='Chart Generator', id='run-45177a17-7ae8-43b0-b73e-f9d3d5c5d456-0', tool_calls=[], invalid_tool_calls=[])], 'sender': 'Chart Generator'}

恭喜您!您已经创建了一个多代理工作流。现在,您正朝着创建多个AI代理共同完成复杂任务的方向前进。

声明

本文由山行翻译整理自:https://vijaykumarkartha.medium.com/multiple-ai-agents-creating-multi-agent-workflows-using-langgraph-and-langchain-0587406ec4e6,如果对您有帮助,请帮忙点赞、关注、收藏,谢谢~

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-05-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 山行AI 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
OFC 2025 TeraHop报告:AI数据中心光连接技术
在人工智能技术快速发展的背景下,AI数据中心的建设对光模块技术提出了更高要求。TeraHop在OFC 2025会议上的报告,深入探讨了这一领域的现状与未来。
光芯
2025/04/22
1980
OFC 2025 TeraHop报告:AI数据中心光连接技术
盘点:光通信的五个发展趋势
在消费互联网取得巨大成功的基础上,我们开辟了行业互联网这个新蓝海,并据此提出了数字经济和数字化转型战略。
鲜枣课堂
2022/04/07
8220
盘点:光通信的五个发展趋势
OFC 2025 Google报告: OCS技术使能AI集群的高效扩展,显著提升系统可用性和能效
5. 总结 Google 通过光交换技术实现了 AI 集群的高效扩展,显著提升系统可用性和能效。OCS 的动态拓扑调整、WDM 复用和环形器设计,使其成为大规模 AI 训练的关键技术。未来,随着超大规模数据中心的发展,OCS 技术将在行业中发挥更重要作用,推动 AI 基础设施的革新。
光芯
2025/04/08
2010
OFC 2025 Google报告: OCS技术使能AI集群的高效扩展,显著提升系统可用性和能效
OFC 2025预热(一):大厂的一些关于数据中心光互连的邀请报告
OFC 2025周末就要开了,看点多多。今天先来看看会议上大厂们在数据中心光互连这一块会有哪些报告分享吧。
光芯
2025/04/08
2490
OFC 2025预热(一):大厂的一些关于数据中心光互连的邀请报告
重磅!诺基亚完成欧洲800G光通信突破,引领光通信技术浪潮
诺基亚携手Zayo Europe完成欧洲首例800G超长距传输,创下千公里级光通信新纪录。
通信行业搬砖工
2025/04/13
1110
重磅!诺基亚完成欧洲800G光通信突破,引领光通信技术浪潮
骨干网光通信的最新趋势
大家也许都有所耳闻,从去年开始,国内运营商骨干网已经全面拉开了400G商用的帷幕。
鲜枣课堂
2024/05/17
2380
骨干网光通信的最新趋势
AI 如何改变光网络的未来?
随着大型企业及超大规模网络对人工智能工作负载和高带宽需求的日益增长,对光纤连接技术的需求也在显著上升。思科光学系统的高级副总裁兼总经理Bill Gartner指出,由于光纤具有卓越的传输能力,它已经成为连接数据中心、服务器、路由器、交换机以及网络架构中广泛分布的各个组件,以实现远距离高速数据传输的首选技术。
通往ICT之路
2024/07/12
2160
AI 如何改变光网络的未来?
光电共封装(CPO)产业链投资机会梳理
2025年3月18日,在GTC2025发布会上,英伟达推出了全新的NVIDIA Photonics硅光子技术。这项技术通过共封装光学(CPO)取代传统的可插拔光学收发器,使光纤直接连接到交换机,大幅减少数据中心的功耗。据英伟达测算,该技术可降低40MW的功耗,并提高AI计算集群的网络传输效率,为未来超大规模AI数据中心奠定基础。基于此,英伟达推出Spectrum-X与Quantum-X硅光子网络交换机,通过将电子电路与光通信技术深度融合,助力AI工厂连接跨地域的数百万GPU集群,从而降低能耗与运营成本。这表明,CPO技术为未来发展趋势。
AIGC部落
2025/03/21
1910
光电共封装(CPO)产业链投资机会梳理
AFL白皮书:用于AI数据中心的高级网络
本文探讨针对AI工作负载优化的数据中心的关键考量因素。由于大语言模型(LLMs)对计算能力需求不断增长,数据中心需先进网络和创新物理层解决方案。还将了解AI数据中心在能耗、冷却、物理空间、网络拓扑、前后端网络及可扩展性方面的特点,为后续深入研究奠定基础。
AIGC部落
2025/03/24
1130
AFL白皮书:用于AI数据中心的高级网络
Nvidia探索光交换OCS在数据中心及高性能计算集群中的应用
这个工作是Nvidia最近在Optica旗下的JOCN期刊上发表的一篇邀请文章(https://doi.org/10.1364/JOCN.534317),主要内容今年在OFC都讲过,有些公众号也介绍过了。主要通过将OCS引入到L1物理层,实现物理层的可重构、可编程,能够实现数据中心的高效率自动故障恢复以及在深度学习训练中实现拓扑精简和优化。这里做个简单翻译重温一下,细节大家可以看看原文
光芯
2025/04/08
1460
Nvidia探索光交换OCS在数据中心及高性能计算集群中的应用
2023年已过大半,光通信领域有哪些值得关注的技术趋势?
引言:上个星期,小枣君去深圳参加了CIOE中国光博会,获得了一些光通信领域的最新技术动态进展。今天,我来和大家做一个分享。
鲜枣课堂
2023/10/20
7560
2023年已过大半,光通信领域有哪些值得关注的技术趋势?
【硬核扫盲】到底什么是相干光通信?
相干光通信,英文全称叫做Coherent Optical Communication,是光纤通信领域的一项技术。
鲜枣课堂
2022/05/23
2.4K0
【硬核扫盲】到底什么是相干光通信?
OFC 2025预热(二):短距光互连的明星初创公司们
今天来看看OFC 2025上那些明星初创公司都介绍了哪些有意思的进展。主要介绍的公司之前都有陆续写过,包括Ayar labs,、Lightmatter、Celestial AI、OpenLight、Xscape、Lucidean等。Lightmatter和Celestial AI的验证结果展示感觉是脚步最快、最惊艳的(也体现到他们的融资上),Ayar Labs在瞄准大规模制造优化耦合封装方案,而Xscape、Lucidean公司也有了不俗的进展,Nubis展示了新的应用场景,奇点光子开始露面,Openlight平台在持续演进,昨天Tower新闻稿提到的400G/lane技术看会不会在会场有介绍。
光芯
2025/04/08
1800
OFC 2025预热(二):短距光互连的明星初创公司们
DeepSeek回答对光通信领域的影响
问了一下DeepSeek,让他回答一下他自己对光模块、CPO/OIO以及OCS的影响,以下是他的回答,仅供娱乐:
光芯
2025/04/08
1120
DeepSeek回答对光通信领域的影响
OFC 2025:薄膜铌酸锂的产业化讨论
该论坛围绕薄膜铌酸锂(TFLN)技术在光通信领域的应用与发展展开,来自Ciena、新易盛、富士通、光库、AFR的代表分别从客户需求、技术研发、产品商业化等角度进行阐述,并在最后进行了问答环节,探讨了TFLN技术的现状、挑战与未来前景。
光芯
2025/04/11
1420
OFC 2025:薄膜铌酸锂的产业化讨论
OFC2024: CPO时代来临了吗?
今年的OFC大会上,来自Marvell的Matthew Traverso做了题为"Advancement in CPO and Ecosystem"的邀请报告,系统地梳理了芯片大厂在CPO领域的进展。小豆芽这里整理下相关信息,供大家参考。
光学小豆芽
2024/06/07
1.3K2
OFC2024: CPO时代来临了吗?
谷歌TPU超算,大模型性能超英伟达,已部署数十台:图灵奖得主新作
机器之心报道 编辑:泽南 性能和效率都超越英伟达 A100,这样的超算我有不止十台。 我们还没有看到能与 ChatGPT 相匹敌的 AI 大模型,但在算力基础上,领先的可能并不是微软和 OpenAI。 本周二,谷歌公布了其训练语言大模型的超级计算机的细节,基于 TPU 的超算系统已经可以比英伟达的同类更加快速、节能。 谷歌张量处理器(tensor processing unit,TPU)是该公司为机器学习定制的专用芯片(ASIC),第一代发布于 2016 年,成为了 AlphaGo 背后的算力。与 GPU
机器之心
2023/04/06
7140
谷歌TPU超算,大模型性能超英伟达,已部署数十台:图灵奖得主新作
从千兆到万兆:中国光通信的“标准”之路!
近日,由中国通信标准化协会(CCSA)主办,烽火通信与光迅科技联合承办的TC6 WG3/WG4第100次会议在湖北武汉顺利召开。本次会议聚焦通信领域前沿技术,围绕预成端ODN、多模光纤、XPON光模块等关键技术标准进行了统一规范及更新,同时对光纤频谱资源的拓展应用等内容开展了深入研讨。此次会议不仅标志着中国通信标准化工作的重要里程碑,也为未来光通信技术的发展指明了方向。
通往ICT之路
2025/03/18
1070
从千兆到万兆:中国光通信的“标准”之路!
800G光模块:引领未来数据中心与网络通信的新引擎
随着5G、云计算、人工智能和大数据技术的飞速发展,全球数据流量呈现爆发式增长。据预测,到2025年,全球数据总量将达到175ZB(泽字节),这对网络带宽和传输效率提出了前所未有的挑战。在这一背景下,800G光模块作为下一代高速光通信的核心组件,正迅速成为数据中心和网络通信领域的热门话题。本文将为您深入解析800G光模块的技术优势、应用场景以及市场前景。
武汉格凌科技
2025/02/07
4120
800G光模块:引领未来数据中心与网络通信的新引擎
解读中国电信韦主任关于光通信发展的新趋势思考
韦乐平:中国电信集团科技委主任,是一位深耕通信行业多年的杰出技术专家和领导者。他在光通信领域拥有深厚的技术背景和丰富的实践经验,被誉为光通信发展的权威人士。
通信行业搬砖工
2023/10/24
3600
解读中国电信韦主任关于光通信发展的新趋势思考
推荐阅读
相关推荐
OFC 2025 TeraHop报告:AI数据中心光连接技术
更多 >
LV.0
这个人很懒,什么都没有留下~
目录
  • 前言
  • 自我反省
  • 多代理
  • 使用LangGraph进行多代理工作流
  • 协作多代理
  • 图表蓝图
  • 图中的信息流动
  • 安装包
  • 导入库
  • 创建工具
  • Tavily搜索工具
  • Python 工具
  • 创建状态
  • 在图中。我们将为每个代理和工具创建不同的节点
  • 创建工具节点
  • 创建路由器
  • 任一代理都可以决定结束
  • 创建LLM
  • 创建代理
  • 创建给定代理的节点的辅助函数
  • 从代理节点创建研究者节点和图表节点
  • 创建图表
  • 调用多代理工作流程
  • 声明
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档