LangChain学习记录(四)Agent

一、基本介绍

        大模型跟人脑一样存储了大量的知识,我们不仅希望用这些知识来做一些简单的问答,我们更希望它也可以像人一样做一些自主决策,这就意味着要求它能够在没有人参与的情况下独立完成一些具有一定复杂度的任务。这个完成任务的过程就包括将任务切分成一些具体的小任务,以及每一步完成后接下来要做什么等这样的推理过程。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:
```
{
  &#
### LangChain框架介绍 LangChain 是一个专为利用大型语言模型(LLM)创建应用而设计的框架[^1]。该框架不仅简化了与这些强大模型之间的交互过程,还提供了多种机制用于处理数据检索、对话管理以及其他复杂的任务流控制。 #### 架构特点 核心在于其模块化的设计理念,允许开发者通过组合不同的组件快速搭建自定义解决方案。这种灵活性使得即使是不具备深厚机器学习背景的人也能高效地实现智能化服务[^2]。 - **工具集**:内置了一系列实用的功能库,涵盖了从基础的数据预处理到高级别的自然语言理解等多个方面。 - **接口支持**:为了方便集成第三方API和服务,特别优化了对外部资源调用的支持程度。 - **逻辑编排**:借助于精心设计的工作流引擎,可以轻松定义并执行多步骤的任务序列,在各个阶段之间传递必要的状态信息。 ### 主要模块解析 以下是构成整个系统的几个重要部分: - **Prompt Templates (提示模板)** :用来定制输入给AI模型的具体形式,从而影响最终输出的质量和风格。 - **Memory Components (记忆组件)** :负责保存会话历史记录或其他持久化的上下文资料,以便后续查询或决策时参考。 - **Tool Integration (工具整合)** :实现了与其他软件平台无缝对接的能力,扩大了可用资源池的同时也增强了整体功能性。 - **Chains and Agents (链路与代理)** :作为连接各独立单元的核心枢纽,确保所有操作都能按照预定顺序顺利进行下去。 ### 安装指南 对于希望尝试这一先进技术的人来说,官方文档给出了详细的环境配置说明。通常情况下只需要几条简单的命令就能完成基本设置: ```bash pip install langchain ``` 之后便可以根据项目需求进一步探索更多特性。 ### 使用实例 下面给出一段Python代码片段展示如何初始化一个简单的LangChain应用: ```python from langchain import LangChain # 创建一个新的LangChain实例 app = LangChain() # 设置初始参数... app.configure(prompt_template="你好, {name}!", memory={"context": "这是一个测试"}) print(app.run(name="张三")) ``` 这段脚本展示了最基本的启动流程——指定想要使用的提示语句模式,并传入一些额外的记忆项供内部算法参考;最后调用`run()`函数触发实际运行,向用户提供个性化的问候消息。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值