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

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

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

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

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

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

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

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

0.1 Google搜索Tool

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

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

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

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

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

bash 复制代码
pip install google-search-results

0.2 运行示例程序

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

  • 示例程序完整代码
bash 复制代码
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的工具。

python 复制代码
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包:

bash 复制代码
pip install langchainhub

执行以下代码:

python 复制代码
from langchain import hub
import json

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

print(prompt.template)

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

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

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

python 复制代码
# 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

python 复制代码
from langchain.agents import create_react_agent
agent = create_react_agent(llm, tools, prompt)

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

bash 复制代码
pip install langchain --upgrade

1.4 创建Agent执行器

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

1.5 运行Agent

python 复制代码
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类型可供使用。详细可参考:python.langchain.com/docs/module...

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
python 复制代码
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/openai-functions-agent")
  • OpenAI tools
python 复制代码
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/openai-tools-agent")
  • XML Agent
python 复制代码
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/xml-agent-convo")
  • JSON Chat Agent
python 复制代码
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/react-chat-json")
  • Structured chat
python 复制代码
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/structured-chat-agent")
  • ReAct
python 复制代码
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/react")
  • Self-ask with search
python 复制代码
# 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 一起交流💬,一起进步💪。
  • 微信公众号也可搜【同学小张】 🙏
  • 踩坑不易,感谢关注和围观

本站文章一览:

相关推荐
回眸&啤酒鸭4 天前
【回眸】Minicart 电商购物车核心功能落地指南
人工智能
一隅论数智4 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
AI的探索之旅4 天前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
AlbertZein4 天前
Step-5-Preview 上手实测:3D 游戏、金融分析、网页设计一次跑完
人工智能·aigc
LaughingZhu4 天前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
美狐美颜SDK开放平台4 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
wukangjupingbb4 天前
智能网联汽车安全能力框架
人工智能
龙亘川4 天前
明月照湾区,智启新赛道:从顶流文旅IP盛会看智慧文旅升级路径
人工智能·智慧城市·开源软件·数据可视化
飞猫的边缘AI4 天前
边缘AI应用:家用AI摄像头怎么做数据训练?
人工智能·边缘计算·ai算法·边缘ai