【AI大模型应用开发】【LangChain系列】5. 实战LangChain的智能体Agents模块

大家好,我是【同学小张】。持续学习,持续干货输出,关注我,跟我一起学AI大模型技能。

在我前面的MetaGPT系列文章中,已经对智能体有了一个认知,重温一下:

智能体 = LLM+观察+思考+行动+记忆

  • 将大语言模型作为一个推理引擎。给定一个任务,智能体自动生成完成任务所需的步骤,执行相应动作(例如选择并调用工具),直到任务完成。

更详细的智能体相关概念可看我前面的文章:
【AI的未来 – AI Agent系列】【MetaGPT】1. AI Agent如何重构世界

本文我们来学习下LangChain中的智能体模块怎么用。

文章目录

    • 0. 从一个例子认识LangChian的Agent
      • 0.1 Google搜索Tool
        • 0.1.1 注册Google并获取搜索API的key
      • 0.2 运行示例程序
      • 0.3 运行结果解释
    • 1. Agent实现步骤拆解
      • 1.1 先定义工具Tools
      • 1.2 Prompt模板
      • 1.3 创建Agent
      • 1.4 创建Agent执行器
      • 1.5 运行Agent
      • 1.6 运行结果及遇到的坑
    • 2. 补充知识
      • 2.1 AgentTypes
      • 2.2 各AgentTypes的Prompt模板

0. 从一个例子认识LangChian的Agent

下面,我们以一个Google搜索的例子来直观认识下LangChain的Agent。

0.1 Google搜索Tool

0.1.1 注册Google并获取搜索API的key

Google搜索需要借助 Serpapi 来进行实现,Serpapi 提供了 Google 搜索的 API 接口。

(1)去官网:https://serpapi.com/ 注册一个账号,获取自己的key


(2)像OpenAI的key一样添加到环境变量的配置文件中。

(3)安装google检索依赖的Python包

pip install google-search-results

0.2 运行示例程序

咱们先不看LangChain的Agent的概念、接口及原理,先来一个简单的使用示例,运行起来,看下LangChain的Agent都能干什么。

  • 示例程序完整代码
import os
# 加载 .env 到环境变量
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

from langchain_openai import ChatOpenAI

llm = ChatOpenAI() # 默认是gpt-3.5-turbo


# 定义 tools
from langchain.agents import load_tools
tools = load_tools(["serpapi"])

from langchain.agents import initialize_agent
from langchain.agents import AgentType
# 工具加载后都需要初始化,verbose 参数为 True,会打印全部的执行详情
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
# 运行 agent
agent.run("今天的日期是什么? 历史上的今天发生了什么大事?用中文回答")
  • 运行结果

0.3 运行结果解释

从上面运行结果可以看到此Agent的运行过程:
(1)先总结了任务和思考了步骤:检索当前日期,然后检索这个日期上发生的历史事件
(2)执行检索当前日期的步骤:Action是Search,输入是“今天的日期”
(3)得到了今天的日期:Observation的结果
(4)再一次思考:我现在已经知道了当前日期
(5)执行第二步:Action是Search,输入是“历史上的今天发生了什么大事”
(6)得到了第二步的结果
(7)再思考:知道了历史上的今天发生了什么
(8)总结输出最终回复

简单概括:思考 —> 得到结果 —> 思考 —> 得到结果 —> … —> 思考 —> 总结

到这里,相信你已经大体知道Agent是干什么的了。下面,我们拆解下Agent的实现。

1. Agent实现步骤拆解

1.1 先定义工具Tools

  • 可以是一个函数或三方 API
  • 也可以把一个 Chain 或者 Agent 的 run()作为一个 Tool

在上面的例子中,我们使用了官方内置的Tool:serpapi,这也是可以自己定义的。例如下面的代码,自定义了一个weekday的工具。

import calendar
import dateutil.parser as parser
from datetime import date
from langchain.tools import Tool, tool

# 自定义工具
@tool("weekday")
def weekday(date_str: str) -> str:
    """Convert date to weekday name"""
    d = parser.parse(date_str)
    return calendar.day_name[d.weekday()]

tools += [weekday] ## 将自定义的tool添加到tools数组中

1.2 Prompt模板

要想写好Agent,Prompt模板也不可或缺。LangChain提供了一些Prompt模板,可以直接下载修改使用。再也不用绞尽脑汁自己从零开始写Prompt了!

先安装下Python包:

pip install langchainhub

执行以下代码:

from langchain import hub
import json

# 下载一个现有的 Prompt 模板
prompt = hub.pull("hwchase17/react")

