小智头像图片
AI教程 2025年01月16日
0 收藏 0 点赞 506 浏览 11729 个字
摘要 :

面向开发者的LLM入门课程-内置工具PythonREPLTool: 使用LangChain内置工具PythonREPLTool 我们创建一个能将顾客名字转换为拼音的 python 代理,步骤与上一部分的一样:……

哈喽!伙伴们,我是小智,你们的AI向导。欢迎来到每日的AI学习时间。今天,我们将一起深入AI的奇妙世界,探索“面向开发者的LLM入门课程-内置工具PythonREPLTool”,并学会本篇文章中所讲的全部知识点。还是那句话“不必远征未知,只需唤醒你的潜能!”跟着小智的步伐,我们终将学有所成,学以致用,并发现自身的更多可能性。话不多说,现在就让我们开始这场激发潜能的AI学习之旅吧。

面向开发者的LLM入门课程-内置工具PythonREPLTool

面向开发者的LLM入门课程-内置工具PythonREPLTool:

使用LangChain内置工具PythonREPLTool

我们创建一个能将顾客名字转换为拼音的 python 代理,步骤与上一部分的一样:

from langchain.agents.agent_toolkits import create_python_agent
from langchain.tools.python.tool import PythonREPLTool

agent = create_python_agent(
llm, #使用前面一节已经加载的大语言模型
tool=PythonREPLTool(), #使用Python交互式环境工具 REPLTool
verbose=True #输出中间步骤
)
customer_list = [“小明”,”小黄”,”小红”,”小蓝”,”小橘”,”小绿”,]

agent.run(f”将使用pinyin拼音库这些客户名字转换为拼音,并打印输出列表: {customer_list}。”)

> Entering new AgentExecutor chain…
I need to use the pinyin library to convert the names to pinyin. I can then print
out the list of converted names.
Action: Python_REPL
Action Input: import pinyin
Observation:
Thought:I have imported the pinyin library. Now I can use it to convert the names
to pinyin.
Action: Python_REPL
Action Input: names = [‘小明’, ‘小黄’, ‘小红’, ‘小蓝’, ‘小橘’, ‘小绿’]
pinyin_names = [pinyin.get(i, format=’strip’) for i in names]
print(pinyin_names)
Observation: [‘xiaoming’, ‘xiaohuang’, ‘xiaohong’, ‘xiaolan’, ‘xiaoju’, ‘xiaolv’]

Thought:I have successfully converted the names to pinyin and printed out the
list of converted names.
Final Answer: [‘xiaoming’, ‘xiaohuang’, ‘xiaohong’, ‘xiaolan’, ‘xiaoju’,
‘xiaolv’]

> Finished chain.

“[‘xiaoming’, ‘xiaohuang’, ‘xiaohong’, ‘xiaolan’, ‘xiaoju’, ‘xiaolv’]”

在调试(debug)模式下再次运行,我们可以把上面的6步分别对应到下面的具体流程

1. 模型对于接下来需要做什么,给出思考(Thought)
· [chain/start] [1:chain:AgentExecutor] Entering Chain run with input
· [chain/start] [1:chain:AgentExecutor > 2:chain:LLMChain] Entering Chain run with input
· [llm/start] [1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] Entering LLM run with input
· [llm/end] [1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] [1.91s] Exiting LLM run with output
· [chain/end] [1:chain:AgentExecutor > 2:chain:LLMChain] [1.91s] Exiting Chain run with output

2. 模型基于思考采取行动(Action), 因为使用的工具不同,Action的输出也和之前有所不同,这里输出的为python代码 import pinyin
· [tool/start] [1:chain:AgentExecutor > 4:tool:Python REPL] Entering Tool run with input
· [tool/end] [1:chain:AgentExecutor > 4:tool:Python_REPL] [1.28ms] Exiting Tool run with output

3. 模型得到观察(Observation)
· [chain/start] [1:chain:AgentExecutor > 5:chain:LLMChain] Entering Chain run with input

