「LangChain 学习笔记」LangChain大模型应用开发:代理 (Agent)
「LangChain大模型应用开发」 系列文章目录:
LangChain大模型应用开发:模型,提示和输出解释器
LangChain大模型应用开发:储存(Memory)
LangChain大模型应用开发:模型链(Chains)
LangChain大模型应用开发:基于文档的问答
LangChain大模型应用开发:评估
LangChain大模型应用开发:代理 (Agent)
目录
1. 使用LangChain内置工具llm-math和wikipedia
2. 定义自己的工具并在代理中使用
将自定义工具和内置工具结合
大型语言模型(LLMs)非常强大,但它们缺乏“最笨”的计算机程序可以轻松处理的特定能力。LLM 对逻辑推理、计算和检索外部信息的能力较弱,这与最简单的计算机程序形成对比。例如,语言模型无法准确回答简单的计算问题,还有当询问最近发生的事件时,其回答也可能过时或错误,因为无法主动获取最新信息。这是由于当前语言模型仅依赖预训练数据,与外界“断开”。要克服这一缺陷,LangChain框架提出了“代理”(Agent)的解决方案。
代理作为语言模型的外部模块,可提供计算、逻辑、检索等功能的支持,使语言模型获得异常强大的推理和获取信息的超能力。
在本章中,我们将详细介绍代理的工作机制、种类、以及如何在LangChain中将其与语言模型配合,构建功能更全面、智能程度更高的应用程序。代理机制极大扩展了语言模型的边界,是当前提升其智能的重要途径之一。让我们开始学习如何通过代理释放语言模型的最大潜力。
1. 使用LangChain内置工具llm-math和wikipedia
要使用代理 (Agents) ,我们需要三样东西:
- 一个基本的 LLM。
- 我们将要进行交互的工具 Tools。
- 一个控制交互的代理 (Agents) 。
from langchain_openai import ChatOpenAIllm = ChatOpenAI(api_key="你的API key",base_url="https://qianfan.baidubce.com/v2",model="ernie-3.5-8k",temperature=0
)
接下来,初始化工具 Tool ,我们可以创建自定义工具 Tool 或加载预构建工具 Tool。无论哪种情况,工具 Tool 都是一个给定工具 名称 name 和 描述 description 的 实用链。
- llm-math 工具结合语言模型和计算器用以进行数学计算。
- wikipedia工具通过API连接到wikipedia进行搜索查询。
from langchain.agents import load_tools, initialize_agenttools = load_tools(["llm-math","wikipedia"], llm=llm #第一步初始化的模型
)
现在我们有了 LLM 和工具,最后让我们初始化一个简单的代理 (Agents) :
from langchain.agents import AgentType
# 初始化代理
agent= initialize_agent(tools, #第二步加载的工具llm, #第一步初始化的模型agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, #代理类型handle_parsing_errors=True, #处理解析错误verbose = True #输出中间步骤
)
- agent: 代理类型。这里使用的是AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION。其中CHAT代表代理模型为针对对话优化的模型;Zero-shot 意味着代理 (Agents) 仅在当前操作上起作用,即它没有记忆;REACT代表针对REACT设计的提示模版。DESCRIPTION根据工具的描述 description 来决定使用哪个工具。(我们不会在本章中讨论 * REACT 框架 ,但您可以将其视为 LLM 可以循环进行 Reasoning 和 Action 步骤的过程。它启用了一个多步骤的过程来识别答案。)
- handle_parsing_errors: 是否处理解析错误。当发生解析错误时,将错误信息返回给大模型,让其进行纠正。
- verbose: 是否输出中间步骤结果。
下面使用代理(llm-math 工具结合语言模型和计算器用以进行数学计算)回答数学问题。
agent("计算300的25%")
> Entering new AgentExecutor chain...
Question: 计算300的25%
Thought: 要计算300的25%,我可以将300乘以0.25(因为25%等于0.25)。
Action:
```json
{"action": "Calculator","action_input": "300 * 0.25"
}
```
Observation: Answer: 75.0
Thought:I now know the final answer
Final Answer: 300的25%是75。> Finished chain.{'input': '计算300的25%', 'output': '300的25%是75。'}
上面的过程可以总结为下:
模型对于接下来需要做什么,给出思考。
思考:我可以使用计算工具来计算300的25%
模型基于思考采取行动。
行动: 使用计算器(calculator),输入(action_input)300*0.25
模型得到观察。
观察:答案: 75.0
基于观察,模型对于接下来需要做什么,给出思考。
思考: 计算工具返回了300的25%,答案为75
给出最终答案(Final Answer)。
最终答案: 300的25%等于75。
以字典的形式给出最终答案。
另一个例子(wikipedia工具通过API连接到wikipedia进行搜索查询)。
question = "Tom M. Mitchell是一位美国计算机科学家,\
也是卡内基梅隆大学(CMU)的创始人大学教授。\
他写了哪本书呢?"agent(question)
> Entering new AgentExecutor chain...
Question: Tom M. Mitchell是哪本书的作者?
Thought: I need to find out which book was written by Tom M. Mitchell. I can use Wikipedia to search for this information.
Action:
```json
{"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:Thought: The Wikipedia page for Tom M. Mitchell mentions that he is the author of the textbook Machine Learning.
Action:
```json
{"action": "wikipedia","action_input": "Machine Learning by 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: Machine learning
Summary: Machine learning (ML) is a field of study in artificial intelligence concerned with the development and study of statistical algorithms that can learn from data and generalise to unseen data, and thus perform tasks without explicit instructions. Within a subdiscipline in machine learning, advances in the field of deep learning have allowed neural networks, a class of statistical algorithms, to surpass many previous machine learning approaches in performance.
ML finds application in many fields, including natural language processing, computer vision, speech recognition, email filtering, agriculture, and medicine. The application of ML to business problems is known as predictive analytics.
Statistics and mathematical optimisation (mathematical programming) methods comprise the foundations of machine learning. Data mining is a related field of study, focusing on exploratory data analysis (EDA) via unsupervised learning.
From a theoretical viewpoint, probably approximately correct learning provides a framework for describing machine learning.Page: Margaret Mitchell (scientist)
...
Thought:Thought: The Wikipedia page for Tom M. Mitchell confirms that he is the author of the textbook "Machine Learning". No further action is needed.
Final Answer: Tom M. Mitchell wrote the textbook "Machine Learning".> Finished chain.
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
上面的过程可以总结为下:
模型对于接下来需要做什么,给出思考(Thought)
思考:我应该使用维基百科去搜索。
模型基于思考采取行动(Action)
行动: 使用维基百科,输入Tom M. Mitchell
模型得到观察(Observation)
观测: 页面: Tom M. Mitchell,页面: Tom Mitchell (澳大利亚足球运动员)
基于观察,模型对于接下来需要做什么,给出思考(Thought)
思考: Tom M. Mitchell写的书是Machine Learning
给出最终答案(Final Answer)
最终答案: Machine Learning
以字典的形式给出最终答案。
2. 定义自己的工具并在代理中使用
在本节,我们将创建和使用自定义时间工具。LangChian tool 函数装饰器可以应用用于任何函数,将函数转化为LangChain 工具,使其成为代理可调用的工具。我们需要给函数加上非常详细的文档字符串, 使得代理知道在什么情况下、如何使用该函数/工具。比如下面的函数time
,我们加上了详细的文档字符串。
# 导入tool函数装饰器
from langchain.agents import tool
from datetime import date@tool
def time(text: str) -> str:"""返回今天的日期,用于任何需要知道今天日期的问题。\输入应该总是一个空字符串,\这个函数将总是返回今天的日期,任何日期计算应该在这个函数之外进行。"""return str(date.today())# 初始化代理
agent= initialize_agent(tools=[time], #将刚刚创建的时间工具加入代理llm=llm, #初始化的模型agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, #代理类型handle_parsing_errors=True, #处理解析错误verbose = True #输出中间步骤
)# 使用代理询问今天的日期.
# 注: 代理有时候可能会出错(该功能正在开发中)。如果出现错误,请尝试再次运行它。
agent("今天的日期是什么?")
> Entering new AgentExecutor chain...
Question: 今天的日期是什么?
Thought: I need to use the tool 'time' to get the current date.
Action:
```json
{"action": "time","action_input": ""
}
```
Observation: 2025-08-29
Thought:I now know the final answer
Final Answer: 今天的日期是2025年8月29日。> Finished chain.{'input': '今天的日期是什么?', 'output': '今天的日期是2025年8月29日。'}
将自定义工具和内置工具结合
# 导入tool函数装饰器
from langchain.agents import tool
from datetime import date@tool
def time(text: str) -> str:"""返回今天的日期,用于任何需要知道今天日期的问题。\输入应该总是一个空字符串,\这个函数将总是返回今天的日期,任何日期计算应该在这个函数之外进行。"""return str(date.today())tools = load_tools(["ddg-search"]) #这里使用DuckDuckGo 搜索
tools.append(time)# 初始化代理
agent= initialize_agent(tools=tools, #将刚刚创建的时间工具加入代理llm=llm, #初始化的模型agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, #代理类型handle_parsing_errors=True, #处理解析错误verbose = True #输出中间步骤
)# 使用代理询问今天的日期.
# 注: 代理有时候可能会出错(该功能正在开发中)。如果出现错误,请尝试再次运行它。today = agent("今天的日期是什么?,以年月日格式输出")
print(today)
print(agent(f"{today}的金价是多少?用中文输出结果。"))
{'input': "{'input': '今天的日期是什么?,以年月日格式输出', 'output': '2025年08月29日'}的金价是多少?用中文输出结果。", 'output': '根据搜索到的信息,2025年08月29日的金价,上海黄金交易所的现货黄金价格为1克黄金等于779.86元人民币。同时,也有信息显示当日的18k金价格为596元/克。请注意,金价可能因市场波动而有所变化,这里提供的信息仅供参考。'}