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

面向开发者的LLM入门课程-代理英文版(2): 2. 使用LangChain内置工具PythonREPLTool from langchain.agents.agent_toolkits import create_python_agent from langcha……

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

面向开发者的LLM入门课程-代理英文版(2)

面向开发者的LLM入门课程-代理英文版(2):

2. 使用LangChain内置工具PythonREPLTool

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 = [[“Harrison”, “Chase”],
[“Lang”, “Chain”],
[“Dolly”, “Too”],
[“Elle”, “Elem”],
[“Geoff”,”Fusion”],
[“Trance”,”Former”],
[“Jen”,”Ayai”]
]
agent.run(f”””Sort these customers by
last name and then first name
and print the output: {customer_list}”””)

> Entering new AgentExecutor chain…
I can use the `sorted()` function to sort the list of customers. I will need to
provide a key function that specifies the sorting order based on last name and
then first name.
Action: Python_REPL
Action Input: sorted([[‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’], [‘Dolly’, ‘Too’],
[‘Elle’, ‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Jen’, ‘Ayai’]],
key=lambda x: (x[1], x[0]))
Observation:
Thought:The customers have been sorted by last name and then first name.
Final Answer: [[‘Jen’, ‘Ayai’], [‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’],
[‘Elle’, ‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Dolly’, ‘Too’]]

> Finished chain.

“[[‘Jen’, ‘Ayai’], [‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’], [‘Elle’, ‘Elem’],
[‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Dolly’, ‘Too’]]”

import langchain
langchain.debug=True
agent.run(f”””Sort these customers by
last name and then first name
and print the output: {customer_list}”””)
langchain.debug=False

[chain/start] [1:chain:AgentExecutor] Entering Chain run with input:
{
“input”: “Sort these customers by last name and then first name and print the
output: [[‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’], [‘Dolly’, ‘Too’], [‘Elle’,
‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Jen’, ‘Ayai’]]”
}
[chain/start] [1:chain:AgentExecutor > 2:chain:LLMChain] Entering Chain run with
input:
{
“input”: “Sort these customers by last name and then first name and print the
output: [[‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’], [‘Dolly’, ‘Too’], [‘Elle’,
‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Jen’, ‘Ayai’]]”,
“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: Sort these customers by last
name and then first name and print the output: [[‘Harrison’, ‘Chase’], [‘Lang’,
‘Chain’], [‘Dolly’, ‘Too’], [‘Elle’, ‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’,
‘Former’], [‘Jen’, ‘Ayai’]]nThought:”
]
}
[llm/end] [1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] [4.59s]
Exiting LLM run with output:
{
“generations”: [
[
{
“text”: “I can use the `sorted()` function to sort the list of customers.
I will need to provide a key function that specifies the sorting order based on
last name and then first name.nAction: Python_REPLnAction Input:
sorted([[‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’], [‘Dolly’, ‘Too’], [‘Elle’,
‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Jen’, ‘Ayai’]], key=lambda
x: (x[1], x[0]))”,
“generation_info”: {
“finish_reason”: “stop”
},
“message”: {
“lc”: 1,
“type”: “constructor”,
“id”: [
“langchain”,
“schema”,
“messages”,
“AIMessage”
],
“kwargs”: {
“content”: “I can use the `sorted()` function to sort the list of
customers. I will need to provide a key function that specifies the sorting order
based on last name and then first name.nAction: Python_REPLnAction Input:
sorted([[‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’], [‘Dolly’, ‘Too’], [‘Elle’,
‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Jen’, ‘Ayai’]], key=lambda
x: (x[1], x[0]))”,
“additional_kwargs”: {}
}
}
}
]
],
“llm_output”: {
“token_usage”: {
“prompt_tokens”: 328,
“completion_tokens”: 112,
“total_tokens”: 440
},
“model_name”: “gpt-3.5-turbo”
},
“run”: null
}
[chain/end] [1:chain:AgentExecutor > 2:chain:LLMChain] [4.59s] Exiting Chain run
with output:
{
“text”: “I can use the `sorted()` function to sort the list of customers. I
will need to provide a key function that specifies the sorting order based on
last name and then first name.nAction: Python_REPLnAction Input:
sorted([[‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’], [‘Dolly’, ‘Too’], [‘Elle’,
‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Jen’, ‘Ayai’]], key=lambda
x: (x[1], x[0]))”
}
[tool/start] [1:chain:AgentExecutor > 4:tool:Python_REPL] Entering Tool run with
input:
“sorted([[‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’], [‘Dolly’, ‘Too’], [‘Elle’,
‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Jen’, ‘Ayai’]], key=lambda
x: (x[1], x[0]))”
[tool/end] [1:chain:AgentExecutor > 4:tool:Python_REPL] [1.35ms] Exiting Tool run
with output:
“”
[chain/start] [1:chain:AgentExecutor > 5:chain:LLMChain] Entering Chain run with
input:
{
“input”: “Sort these customers by last name and then first name and print the
output: [[‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’], [‘Dolly’, ‘Too’], [‘Elle’,
‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Jen’, ‘Ayai’]]”,
“agent_scratchpad”: “I can use the `sorted()` function to sort the list of
customers. I will need to provide a key function that specifies the sorting order
based on last name and then first name.nAction: Python_REPLnAction Input:
sorted([[‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’], [‘Dolly’, ‘Too’], [‘Elle’,
‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Jen’, ‘Ayai’]], key=lambda
x: (x[1], x[0]))nObservation: 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: Sort these customers by last
name and then first name and print the output: [[‘Harrison’, ‘Chase’], [‘Lang’,
‘Chain’], [‘Dolly’, ‘Too’], [‘Elle’, ‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’,
‘Former’], [‘Jen’, ‘Ayai’]]nThought:I can use the `sorted()` function to sort
the list of customers. I will need to provide a key function that specifies the
sorting order based on last name and then first name.nAction:
Python_REPLnAction Input: sorted([[‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’],
[‘Dolly’, ‘Too’], [‘Elle’, ‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’],
[‘Jen’, ‘Ayai’]], key=lambda x: (x[1], x[0]))nObservation: nThought:”
]
}
[llm/end] [1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI] [3.89s]
Exiting LLM run with output:
{
“generations”: [
[
{
“text”: “The customers have been sorted by last name and then first
name.nFinal Answer: [[‘Jen’, ‘Ayai’], [‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’],
[‘Elle’, ‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Dolly’, ‘Too’]]”,
“generation_info”: {
“finish_reason”: “stop”
},
“message”: {
“lc”: 1,
“type”: “constructor”,
“id”: [
“langchain”,
“schema”,
“messages”,
“AIMessage”
],
“kwargs”: {
“content”: “The customers have been sorted by last name and then
first name.nFinal Answer: [[‘Jen’, ‘Ayai’], [‘Harrison’, ‘Chase’], [‘Lang’,
‘Chain’], [‘Elle’, ‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Dolly’,
‘Too’]]”,
“additional_kwargs”: {}
}
}
}
]
],
“llm_output”: {
“token_usage”: {
“prompt_tokens”: 445,
“completion_tokens”: 67,
“total_tokens”: 512
},
“model_name”: “gpt-3.5-turbo”
},
“run”: null
}
[chain/end] [1:chain:AgentExecutor > 5:chain:LLMChain] [3.89s] Exiting Chain run
with output:
{
“text”: “The customers have been sorted by last name and then first
name.nFinal Answer: [[‘Jen’, ‘Ayai’], [‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’],
[‘Elle’, ‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Dolly’, ‘Too’]]”
}
[chain/end] [1:chain:AgentExecutor] [8.49s] Exiting Chain run with output:
{
“output”: “[[‘Jen’, ‘Ayai’], [‘Harrison’, ‘Chase’], [‘Lang’, ‘Chain’], [‘Elle’,
‘Elem’], [‘Geoff’, ‘Fusion’], [‘Trance’, ‘Former’], [‘Dolly’, ‘Too’]]”
}

