{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "2caa79ba-45e3-437c-9cb6-e433f443f0bf", "metadata": {}, "source": [ "# 7. 代理\n", "\n", "
思考:我们需要计算300的25%,这个过程中需要用到乘法和除法。
\n", "\n", "2. 模型基于思考采取行动(Action)\n", "行动: 使用计算器(calculator),输入300*0.25
\n", "3. 模型得到观察(Observation)\n", "观察:答案: 75.0
\n", "\n", "4. 基于观察,模型对于接下来需要做什么,给出思考(Thought)\n", "思考: 我们的问题有了答案
\n", "\n", "5. 给出最终答案(Final Answer)\n", "最终答案: 75.0
\n", "5. 以字典的形式给出最终答案。" ] }, { "attachments": {}, "cell_type": "markdown", "id": "bdd13d06-0f0e-4918-ac28-339f524f8c76", "metadata": {}, "source": [ "#### 4️⃣.2️⃣ Tom M. Mitchell的书" ] }, { "cell_type": "code", "execution_count": 18, "id": "f43b5cbf-1011-491b-90e8-09cf903fb866", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3mI need to find out what book Tom M. Mitchell wrote.\n", "Action: Search for Tom M. Mitchell's books.\n", "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", "Observation: Search for Tom M. Mitchell's books. is not a valid tool, try another one.\n", "Thought:\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", "Action: Search for \"Tom M. Mitchell books\"\n", "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", "Thought:\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", "Action: Search for \"Tom M. Mitchell books\"\n", "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", "Thought:" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.思考:我应该使用维基百科去搜索。
\n", "\n", "2. 模型基于思考采取行动(Action)\n", "行动: 使用维基百科,输入Tom M. Mitchell
\n", "3. 模型得到观察(Observation)\n", "观测: 页面: Tom M. Mitchell,页面: Tom Mitchell (澳大利亚足球运动员)
\n", "\n", "4. 基于观察,模型对于接下来需要做什么,给出思考(Thought)\n", "思考: Tom M. Mitchell写的书是Machine Learning
\n", "\n", "5. 给出最终答案(Final Answer)\n", "最终答案: Machine Learning
\n", "5. 以字典的形式给出最终答案。\n", "\n", "\n", "值得注意的是,模型每次运行推理的过程可能存在差异,但最终的结果一致。" ] }, { "attachments": {}, "cell_type": "markdown", "id": "5901cab6-a7c9-4590-b35d-d41c29e39a39", "metadata": {}, "source": [ "### 7.1.2 使用PythonREPLTool工具" ] }, { "attachments": {}, "cell_type": "markdown", "id": "12b8d837-524c-46b8-9189-504808cf1f93", "metadata": {}, "source": [ "#### 1️⃣ 创建pyhon代理" ] }, { "cell_type": "code", "execution_count": 8, "id": "6c60d933-168d-4834-b29c-b2b43ade80cc", "metadata": {}, "outputs": [], "source": [ "from langchain.agents.agent_toolkits import create_python_agent\n", "from langchain.tools.python.tool import PythonREPLTool\n", "\n", "agent = create_python_agent(\n", " llm, #使用前面一节已经加载的大语言模型\n", " tool=PythonREPLTool(), #使用Python交互式环境工具(REPLTool)\n", " verbose=True #输出中间步骤\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "d32ed40c-cbcd-4efd-b044-57e611f5fab5", "metadata": { "tags": [] }, "source": [ "#### 2️⃣ 使用代理对顾客名字进行排序" ] }, { "cell_type": "code", "execution_count": 14, "id": "3d416f28-8fdb-4761-a4f9-d46c6d37fb3d", "metadata": {}, "outputs": [], "source": [ "customer_list = [\"小明\",\"小黄\",\"小红\",\"小蓝\",\"小橘\",\"小绿\",]" ] }, { "cell_type": "code", "execution_count": 22, "id": "d0fbfc4c", "metadata": {}, "outputs": [], "source": [ "!pip install -q pinyin" ] }, { "cell_type": "code", "execution_count": 23, "id": "ba7f9a50-acf6-4fba-a1b9-503baa80266b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new chain...\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[32;1m\u001b[1;3m我需要使用拼音库来将客户名字转换为拼音。我可以使用Python的pinyin库来实现这个功能。\n", "Action: Python_REPL\n", "Action Input: import pinyin\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3m\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m我现在可以使用pinyin库来将客户名字转换为拼音了。\n", "Action: Python_REPL\n", "Action Input: names = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3m\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m我现在可以使用pinyin库将names列表中的客户名字转换为拼音了。\n", "Action: Python_REPL\n", "Action Input: pinyin_names = [pinyin.get(i, format='strip', delimiter='') for i in names]\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3m\u001b[0m\n", "Thought:" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.[chain/start] [1:chain:AgentExecutor] Entering Chain run with input
\n", " -[chain/start] [1:chain:AgentExecutor > 2:chain:LLMChain] Entering Chain run with input
\n", " -[llm/start] [1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] Entering LLM run with input
\n", " -[llm/end] [1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] [12.25s] Exiting LLM run with output
\n", " -[chain/end] [1:chain:AgentExecutor > 2:chain:LLMChain] [12.25s] Exiting Chain run with output
\n", "2. 模型基于思考采取行动(Action), 因为使用的工具不同,Action的输出也和之前有所不同,这里输出的为python代码\n", " -[tool/start] [1:chain:AgentExecutor > 4:tool:Python REPL] Entering Tool run with input
\n", " -[tool/end] [1:chain:AgentExecutor > 4:tool:Python REPL] [2.2239999999999998ms] Exiting Tool run with output
\n", "3. 模型得到观察(Observation) \n", " -[chain/start] [1:chain:AgentExecutor > 5:chain:LLMChain] Entering Chain run with input
\n", "4. 基于观察,模型对于接下来需要做什么,给出思考(Thought) \n", " -[llm/start] [1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI] Entering LLM run with input
\n", " -[llm/end] [1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI] [6.94s] Exiting LLM run with output
\n", " \n", "5. 给出最终答案(Final Answer) \n", " -[chain/end] [1:chain:AgentExecutor > 5:chain:LLMChain] [6.94s] Exiting Chain run with output
\n", "6. 返回最终答案。\n", " -[chain/end] [1:chain:AgentExecutor] [19.20s] Exiting Chain run with output
" ] }, { "cell_type": "code", "execution_count": 25, "id": "92b8961b-b990-4540-a7f9-e4701dfaa616", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor] Entering Chain run with input:\n", "\u001b[0m{\n", " \"input\": \"在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。\"\n", "}\n", "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 2:chain:LLMChain] Entering Chain run with input:\n", "\u001b[0m{\n", " \"input\": \"在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。\",\n", " \"agent_scratchpad\": \"\",\n", " \"stop\": [\n", " \"\\nObservation:\",\n", " \"\\n\\tObservation:\"\n", " ]\n", "}\n", "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] Entering LLM run with input:\n", "\u001b[0m{\n", " \"prompts\": [\n", " \"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:\"\n", " ]\n", "}\n", "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] [2.91s] Exiting LLM run with output:\n", "\u001b[0m{\n", " \"generations\": [\n", " [\n", " {\n", " \"text\": \"我需要使用拼音转换库来将这些客户名字转换为拼音。我可以使用Python的pinyin库来完成这个任务。\\nAction: Python_REPL\\nAction Input: import pinyin\",\n", " \"generation_info\": null,\n", " \"message\": {\n", " \"content\": \"我需要使用拼音转换库来将这些客户名字转换为拼音。我可以使用Python的pinyin库来完成这个任务。\\nAction: Python_REPL\\nAction Input: import pinyin\",\n", " \"additional_kwargs\": {},\n", " \"example\": false\n", " }\n", " }\n", " ]\n", " ],\n", " \"llm_output\": {\n", " \"token_usage\": {\n", " \"prompt_tokens\": 322,\n", " \"completion_tokens\": 50,\n", " \"total_tokens\": 372\n", " },\n", " \"model_name\": \"gpt-3.5-turbo\"\n", " },\n", " \"run\": null\n", "}\n", "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 2:chain:LLMChain] [2.91s] Exiting Chain run with output:\n", "\u001b[0m{\n", " \"text\": \"我需要使用拼音转换库来将这些客户名字转换为拼音。我可以使用Python的pinyin库来完成这个任务。\\nAction: Python_REPL\\nAction Input: import pinyin\"\n", "}\n", "\u001b[32;1m\u001b[1;3m[tool/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 4:tool:Python_REPL] Entering Tool run with input:\n", "\u001b[0m\"import pinyin\"\n", "\u001b[36;1m\u001b[1;3m[tool/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 4:tool:Python_REPL] [0.0ms] Exiting Tool run with output:\n", "\u001b[0m\"\"\n", "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 5:chain:LLMChain] Entering Chain run with input:\n", "\u001b[0m{\n", " \"input\": \"在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。\",\n", " \"agent_scratchpad\": \"我需要使用拼音转换库来将这些客户名字转换为拼音。我可以使用Python的pinyin库来完成这个任务。\\nAction: Python_REPL\\nAction Input: import pinyin\\nObservation: \\nThought:\",\n", " \"stop\": [\n", " \"\\nObservation:\",\n", " \"\\n\\tObservation:\"\n", " ]\n", "}\n", "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI] Entering LLM run with input:\n", "\u001b[0m{\n", " \"prompts\": [\n", " \"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的pinyin库来完成这个任务。\\nAction: Python_REPL\\nAction Input: import pinyin\\nObservation: \\nThought:\"\n", " ]\n", "}\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.思考:我需要使用 time 工具来获取今天的日期
\n", "\n", "2. 模型基于思考采取行动(Action), 因为使用的工具不同,Action的输出也和之前有所不同,这里输出的为python代码\n", "行动: 使用time工具,输入为空字符串
\n", " \n", " \n", "3. 模型得到观察(Observation)\n", "观测: 2023-07-04
\n", " \n", " \n", " \n", "4. 基于观察,模型对于接下来需要做什么,给出思考(Thought)\n", "思考: 我已成功使用 time 工具检索到了今天的日期
\n", "\n", "5. 给出最终答案(Final Answer)\n", "最终答案: 今天的日期是2023-07-04.
\n", "6. 返回最终答案。" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 5 }