File size: 3,882 Bytes
eefd46e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{
 "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
}