面向开发者的LLM入门课程-代理英文版(3)
面向开发者的LLM入门课程-代理英文版(3):3. 定义自己的工具并在代理中使用 # 导入tool函数装饰器 from langchain.agents imp...

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

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

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

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

相关推荐

AI写作-小说大纲在小说中的作用: 大纲主要讲两个部分: 一、大纲有什么用。 二、如何写大纲。 我上…

小智头像图片
144

AI写作-小说大纲的写法和新手常见的问题: 对于大纲的问题,相信大家深深浅浅的都有些了解。今天结…

小智头像图片
437

AI写作-小说的基础提纲结构: 很多的新作者在推出了大量的作品后,却是留给读者的是迷茫,包括作者…

小智头像图片
437

AI写作-小说大纲详解: 案例一:第 1-2 章(通过的大纲分章) 涵柔睁开眼睛,发现正被一个脑满肠肥的…

小智头像图片
437

AI写作-小说大纲格式与内容概述: 什么叫大纲?大纲是一本小说的根本。他为小说提供了精神主旨,主要…

小智头像图片
437

AI写作-如何利用日常生活写小说: 一、举例 1.身边之人是熟人,但是是关系平平的同事, 这种的大家…

小智头像图片
151

AI绘画-Liblibai简易上手教程​:prompt简易技巧: prompt简易技巧​ 认识prompt​ 好说,你就这么理…

小智头像图片
437

AI绘画-Liblibai简易上手教程​:简明操作流程: 简明操作流程​ 文生图​ 1.定主题:你需要生成一张…

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

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

助力原创内容

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

扫描二维码

手机访问本站

二维码
vip弹窗图片