微信扫码
添加专属顾问
我要投稿
对于多智能体框架来说,其主要攻关的方向是多智能体间交互的数据流组织、智能体的创建等方向,而对于单个智能体内的方法,还是需要自己去实现,而这时候,多框架的组合就会使开发变得很方便。例如,你想用AgentScope实现一个文档读取和拆分的智能体,AgentScope并没有提供类似的很好用的工具,而LangChain中则集成了大量的读取各种文档和拆分文档的方法。将LangChain和AgentScope结合,可以省去自己写文档读取和拆分的方法,大大提高开发效率。
本文以一个例子来看下AgentScope框架和LangChain如何实现融合使用,真的超级简单,一看就懂。这是一种思路,为多种框架的融合使用提供借鉴。
参考:https://github.com/modelscope/agentscope/blob/main/examples/conversation_with_langchain/conversation_with_langchain.py
回顾一下使用AgentScope定义自己的Agent的步骤:
(1)继承AgentBase基类
(2)实现__init__
方法
(3)实现reply
方法
于是,有了框架如下:
class LangChainAgent(AgentBase):
def__init__(self, name: str)->None:
......
defreply(self, x: Optional[dict] = None)->Msg:
......
__init__
方法init方法,主要是构建一个 LangChain 的 Chain。
def __init__(self, name: str)->None:
# Disable AgentScope memory and use langchain memory instead
super().__init__(name, use_memory=False)
# [START] BY LANGCHAIN
# Create a memory in langchain
memory =ConversationBufferMemory(memory_key="chat_history")
# prompt
template ="""
You are a helpful assistant, and your goal is to help the user.
{chat_history}
Human: {human_input}
Assistant:"""
prompt =PromptTemplate(
input_variables=["chat_history","human_input"],
template=template,
)
llm =OpenAI(openai_api_key=os.environ["OPENAI_API_KEY"])
# Prepare a chain and manage the memory by LLMChain in langchain
self.llm_chain =LLMChain(
llm=llm,
prompt=prompt,
verbose=False,
memory=memory,
)
# [END] BY LANGCHAIN
以上代码包含了 LangChain 创建一个对话Chain的过程:
(1)使用了 LangChain 的Prompt模板
(2)创建了 LLMChain
(3)使用了 LangChain 的 ConversationBufferMemory,来实现对话历史的记录。
如果对 LangChain 的这一套操作比较熟悉,那很容易想到可以将 ConversationBufferMemory 替换成 LangChain 中其它的记忆类型,从而实现不同的记忆方式。将 LLMChain 换成不同的 Chain,从而不实现不同的能力,比只是用 AgentScope 中的 memory 和 llm,方便了很多。
reply
方法reply
方法,主要是调用 Chain 的 predict 方法,实现对话功能。
def reply(self, x: Optional[dict] = None) -> Msg:
response_str = self.llm_chain.predict(human_input=x.content)
return Msg(name=self.name, content=response_str, role="assistant")
注意这里返回的是 Msg 对象。这是为了与其它的 Agent 交互,所以需要返回一个 Msg 对象。AgentScope 中,Msg 对象是 Agent 和 Agent 之间的信息传递对象。
# init AgentScope
agentscope.init()
agent = LangChainAgent(name="Assistant")
msg = Msg(name="user", content="你好")
response = agent(msg)
print(response)
运行结果:
可以让该智能体与其它智能体交互,实现多智能体运行。
# Create a user agent from AgentScope
user =UserAgent("User")
msg =None
whileTrue:
# User input
msg = user(msg)
if msg.content =="exit":
break
# Agent speaks
msg = agent(msg)
print(msg)
运行结果:
本文我们介绍了如何将 AgentScope 和 LangChain 框架结合起来使用:在初始化时,创建 LangChain 的 Chain,在 reply 方法中调用 Chain 的 predict 方法,从而实现对话功能。注意的是,为了让我们自己的智能体能与其它智能体交互,reply 方法需要返回一个 Msg 对象。
当然,这种多框架融合使用的方式,也有缺点,比如会使得项目代码很臃肿,依赖较多。所以,按需使用。
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费场景POC验证,效果验证后签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2025-06-13
【LangChain 团队重磅实测报告】多智能体架构揭秘:谁才是性能之王?
2025-06-13
大模型_百炼:MCP让我焦虑
2025-06-11
前端学AI之LangChain.js入门教程:实现智能对话机器人
2025-06-08
Langfuse:重新定义LLM应用开发与运维的可观测性
2025-06-08
Langgraph实战--自定义embeding
2025-06-07
为 AI Agent 铺路:深度解析下一代应用的核心基建 LangGraph
2025-06-05
智能体框架怎么选?LangChain、Dify、CrewAI、AutoGen五大框架横向对比
2025-06-04
吴恩达对谈LangChain创始人:企业构建Agen系统的核心认知!
2025-03-20
2025-03-17
2025-05-08
2025-04-18
2025-03-22
2025-05-06
2025-03-23
2025-04-13
2025-05-28
2025-03-22
2025-06-13
2025-05-21
2025-05-19
2025-05-08
2025-05-06
2025-04-22
2025-04-18
2025-03-22