import os
import openai
from dotenv import load_dotenv, find_dotenv
# 读取本地/项目的环境变量。
# find_dotenv()寻找并定位.env文件的路径
# load_dotenv()读取该.env文件,并将其中的环境变量加载到当前的运行环境中
# 如果你设置的是全局的环境变量,这行代码则没有任何作用。
_ = load_dotenv(find_dotenv())
# 获取环境变量 OPENAI_API_KEY
openai.api_key = os.environ['OPENAI_API_KEY']
二、LangChain内置工具¶
from langchain.agents import load_tools, initialize_agent
from langchain.agents import AgentType
from langchain.python import PythonREPL
from langchain.chat_models.openai import ChatOpenAI
2.1 使用llm-math和wikipedia工具¶
1️⃣ 初始化大语言模型¶
- 默认密钥
openai_api_key为环境变量OPENAI_API_KEY。因此在运行以下代码之前,确保你已经设置环境变量OPENAI_API_KEY。如果还没有密钥,请获取你的API Key 。 - 默认模型
model_name为gpt-3.5-turbo。 - 更多关于模型默认参数请查看这里。
# 参数temperature设置为0.0,从而减少生成答案的随机性。
llm = ChatOpenAI(temperature=0)
/Users/lta/anaconda3/envs/cookbook/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The class `ChatOpenAI` was deprecated in LangChain 0.0.10 and will be removed in 0.3.0. An updated version of the class exists in the langchain-openai package and should be used instead. To use it run `pip install -U langchain-openai` and import as `from langchain_openai import ChatOpenAI`. warn_deprecated(
2️⃣ 加载工具包¶
llm-math工具结合语言模型和计算器用以进行数学计算wikipedia工具通过API连接到wikipedia进行搜索查询。
tools = load_tools(
["llm-math","wikipedia"],
llm=llm #第一步初始化的模型
)
3️⃣ 初始化代理¶
agent: 代理类型。这里使用的是AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION。其中CHAT代表代理模型为针对对话优化的模型,REACT代表针对REACT设计的提示模版。handle_parsing_errors: 是否处理解析错误。当发生解析错误时,将错误信息返回给大模型,让其进行纠正。verbose: 是否输出中间步骤结果。
agent= initialize_agent(
tools, #第二步加载的工具
llm, #第一步初始化的模型
agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, #代理类型
handle_parsing_errors=True, #处理解析错误
verbose = True #输出中间步骤
)
/Users/lta/anaconda3/envs/cookbook/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The function `initialize_agent` was deprecated in LangChain 0.1.0 and will be removed in 0.3.0. Use Use new agent constructor methods like create_react_agent, create_json_agent, create_structured_chat_agent, etc. instead. warn_deprecated(
4️⃣.1️⃣ 使用代理回答数学问题¶
agent("计算300的25%,思考过程请使用中文。")
/Users/lta/anaconda3/envs/cookbook/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The method `Chain.__call__` was deprecated in langchain 0.1.0 and will be removed in 0.3.0. Use invoke instead. warn_deprecated(
> Entering new AgentExecutor chain... Question: 计算300的25%,思考过程请使用中文。 Thought: 首先,我们知道25%可以表示为0.25,然后将300乘以0.25即可得到答案。 Action: ``` { "action": "Calculator", "action_input": "300*0.25" } ``` Observation: Answer: 75.0 Thought:Final Answer: 75.0 > Finished chain.
{'input': '计算300的25%,思考过程请使用中文。', 'output': '75.0'}
✅ 总结
模型对于接下来需要做什么,给出思考(Thought)
思考:我们需要计算300的25%,这个过程中需要用到乘法和除法。
模型基于思考采取行动(Action)
行动: 使用计算器(calculator),输入300*0.25
模型得到观察(Observation)
观察:答案: 75.0
基于观察,模型对于接下来需要做什么,给出思考(Thought)
思考: 我们的问题有了答案
给出最终答案(Final Answer)
最终答案: 75.0
以字典的形式给出最终答案。
4️⃣.2️⃣ Tom M. Mitchell的书¶
question = "Tom M. Mitchell is an American computer scientist \
and the Founders University Professor at Carnegie Mellon University (CMU)\
what book did he write?"
agent(question)
> Entering new AgentExecutor chain... Thought: I can use Wikipedia to find out which book Tom M. Mitchell wrote. Action: ``` { "action": "wikipedia", "action_input": "Tom M. Mitchell" } ```
/Users/lta/anaconda3/envs/cookbook/lib/python3.10/site-packages/wikipedia/wikipedia.py:389: GuessedAtParserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.
The code that caused this warning is on line 389 of the file /Users/lta/anaconda3/envs/cookbook/lib/python3.10/site-packages/wikipedia/wikipedia.py. To get rid of this warning, pass the additional argument 'features="html.parser"' to the BeautifulSoup constructor.
lis = BeautifulSoup(html).find_all('li')
Observation: Page: Tom M. Mitchell Summary: Tom Michael Mitchell (born August 9, 1951) is an American computer scientist and the Founders University Professor at Carnegie Mellon University (CMU). He is a founder and former Chair of the Machine Learning Department at CMU. Mitchell is known for his contributions to the advancement of machine learning, artificial intelligence, and cognitive neuroscience and is the author of the textbook Machine Learning. He is a member of the United States National Academy of Engineering since 2010. He is also a Fellow of the American Academy of Arts and Sciences, the American Association for the Advancement of Science and a Fellow and past President of the Association for the Advancement of Artificial Intelligence. In October 2018, Mitchell was appointed as the Interim Dean of the School of Computer Science at Carnegie Mellon. Page: Tom Mitchell (Australian footballer) Summary: Thomas Mitchell (born 31 May 1993) is a professional Australian rules footballer playing for the Collingwood Football Club in the Australian Football League (AFL). He previously played for the Sydney Swans from 2012 to 2016, and the Hawthorn Football Club between 2017 and 2022. Mitchell won the Brownlow Medal as the league's best and fairest player in 2018 and set the record for the most disposals in a VFL/AFL match, accruing 54 in a game against Collingwood during that season. He would later join them in 2023, en route to winning the 2023 AFL Grand Final and his first AFL premiership. Thought:I have found that Tom M. Mitchell wrote the textbook "Machine Learning." Final Answer: Machine Learning > Finished chain.
{'input': 'Tom M. Mitchell is an American computer scientist and the Founders University Professor at Carnegie Mellon University (CMU)what book did he write?',
'output': 'Machine Learning'}
✅ 总结
模型对于接下来需要做什么,给出思考(Thought)
思考:我应该使用维基百科去搜索。
模型基于思考采取行动(Action)
行动: 使用维基百科,输入Tom M. Mitchell
模型得到观察(Observation)
观测: 页面: Tom M. Mitchell,页面: Tom Mitchell (澳大利亚足球运动员)
基于观察,模型对于接下来需要做什么,给出思考(Thought)
思考: Tom M. Mitchell写的书是Machine Learning
给出最终答案(Final Answer)
最终答案: Machine Learning
以字典的形式给出最终答案。
值得注意的是,模型每次运行推理的过程可能存在差异,但最终的结果一致。
2.2 使用PythonREPLTool工具¶
1️⃣ 创建pyhon代理¶
from langchain_experimental.agents.agent_toolkits.python.base import create_python_agent
from langchain_experimental.tools.python.tool import PythonREPLTool
agent = create_python_agent(
llm, #使用前面一节已经加载的大语言模型
tool=PythonREPLTool(), #使用Python交互式环境工具(REPLTool)
verbose=True #输出中间步骤
)
2️⃣ 使用代理对顾客名字进行排序¶
customer_list = ["小明","小黄","小红","小蓝","小橘","小绿",]
agent.run(f"""在这些客户名字转换为拼音\
并打印输出列表: {customer_list}\
思考过程请使用中文。""")
> Entering new AgentExecutor chain...
/Users/lta/anaconda3/envs/cookbook/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain 0.1.0 and will be removed in 0.3.0. Use invoke instead. warn_deprecated( Python REPL can execute arbitrary code. Use with caution.
我们可以使用Python的拼音库来将中文名字转换为拼音。 Action: Python_REPL Action Input: ```python from xpinyin import Pinyin p = Pinyin() names = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿'] pinyins = [p.get_pinyin(name, '') for name in names] print(pinyins) ``` Observation: ['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv'] Thought:我们已经成功将这些客户名字转换为拼音。 Final Answer: ['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv'] > Finished chain.
"['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']"
3️⃣ 使用调试模式¶
在调试(debug)模式下再次运行,我们可以把上面的6步分别对应到下面的具体流程
模型对于接下来需要做什么,给出思考(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] [12.25s] Exiting LLM run with output
[chain/end] [1:chain:AgentExecutor > 2:chain:LLMChain] [12.25s] Exiting Chain run with output
模型基于思考采取行动(Action), 因为使用的工具不同,Action的输出也和之前有所不同,这里输出的为python代码
[tool/start] [1:chain:AgentExecutor > 4:tool:Python REPL] Entering Tool run with input
[tool/end] [1:chain:AgentExecutor > 4:tool:Python REPL] [2.2239999999999998ms] Exiting Tool run with output
模型得到观察(Observation)
[chain/start] [1:chain:AgentExecutor > 5:chain:LLMChain] Entering Chain run with input
基于观察,模型对于接下来需要做什么,给出思考(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] [6.94s] Exiting LLM run with output
给出最终答案(Final Answer)
[chain/end] [1:chain:AgentExecutor > 5:chain:LLMChain] [6.94s] Exiting Chain run with output
返回最终答案。
[chain/end] [1:chain:AgentExecutor] [19.20s] Exiting Chain run with output
import langchain
langchain.debug=True
agent.run(f"""在这些客户名字转换为拼音\
并打印输出列表: {customer_list}\
思考过程请使用中文。""")
langchain.debug=False
[chain/start] [chain:AgentExecutor] Entering Chain run with input: { "input": "在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。" } [chain/start] [chain:AgentExecutor > chain:LLMChain] Entering Chain run with input: { "input": "在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。", "agent_scratchpad": "", "stop": [ "\nObservation:", "\n\tObservation:" ] } [llm/start] [chain:AgentExecutor > chain:LLMChain > 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.\n\n\nPython_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(...)`.\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [Python_REPL]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: 在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。\nThought:" ] } [llm/end] [chain:AgentExecutor > chain:LLMChain > llm:ChatOpenAI] [2.74s] Exiting LLM run with output: { "generations": [ [ { "text": "我们可以使用Python的拼音库来将中文名字转换为拼音。\nAction: Python_REPL\nAction Input: \n```python\nfrom xpinyin import Pinyin\np = Pinyin()\nnames = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']\npinyins = [p.get_pinyin(name, '') for name in names]\nprint(pinyins)\n```", "generation_info": { "finish_reason": "stop", "logprobs": null }, "type": "ChatGeneration", "message": { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "我们可以使用Python的拼音库来将中文名字转换为拼音。\nAction: Python_REPL\nAction Input: \n```python\nfrom xpinyin import Pinyin\np = Pinyin()\nnames = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']\npinyins = [p.get_pinyin(name, '') for name in names]\nprint(pinyins)\n```", "response_metadata": { "token_usage": { "completion_tokens": 104, "prompt_tokens": 322, "total_tokens": 426 }, "model_name": "gpt-3.5-turbo", "system_fingerprint": null, "finish_reason": "stop", "logprobs": null }, "type": "ai", "id": "run-1d856a5f-0d28-4156-bda0-c1bfb8a1e22f-0", "tool_calls": [], "invalid_tool_calls": [] } } } ] ], "llm_output": { "token_usage": { "completion_tokens": 104, "prompt_tokens": 322, "total_tokens": 426 }, "model_name": "gpt-3.5-turbo", "system_fingerprint": null }, "run": null } [chain/end] [chain:AgentExecutor > chain:LLMChain] [2.75s] Exiting Chain run with output: { "text": "我们可以使用Python的拼音库来将中文名字转换为拼音。\nAction: Python_REPL\nAction Input: \n```python\nfrom xpinyin import Pinyin\np = Pinyin()\nnames = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']\npinyins = [p.get_pinyin(name, '') for name in names]\nprint(pinyins)\n```" } [tool/start] [chain:AgentExecutor > tool:Python_REPL] Entering Tool run with input: "```python from xpinyin import Pinyin p = Pinyin() names = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿'] pinyins = [p.get_pinyin(name, '') for name in names] print(pinyins) ```" [tool/end] [chain:AgentExecutor > tool:Python_REPL] [16ms] Exiting Tool run with output: "['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']" [chain/start] [chain:AgentExecutor > chain:LLMChain] Entering Chain run with input: { "input": "在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。", "agent_scratchpad": "我们可以使用Python的拼音库来将中文名字转换为拼音。\nAction: Python_REPL\nAction Input: \n```python\nfrom xpinyin import Pinyin\np = Pinyin()\nnames = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']\npinyins = [p.get_pinyin(name, '') for name in names]\nprint(pinyins)\n```\nObservation: ['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']\n\nThought:", "stop": [ "\nObservation:", "\n\tObservation:" ] } [llm/start] [chain:AgentExecutor > chain:LLMChain > 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.\n\n\nPython_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(...)`.\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [Python_REPL]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: 在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。\nThought:我们可以使用Python的拼音库来将中文名字转换为拼音。\nAction: Python_REPL\nAction Input: \n```python\nfrom xpinyin import Pinyin\np = Pinyin()\nnames = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']\npinyins = [p.get_pinyin(name, '') for name in names]\nprint(pinyins)\n```\nObservation: ['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']\n\nThought:" ] } [llm/end] [chain:AgentExecutor > chain:LLMChain > llm:ChatOpenAI] [1.26s] Exiting LLM run with output: { "generations": [ [ { "text": "我们成功地将中文名字转换为拼音。\nFinal Answer: ['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']", "generation_info": { "finish_reason": "stop", "logprobs": null }, "type": "ChatGeneration", "message": { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "我们成功地将中文名字转换为拼音。\nFinal Answer: ['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']", "response_metadata": { "token_usage": { "completion_tokens": 45, "prompt_tokens": 458, "total_tokens": 503 }, "model_name": "gpt-3.5-turbo", "system_fingerprint": null, "finish_reason": "stop", "logprobs": null }, "type": "ai", "id": "run-90afa3d5-a827-4279-bee2-4447fd057653-0", "tool_calls": [], "invalid_tool_calls": [] } } } ] ], "llm_output": { "token_usage": { "completion_tokens": 45, "prompt_tokens": 458, "total_tokens": 503 }, "model_name": "gpt-3.5-turbo", "system_fingerprint": null }, "run": null } [chain/end] [chain:AgentExecutor > chain:LLMChain] [1.26s] Exiting Chain run with output: { "text": "我们成功地将中文名字转换为拼音。\nFinal Answer: ['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']" } [chain/end] [chain:AgentExecutor] [4.03s] Exiting Chain run with output: { "output": "['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']" }
三、 定义自己的工具并在代理中使用¶
3.1 创建和使用自定义时间工具¶
# 导入tool函数装饰器
from langchain.agents import tool
from datetime import date
1️⃣ 使用tool函数装饰器构建自定义工具¶
tool函数装饰器可以应用用于任何函数,将函数转化为LongChain工具,使其成为代理可调用的工具。
我们需要给函数加上非常详细的文档字符串, 使得代理知道在什么情况下、如何使用该函数/工具。
比如下面的函数time,我们加上了详细的文档字符串
"""
返回今天的日期,用于任何与获取今天日期相关的问题。
输入应该始终是一个空字符串,该函数将始终返回今天的日期。
任何日期的计算应该在此函数之外进行。
"""
@tool
def time(text: str) -> str:
"""
返回今天的日期,用于任何需要知道今天日期的问题。\
输入应该总是一个空字符串,\
这个函数将总是返回今天的日期,任何日期计算应该在这个函数之外进行。
"""
return str(date.today())
2️⃣ 初始化代理¶
agent= initialize_agent(
tools=[time], #将刚刚创建的时间工具加入代理
llm=llm, #初始化的模型
agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, #代理类型
handle_parsing_errors=True, #处理解析错误
verbose = True #输出中间步骤
)
3️⃣ 使用代理询问今天的日期¶
注: 代理有时候可能会出错(该功能正在开发中)。如果出现错误,请尝试再次运行它。
agent("今天的日期是?")
> Entering new AgentExecutor chain... Question: What is today's date? Thought: I can use the time tool to get today's date. Action: ``` { "action": "time", "action_input": "" } ``` Observation: 2024-06-11 Thought:Final Answer: 2024-06-11 > Finished chain.
{'input': '今天的日期是?', 'output': '2024-06-11'}
✅ 总结
模型对于接下来需要做什么,给出思考(Thought)
思考:我需要使用 time 工具来获取今天的日期
模型基于思考采取行动(Action), 因为使用的工具不同,Action的输出也和之前有所不同,这里输出的为python代码
行动: 使用time工具,输入为空字符串
模型得到观察(Observation)
观测: 2023-07-04
基于观察,模型对于接下来需要做什么,给出思考(Thought)
思考: 我已成功使用 time 工具检索到了今天的日期
给出最终答案(Final Answer)
最终答案: 今天的日期是2023-07-04.
返回最终答案。