程序员的 AI 开发第一课
郑晔
开源项目 Moco 作者
1689 人已学习
新⼈⾸单¥59
程序员的 AI 开发第一课
15
15
1.0x
00:00/00:00
登录|注册

14|用LangChain实现一个Agent

你好,我是郑晔!
上一讲,我们抛开了 LangChain,基于 OpenAI Python 程序库实现了一个 Agent,主要是为了让你更好地理解 Agent 的运作机理。其中最核心的部分就是一个循环,不断地执行各种动作,直到判断运行的结果是停下来。
现在,你已经知道了 Agent 是怎样运作的,这一讲,我们再回来看看如何用 LangChain 实现一个 Agent,相信有了之前的铺垫,这一讲的代码就比较容易理解了。

基于 LangChain 实现的 Agent

从实现的功能上讲,我们这一讲要实现的功能和上一讲是完全相同的,只不过我们采用了 LangChain 的基础设施,代码如下所示:
from langchain.agents import AgentExecutor, create_react_agent
from langchain_core.prompts import PromptTemplate
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
@tool
def calculate(what: str) -> float:
"""Runs a calculation and returns the number - uses Python so be sure to use floating point syntax if necessary"""
return eval(what)
@tool
def ask_fruit_unit_price(fruit: str) -> str:
"""Asks the user for the price of a fruit"""
if fruit.casefold() == "apple":
return "Apple unit price is 10/kg"
elif fruit.casefold() == "banana":
return "Banana unit price is 6/kg"
else:
return "{} unit price is 20/kg".format(fruit)
prompt = PromptTemplate.from_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
Observation: the result of the action
... (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!
Question: {input}
Thought:{agent_scratchpad}''')
tools = [calculate, ask_fruit_unit_price]
model = ChatOpenAI(model="gpt-4o-mini")
agent = create_react_agent(model, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
result = agent_executor.invoke({
"input": "What is the total price of 3 kg of apple and 2 kg of banana?"
})
print(result)
确认放弃笔记?
放弃后所记笔记将不保留。
新功能上线,你的历史笔记已初始化为私密笔记,是否一键批量公开?
批量公开的笔记不会为你同步至部落
公开
同步至部落
取消
完成
0/2000
荧光笔
直线
曲线
笔记
复制
AI
  • 深入了解
  • 翻译
    • 英语
    • 中文简体
    • 法语
    • 德语
    • 日语
    • 韩语
    • 俄语
    • 西班牙语
  • 解释
  • 总结

1. 本文介绍了如何使用LangChain实现一个Agent,通过代码示例展示了Agent的运作机理和实现过程。 2. 文中详细介绍了如何基于LangChain实现Agent的功能,包括工具的定义和使用、提示词的构建以及Agent的组装。 3. 通过示例代码展示了Agent的执行过程,包括询问水果单价和计算总价的具体步骤。 4. 介绍了AgentExecutor的作用,以及如何使用该工具来执行Agent并输出结果。 5. 工具(Tool)和工具包(Toolkit)的概念在LangChain中起着重要作用,工具负责与模型交互,工具包是一组相关的工具。 6. 学习编写工具是构建基于LangChain的Agent的关键点。 7. LangChain社区提供了大量的工具,可以用它们来改造代码,实现新的Agent。

仅可试看部分内容,如需阅读全部内容,请付费购买文章所属专栏
《程序员的 AI 开发第一课》
新⼈⾸单¥59
立即购买
登录 后留言

精选留言

由作者筛选后的优质留言将会公开显示,欢迎踊跃留言。
收起评论
显示
设置
留言
收藏
沉浸
阅读
分享
手机端
快捷键
回顶部
文章页面操作
MAC
windows
作用
esc
esc
退出沉浸式阅读
shift + f
f11
进入/退出沉浸式
command + ⬆️
home
滚动到页面顶部
command + ⬇️
end
滚动到页面底部
⬅️ (仅针对订阅)
⬅️ (仅针对订阅)
上一篇
➡️ (仅针对订阅)
➡️ (仅针对订阅)
下一篇
command + j
page up
向下滚动一屏
command + k
page down
向上滚动一屏
p
p
音频播放/暂停
j
j
向下滚动一点
k
k
向上滚动一点
空格
空格
向下滚动一屏
播放器操作
MAC
windows
作用
esc
esc
退出全屏
⬅️
⬅️
快退
➡️
➡️
快进
空格
空格
视频播放/暂停(视频全屏时生效)