{ "cells": [ { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from langchain.agents import load_tools\n", "from langchain.agents import initialize_agent\n", "from langchain.agents import AgentType\n", "from langchain.llms import OpenAI" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "llm = OpenAI(temperature=0, model_name='text-davinci-002')\n", "tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, return_intermediate_steps=True)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m I need to know who the current CEO of TikTok is, and then I need to do some math.\n", "Action: Search\n", "Action Input: \"TikTok CEO\"\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3mShou Chew\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I need to know how old Shou Chew is.\n", "Action: Search\n", "Action Input: \"Shou Chew age\"\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3m40 years\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I need to do some math.\n", "Action: Calculator\n", "Action Input: 40+10\u001b[0m\n", "Observation: \u001b[33;1m\u001b[1;3mAnswer: 50\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", "Final Answer: The current CEO of TikTok is Shou Chew, and he is 50 years old.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] } ], "source": [ "response = agent({\"input\":\"抖音现任的CEO是谁?他年龄加10岁等于多少?\"})" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[\n", " [\n", " [\n", " \"Search\",\n", " \"TikTok CEO\",\n", " \" I need to know who the current CEO of TikTok is, and then I need to do some math.\\nAction: Search\\nAction Input: \\\"TikTok CEO\\\"\"\n", " ],\n", " \"Shou Chew\"\n", " ],\n", " [\n", " [\n", " \"Search\",\n", " \"Shou Chew age\",\n", " \" I need to know how old Shou Chew is.\\nAction: Search\\nAction Input: \\\"Shou Chew age\\\"\"\n", " ],\n", " \"40 years\"\n", " ],\n", " [\n", " [\n", " \"Calculator\",\n", " \"40+10\",\n", " \" I need to do some math.\\nAction: Calculator\\nAction Input: 40+10\"\n", " ],\n", " \"Answer: 50\"\n", " ]\n", "]\n" ] } ], "source": [ "# The actual return type is a NamedTuple for the agent action, and then an observation\n", "# print(response[\"intermediate_steps\"])\n", "\n", "import json\n", "print(json.dumps(response[\"intermediate_steps\"], indent=2))" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.10.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }