examples

Форк
0
/
02a-actions-weather-api.ipynb 
389 строк · 16.1 Кб
1
{
2
  "cells": [
3
    {
4
      "attachments": {},
5
      "cell_type": "markdown",
6
      "metadata": {
7
        "id": "hNbFoillfVmR"
8
      },
9
      "source": [
10
        "[![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/chatbots/nemo-guardrails/02a-actions-weather-api.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/chatbots/nemo-guardrails/02a-actions-weather-api.ipynb)"
11
      ]
12
    },
13
    {
14
      "cell_type": "code",
15
      "execution_count": 11,
16
      "metadata": {
17
        "colab": {
18
          "base_uri": "https://localhost:8080/"
19
        },
20
        "id": "pZ8GfhOHfVmS",
21
        "outputId": "3d0bbb9b-2546-4b51-dc15-cba48e07b4ab"
22
      },
23
      "outputs": [
24
        {
25
          "name": "stdout",
26
          "output_type": "stream",
27
          "text": [
28
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.9/13.9 MB\u001b[0m \u001b[31m41.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
29
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.6/73.6 kB\u001b[0m \u001b[31m7.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
30
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m50.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
31
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.4/1.4 MB\u001b[0m \u001b[31m55.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
32
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m60.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
33
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m647.5/647.5 kB\u001b[0m \u001b[31m39.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
34
            "\u001b[?25h  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
35
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m86.0/86.0 kB\u001b[0m \u001b[31m7.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
36
            "\u001b[?25h  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
37
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m57.1/57.1 kB\u001b[0m \u001b[31m6.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
38
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m67.0/67.0 kB\u001b[0m \u001b[31m6.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
39
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m5.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
40
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m71.5/71.5 kB\u001b[0m \u001b[31m7.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
41
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m69.6/69.6 kB\u001b[0m \u001b[31m6.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
42
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m90.0/90.0 kB\u001b[0m \u001b[31m9.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
43
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m7.4/7.4 MB\u001b[0m \u001b[31m69.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
44
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m52.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
45
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m268.8/268.8 kB\u001b[0m \u001b[31m24.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
46
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m5.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
47
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.4/49.4 kB\u001b[0m \u001b[31m3.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
48
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m7.8/7.8 MB\u001b[0m \u001b[31m42.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
49
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m46.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
50
            "\u001b[?25h  Building wheel for annoy (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
51
            "  Building wheel for sentence-transformers (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
52
            "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
53
            "ipython 7.34.0 requires jedi>=0.16, which is not installed.\n",
54
            "cvxpy 1.3.2 requires setuptools>65.5.1, but you have setuptools 65.5.1 which is incompatible.\u001b[0m\u001b[31m\n",
55
            "\u001b[0m"
56
          ]
57
        }
58
      ],
59
      "source": [
60
        "!pip install -qU \\\n",
61
        "    nemoguardrails==0.4.0 \\\n",
62
        "    openai==0.27.8"
63
      ]
64
    },
65
    {
66
      "attachments": {},
67
      "cell_type": "markdown",
68
      "metadata": {
69
        "id": "GZXGTSv2f_Fy"
70
      },
71
      "source": [
72
        "Set your OpenAI API key:"
73
      ]
74
    },
75
    {
76
      "cell_type": "code",
77
      "execution_count": 12,
78
      "metadata": {
79
        "id": "Oc4N2tXxgBMp"
80
      },
81
      "outputs": [],
82
      "source": [
83
        "import os\n",
84
        "\n",
85
        "os.environ[\"OPENAI_API_KEY\"] = \"sk-...\""
86
      ]
87
    },
88
    {
89
      "attachments": {},
90
      "cell_type": "markdown",
91
      "metadata": {
92
        "id": "WuVyO37cfVmT"
93
      },
94
      "source": [
95
        "# Adding Actions: Weather API\n",
96
        "\n",
97
        "In Guardrails we can add _actions_ to our colang files. These actions allow us to execute code, for example:\n",
98
        "\n",
99
        "```\n",
100
        "define flow\n",
101
        "    user ask question\n",
102
        "    $answer = execute qa_func(prompt=$last_user_message)\n",
103
        "    bot $answer\n",
104
        "```\n",
105
        "\n",
106
        "In this Colang flow we expect the user to ask some question (`user ask question`), if so we run an _action_ (equivalent to a Python function) called `qa_func`, we also pass the `$last_user_message` (a default variable set by Guardrails) to this function via the function's `prompt` parameter.\n",
107
        "\n",
108
        "The function/action returns a value which we store in the `$answer` context variable, we then tell the bot to return this answer to the user (`bot $answer`).\n",
109
        "\n",
110
        "Let's take a look at an example colang file for this:"
111
      ]
112
    },
113
    {
114
      "cell_type": "code",
115
      "execution_count": 43,
116
      "metadata": {
117
        "id": "F41pofCJfVmT"
118
      },
119
      "outputs": [],
120
      "source": [
121
        "colang_content = \"\"\"\n",
122
        "# define limits\n",
123
        "define user ask weather\n",
124
        "    \"how is the weather today?\"\n",
125
        "    \"should I wear a coat?\"\n",
126
        "\n",
127
        "define bot answer weather\n",
128
        "    bot report weather\n",
129
        "\n",
130
        "define flow weather\n",
131
        "    user ask weather\n",
132
        "    $coords = execute location_api()\n",
133
        "    $weather = execute weather_api(coords=$coords)\n",
134
        "    bot answer weather\n",
135
        "\n",
136
        "# here we use the chatbot for anything else\n",
137
        "define flow\n",
138
        "    user ...\n",
139
        "    bot greeting\n",
140
        "\"\"\"\n",
141
        "yaml_content = \"\"\"\n",
142
        "models:\n",
143
        "- type: main\n",
144
        "  engine: openai\n",
145
        "  model: text-davinci-003\n",
146
        "\"\"\""
147
      ]
148
    },
149
    {
150
      "attachments": {},
151
      "cell_type": "markdown",
152
      "metadata": {
153
        "id": "sKS-7hfjIR2h"
154
      },
155
      "source": [
156
        "In our Colang we specify the `weather` dialogue flow with two calls to actions, one for a location API and the next for a weather API (using info from the location API)."
157
      ]
158
    },
159
    {
160
      "cell_type": "code",
161
      "execution_count": 44,
162
      "metadata": {
163
        "id": "JQQEhEZtsHBQ"
164
      },
165
      "outputs": [],
166
      "source": [
167
        "import requests\n",
168
        "\n",
169
        "async def weather_api(coords: list):\n",
170
        "    latitude, longitude = coords\n",
171
        "    res = requests.get(\n",
172
        "        \"https://api.open-meteo.com/v1/forecast\",\n",
173
        "        params={\n",
174
        "            \"latitude\": latitude,\n",
175
        "            \"longitude\": longitude,\n",
176
        "            \"current_weather\": \"true\"\n",
177
        "        }\n",
178
        "    )\n",
179
        "    weather = res.json()[\"current_weather\"]\n",
180
        "    weather_report = f\"\"\"The current weather is:\n",
181
        "    temperature: {weather[\"temperature\"]}\n",
182
        "    windspeed: {weather[\"windspeed\"]}\n",
183
        "    wind direction: {weather[\"winddirection\"]} degrees\n",
184
        "    And it is {\"daytime\" if weather[\"is_day\"] else \"nightime\"}\"\"\"\n",
185
        "    return weather_report\n",
186
        "\n",
187
        "async def location_api():\n",
188
        "    res = requests.get(\"http://ip-api.com/json/\")\n",
189
        "    return res.json()['lat'], res.json()['lon']"
190
      ]
191
    },
192
    {
193
      "attachments": {},
194
      "cell_type": "markdown",
195
      "metadata": {
196
        "id": "83ZFNkebfVmT"
197
      },
198
      "source": [
199
        "We've defined our location and weather APIs required for our bot to give us up to date advice on the weather in our current location."
200
      ]
201
    },
202
    {
203
      "cell_type": "code",
204
      "execution_count": 45,
205
      "metadata": {
206
        "id": "GGEaBLR9fVmU"
207
      },
208
      "outputs": [],
209
      "source": [
210
        "from nemoguardrails import LLMRails, RailsConfig\n",
211
        "\n",
212
        "# initialize rails config\n",
213
        "config = RailsConfig.from_content(\n",
214
        "    colang_content=colang_content,\n",
215
        "    yaml_content=yaml_content\n",
216
        ")\n",
217
        "# create rails\n",
218
        "rails = LLMRails(config, verbose=True)"
219
      ]
220
    },
221
    {
222
      "attachments": {},
223
      "cell_type": "markdown",
224
      "metadata": {
225
        "id": "Dkzl4ZU7fVmU"
226
      },
227
      "source": [
228
        "We need to register the two functions as actions like so:"
229
      ]
230
    },
231
    {
232
      "cell_type": "code",
233
      "execution_count": 46,
234
      "metadata": {
235
        "id": "B8jjXe3MfVmU"
236
      },
237
      "outputs": [],
238
      "source": [
239
        "rails.register_action(action=location_api, name=\"location_api\")\n",
240
        "rails.register_action(action=weather_api, name=\"weather_api\")"
241
      ]
242
    },
243
    {
244
      "attachments": {},
245
      "cell_type": "markdown",
246
      "metadata": {
247
        "id": "CNQ3Si4ufVmU"
248
      },
249
      "source": [
250
        "Let's try our guardrails..."
251
      ]
252
    },
253
    {
254
      "cell_type": "code",
255
      "execution_count": 47,
256
      "metadata": {
257
        "colab": {
258
          "base_uri": "https://localhost:8080/",
259
          "height": 36
260
        },
261
        "id": "wMiR0TfWfVmU",
262
        "outputId": "b7634af8-6c2c-405d-a5ae-cbc16460f856"
263
      },
264
      "outputs": [
265
        {
266
          "data": {
267
            "application/vnd.google.colaboratory.intrinsic+json": {
268
              "type": "string"
269
            },
270
            "text/plain": [
271
              "'Hi there! How can I be of assistance?'"
272
            ]
273
          },
274
          "execution_count": 47,
275
          "metadata": {},
276
          "output_type": "execute_result"
277
        }
278
      ],
279
      "source": [
280
        "await rails.generate_async(prompt=\"hello\")"
281
      ]
282
    },
283
    {
284
      "attachments": {},
285
      "cell_type": "markdown",
286
      "metadata": {
287
        "id": "yRVLJc3yfVmV"
288
      },
289
      "source": [
290
        "Now we can try again:"
291
      ]
292
    },
293
    {
294
      "cell_type": "code",
295
      "execution_count": 48,
296
      "metadata": {
297
        "colab": {
298
          "base_uri": "https://localhost:8080/",
299
          "height": 53
300
        },
301
        "id": "bgRfARA2fVmV",
302
        "outputId": "0e5c00ae-7aa3-4e18-b647-2e62fc0d1c22"
303
      },
304
      "outputs": [
305
        {
306
          "data": {
307
            "application/vnd.google.colaboratory.intrinsic+json": {
308
              "type": "string"
309
            },
310
            "text/plain": [
311
              "'The current weather in Washington, DC is 27.8 degrees, with a wind speed of 13.7 mph and a wind direction of 275.0 degrees. It is currently daytime.'"
312
            ]
313
          },
314
          "execution_count": 48,
315
          "metadata": {},
316
          "output_type": "execute_result"
317
        }
318
      ],
319
      "source": [
320
        "await rails.generate_async(prompt=\"how is the weather?\")"
321
      ]
322
    },
323
    {
324
      "cell_type": "code",
325
      "execution_count": 49,
326
      "metadata": {
327
        "colab": {
328
          "base_uri": "https://localhost:8080/",
329
          "height": 53
330
        },
331
        "id": "fXK1TfuMGo8A",
332
        "outputId": "9088d716-10aa-4a20-e8e9-26e2461826a8"
333
      },
334
      "outputs": [
335
        {
336
          "data": {
337
            "application/vnd.google.colaboratory.intrinsic+json": {
338
              "type": "string"
339
            },
340
            "text/plain": [
341
              "'It is currently 27.8 degrees outside with a windspeed of 13.7 and a wind direction of 275.0 degrees. Based on this, it is recommended that you wear a sweatshirt today.'"
342
            ]
343
          },
344
          "execution_count": 49,
345
          "metadata": {},
346
          "output_type": "execute_result"
347
        }
348
      ],
349
      "source": [
350
        "await rails.generate_async(prompt=\"do I need a sweatshirt today?\")"
351
      ]
352
    },
353
    {
354
      "attachments": {},
355
      "cell_type": "markdown",
356
      "metadata": {
357
        "id": "bn4w59GbfVmV"
358
      },
359
      "source": [
360
        "Great, we can see our weather checking Guardrails are working!"
361
      ]
362
    }
363
  ],
364
  "metadata": {
365
    "colab": {
366
      "provenance": []
367
    },
368
    "kernelspec": {
369
      "display_name": "redacre",
370
      "language": "python",
371
      "name": "python3"
372
    },
373
    "language_info": {
374
      "codemirror_mode": {
375
        "name": "ipython",
376
        "version": 3
377
      },
378
      "file_extension": ".py",
379
      "mimetype": "text/x-python",
380
      "name": "python",
381
      "nbconvert_exporter": "python",
382
      "pygments_lexer": "ipython3",
383
      "version": "3.11.3"
384
    },
385
    "orig_nbformat": 4
386
  },
387
  "nbformat": 4,
388
  "nbformat_minor": 0
389
}
390

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

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

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

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