如何使用LangGraph平台部署CrewAI、AutoGen和其他框架¶
LangGraph平台提供了部署代理的基础设施。这与LangGraph无缝集成,但也可以与其他框架一起使用。实现这一目标的方法是将代理封装在一个单一的LangGraph节点中,使其成为整个图。
这样可以让你部署到LangGraph平台上,并能够获得许多好处。你将获得水平可扩展的基础设施、用于处理突发操作的任务队列、支持短期记忆的持久层以及长期记忆支持。
在本指南中,我们将展示如何使用AutoGen代理来实现这一点,但这种方法也适用于在其他框架(如CrewAI、LlamaIndex等)中定义的代理。
设置¶
import getpass
import os
def _set_env(var: str):
if not os.environ.get(var):
os.environ[var] = getpass.getpass(f"{var}: ")
_set_env("OPENAI_API_KEY")
定义自动生成代理¶
在这里我们定义我们的AutoGen代理。来自 https://github.com/microsoft/autogen/blob/0.2/notebook/agentchat_web_info.ipynb
import autogen
import os
config_list = [{"model": "gpt-4o", "api_key": os.environ["OPENAI_API_KEY"]}]
llm_config = {
"timeout": 600,
"cache_seed": 42,
"config_list": config_list,
"temperature": 0,
}
autogen_agent = autogen.AssistantAgent(
name="assistant",
llm_config=llm_config,
)
user_proxy = autogen.UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER",
max_consecutive_auto_reply=10,
is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
code_execution_config={
"work_dir": "web",
"use_docker": False,
}, # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.
llm_config=llm_config,
system_message="Reply TERMINATE if the task has been solved at full satisfaction. Otherwise, reply CONTINUE, or the reason why the task is not solved yet.",
)
封装在LangGraph中¶
我们现在将AutoGen代理封装在一个单一的LangGraph节点中,并使其成为整个图。 主要涉及的是为该节点定义输入和输出模式,如果你手动部署此代理,你需要完成这些定义,因此这并不会增加额外的工作。
from langgraph.graph import StateGraph, MessagesState
def call_autogen_agent(state: MessagesState):
last_message = state["messages"][-1]
response = user_proxy.initiate_chat(autogen_agent, message=last_message.content)
# get the final response from the agent
content = response.chat_history[-1]["content"]
return {"messages": {"role": "assistant", "content": content}}
graph = StateGraph(MessagesState)
graph.add_node(call_autogen_agent)
graph.set_entry_point("call_autogen_agent")
graph = graph.compile()
API Reference: StateGraph
使用LangGraph平台部署¶
您可以像平常一样使用LangGraph平台进行部署。更多详情请参阅这些说明。