examples

Форк
0
/
xx-quick-agents-intro.ipynb 
621 строка · 36.9 Кб
1
{
2
  "cells": [
3
    {
4
      "attachments": {},
5
      "cell_type": "markdown",
6
      "metadata": {},
7
      "source": [
8
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/xx-quick-agents-intro.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/xx-quick-agents-intro.ipynb)"
9
      ]
10
    },
11
    {
12
      "cell_type": "code",
13
      "execution_count": 1,
14
      "metadata": {
15
        "colab": {
16
          "base_uri": "https://localhost:8080/"
17
        },
18
        "id": "97UOI19nH9Gl",
19
        "outputId": "ae8fdff2-aa3b-4275-a779-21f1697cf453"
20
      },
21
      "outputs": [
22
        {
23
          "name": "stdout",
24
          "output_type": "stream",
25
          "text": [
26
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m489.1/489.1 KB\u001b[0m \u001b[31m7.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
27
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m70.3/70.3 KB\u001b[0m \u001b[31m3.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
28
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m20.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
29
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m158.8/158.8 KB\u001b[0m \u001b[31m8.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
30
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m114.2/114.2 KB\u001b[0m \u001b[31m1.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
31
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m264.6/264.6 KB\u001b[0m \u001b[31m10.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
32
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.1/49.1 KB\u001b[0m \u001b[31m2.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
33
            "\u001b[?25h"
34
          ]
35
        }
36
      ],
37
      "source": [
38
        "!pip install -qU langchain openai"
39
      ]
40
    },
41
    {
42
      "attachments": {},
43
      "cell_type": "markdown",
44
      "metadata": {
45
        "id": "nEM8P_2rp2tC"
46
      },
47
      "source": [
48
        "# Quick Agents Intro\n",
49
        "\n",
50
        "#### [LangChain Handbook](https://pinecone.io/learn/langchain/)\n",
51
        "\n",
52
        "In this notebook we'll see a quick intro to LangChain *agents*. For a more thorough introduction, check out [this notebook](https://github.com/pinecone-io/examples/blob/master/generation/langchain/handbook/06-langchain-agents.ipynb).\n",
53
        "\n",
54
        "To use agents we need three main components:\n",
55
        "\n",
56
        "* **L**arge **L**anguage **M**odels (**LLMs**)\n",
57
        "* **Tools**\n",
58
        "* The **Agents** themselves\n",
59
        "\n",
60
        "Let's get started by initializing a LLM:"
61
      ]
62
    },
63
    {
64
      "cell_type": "code",
65
      "execution_count": 2,
66
      "metadata": {
67
        "id": "7VUCkEdEF2f8"
68
      },
69
      "outputs": [],
70
      "source": [
71
        "from langchain import OpenAI\n",
72
        "\n",
73
        "llm = OpenAI(\n",
74
        "    openai_api_key=\"OPENAI_API_KEY\",  # platform.openai.com\n",
75
        "    temperature=0,\n",
76
        "    model_name=\"text-davinci-003\"\n",
77
        ")"
78
      ]
79
    },
80
    {
81
      "attachments": {},
82
      "cell_type": "markdown",
83
      "metadata": {
84
        "id": "SBYRoo4Uq58r"
85
      },
86
      "source": [
87
        "Next, initialize a *calculator* tool using a `LLMMathChain`:"
88
      ]
89
    },
90
    {
91
      "cell_type": "code",
92
      "execution_count": 3,
93
      "metadata": {
94
        "id": "01qXr4jPHnH1"
95
      },
96
      "outputs": [],
97
      "source": [
98
        "from langchain.chains import LLMMathChain\n",
99
        "from langchain.agents import Tool\n",
100
        "\n",
101
        "llm_math = LLMMathChain(llm=llm)\n",
102
        "\n",
103
        "# initialize the math tool\n",
104
        "math_tool = Tool(\n",
105
        "    name='Calculator',\n",
106
        "    func=llm_math.run,\n",
107
        "    description='Useful for when you need to answer questions about math.'\n",
108
        ")\n",
109
        "# when giving tools to LLM, we must pass as list of tools\n",
110
        "tools = [math_tool]"
111
      ]
112
    },
113
    {
114
      "cell_type": "code",
115
      "execution_count": 4,
116
      "metadata": {
117
        "colab": {
118
          "base_uri": "https://localhost:8080/"
119
        },
120
        "id": "YvNlbIHsCOS6",
121
        "outputId": "01c72dc5-4405-4695-bd92-7e1acb7cedf8"
122
      },
123
      "outputs": [
124
        {
125
          "data": {
126
            "text/plain": [
127
              "('Calculator', 'Useful for when you need to answer questions about math.')"
128
            ]
129
          },
130
          "execution_count": 4,
131
          "metadata": {},
132
          "output_type": "execute_result"
133
        }
134
      ],
135
      "source": [
136
        "tools[0].name, tools[0].description"
137
      ]
138
    },
139
    {
140
      "cell_type": "code",
141
      "execution_count": 5,
142
      "metadata": {
143
        "id": "atEddXO1B1-Q"
144
      },
145
      "outputs": [],
146
      "source": [
147
        "from langchain.agents import load_tools\n",
148
        "\n",
149
        "tools = load_tools(\n",
150
        "    ['llm-math'],\n",
151
        "    llm=llm\n",
152
        ")"
153
      ]
154
    },
155
    {
156
      "cell_type": "code",
157
      "execution_count": 6,
158
      "metadata": {
159
        "colab": {
160
          "base_uri": "https://localhost:8080/"
161
        },
162
        "id": "UVeXFJYYCWXJ",
163
        "outputId": "5c977b7c-ab3a-401a-bbfb-19b806118a3b"
164
      },
165
      "outputs": [
166
        {
167
          "data": {
168
            "text/plain": [
169
              "('Calculator', 'Useful for when you need to answer questions about math.')"
170
            ]
171
          },
172
          "execution_count": 6,
173
          "metadata": {},
174
          "output_type": "execute_result"
175
        }
176
      ],
177
      "source": [
178
        "tools[0].name, tools[0].description"
179
      ]
180
    },
181
    {
182
      "attachments": {},
183
      "cell_type": "markdown",
184
      "metadata": {
185
        "id": "YanQBaD3rAMk"
186
      },
187
      "source": [
188
        "Now we use our `tools` (containing a single tool for now) to initialize an agent:"
189
      ]
190
    },
191
    {
192
      "cell_type": "code",
193
      "execution_count": 7,
194
      "metadata": {
195
        "id": "e-sxCbhtIqTj"
196
      },
197
      "outputs": [],
198
      "source": [
199
        "from langchain.agents import initialize_agent\n",
200
        "\n",
201
        "zero_shot_agent = initialize_agent(\n",
202
        "\tagent=\"zero-shot-react-description\",\n",
203
        "\ttools=tools,\n",
204
        "\tllm=llm,\n",
205
        "\tverbose=True,\n",
206
        "\tmax_iterations=3\n",
207
        ")"
208
      ]
209
    },
210
    {
211
      "attachments": {},
212
      "cell_type": "markdown",
213
      "metadata": {
214
        "id": "YpTprlzAlNK8"
215
      },
216
      "source": [
217
        "Let's try asking some questions:"
218
      ]
219
    },
220
    {
221
      "cell_type": "code",
222
      "execution_count": 8,
223
      "metadata": {
224
        "colab": {
225
          "base_uri": "https://localhost:8080/"
226
        },
227
        "id": "4BJ_ky2Pdja-",
228
        "outputId": "94f9c715-671c-4094-8884-2fa766145f06"
229
      },
230
      "outputs": [
231
        {
232
          "name": "stdout",
233
          "output_type": "stream",
234
          "text": [
235
            "\n",
236
            "\n",
237
            "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
238
            "\u001b[32;1m\u001b[1;3m I need to calculate this expression\n",
239
            "Action: Calculator\n",
240
            "Action Input: (4.5*2.1)^2.2\u001b[0m\n",
241
            "Observation: \u001b[36;1m\u001b[1;3mAnswer: 139.94261298333066\n",
242
            "\u001b[0m\n",
243
            "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
244
            "Final Answer: 139.94261298333066\u001b[0m\n",
245
            "\n",
246
            "\u001b[1m> Finished chain.\u001b[0m\n"
247
          ]
248
        },
249
        {
250
          "data": {
251
            "text/plain": [
252
              "{'input': 'what is (4.5*2.1)^2.2?', 'output': '139.94261298333066'}"
253
            ]
254
          },
255
          "execution_count": 8,
256
          "metadata": {},
257
          "output_type": "execute_result"
258
        }
259
      ],
260
      "source": [
261
        "zero_shot_agent(\"what is (4.5*2.1)^2.2?\")"
262
      ]
263
    },
264
    {
265
      "cell_type": "code",
266
      "execution_count": 9,
267
      "metadata": {
268
        "colab": {
269
          "base_uri": "https://localhost:8080/"
270
        },
271
        "id": "uii8408sdjLD",
272
        "outputId": "d0ab2e03-1682-4fcb-a49f-6be126e4d831"
273
      },
274
      "outputs": [
275
        {
276
          "data": {
277
            "text/plain": [
278
              "139.94261298333066"
279
            ]
280
          },
281
          "execution_count": 9,
282
          "metadata": {},
283
          "output_type": "execute_result"
284
        }
285
      ],
286
      "source": [
287
        "(4.5*2.1)**2.2"
288
      ]
289
    },
290
    {
291
      "attachments": {},
292
      "cell_type": "markdown",
293
      "metadata": {
294
        "id": "5tmxoqqFrHMM"
295
      },
296
      "source": [
297
        "Looks good, let's try something else:"
298
      ]
299
    },
300
    {
301
      "cell_type": "code",
302
      "execution_count": 10,
303
      "metadata": {
304
        "colab": {
305
          "base_uri": "https://localhost:8080/"
306
        },
307
        "id": "DlGXFuSgh7kW",
308
        "outputId": "23d11270-e6d3-4d9a-ce98-2ec091db4d94"
309
      },
310
      "outputs": [
311
        {
312
          "name": "stdout",
313
          "output_type": "stream",
314
          "text": [
315
            "\n",
316
            "\n",
317
            "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
318
            "\u001b[32;1m\u001b[1;3m I need to figure out how many apples are in the boxes\n",
319
            "Action: Calculator\n",
320
            "Action Input: 8 * 2.5\u001b[0m\n",
321
            "Observation: \u001b[36;1m\u001b[1;3mAnswer: 20.0\n",
322
            "\u001b[0m\n",
323
            "Thought:\u001b[32;1m\u001b[1;3m I need to add the apples Mary has to the apples in the boxes\n",
324
            "Action: Calculator\n",
325
            "Action Input: 4 + 20.0\u001b[0m\n",
326
            "Observation: \u001b[36;1m\u001b[1;3mAnswer: 24.0\n",
327
            "\u001b[0m\n",
328
            "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
329
            "Final Answer: We have 24 apples.\u001b[0m\n",
330
            "\n",
331
            "\u001b[1m> Finished chain.\u001b[0m\n"
332
          ]
333
        },
334
        {
335
          "data": {
336
            "text/plain": [
337
              "{'input': 'if Mary has four apples and Giorgio brings two and a half apple boxes (apple box contains eight apples), how many apples do we have?',\n",
338
              " 'output': 'We have 24 apples.'}"
339
            ]
340
          },
341
          "execution_count": 10,
342
          "metadata": {},
343
          "output_type": "execute_result"
344
        }
345
      ],
346
      "source": [
347
        "zero_shot_agent(\"if Mary has four apples and Giorgio brings two and a half apple \"\n",
348
        "                \"boxes (apple box contains eight apples), how many apples do we \"\n",
349
        "                \"have?\")"
350
      ]
351
    },
352
    {
353
      "attachments": {},
354
      "cell_type": "markdown",
355
      "metadata": {
356
        "id": "jhHB8xEokLR3"
357
      },
358
      "source": [
359
        "But what if we ask a non-math question? Something that a plain and simple LLM should be able to answer easily?"
360
      ]
361
    },
362
    {
363
      "cell_type": "code",
364
      "execution_count": 11,
365
      "metadata": {
366
        "colab": {
367
          "base_uri": "https://localhost:8080/",
368
          "height": 496
369
        },
370
        "id": "hxSpYf8vZpBv",
371
        "outputId": "805a8db7-1513-4a95-d357-7cb17b4ab1fc"
372
      },
373
      "outputs": [
374
        {
375
          "name": "stdout",
376
          "output_type": "stream",
377
          "text": [
378
            "\n",
379
            "\n",
380
            "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
381
            "\u001b[32;1m\u001b[1;3m I need to look up the answer\n",
382
            "Action: Look up\n",
383
            "Action Input: Capital of Norway\u001b[0m\n",
384
            "Observation: Look up is not a valid tool, try another one.\n",
385
            "Thought:\u001b[32;1m\u001b[1;3m I need to find the answer using a tool\n",
386
            "Action: Calculator\n",
387
            "Action Input: N/A\u001b[0m"
388
          ]
389
        },
390
        {
391
          "ename": "ValueError",
392
          "evalue": "ignored",
393
          "output_type": "error",
394
          "traceback": [
395
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
396
            "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
397
            "\u001b[0;32m<ipython-input-11-c49ec2f16b95>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mzero_shot_agent\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"what is the capital of Norway?\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
398
            "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/langchain/chains/base.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, inputs, return_only_outputs)\u001b[0m\n\u001b[1;32m    114\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    115\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcallback_manager\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_chain_error\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mverbose\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 116\u001b[0;31m             \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    117\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcallback_manager\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_chain_end\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mverbose\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    118\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprep_outputs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreturn_only_outputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
399
            "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/langchain/chains/base.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, inputs, return_only_outputs)\u001b[0m\n\u001b[1;32m    111\u001b[0m         )\n\u001b[1;32m    112\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 113\u001b[0;31m             \u001b[0moutputs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    114\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    115\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcallback_manager\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_chain_error\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mverbose\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
400
            "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/langchain/agents/agent.py\u001b[0m in \u001b[0;36m_call\u001b[0;34m(self, inputs)\u001b[0m\n\u001b[1;32m    635\u001b[0m         \u001b[0;31m# We now enter the agent loop (until it returns something).\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    636\u001b[0m         \u001b[0;32mwhile\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_should_continue\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0miterations\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 637\u001b[0;31m             next_step_output = self._take_next_step(\n\u001b[0m\u001b[1;32m    638\u001b[0m                 \u001b[0mname_to_tool_map\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolor_mapping\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mintermediate_steps\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    639\u001b[0m             )\n",
401
            "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/langchain/agents/agent.py\u001b[0m in \u001b[0;36m_take_next_step\u001b[0;34m(self, name_to_tool_map, color_mapping, inputs, intermediate_steps)\u001b[0m\n\u001b[1;32m    567\u001b[0m                 \u001b[0mtool_run_kwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"llm_prefix\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    568\u001b[0m             \u001b[0;31m# We then call the tool on the tool input to get an observation\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 569\u001b[0;31m             observation = tool.run(\n\u001b[0m\u001b[1;32m    570\u001b[0m                 \u001b[0moutput\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtool_input\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mverbose\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolor\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcolor\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mtool_run_kwargs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    571\u001b[0m             )\n",
402
            "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/langchain/tools/base.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, tool_input, verbose, start_color, color, **kwargs)\u001b[0m\n\u001b[1;32m     71\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mException\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     72\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcallback_manager\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_tool_error\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mverbose_\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 73\u001b[0;31m             \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     74\u001b[0m         self.callback_manager.on_tool_end(\n\u001b[1;32m     75\u001b[0m             \u001b[0mobservation\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mverbose_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolor\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcolor\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
403
            "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/langchain/tools/base.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, tool_input, verbose, start_color, color, **kwargs)\u001b[0m\n\u001b[1;32m     68\u001b[0m         )\n\u001b[1;32m     69\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 70\u001b[0;31m             \u001b[0mobservation\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_run\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtool_input\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     71\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mException\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     72\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcallback_manager\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_tool_error\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mverbose_\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
404
            "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/langchain/agents/tools.py\u001b[0m in \u001b[0;36m_run\u001b[0;34m(self, tool_input)\u001b[0m\n\u001b[1;32m     15\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_run\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtool_input\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     16\u001b[0m         \u001b[0;34m\"\"\"Use the tool.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtool_input\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     19\u001b[0m     \u001b[0;32masync\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_arun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtool_input\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
405
            "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/langchain/chains/base.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m    211\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    212\u001b[0m                 \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"`run` supports only one positional argument.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 213\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moutput_keys\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    214\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    215\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mkwargs\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
406
            "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/langchain/chains/base.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, inputs, return_only_outputs)\u001b[0m\n\u001b[1;32m    114\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    115\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcallback_manager\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_chain_error\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mverbose\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 116\u001b[0;31m             \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    117\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcallback_manager\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_chain_end\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mverbose\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    118\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprep_outputs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreturn_only_outputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
407
            "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/langchain/chains/base.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, inputs, return_only_outputs)\u001b[0m\n\u001b[1;32m    111\u001b[0m         )\n\u001b[1;32m    112\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 113\u001b[0;31m             \u001b[0moutputs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    114\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    115\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcallback_manager\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_chain_error\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mverbose\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
408
            "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/langchain/chains/llm_math/base.py\u001b[0m in \u001b[0;36m_call\u001b[0;34m(self, inputs)\u001b[0m\n\u001b[1;32m     75\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcallback_manager\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_text\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minput_key\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mverbose\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     76\u001b[0m         \u001b[0mt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mllm_executor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mquestion\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minput_key\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstop\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"```output\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 77\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_process_llm_result\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     78\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     79\u001b[0m     \u001b[0;32masync\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_acall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mDict\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mDict\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
409
            "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/langchain/chains/llm_math/base.py\u001b[0m in \u001b[0;36m_process_llm_result\u001b[0;34m(self, t)\u001b[0m\n\u001b[1;32m     66\u001b[0m             \u001b[0manswer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Answer: \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Answer:\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     67\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 68\u001b[0;31m             \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"unknown format from LLM: {t}\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     69\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moutput_key\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0manswer\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     70\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
410
            "\u001b[0;31mValueError\u001b[0m: unknown format from LLM: N/A"
411
          ]
412
        }
413
      ],
414
      "source": [
415
        "zero_shot_agent(\"what is the capital of Norway?\")"
416
      ]
417
    },
418
    {
419
      "attachments": {},
420
      "cell_type": "markdown",
421
      "metadata": {
422
        "id": "ujEuxQC6kYCC"
423
      },
424
      "source": [
425
        "Unfortunately we end up hitting an error. That is because the agent is enforcing the use of a tool (this isn't always the case). A workaround we can implement is to just add another tool that can asnswer this question.\n",
426
        "\n",
427
        "If we'd like the agent to answer this \"general knowledge\" question. We can, we just need to link it to an LLM tool.\n",
428
        "\n",
429
        "We initialize the tool like so:"
430
      ]
431
    },
432
    {
433
      "cell_type": "code",
434
      "execution_count": 12,
435
      "metadata": {
436
        "id": "aOYwjJ9abLa3"
437
      },
438
      "outputs": [],
439
      "source": [
440
        "from langchain.prompts import PromptTemplate\n",
441
        "from langchain.chains import LLMChain\n",
442
        "\n",
443
        "prompt = PromptTemplate(\n",
444
        "    input_variables=[\"query\"],\n",
445
        "    template=\"{query}\"\n",
446
        ")\n",
447
        "\n",
448
        "llm_chain = LLMChain(llm=llm, prompt=prompt)\n",
449
        "\n",
450
        "# initialize the LLM tool\n",
451
        "llm_tool = Tool(\n",
452
        "    name='Language Model',\n",
453
        "    func=llm_chain.run,\n",
454
        "    description='use this tool for general purpose queries and logic'\n",
455
        ")"
456
      ]
457
    },
458
    {
459
      "attachments": {},
460
      "cell_type": "markdown",
461
      "metadata": {
462
        "id": "uyUVJF4vk6RC"
463
      },
464
      "source": [
465
        "Add the new tool to our `tools` list:"
466
      ]
467
    },
468
    {
469
      "cell_type": "code",
470
      "execution_count": 13,
471
      "metadata": {
472
        "id": "-BbA0ttRa3yG"
473
      },
474
      "outputs": [],
475
      "source": [
476
        "tools.append(llm_tool)"
477
      ]
478
    },
479
    {
480
      "attachments": {},
481
      "cell_type": "markdown",
482
      "metadata": {
483
        "id": "kUNnqeoQk891"
484
      },
485
      "source": [
486
        "Now we reinitialize the agent with our two tools (`\"Calculator\"` and `\"Language Model\"`)"
487
      ]
488
    },
489
    {
490
      "cell_type": "code",
491
      "execution_count": 14,
492
      "metadata": {
493
        "id": "1uGZQJHocG4L"
494
      },
495
      "outputs": [],
496
      "source": [
497
        "zero_shot_agent = initialize_agent(\n",
498
        "    agent=\"zero-shot-react-description\",\n",
499
        "    tools=tools,\n",
500
        "    llm=llm,\n",
501
        "    verbose=True,\n",
502
        "    max_iterations=3\n",
503
        ")"
504
      ]
505
    },
506
    {
507
      "cell_type": "code",
508
      "execution_count": 15,
509
      "metadata": {
510
        "colab": {
511
          "base_uri": "https://localhost:8080/"
512
        },
513
        "id": "NdI_4nkMcMMC",
514
        "outputId": "8724dfc8-195d-43f8-c5cb-0aa5de0a310a"
515
      },
516
      "outputs": [
517
        {
518
          "name": "stdout",
519
          "output_type": "stream",
520
          "text": [
521
            "\n",
522
            "\n",
523
            "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
524
            "\u001b[32;1m\u001b[1;3m I need to find out what the capital of Norway is\n",
525
            "Action: Language Model\n",
526
            "Action Input: What is the capital of Norway?\u001b[0m\n",
527
            "Observation: \u001b[33;1m\u001b[1;3m\n",
528
            "\n",
529
            "The capital of Norway is Oslo.\u001b[0m\n",
530
            "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
531
            "Final Answer: The capital of Norway is Oslo.\u001b[0m\n",
532
            "\n",
533
            "\u001b[1m> Finished chain.\u001b[0m\n"
534
          ]
535
        },
536
        {
537
          "data": {
538
            "text/plain": [
539
              "{'input': 'what is the capital of Norway?',\n",
540
              " 'output': 'The capital of Norway is Oslo.'}"
541
            ]
542
          },
543
          "execution_count": 15,
544
          "metadata": {},
545
          "output_type": "execute_result"
546
        }
547
      ],
548
      "source": [
549
        "zero_shot_agent(\"what is the capital of Norway?\")"
550
      ]
551
    },
552
    {
553
      "cell_type": "code",
554
      "execution_count": 16,
555
      "metadata": {
556
        "colab": {
557
          "base_uri": "https://localhost:8080/"
558
        },
559
        "id": "rTf_4U4JcOkv",
560
        "outputId": "d2962604-4bba-46a9-d429-cdc2a001a50d"
561
      },
562
      "outputs": [
563
        {
564
          "name": "stdout",
565
          "output_type": "stream",
566
          "text": [
567
            "\n",
568
            "\n",
569
            "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
570
            "\u001b[32;1m\u001b[1;3m I need to calculate this expression\n",
571
            "Action: Calculator\n",
572
            "Action Input: (4.5*2.1)^2.2\u001b[0m\n",
573
            "Observation: \u001b[36;1m\u001b[1;3mAnswer: 139.94261298333066\n",
574
            "\u001b[0m\n",
575
            "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
576
            "Final Answer: 139.94261298333066\u001b[0m\n",
577
            "\n",
578
            "\u001b[1m> Finished chain.\u001b[0m\n"
579
          ]
580
        },
581
        {
582
          "data": {
583
            "text/plain": [
584
              "{'input': 'what is (4.5*2.1)^2.2?', 'output': '139.94261298333066'}"
585
            ]
586
          },
587
          "execution_count": 16,
588
          "metadata": {},
589
          "output_type": "execute_result"
590
        }
591
      ],
592
      "source": [
593
        "zero_shot_agent(\"what is (4.5*2.1)^2.2?\")"
594
      ]
595
    },
596
    {
597
      "attachments": {},
598
      "cell_type": "markdown",
599
      "metadata": {
600
        "id": "7wT2tB30sklx"
601
      },
602
      "source": [
603
        "---"
604
      ]
605
    }
606
  ],
607
  "metadata": {
608
    "colab": {
609
      "provenance": []
610
    },
611
    "kernelspec": {
612
      "display_name": "Python 3",
613
      "name": "python3"
614
    },
615
    "language_info": {
616
      "name": "python"
617
    }
618
  },
619
  "nbformat": 4,
620
  "nbformat_minor": 0
621
}
622

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.