print(prompt.template)

获得Prompt模板内容(我觉得比90%的人自己写的要好):


当然,这类Prompt模板可能不完全符合你的需求,所以你需要在此基础上作一些补充或修改。但是,总比自己从零开始写要好得多。

如果要修改,可以参考我下面的方式,主要注意点是prompt应该是一个PromptTemplate类型,而不是一个字符串

# from langchain import hub
# import json
# # 下载一个现有的 Prompt 模板
# prompt = hub.pull("hwchase17/react")
# print(prompt.template)

from langchain_core.prompts import ChatPromptTemplate
prompt_template = """
Answer the following questions as best you can. You have access to the following tools:

{tools}

Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action,如果其中有日期,请确保只输入日期,格式为:YYYY-MM-DD,不要有任何其它字符
Observation: the result of the action,如果其中有日期,请确保输出的日期格式为:YYYY-MM-DD,不要有任何其它字符
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Begin! Let's think step by step. Take a deep breath.

Question: {input}
Thought:{agent_scratchpad}
"""
prompt = ChatPromptTemplate.from_template(prompt_template)

1.3 创建Agent

准备好llm、tools、prompt之后,创建Agent

from langchain.agents import create_react_agent
agent = create_react_agent(llm, tools, prompt)

可能会报错:ImportError: cannot import name ‘create_react_agent’ from ‘langchain.agents’,解决方法:

pip install langchain --upgrade

1.4 创建Agent执行器

from langchain.agents import AgentExecutor
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
  • 不理解:为什么在创建Agent时传入了tools,这里创建Agent执行器还要再传入一遍tools?难道为多Agent区分各自tools ?

1.5 运行Agent

agent_executor.invoke({"input": "周杰伦生日那天是星期几"})

1.6 运行结果及遇到的坑

运行结果如下:

遇到的坑:

(1)无法识别出第一步应该先检索当前日期,直接就调用了weekday工具

  • 解决办法:优化Promot,加入了 “Let’s think step by step. Take a deep breath.”

不得不说,这两句是真好使

(2)weekday工具的输入不符合要求

  • 解决办法:优化Prompt,限制输入和输出的日期类型,见上文完整的Prompt

目前大模型规划的能力还是不行。以上例子中Agent主要是依靠大模型来进行流程控制,具有很大的不确定性和不可控性。

2. 补充知识

2.1 AgentTypes

LangChain的Agent模块封装了多种Agent类型可供使用。详细可参考:https://python.langchain.com/docs/modules/agents/agent_types/

Agent Type 预期模型类型 支持聊天历史记录 支持多输入工具 支持并行函数调用 需要的模型参数 何时使用
OpenAI Tools 聊天 工具 如果您正在使用最新的 OpenAI 模型(从 1106 开始)
OpenAI Functions 聊天 函数 如果您正在使用一个 OpenAI 模型,或者一个已经针对函数调用进行了微调并且公开了与 OpenAI 相同函数参数的开源模型
XML LLM 如果您正在使用 Anthropic 模型,或其他擅长处理 XML 的模型
Structured Chat 聊天 如果您需要支持具有多个输入工具的场景
JSON Chat 聊天 如果您正在使用擅长处理 JSON 的模型
ReAct LLM 如果您使用的是简单模型
Self Ask With Search LLM 如果您使用的是简单模型,并且只有一个搜索工具

2.2 各AgentTypes的Prompt模板

  • OpenAI functions
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/openai-functions-agent")
  • OpenAI tools
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/openai-tools-agent")
  • XML Agent
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/xml-agent-convo")
  • JSON Chat Agent
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/react-chat-json")
  • Structured chat
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/structured-chat-agent")
  • ReAct
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/react")
  • Self-ask with search
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/self-ask-with-search")

本文就到这里了。咱们对LangChain的Agent模块有了一个初步的认识,并且学会了如何利用LangChain实现一个简单的Agent,如何自定义自己的tool等。

当然,Agent不止于此,LangChain的Agent模块也不止于此,还需要更加细致的学习和挖掘。

如果觉得本文对你有帮助,麻烦点个赞和关注呗 ~~~

  • 大家好,我是同学小张
  • 欢迎 点赞 + 关注 👏,促使我持续学习持续干货输出
  • +v: jasper_8017 一起交流💬,一起进步💪。
  • 微信公众号也可搜【同学小张】 🙏
  • 踩坑不易,感谢关注和围观

本站文章一览:

版权声明:本文为博主作者:同学小张原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/Attitude93/article/details/136032370

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2024年4月22日
下一篇 2024年4月22日

相关推荐