4. 基于观察,模型对于接下来需要做什么,给出思考(Thought)
· [llm/start] [1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI] Entering LLM run with input
· [llm/end] [1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI] [3.48s] Exiting LLM run with output

5. 给出最终答案(Final Answer)
· [chain/end] [1:chain:AgentExecutor > 5:chain:LLMChain] [3.48s] Exiting Chain run with output

6. 返回最终答案。
· [chain/end] [1:chain:AgentExecutor] [19.20s] Exiting Chain run with output

import langchain
langchain.debug=True
agent.run(f”使用pinyin拼音库将这些客户名字转换为拼音,并打印输出列表: {customer_list}”)
langchain.debug=False

[chain/start] [1:chain:AgentExecutor] Entering Chain run with input:
{
“input”: “使用pinyin拼音库将这些客户名字转换为拼音,并打印输出列表: [‘小明’, ‘小黄’, ‘小
红’, ‘小蓝’, ‘小橘’, ‘小绿’]”
}
[chain/start] [1:chain:AgentExecutor > 2:chain:LLMChain] Entering Chain run with
input:
{
“input”: “使用pinyin拼音库将这些客户名字转换为拼音,并打印输出列表: [‘小明’, ‘小黄’, ‘小
红’, ‘小蓝’, ‘小橘’, ‘小绿’]”,
“agent_scratchpad”: “”,
“stop”: [
“nObservation:”,
“ntObservation:”
]
}
[llm/start] [1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI]
Entering LLM run with input:
{
“prompts”: [
“Human: You are an agent designed to write and execute python code to answer
questions.nYou have access to a python REPL, which you can use to execute python
code.nIf you get an error, debug your code and try again.nOnly use the output
of your code to answer the question. nYou might know the answer without running
any code, but you should still run the code to get the answer.nIf it does not
seem like you can write code to answer the question, just return “I don’t know”
as the answer.nnnPython_REPL: A Python shell. Use this to execute python
commands. Input should be a valid python command. If you want to see the output
of a value, you should print it out with `print(…)`.nnUse the following
format:nnQuestion: the input question you must answernThought: you should
always think about what to donAction: the action to take, should be one of
[Python_REPL]nAction Input: the input to the actionnObservation: the result of
the actionn… (this Thought/Action/Action Input/Observation can repeat N
times)nThought: I now know the final answernFinal Answer: the final answer to
the original input questionnnBegin!nnQuestion: 使用pinyin拼音库将这些客户名字转换
为拼音,并打印输出列表: [‘小明’, ‘小黄’, ‘小红’, ‘小蓝’, ‘小橘’, ‘小绿’]nThought:”
]
}
[llm/end] [1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] [2.32s]
Exiting LLM run with output:
{
“generations”: [
[
{
“text”: “I need to use the pinyin library to convert the names to pinyin.
I can then print out the list of converted names.nAction: Python_REPLnAction
Input: import pinyin”,
“generation_info”: {
“finish_reason”: “stop”
},
“message”: {
“lc”: 1,
“type”: “constructor”,
“id”: [
“langchain”,
“schema”,
“messages”,
“AIMessage”
],
“kwargs”: {
“content”: “I need to use the pinyin library to convert the names to
pinyin. I can then print out the list of converted names.nAction:
Python_REPLnAction Input: import pinyin”,
“additional_kwargs”: {}
}
}
}
]
],
“llm_output”: {
“token_usage”: {
“prompt_tokens”: 320,
“completion_tokens”: 39,
“total_tokens”: 359
},
“model_name”: “gpt-3.5-turbo”
},
“run”: null
}
[chain/end] [1:chain:AgentExecutor > 2:chain:LLMChain] [2.33s] Exiting Chain run
with output:
{
“text”: “I need to use the pinyin library to convert the names to pinyin. I can
then print out the list of converted names.nAction: Python_REPLnAction Input:
import pinyin”
}
[tool/start] [1:chain:AgentExecutor > 4:tool:Python_REPL] Entering Tool run with
input:
“import pinyin”
[tool/end] [1:chain:AgentExecutor > 4:tool:Python_REPL] [1.5659999999999998ms]
Exiting Tool run with output:
“”
[chain/start] [1:chain:AgentExecutor > 5:chain:LLMChain] Entering Chain run with
input:
{
“input”: “使用pinyin拼音库将这些客户名字转换为拼音,并打印输出列表: [‘小明’, ‘小黄’, ‘小
红’, ‘小蓝’, ‘小橘’, ‘小绿’]”,
“agent_scratchpad”: “I need to use the pinyin library to convert the names to
pinyin. I can then print out the list of converted names.nAction:
Python_REPLnAction Input: import pinyinnObservation: nThought:”,
“stop”: [
“nObservation:”,
“ntObservation:”
]
}
[llm/start] [1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI]
Entering LLM run with input:
{
“prompts”: [
“Human: You are an agent designed to write and execute python code to answer
questions.nYou have access to a python REPL, which you can use to execute python
code.nIf you get an error, debug your code and try again.nOnly use the output
of your code to answer the question. nYou might know the answer without running
any code, but you should still run the code to get the answer.nIf it does not
seem like you can write code to answer the question, just return “I don’t know”
as the answer.nnnPython_REPL: A Python shell. Use this to execute python
commands. Input should be a valid python command. If you want to see the output
of a value, you should print it out with `print(…)`.nnUse the following
format:nnQuestion: the input question you must answernThought: you should
always think about what to donAction: the action to take, should be one of
[Python_REPL]nAction Input: the input to the actionnObservation: the result of
the actionn… (this Thought/Action/Action Input/Observation can repeat N
times)nThought: I now know the final answernFinal Answer: the final answer to
the original input questionnnBegin!nnQuestion: 使用pinyin拼音库将这些客户名字转换
为拼音,并打印输出列表: [‘小明’, ‘小黄’, ‘小红’, ‘小蓝’, ‘小橘’, ‘小绿’]nThought:I need
to use the pinyin library to convert the names to pinyin. I can then print out
the list of converted names.nAction: Python_REPLnAction Input: import
pinyinnObservation: nThought:”
]
}
[llm/end] [1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI] [4.09s]
Exiting LLM run with output:
{
“generations”: [
[
{
“text”: “I have imported the pinyin library. Now I can use it to convert
the names to pinyin.nAction: Python_REPLnAction Input: names = [‘小明’, ‘小黄’,
‘小红’, ‘小蓝’, ‘小橘’, ‘小绿’]npinyin_names = [pinyin.get(i, format=’strip’) for i
in names]nprint(pinyin_names)”,
“generation_info”: {
“finish_reason”: “stop”
},
“message”: {
“lc”: 1,
“type”: “constructor”,
“id”: [
“langchain”,
“schema”,
“messages”,
“AIMessage”
],
“kwargs”: {
“content”: “I have imported the pinyin library. Now I can use it to
convert the names to pinyin.nAction: Python_REPLnAction Input: names = [‘小明’,
‘小黄’, ‘小红’, ‘小蓝’, ‘小橘’, ‘小绿’]npinyin_names = [pinyin.get(i,
format=’strip’) for i in names]nprint(pinyin_names)”,
“additional_kwargs”: {}
}
}
}
]
],
“llm_output”: {
“token_usage”: {
“prompt_tokens”: 365,
“completion_tokens”: 87,
“total_tokens”: 452
},
“model_name”: “gpt-3.5-turbo”
},
“run”: null
}
[chain/end] [1:chain:AgentExecutor > 5:chain:LLMChain] [4.09s] Exiting Chain run
with output:
{
“text”: “I have imported the pinyin library. Now I can use it to convert the
names to pinyin.nAction: Python_REPLnAction Input: names = [‘小明’, ‘小黄’, ‘小
红’, ‘小蓝’, ‘小橘’, ‘小绿’]npinyin_names = [pinyin.get(i, format=’strip’) for i
in names]nprint(pinyin_names)”
}
[tool/start] [1:chain:AgentExecutor > 7:tool:Python_REPL] Entering Tool run with
input:
“names = [‘小明’, ‘小黄’, ‘小红’, ‘小蓝’, ‘小橘’, ‘小绿’]
pinyin_names = [pinyin.get(i, format=’strip’) for i in names]
print(pinyin_names)”
[tool/end] [1:chain:AgentExecutor > 7:tool:Python_REPL] [0.8809999999999999ms]
Exiting Tool run with output:
“[‘xiaoming’, ‘xiaohuang’, ‘xiaohong’, ‘xiaolan’, ‘xiaoju’, ‘xiaolv’]”
[chain/start] [1:chain:AgentExecutor > 8:chain:LLMChain] Entering Chain run with
input:
{
“input”: “使用pinyin拼音库将这些客户名字转换为拼音,并打印输出列表: [‘小明’, ‘小黄’, ‘小
红’, ‘小蓝’, ‘小橘’, ‘小绿’]”,
“agent_scratchpad”: “I need to use the pinyin library to convert the names to
pinyin. I can then print out the list of converted names.nAction:
Python_REPLnAction Input: import pinyinnObservation: nThought:I have imported
the pinyin library. Now I can use it to convert the names to pinyin.nAction:
Python_REPLnAction Input: names = [‘小明’, ‘小黄’, ‘小红’, ‘小蓝’, ‘小橘’, ‘小
绿’]npinyin_names = [pinyin.get(i, format=’strip’) for i in
names]nprint(pinyin_names)nObservation: [‘xiaoming’, ‘xiaohuang’, ‘xiaohong’,
‘xiaolan’, ‘xiaoju’, ‘xiaolv’]nnThought:”,
“stop”: [
“nObservation:”,
“ntObservation:”
]
}
[llm/start] [1:chain:AgentExecutor > 8:chain:LLMChain > 9:llm:ChatOpenAI]
Entering LLM run with input:
{
“prompts”: [
“Human: You are an agent designed to write and execute python code to answer
questions.nYou have access to a python REPL, which you can use to execute python
code.nIf you get an error, debug your code and try again.nOnly use the output
of your code to answer the question. nYou might know the answer without running
any code, but you should still run the code to get the answer.nIf it does not
seem like you can write code to answer the question, just return “I don’t know”
as the answer.nnnPython_REPL: A Python shell. Use this to execute python
commands. Input should be a valid python command. If you want to see the output
of a value, you should print it out with `print(…)`.nnUse the following
format:nnQuestion: the input question you must answernThought: you should
always think about what to donAction: the action to take, should be one of
[Python_REPL]nAction Input: the input to the actionnObservation: the result of
the actionn… (this Thought/Action/Action Input/Observation can repeat N
times)nThought: I now know the final answernFinal Answer: the final answer to
the original input questionnnBegin!nnQuestion: 使用pinyin拼音库将这些客户名字转换
为拼音,并打印输出列表: [‘小明’, ‘小黄’, ‘小红’, ‘小蓝’, ‘小橘’, ‘小绿’]nThought:I need
to use the pinyin library to convert the names to pinyin. I can then print out
the list of converted names.nAction: Python_REPLnAction Input: import
pinyinnObservation: nThought:I have imported the pinyin library. Now I can use
it to convert the names to pinyin.nAction: Python_REPLnAction Input: names =
[‘小明’, ‘小黄’, ‘小红’, ‘小蓝’, ‘小橘’, ‘小绿’]npinyin_names = [pinyin.get(i,
format=’strip’) for i in names]nprint(pinyin_names)nObservation: [‘xiaoming’,
‘xiaohuang’, ‘xiaohong’, ‘xiaolan’, ‘xiaoju’, ‘xiaolv’]nnThought:”
]
}
[llm/end] [1:chain:AgentExecutor > 8:chain:LLMChain > 9:llm:ChatOpenAI] [2.05s]
Exiting LLM run with output:
{
“generations”: [
[
{
“text”: “I have successfully converted the names to pinyin and printed
out the list of converted names.nFinal Answer: [‘xiaoming’, ‘xiaohuang’,
‘xiaohong’, ‘xiaolan’, ‘xiaoju’, ‘xiaolv’]”,
“generation_info”: {
“finish_reason”: “stop”
},
“message”: {
“lc”: 1,
“type”: “constructor”,
“id”: [
“langchain”,
“schema”,
“messages”,
“AIMessage”
],
“kwargs”: {
“content”: “I have successfully converted the names to pinyin and
printed out the list of converted names.nFinal Answer: [‘xiaoming’, ‘xiaohuang’,
‘xiaohong’, ‘xiaolan’, ‘xiaoju’, ‘xiaolv’]”,
“additional_kwargs”: {}
}
}
}
]
],
“llm_output”: {
“token_usage”: {
“prompt_tokens”: 483,
“completion_tokens”: 48,
“total_tokens”: 531
},
“model_name”: “gpt-3.5-turbo”
},
“run”: null
}
[chain/end] [1:chain:AgentExecutor > 8:chain:LLMChain] [2.05s] Exiting Chain run
with output:
{
“text”: “I have successfully converted the names to pinyin and printed out the
list of converted names.nFinal Answer: [‘xiaoming’, ‘xiaohuang’, ‘xiaohong’,
‘xiaolan’, ‘xiaoju’, ‘xiaolv’]”
}
[chain/end] [1:chain:AgentExecutor] [8.47s] Exiting Chain run with output:
{
“output”: “[‘xiaoming’, ‘xiaohuang’, ‘xiaohong’, ‘xiaolan’, ‘xiaoju’,
‘xiaolv’]”
}

