一、基本介绍
大模型跟人脑一样存储了大量的知识,我们不仅希望用这些知识来做一些简单的问答,我们更希望它也可以像人一样做一些自主决策,这就意味着要求它能够在没有人参与的情况下独立完成一些具有一定复杂度的任务。这个完成任务的过程就包括将任务切分成一些具体的小任务,以及每一步完成后接下来要做什么等这样的推理过程。langchain中的agent就是基于这种目标的一项功能。
这篇博客用来记录agent的学习过程,主要包括如何根据任务配备不同类型的工具(搜索引擎等),以及如何自定义一些工具让代理能够调用。最后构建了一个基于crew的多代理协同完成一项任务的简单workflow。
二、几种Agent示例
1.维基百科搜索代理
langchain中内置了很多供代理使用的工具,维基百科工具实际上是一个维基百科的api程序,它允许将输入的内容在维基百科引擎查询并返回结果。通过代码来看一下。
import os
import openai
from langchain_community.chat_models import ChatOpenAI
from langchain.agents import load_tools,initialize_agent
from langchain.agents import AgentType
os.environ["OPENAI_API_KEY"] = 'xxxx'
openai.api_key = os.environ.get("OPENAI_API_KEY")
llm = ChatOpenAI(temperature = 0)
tools = load_tools(['wikipedia'])
agent = initialize_agent(
tools, # 工具列表
llm, #语言模型
agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, # 代理类型
handle_parsing_errors = True,
verbose = True
)
agent('Tom M. Mitchell is an American computer scientistand the Founders University Professor at Carnegie Mellon University (CMU).what book did he write??')
在initialize_agent当中定义了代理使用的工具,使用的大模型,代理类型等信息。当向这个代理提问时,他的执行过程如下:
> Entering new AgentExecutor chain...
Thought: I can use Wikipedia to find out which book Tom M. Mitchell wrote.
Action:
```
{
"action": "wikipedia",
"action_input": "Tom M. Mitchell"
}
```
Observation: Page: Tom M. Mitchell
Summary: Tom Michael Mitchell (born August 9, 1951) is an American computer scientist and the Founders University Professor at Carnegie Mellon University (CMU). He is a founder and former Chair of the Machine Learning Department at CMU. Mitchell is known for his contributions to the advancement of machine learning, artificial intelligence, and cognitive neuroscience and is the author of the textbook Machine Learning. He is a member of the United States National Academy of Engineering since 2010. He is also a Fellow of the American Academy of Arts and Sciences, the American Association for the Advancement of Science and a Fellow and past President of the Association for the Advancement of Artificial Intelligence. In October 2018, Mitchell was appointed as the Interim Dean of the School of Computer Science at Carnegie Mellon.
Page: Tom Mitchell (Australian footballer)
Summary: Thomas Mitchell (born 31 May 1993) is a professional Australian rules footballer playing for the Collingwood Football Club in the Australian Football League (AFL). He previously played for the Sydney Swans from 2012 to 2016, and the Hawthorn Football Club between 2017 and 2022. Mitchell won the Brownlow Medal as the league's best and fairest player in 2018 and set the record for the most disposals in a VFL/AFL match, accruing 54 in a game against Collingwood during that season. He would later join them in 2023, en route to winning the 2023 AFL Grand Final and his first AFL premiership.
Thought:I have found the information about the book written by Tom M. Mitchell.
Final Answer: The book written by Tom M. Mitchell is "Machine Learning."
> Finished chain.
可以看到,与直接向大模型提问不同的是,它进行了分步和思考。首先会在我们提供的工具箱中寻找解决问题对应的工具,也就是如果有多个工具时,它会自动选择最适合解决问题的工具来处理(选择源于大模型的推理);选择维基百科引擎得到相关资料后,对资料做进一步分析得到最终答案。本例中其实维基百科返回了两条结果,一个是计算机科学家,一个是足球运动员,显然它排除了运动员,给出了正确结果。
2.数学计算代理
语言大模型虽然能处理一些简单的数学问题,但是在复杂的数学问题上处理效果不够理想。例如计算简单的个位数加法能够得到正确结果,但问一个较大数值的数,就有极大概率回答错误,因为本质上语言大模型学习的还是语料信息,并不会真的计算。如下所示,当计算23894+987332时他得到了一个与真实结果(1011226)很接近的错误结果。
llm = ChatOpenAI(model_name = 'gpt-3.5-turbo',temperature = 0.0)
prompt = ChatPromptTemplate.from_template('{input}')
chain = LLMChain(llm = llm,prompt = prompt)
print(chain.run('23894 + 987332'))
>>
The sum of 23894 and 987332 is 1015226.
通过代理中的数学工具就能让它调用数学工具来解决问题。在工具当中添加一个‘llm-math’工具,这个工具实际上是个大模型加数学计算工具构成的chain。
tools = load_tools(['wikipedia','llm-math'],llm=llm)
agent = initialize_agent(
tools, # 工具列表
llm, #语言模型
agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, # 代理类型
handle_parsing_errors = True,
verbose = True
)
agent('23894 + 987332')
>>
> Entering new AgentExecutor chain...
Question: 23894 + 987332
Thought: We can use the Calculator tool to find the sum of these two numbers.
Action:
```
{
&#