大模型从入门到应用——LangChain:代理(Agents)-[计划与执行]

分类目录:《大模型从入门到应用》总目录

LangChain系列文章:


计划与执行代理通过首先规划要做的事情,然后执行子任务来实现目标。这个想法在很大程度上受到了BabyAGI以及《Plan-and-Solve》论文的启发。

  • 规划几乎总是由一个LLM(语言模型)来完成。
  • 执行通常由一个单独的代理(配备工具)来完成。
csharp 复制代码
# 导入模块
from langchain.chat_models import ChatOpenAI
from langchain.experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
from langchain.llms import OpenAI
from langchain import SerpAPIWrapper
from langchain.agents.tools import Tool
from langchain import LLMMathChain

# 工具
search = SerpAPIWrapper()
llm = OpenAI(temperature=0)
llm_math_chain = LLMMathChain.from_llm(llm=llm, verbose=True)
tools = [
    Tool(
        name = "Search",
        func=search.run,
        description="useful for when you need to answer questions about current events"
    ),
    Tool(
        name="Calculator",
        func=llm_math_chain.run,
        description="useful for when you need to answer questions about math"
    ),
]

# 规划器(Planner)、执行器(Executor)和代理(Agent)
model = ChatOpenAI(temperature=0)
planner = load_chat_planner(model)
executor = load_agent_executor(model, tools, verbose=True)
agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)

# 执行示例
agent.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")
Entering new PlanAndExecute chain...
steps=[Step(value="Search for Leo DiCaprio's girlfriend on the internet."), Step(value='Find her current age.'), Step(value='Raise her current age to the 0.43 power using a calculator or programming language.'), Step(value='Output the result.'), Step(value="Given the above steps taken, respond to the user's original question.\n\n")]

日志输出:

复制代码
日志输出:
Entering new AgentExecutor chain...
Action:

```{
  "action": "Search",
  "action_input": "Who is Leo DiCaprio's girlfriend?"
}```


Observation: DiCaprio broke up with girlfriend Camila Morrone, 25, in the summer of 2022, after dating for four years. He's since been linked to another famous supermodel -- Gigi Hadid. The power couple were first supposedly an item in September after being spotted getting cozy during a party at New York Fashion Week.
Thought:Based on the previous observation, I can provide the answer to the current objective. 
Action:
```{
  "action": "Final Answer",
  "action_input": "Leo DiCaprio is currently linked to Gigi Hadid."
}```


Finished chain.
*****

Step: Search for Leo DiCaprio's girlfriend on the internet.

Response: Leo DiCaprio is currently linked to Gigi Hadid.

Entering new AgentExecutor chain...
Action:
```{
  "action": "Search",
  "action_input": "What is Gigi Hadid's current age?"
}```

Observation: 28 years
Thought:Previous steps: steps=[(Step(value="Search for Leo DiCaprio's girlfriend on the internet."), StepResponse(response='Leo DiCaprio is currently linked to Gigi Hadid.'))]

Current objective: value='Find her current age.'

Action:
```{
  "action": "Search",
  "action_input": "What is Gigi Hadid's current age?"
}```


Observation: 28 years
Thought:Previous steps: steps=[(Step(value="Search for Leo DiCaprio's girlfriend on the internet."), StepResponse(response='Leo DiCaprio is currently linked to Gigi Hadid.')), (Step(value='Find her current age.'), StepResponse(response='28 years'))]

Current objective: None

Action:
```{
  "action": "Final Answer",
  "action_input": "Gigi Hadid's current age is 28 years."
}```



Finished chain.
*****

Step: Find her current age.

Response: Gigi Hadid's current age is 28 years.

Entering new AgentExecutor chain...
Action:
```{
  "action": "Calculator",
  "action_input": "28 ** 0.43"
}```


Entering new LLMMathChain chain...
28 ** 0.43
```text28 ** 0.43```
...numexpr.evaluate("28 ** 0.43")...

Answer: 4.1906168361987195
Finished chain.

Observation: Answer: 4.1906168361987195
Thought:The next step is to provide the answer to the user's question.

Action:
```{
  "action": "Final Answer",
  "action_input": "Gigi Hadid's current age raised to the 0.43 power is approximately 4.19."
}```



Finished chain.
*****

Step: Raise her current age to the 0.43 power using a calculator or programming language.

Response: Gigi Hadid's current age raised to the 0.43 power is approximately 4.19.

Entering new AgentExecutor chain...
Action:
```{
  "action": "Final Answer",
  "action_input": "The result is approximately 4.19."
}```


Finished chain.
*****

Step: Output the result.

Response: The result is approximately 4.19.

Entering new AgentExecutor chain...
Action:
```{
  "action": "Final Answer",
  "action_input": "Gigi Hadid's current age raised to the 0.43 power is approximately 4.19."
}```


Finished chain.
*****

Step: Given the above steps taken, respond to the user's original question.



Response: Gigi Hadid's current age raised to the 0.43 power is approximately 4.19.
Finished chain.

输出:

复制代码
"Gigi Hadid's current age raised to the 0.43 power is approximately 4.19."

参考文献:

1\] LangChain官方网站:https://www.langchain.com/ \[2\] LangChain 🦜️🔗 中文网,跟着LangChain一起学LLM/GPT开发:https://www.langchain.com.cn/ \[3\] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架:http://www.cnlangchain.com/

相关推荐
小黑随笔33 分钟前
【Golang玩转本地大模型实战(二):基于Golang + Web实现AI对话页面】
前端·人工智能·golang
上海源易38 分钟前
GEO vs SEO:从搜索引擎到生成引擎的优化新思路
人工智能·搜索引擎·chatgpt·deepseek·ai搜索优化
从零开始学习人工智能42 分钟前
深度解析 MindTorch:无缝迁移 PyTorch 到 MindSpore 的高效工具
人工智能·pytorch·python
Cleo_Gao1 小时前
交我算使用保姆教程:在计算中心利用singularity容器训练深度学习模型
人工智能·深度学习·容器·计算中心
古希腊掌管学习的神1 小时前
[Agent]AI Agent入门02——ReAct 基本理论与实战
人工智能·语言模型·chatgpt·gpt-3·agent
C灿灿数模1 小时前
2025五一杯数学建模C题:社交媒体平台用户分析问题;思路分析+模型代码
数据库·人工智能·python
__Benco1 小时前
OpenHarmony - 小型系统内核(LiteOS-A)(十三),LMS调测
人工智能·harmonyos
Takoony1 小时前
Transformer Prefill阶段并行计算:本质、流程与思考
人工智能·深度学习·transformer
创客匠人老蒋2 小时前
当 AI 成为 “数字新物种”:人类职业的重构与进化
人工智能
说私域2 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的私域电商与微商融合创新研究
人工智能·小程序·开源·零售