面向开发者的LLM入门课程-定义工具在代理中使用
面向开发者的LLM入门课程-定义工具在代理中使用:定义自己的工具并在代理中使用 在本节,我们将创建和使用自定义时间工具。LangChian...

嘿,伙伴们,今天我们的AI探索之旅已经圆满结束。关于“面向开发者的LLM入门课程-内置工具PythonREPLTool”的内容已经分享给大家了。感谢你们的陪伴,希望这次旅程让你对AI能够更了解、更喜欢。谨记,精准提问是解锁AI潜能的钥匙哦!如果有小伙伴想要了解学习更多的AI知识,请关注我们的官网“AI智研社”,保证让你收获满满呦!

微信打赏二维码 微信扫一扫

支付宝打赏二维码 支付宝扫一扫

版权: 转载请注明出处:https://www.ai-blog.cn/2736.html

相关推荐
03-12

DeepSeek企业级部署实战指南: 对于个人开发者或尝鲜者而言,本地想要部署 DeepSeek 有很多种方案…

小智头像图片
69
03-12

如何使用DeepSeek助你增强求职竞争力: 职场篇 常见的简历问题 1. 格式混乱或排版不专业 • 问题:…

小智头像图片
186
03-12

DeepSeek官方提示词:让你的API应用和官方一样强: 本文讨论了DeepSeek官方关于让API应用和官方一…

小智头像图片
85
03-12

关于 DeepSeek 的研究和思考 (Archerman Capital): 关于这几天很火的 DeepSeek, 我们 (Archerman …

小智头像图片
97

AI教程DeepSeek提示词之代码解释: 代码解释​ ​ 对代码进行解释,来帮助理解代码内容。​ ​ ​ 请解…

小智头像图片
94

AI教程DeepSeek提示词之代码改写: 代码改写​ ​ 对代码进行修改,来实现纠错、注释、调优等。​ ​ …

小智头像图片
506

AI教程DeepSeek提示词之代码生成: 代码生成​ ​ 让模型生成一段完成特定功能的代码。​ ​ 用户提示…

小智头像图片
506

AI教程DeepSeek提示词之散文写作: 散文写作​ ​ 让模型根据提示词创作散文。​ ​ 用户提示词: 以孤…

小智头像图片
506
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力原创内容

快速提升站内名气成为大牛

扫描二维码

手机访问本站

二维码
vip弹窗图片