{"cells": [{"cell_type": "markdown", "id": "2caa79ba-45e3-437c-9cb6-e433f443f0bf", "metadata": {}, "source": ["# \u7b2c\u4e03\u7ae0 \u4ee3\u7406\n", "\n", " - [\u4e00\u3001\u8bbe\u7f6eOpenAI API Key](#\u4e00\u3001\u8bbe\u7f6eOpenAI-API-Key)\n", " - [\u4e8c\u3001LangChain\u5185\u7f6e\u5de5\u5177](#\u4e8c\u3001LangChain\u5185\u7f6e\u5de5\u5177)\n", " - [2.1 \u4f7f\u7528llm-math\u548cwikipedia\u5de5\u5177](#2.1-\u4f7f\u7528llm-math\u548cwikipedia\u5de5\u5177)\n", " - [2.2 \u4f7f\u7528PythonREPLTool\u5de5\u5177](#2.2-\u4f7f\u7528PythonREPLTool\u5de5\u5177)\n", " - [\u4e09\u3001 \u5b9a\u4e49\u81ea\u5df1\u7684\u5de5\u5177\u5e76\u5728\u4ee3\u7406\u4e2d\u4f7f\u7528](#\u4e09\u3001-\u5b9a\u4e49\u81ea\u5df1\u7684\u5de5\u5177\u5e76\u5728\u4ee3\u7406\u4e2d\u4f7f\u7528)\n", " - [3.1 \u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u65f6\u95f4\u5de5\u5177](#3.1-\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u65f6\u95f4\u5de5\u5177)\n"]}, {"cell_type": "markdown", "id": "8a31726a-82ac-46e4-a51e-7747fc4c6aed", "metadata": {"tags": []}, "source": ["## \u4e00\u3001\u8bbe\u7f6eOpenAI API Key\n", "\n", "\u767b\u9646 [OpenAI \u8d26\u6237](https://platform.openai.com/account/api-keys) \u83b7\u53d6API Key\uff0c\u7136\u540e\u5c06\u5176\u8bbe\u7f6e\u4e3a\u73af\u5883\u53d8\u91cf\u3002\n", "\n", "- \u5982\u679c\u4f60\u60f3\u8981\u8bbe\u7f6e\u4e3a\u5168\u5c40\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u53c2\u8003[\u77e5\u4e4e\u6587\u7ae0](https://zhuanlan.zhihu.com/p/627665725)\u3002\n", "- \u5982\u679c\u4f60\u60f3\u8981\u8bbe\u7f6e\u4e3a\u672c\u5730/\u9879\u76ee\u73af\u5883\u53d8\u91cf\uff0c\u5728\u672c\u6587\u4ef6\u76ee\u5f55\u4e0b\u521b\u5efa`.env`\u6587\u4ef6, \u6253\u5f00\u6587\u4ef6\u8f93\u5165\u4ee5\u4e0b\u5185\u5bb9\u3002\n", "\n", "
\n", " OPENAI_API_KEY=\"your_api_key\" \n", "
\n", " \n", " \u66ff\u6362\"your_api_key\"\u4e3a\u4f60\u81ea\u5df1\u7684 API Key"]}, {"cell_type": "code", "execution_count": 1, "id": "beebf047-9117-43e3-b7b2-1f06cd3d4275", "metadata": {}, "outputs": [], "source": ["# \u4e0b\u8f7d\u9700\u8981\u7684\u5305python-dotenv\u548copenai\n", "# \u5982\u679c\u4f60\u9700\u8981\u67e5\u770b\u5b89\u88c5\u8fc7\u7a0b\u65e5\u5fd7\uff0c\u53ef\u5220\u9664 -q \n", "!pip install -q python-dotenv\n", "!pip install -q openai"]}, {"cell_type": "code", "execution_count": 2, "id": "312f4c23-fe27-4cda-a645-8e1519c2febf", "metadata": {"tags": []}, "outputs": [], "source": ["import os\n", "import openai\n", "from dotenv import load_dotenv, find_dotenv\n", "\n", "# \u8bfb\u53d6\u672c\u5730/\u9879\u76ee\u7684\u73af\u5883\u53d8\u91cf\u3002\n", "\n", "# find_dotenv()\u5bfb\u627e\u5e76\u5b9a\u4f4d.env\u6587\u4ef6\u7684\u8def\u5f84\n", "# load_dotenv()\u8bfb\u53d6\u8be5.env\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u4e2d\u7684\u73af\u5883\u53d8\u91cf\u52a0\u8f7d\u5230\u5f53\u524d\u7684\u8fd0\u884c\u73af\u5883\u4e2d \n", "# \u5982\u679c\u4f60\u8bbe\u7f6e\u7684\u662f\u5168\u5c40\u7684\u73af\u5883\u53d8\u91cf\uff0c\u8fd9\u884c\u4ee3\u7801\u5219\u6ca1\u6709\u4efb\u4f55\u4f5c\u7528\u3002\n", "_ = load_dotenv(find_dotenv())\n", "\n", "# \u83b7\u53d6\u73af\u5883\u53d8\u91cf OPENAI_API_KEY\n", "openai.api_key = os.environ['OPENAI_API_KEY'] "]}, {"cell_type": "markdown", "id": "631c764b-68fa-483a-80a5-9a322cd1117c", "metadata": {}, "source": ["## \u4e8c\u3001LangChain\u5185\u7f6e\u5de5\u5177"]}, {"cell_type": "code", "execution_count": 2, "id": "8ab03352-1554-474c-afda-ec8974250e31", "metadata": {}, "outputs": [], "source": ["# \u5982\u679c\u9700\u8981\u67e5\u770b\u5b89\u88c5\u8fc7\u7a0b\u65e5\u5fd7\uff0c\u53ef\u5220\u9664 -q \n", "# -U \u5b89\u88c5\u5230\u6700\u65b0\u7248\u672c\u7684 wikipedia. \u5176\u529f\u80fd\u540c --upgrade \n", "!pip install -U -q wikipedia\n", "!pip install -q openai"]}, {"cell_type": "code", "execution_count": 3, "id": "bc80a8a7-a66e-4fbb-8d57-05004a3e77c7", "metadata": {}, "outputs": [], "source": ["from langchain.agents import load_tools, initialize_agent\n", "from langchain.agents import AgentType\n", "from langchain.python import PythonREPL\n", "from langchain.chat_models import ChatOpenAI"]}, {"cell_type": "markdown", "id": "d5a3655c-4d5e-4a86-8bf4-a11bd1525059", "metadata": {}, "source": ["### 2.1 \u4f7f\u7528llm-math\u548cwikipedia\u5de5\u5177"]}, {"cell_type": "markdown", "id": "e484d285-2110-4a06-a360-55313d6d9ffc", "metadata": {}, "source": ["#### 1\ufe0f\u20e3 \u521d\u59cb\u5316\u5927\u8bed\u8a00\u6a21\u578b\n", "\n", "- \u9ed8\u8ba4\u5bc6\u94a5`openai_api_key`\u4e3a\u73af\u5883\u53d8\u91cf`OPENAI_API_KEY`\u3002\u56e0\u6b64\u5728\u8fd0\u884c\u4ee5\u4e0b\u4ee3\u7801\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf`OPENAI_API_KEY`\u3002\u5982\u679c\u8fd8\u6ca1\u6709\u5bc6\u94a5\uff0c\u8bf7[\u83b7\u53d6\u4f60\u7684API Key](https://platform.openai.com/account/api-keys) \u3002\n", "- \u9ed8\u8ba4\u6a21\u578b`model_name`\u4e3a`gpt-3.5-turbo`\u3002\n", "- \u66f4\u591a\u5173\u4e8e\u6a21\u578b\u9ed8\u8ba4\u53c2\u6570\u8bf7\u67e5\u770b[\u8fd9\u91cc](https://github.com/hwchase17/langchain/blob/master/langchain/chat_models/openai.py)\u3002"]}, {"cell_type": "code", "execution_count": 4, "id": "71e5894f-fc50-478b-a3d3-58891cc46ffc", "metadata": {}, "outputs": [], "source": ["# \u53c2\u6570temperature\u8bbe\u7f6e\u4e3a0.0\uff0c\u4ece\u800c\u51cf\u5c11\u751f\u6210\u7b54\u6848\u7684\u968f\u673a\u6027\u3002\n", "llm = ChatOpenAI(temperature=0)"]}, {"cell_type": "markdown", "id": "d7f75023-1825-4d74-bc8e-2c362f551fd1", "metadata": {"tags": []}, "source": ["#### 2\ufe0f\u20e3 \u52a0\u8f7d\u5de5\u5177\u5305\n", "- `llm-math` \u5de5\u5177\u7ed3\u5408\u8bed\u8a00\u6a21\u578b\u548c\u8ba1\u7b97\u5668\u7528\u4ee5\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\n", "- `wikipedia`\u5de5\u5177\u901a\u8fc7API\u8fde\u63a5\u5230wikipedia\u8fdb\u884c\u641c\u7d22\u67e5\u8be2\u3002"]}, {"cell_type": "code", "execution_count": 5, "id": "3541b866-8a35-4b6f-bfe6-66edb1d666cd", "metadata": {}, "outputs": [], "source": ["tools = load_tools(\n", " [\"llm-math\",\"wikipedia\"], \n", " llm=llm #\u7b2c\u4e00\u6b65\u521d\u59cb\u5316\u7684\u6a21\u578b\n", ")"]}, {"cell_type": "markdown", "id": "e5b4fcc8-8817-4a94-b154-3f328480e441", "metadata": {}, "source": ["#### 3\ufe0f\u20e3 \u521d\u59cb\u5316\u4ee3\u7406\n", "\n", "- `agent`: \u4ee3\u7406\u7c7b\u578b\u3002\u8fd9\u91cc\u4f7f\u7528\u7684\u662f`AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION`\u3002\u5176\u4e2d`CHAT`\u4ee3\u8868\u4ee3\u7406\u6a21\u578b\u4e3a\u9488\u5bf9\u5bf9\u8bdd\u4f18\u5316\u7684\u6a21\u578b\uff0c`REACT`\u4ee3\u8868\u9488\u5bf9REACT\u8bbe\u8ba1\u7684\u63d0\u793a\u6a21\u7248\u3002\n", "- `handle_parsing_errors`: \u662f\u5426\u5904\u7406\u89e3\u6790\u9519\u8bef\u3002\u5f53\u53d1\u751f\u89e3\u6790\u9519\u8bef\u65f6\uff0c\u5c06\u9519\u8bef\u4fe1\u606f\u8fd4\u56de\u7ed9\u5927\u6a21\u578b\uff0c\u8ba9\u5176\u8fdb\u884c\u7ea0\u6b63\u3002\n", "- `verbose`: \u662f\u5426\u8f93\u51fa\u4e2d\u95f4\u6b65\u9aa4\u7ed3\u679c\u3002"]}, {"cell_type": "code", "execution_count": 6, "id": "e7cdc782-2b8c-4649-ae1e-cabacee1a757", "metadata": {}, "outputs": [], "source": ["agent= initialize_agent(\n", " tools, #\u7b2c\u4e8c\u6b65\u52a0\u8f7d\u7684\u5de5\u5177\n", " llm, #\u7b2c\u4e00\u6b65\u521d\u59cb\u5316\u7684\u6a21\u578b\n", " agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, #\u4ee3\u7406\u7c7b\u578b\n", " handle_parsing_errors=True, #\u5904\u7406\u89e3\u6790\u9519\u8bef\n", " verbose = True #\u8f93\u51fa\u4e2d\u95f4\u6b65\u9aa4\n", ")"]}, {"cell_type": "markdown", "id": "b980137a-a1d2-4c19-80c8-ec380f9c1efe", "metadata": {"tags": []}, "source": ["#### 4\ufe0f\u20e3.1\ufe0f\u20e3 \u4f7f\u7528\u4ee3\u7406\u56de\u7b54\u6570\u5b66\u95ee\u9898"]}, {"cell_type": "code", "execution_count": 7, "id": "1e2bcd3b-ae72-4fa1-b38e-d0ce650e1f31", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\n", "\n", "\u001b[1m> Entering new chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3mQuestion: \u8ba1\u7b97300\u768425%\n", "Thought: \u6211\u53ef\u4ee5\u4f7f\u7528\u8ba1\u7b97\u5668\u6765\u8ba1\u7b97\u8fd9\u4e2a\u767e\u5206\u6bd4\n", "Action:\n", "```\n", "{\n", " \"action\": \"Calculator\",\n", " \"action_input\": \"300 * 25 / 100\"\n", "}\n", "```\n", "\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3mAnswer: 75.0\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m\u6211\u73b0\u5728\u77e5\u9053\u6700\u7ec8\u7b54\u6848\u4e86\n", "Final Answer: 300\u768425%\u662f75.0\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n"]}, {"data": {"text/plain": ["{'input': '\u8ba1\u7b97300\u768425%\uff0c\u8bf7\u7528\u4e2d\u6587', 'output': '300\u768425%\u662f75.0'}"]}, "execution_count": 7, "metadata": {}, "output_type": "execute_result"}], "source": ["agent(\"\u8ba1\u7b97300\u768425%\uff0c\u601d\u8003\u8fc7\u7a0b\u8bf7\u4f7f\u7528\u4e2d\u6587\u3002\") "]}, {"cell_type": "markdown", "id": "05bb0811-d71e-4016-868b-efbb651d8e59", "metadata": {}, "source": ["\u2705 **\u603b\u7ed3**\n", "\n", "1. \u6a21\u578b\u5bf9\u4e8e\u63a5\u4e0b\u6765\u9700\u8981\u505a\u4ec0\u4e48\uff0c\u7ed9\u51fa\u601d\u8003\uff08Thought\uff09 \n", " \n", "\u601d\u8003\uff1a\u6211\u4eec\u9700\u8981\u8ba1\u7b97300\u768425%\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u9700\u8981\u7528\u5230\u4e58\u6cd5\u548c\u9664\u6cd5\u3002
\n", "\n", "2. \u6a21\u578b\u57fa\u4e8e\u601d\u8003\u91c7\u53d6\u884c\u52a8\uff08Action\uff09\n", "\u884c\u52a8: \u4f7f\u7528\u8ba1\u7b97\u5668\uff08calculator\uff09\uff0c\u8f93\u5165300*0.25
\n", "3. \u6a21\u578b\u5f97\u5230\u89c2\u5bdf\uff08Observation\uff09\n", "\u89c2\u5bdf\uff1a\u7b54\u6848: 75.0
\n", "\n", "4. \u57fa\u4e8e\u89c2\u5bdf\uff0c\u6a21\u578b\u5bf9\u4e8e\u63a5\u4e0b\u6765\u9700\u8981\u505a\u4ec0\u4e48\uff0c\u7ed9\u51fa\u601d\u8003\uff08Thought\uff09\n", "\u601d\u8003: \u6211\u4eec\u7684\u95ee\u9898\u6709\u4e86\u7b54\u6848
\n", "\n", "5. \u7ed9\u51fa\u6700\u7ec8\u7b54\u6848\uff08Final Answer\uff09\n", "\u6700\u7ec8\u7b54\u6848: 75.0
\n", "5. \u4ee5\u5b57\u5178\u7684\u5f62\u5f0f\u7ed9\u51fa\u6700\u7ec8\u7b54\u6848\u3002"]}, {"cell_type": "markdown", "id": "bdd13d06-0f0e-4918-ac28-339f524f8c76", "metadata": {}, "source": ["#### 4\ufe0f\u20e3.2\ufe0f\u20e3 Tom M. Mitchell\u7684\u4e66"]}, {"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.\u601d\u8003\uff1a\u6211\u5e94\u8be5\u4f7f\u7528\u7ef4\u57fa\u767e\u79d1\u53bb\u641c\u7d22\u3002
\n", "\n", "2. \u6a21\u578b\u57fa\u4e8e\u601d\u8003\u91c7\u53d6\u884c\u52a8\uff08Action\uff09\n", "\u884c\u52a8: \u4f7f\u7528\u7ef4\u57fa\u767e\u79d1\uff0c\u8f93\u5165Tom M. Mitchell
\n", "3. \u6a21\u578b\u5f97\u5230\u89c2\u5bdf\uff08Observation\uff09\n", "\u89c2\u6d4b: \u9875\u9762: Tom M. Mitchell\uff0c\u9875\u9762: Tom Mitchell (\u6fb3\u5927\u5229\u4e9a\u8db3\u7403\u8fd0\u52a8\u5458)
\n", "\n", "4. \u57fa\u4e8e\u89c2\u5bdf\uff0c\u6a21\u578b\u5bf9\u4e8e\u63a5\u4e0b\u6765\u9700\u8981\u505a\u4ec0\u4e48\uff0c\u7ed9\u51fa\u601d\u8003\uff08Thought\uff09\n", "\u601d\u8003: Tom M. Mitchell\u5199\u7684\u4e66\u662fMachine Learning
\n", "\n", "5. \u7ed9\u51fa\u6700\u7ec8\u7b54\u6848\uff08Final Answer\uff09\n", "\u6700\u7ec8\u7b54\u6848: Machine Learning
\n", "5. \u4ee5\u5b57\u5178\u7684\u5f62\u5f0f\u7ed9\u51fa\u6700\u7ec8\u7b54\u6848\u3002\n", "\n", "\n", "\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6a21\u578b\u6bcf\u6b21\u8fd0\u884c\u63a8\u7406\u7684\u8fc7\u7a0b\u53ef\u80fd\u5b58\u5728\u5dee\u5f02\uff0c\u4f46\u6700\u7ec8\u7684\u7ed3\u679c\u4e00\u81f4\u3002"]}, {"cell_type": "markdown", "id": "5901cab6-a7c9-4590-b35d-d41c29e39a39", "metadata": {}, "source": ["### 2.2 \u4f7f\u7528PythonREPLTool\u5de5\u5177"]}, {"cell_type": "markdown", "id": "12b8d837-524c-46b8-9189-504808cf1f93", "metadata": {}, "source": ["#### 1\ufe0f\u20e3 \u521b\u5efapyhon\u4ee3\u7406"]}, {"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, #\u4f7f\u7528\u524d\u9762\u4e00\u8282\u5df2\u7ecf\u52a0\u8f7d\u7684\u5927\u8bed\u8a00\u6a21\u578b\n", " tool=PythonREPLTool(), #\u4f7f\u7528Python\u4ea4\u4e92\u5f0f\u73af\u5883\u5de5\u5177\uff08REPLTool\uff09\n", " verbose=True #\u8f93\u51fa\u4e2d\u95f4\u6b65\u9aa4\n", ")"]}, {"cell_type": "markdown", "id": "d32ed40c-cbcd-4efd-b044-57e611f5fab5", "metadata": {"tags": []}, "source": ["#### 2\ufe0f\u20e3 \u4f7f\u7528\u4ee3\u7406\u5bf9\u987e\u5ba2\u540d\u5b57\u8fdb\u884c\u6392\u5e8f"]}, {"cell_type": "code", "execution_count": 14, "id": "3d416f28-8fdb-4761-a4f9-d46c6d37fb3d", "metadata": {}, "outputs": [], "source": ["customer_list = [\"\u5c0f\u660e\",\"\u5c0f\u9ec4\",\"\u5c0f\u7ea2\",\"\u5c0f\u84dd\",\"\u5c0f\u6a58\",\"\u5c0f\u7eff\",]"]}, {"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", "\u001b[32;1m\u001b[1;3m\u6211\u9700\u8981\u4f7f\u7528\u62fc\u97f3\u5e93\u6765\u5c06\u5ba2\u6237\u540d\u5b57\u8f6c\u6362\u4e3a\u62fc\u97f3\u3002\u6211\u53ef\u4ee5\u4f7f\u7528Python\u7684pinyin\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002\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\u6211\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528pinyin\u5e93\u6765\u5c06\u5ba2\u6237\u540d\u5b57\u8f6c\u6362\u4e3a\u62fc\u97f3\u4e86\u3002\n", "Action: Python_REPL\n", "Action Input: names = ['\u5c0f\u660e', '\u5c0f\u9ec4', '\u5c0f\u7ea2', '\u5c0f\u84dd', '\u5c0f\u6a58', '\u5c0f\u7eff']\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3m\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m\u6211\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528pinyin\u5e93\u5c06names\u5217\u8868\u4e2d\u7684\u5ba2\u6237\u540d\u5b57\u8f6c\u6362\u4e3a\u62fc\u97f3\u4e86\u3002\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. \u6a21\u578b\u57fa\u4e8e\u601d\u8003\u91c7\u53d6\u884c\u52a8\uff08Action), \u56e0\u4e3a\u4f7f\u7528\u7684\u5de5\u5177\u4e0d\u540c\uff0cAction\u7684\u8f93\u51fa\u4e5f\u548c\u4e4b\u524d\u6709\u6240\u4e0d\u540c\uff0c\u8fd9\u91cc\u8f93\u51fa\u7684\u4e3apython\u4ee3\u7801\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. \u6a21\u578b\u5f97\u5230\u89c2\u5bdf\uff08Observation\uff09 \n", " -[chain/start] [1:chain:AgentExecutor > 5:chain:LLMChain] Entering Chain run with input
\n", "4. \u57fa\u4e8e\u89c2\u5bdf\uff0c\u6a21\u578b\u5bf9\u4e8e\u63a5\u4e0b\u6765\u9700\u8981\u505a\u4ec0\u4e48\uff0c\u7ed9\u51fa\u601d\u8003\uff08Thought\uff09 \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. \u7ed9\u51fa\u6700\u7ec8\u7b54\u6848\uff08Final Answer\uff09 \n", " -[chain/end] [1:chain:AgentExecutor > 5:chain:LLMChain] [6.94s] Exiting Chain run with output
\n", "6. \u8fd4\u56de\u6700\u7ec8\u7b54\u6848\u3002\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\": \"\u5728\u8fd9\u4e9b\u5ba2\u6237\u540d\u5b57\u8f6c\u6362\u4e3a\u62fc\u97f3\u5e76\u6253\u5370\u8f93\u51fa\u5217\u8868\uff1a ['\u5c0f\u660e', '\u5c0f\u9ec4', '\u5c0f\u7ea2', '\u5c0f\u84dd', '\u5c0f\u6a58', '\u5c0f\u7eff']\u601d\u8003\u8fc7\u7a0b\u8bf7\u4f7f\u7528\u4e2d\u6587\u3002\"\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\": \"\u5728\u8fd9\u4e9b\u5ba2\u6237\u540d\u5b57\u8f6c\u6362\u4e3a\u62fc\u97f3\u5e76\u6253\u5370\u8f93\u51fa\u5217\u8868\uff1a ['\u5c0f\u660e', '\u5c0f\u9ec4', '\u5c0f\u7ea2', '\u5c0f\u84dd', '\u5c0f\u6a58', '\u5c0f\u7eff']\u601d\u8003\u8fc7\u7a0b\u8bf7\u4f7f\u7528\u4e2d\u6587\u3002\",\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: \u5728\u8fd9\u4e9b\u5ba2\u6237\u540d\u5b57\u8f6c\u6362\u4e3a\u62fc\u97f3\u5e76\u6253\u5370\u8f93\u51fa\u5217\u8868\uff1a ['\u5c0f\u660e', '\u5c0f\u9ec4', '\u5c0f\u7ea2', '\u5c0f\u84dd', '\u5c0f\u6a58', '\u5c0f\u7eff']\u601d\u8003\u8fc7\u7a0b\u8bf7\u4f7f\u7528\u4e2d\u6587\u3002\\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\": \"\u6211\u9700\u8981\u4f7f\u7528\u62fc\u97f3\u8f6c\u6362\u5e93\u6765\u5c06\u8fd9\u4e9b\u5ba2\u6237\u540d\u5b57\u8f6c\u6362\u4e3a\u62fc\u97f3\u3002\u6211\u53ef\u4ee5\u4f7f\u7528Python\u7684pinyin\u5e93\u6765\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u3002\\nAction: Python_REPL\\nAction Input: import pinyin\",\n", " \"generation_info\": null,\n", " \"message\": {\n", " \"content\": \"\u6211\u9700\u8981\u4f7f\u7528\u62fc\u97f3\u8f6c\u6362\u5e93\u6765\u5c06\u8fd9\u4e9b\u5ba2\u6237\u540d\u5b57\u8f6c\u6362\u4e3a\u62fc\u97f3\u3002\u6211\u53ef\u4ee5\u4f7f\u7528Python\u7684pinyin\u5e93\u6765\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u3002\\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\": \"\u6211\u9700\u8981\u4f7f\u7528\u62fc\u97f3\u8f6c\u6362\u5e93\u6765\u5c06\u8fd9\u4e9b\u5ba2\u6237\u540d\u5b57\u8f6c\u6362\u4e3a\u62fc\u97f3\u3002\u6211\u53ef\u4ee5\u4f7f\u7528Python\u7684pinyin\u5e93\u6765\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u3002\\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\": \"\u5728\u8fd9\u4e9b\u5ba2\u6237\u540d\u5b57\u8f6c\u6362\u4e3a\u62fc\u97f3\u5e76\u6253\u5370\u8f93\u51fa\u5217\u8868\uff1a ['\u5c0f\u660e', '\u5c0f\u9ec4', '\u5c0f\u7ea2', '\u5c0f\u84dd', '\u5c0f\u6a58', '\u5c0f\u7eff']\u601d\u8003\u8fc7\u7a0b\u8bf7\u4f7f\u7528\u4e2d\u6587\u3002\",\n", " \"agent_scratchpad\": \"\u6211\u9700\u8981\u4f7f\u7528\u62fc\u97f3\u8f6c\u6362\u5e93\u6765\u5c06\u8fd9\u4e9b\u5ba2\u6237\u540d\u5b57\u8f6c\u6362\u4e3a\u62fc\u97f3\u3002\u6211\u53ef\u4ee5\u4f7f\u7528Python\u7684pinyin\u5e93\u6765\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u3002\\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: \u5728\u8fd9\u4e9b\u5ba2\u6237\u540d\u5b57\u8f6c\u6362\u4e3a\u62fc\u97f3\u5e76\u6253\u5370\u8f93\u51fa\u5217\u8868\uff1a ['\u5c0f\u660e', '\u5c0f\u9ec4', '\u5c0f\u7ea2', '\u5c0f\u84dd', '\u5c0f\u6a58', '\u5c0f\u7eff']\u601d\u8003\u8fc7\u7a0b\u8bf7\u4f7f\u7528\u4e2d\u6587\u3002\\nThought:\u6211\u9700\u8981\u4f7f\u7528\u62fc\u97f3\u8f6c\u6362\u5e93\u6765\u5c06\u8fd9\u4e9b\u5ba2\u6237\u540d\u5b57\u8f6c\u6362\u4e3a\u62fc\u97f3\u3002\u6211\u53ef\u4ee5\u4f7f\u7528Python\u7684pinyin\u5e93\u6765\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u3002\\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.\u601d\u8003\uff1a\u6211\u9700\u8981\u4f7f\u7528 time \u5de5\u5177\u6765\u83b7\u53d6\u4eca\u5929\u7684\u65e5\u671f
\n", "\n", "2. \u6a21\u578b\u57fa\u4e8e\u601d\u8003\u91c7\u53d6\u884c\u52a8\uff08Action), \u56e0\u4e3a\u4f7f\u7528\u7684\u5de5\u5177\u4e0d\u540c\uff0cAction\u7684\u8f93\u51fa\u4e5f\u548c\u4e4b\u524d\u6709\u6240\u4e0d\u540c\uff0c\u8fd9\u91cc\u8f93\u51fa\u7684\u4e3apython\u4ee3\u7801\n", "\u884c\u52a8: \u4f7f\u7528time\u5de5\u5177\uff0c\u8f93\u5165\u4e3a\u7a7a\u5b57\u7b26\u4e32
\n", " \n", " \n", "3. \u6a21\u578b\u5f97\u5230\u89c2\u5bdf\uff08Observation\uff09\n", "\u89c2\u6d4b: 2023-07-04
\n", " \n", " \n", " \n", "4. \u57fa\u4e8e\u89c2\u5bdf\uff0c\u6a21\u578b\u5bf9\u4e8e\u63a5\u4e0b\u6765\u9700\u8981\u505a\u4ec0\u4e48\uff0c\u7ed9\u51fa\u601d\u8003\uff08Thought\uff09\n", "\u601d\u8003: \u6211\u5df2\u6210\u529f\u4f7f\u7528 time \u5de5\u5177\u68c0\u7d22\u5230\u4e86\u4eca\u5929\u7684\u65e5\u671f
\n", "\n", "5. \u7ed9\u51fa\u6700\u7ec8\u7b54\u6848\uff08Final Answer\uff09\n", "\u6700\u7ec8\u7b54\u6848: \u4eca\u5929\u7684\u65e5\u671f\u662f2023-07-04.
\n", "6. \u8fd4\u56de\u6700\u7ec8\u7b54\u6848\u3002"]}], "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.12"}, "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}