examples

Форк
0
/
01-variables-and-flows.ipynb 
388 строк · 10.3 Кб
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/chatbots/nemo-guardrails/01-variables-and-flows.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/01-variables-and-flows.ipynb)"
9
      ]
10
    },
11
    {
12
      "cell_type": "code",
13
      "execution_count": 1,
14
      "metadata": {
15
        "id": "r1o27sM3we_3"
16
      },
17
      "outputs": [],
18
      "source": [
19
        "!pip install -qU \\\n",
20
        "    nemoguardrails==0.4.0 \\\n",
21
        "    openai==0.27.8"
22
      ]
23
    },
24
    {
25
      "attachments": {},
26
      "cell_type": "markdown",
27
      "metadata": {
28
        "id": "pO2LkrOb2i74"
29
      },
30
      "source": [
31
        "# Variables and Flows\n",
32
        "\n",
33
        "Colang also allows us to use logical flows (if-else) and insert variables into our rails. There are several ways of inputing variables. When inserting the full conversational memory into our rails (rather than a single `prompt` as we have been doing so far) we can use the `context` message to initialize variables.\n",
34
        "\n",
35
        "Let's begin by doing that. First, we'll initialize a simple Rail."
36
      ]
37
    },
38
    {
39
      "cell_type": "code",
40
      "execution_count": 1,
41
      "metadata": {
42
        "id": "IX9eHPvKxJHK"
43
      },
44
      "outputs": [],
45
      "source": [
46
        "import os\n",
47
        "\n",
48
        "os.environ[\"OPENAI_API_KEY\"] = os.environ.get(\"OPENAI_API_KEY\") or \"YOUR_API_KEY\""
49
      ]
50
    },
51
    {
52
      "cell_type": "code",
53
      "execution_count": 2,
54
      "metadata": {
55
        "id": "DPjxD_5mxaTK"
56
      },
57
      "outputs": [],
58
      "source": [
59
        "colang_content = \"\"\"\n",
60
        "define user greeting\n",
61
        "    \"Hey there!\"\n",
62
        "    \"How are you?\"\n",
63
        "    \"What's up?\"\n",
64
        "\n",
65
        "define bot name greeting\n",
66
        "    \"Hey $name!\"\n",
67
        "\n",
68
        "define flow\n",
69
        "    user greeting\n",
70
        "    if $name\n",
71
        "        bot name greeting\n",
72
        "    else\n",
73
        "        bot greeting\n",
74
        "\"\"\"\n",
75
        "yaml_content = \"\"\"\n",
76
        "models:\n",
77
        "- type: main\n",
78
        "  engine: openai\n",
79
        "  model: text-davinci-003\n",
80
        "\"\"\""
81
      ]
82
    },
83
    {
84
      "attachments": {},
85
      "cell_type": "markdown",
86
      "metadata": {
87
        "id": "SQsM7utD3SKl"
88
      },
89
      "source": [
90
        "Initialize the Rail:"
91
      ]
92
    },
93
    {
94
      "cell_type": "code",
95
      "execution_count": 3,
96
      "metadata": {
97
        "id": "yu_R040G3TdI"
98
      },
99
      "outputs": [],
100
      "source": [
101
        "from nemoguardrails import LLMRails, RailsConfig\n",
102
        "\n",
103
        "# initialize rails config\n",
104
        "config = RailsConfig.from_content(\n",
105
        "    colang_content=colang_content,\n",
106
        "    yaml_content=yaml_content\n",
107
        ")\n",
108
        "# create rails\n",
109
        "rails = LLMRails(config)"
110
      ]
111
    },
112
    {
113
      "attachments": {},
114
      "cell_type": "markdown",
115
      "metadata": {
116
        "id": "q9BLM2he3Uyj"
117
      },
118
      "source": [
119
        "Now, to begin using the `messages` input we need to set a list of messages which must be structured as a list of dictionary objects each containing `\"role\"` and `\"content\"` keys. Guardrails allows us to use three roles, `\"context\"`, `\"user\"`, and `\"assistant\"`."
120
      ]
121
    },
122
    {
123
      "cell_type": "code",
124
      "execution_count": 4,
125
      "metadata": {
126
        "id": "bqsPOCZM3p36"
127
      },
128
      "outputs": [],
129
      "source": [
130
        "messages = [\n",
131
        "    {\"role\": \"context\", \"content\": \"\"},\n",
132
        "    {\"role\": \"user\", \"content\": \"Hey there!\"}\n",
133
        "    #{\"role\": \"assistant\",\n",
134
        "    # \"content\": \"Hi! How are you? Is there anything I can help with?\"},\n",
135
        "]"
136
      ]
137
    },
138
    {
139
      "cell_type": "code",
140
      "execution_count": 5,
141
      "metadata": {
142
        "colab": {
143
          "base_uri": "https://localhost:8080/"
144
        },
145
        "id": "aVcKmJ685FV9",
146
        "outputId": "2dc4c64b-1d88-41be-8fe8-4edd2be0aa60"
147
      },
148
      "outputs": [
149
        {
150
          "data": {
151
            "text/plain": [
152
              "{'role': 'assistant', 'content': 'Hey there! How can I help you?'}"
153
            ]
154
          },
155
          "execution_count": 5,
156
          "metadata": {},
157
          "output_type": "execute_result"
158
        }
159
      ],
160
      "source": [
161
        "await rails.generate_async(messages=messages)"
162
      ]
163
    },
164
    {
165
      "attachments": {},
166
      "cell_type": "markdown",
167
      "metadata": {
168
        "id": "Zp8g2eX-_Gh2"
169
      },
170
      "source": [
171
        "Right now the bot doesn't greet us with our name because we left the `context` empty. Instead, let's try passing in the `$name` parameter."
172
      ]
173
    },
174
    {
175
      "cell_type": "code",
176
      "execution_count": 11,
177
      "metadata": {
178
        "id": "ePFrXLBT5V2e"
179
      },
180
      "outputs": [],
181
      "source": [
182
        "messages = [\n",
183
        "    {\"role\": \"context\", \"content\": {\"name\": \"James\"}},\n",
184
        "    {\"role\": \"assistant\",\n",
185
        "     \"content\": \"Hi! How are you? Is there anything I can help with?\"},\n",
186
        "    {\"role\": \"user\", \"content\": \"Hey there!\"}\n",
187
        "]"
188
      ]
189
    },
190
    {
191
      "cell_type": "code",
192
      "execution_count": 12,
193
      "metadata": {
194
        "colab": {
195
          "base_uri": "https://localhost:8080/"
196
        },
197
        "id": "ZgUXe3dg8nlo",
198
        "outputId": "c2607df8-3e9a-405b-a79c-1cfc56a5f7ea"
199
      },
200
      "outputs": [
201
        {
202
          "data": {
203
            "text/plain": [
204
              "{'role': 'assistant', 'content': 'Hey James!'}"
205
            ]
206
          },
207
          "execution_count": 12,
208
          "metadata": {},
209
          "output_type": "execute_result"
210
        }
211
      ],
212
      "source": [
213
        "await rails.generate_async(messages=messages)"
214
      ]
215
    },
216
    {
217
      "attachments": {},
218
      "cell_type": "markdown",
219
      "metadata": {
220
        "id": "2Ocz_xZw5HGG"
221
      },
222
      "source": [
223
        "Above we're using the `context` to set context variables within our Rails. Sometimes we may want to set them from within a conversation. Let's see how we could grab the users name and pass it into a `$name` variable."
224
      ]
225
    },
226
    {
227
      "cell_type": "code",
228
      "execution_count": 8,
229
      "metadata": {
230
        "id": "mEqWmQLB5ba9"
231
      },
232
      "outputs": [],
233
      "source": [
234
        "colang_content = \"\"\"\n",
235
        "define user give name\n",
236
        "    \"My name is James\"\n",
237
        "    \"I'm Julio\"\n",
238
        "    \"Sono Andrea\"\n",
239
        "\n",
240
        "define user greeting\n",
241
        "    \"Hey there!\"\n",
242
        "    \"How are you?\"\n",
243
        "    \"What's up?\"\n",
244
        "\n",
245
        "define bot name greeting\n",
246
        "    \"Hey $name!\"\n",
247
        "\n",
248
        "define flow give name\n",
249
        "    user give name\n",
250
        "    $name = ...\n",
251
        "    bot name greeting\n",
252
        "\n",
253
        "define flow\n",
254
        "    user greeting\n",
255
        "    if not $name\n",
256
        "        bot ask name\n",
257
        "    else\n",
258
        "        bot name greeting\n",
259
        "\"\"\""
260
      ]
261
    },
262
    {
263
      "attachments": {},
264
      "cell_type": "markdown",
265
      "metadata": {
266
        "id": "LxRmdI6_AjTu"
267
      },
268
      "source": [
269
        "Remove `$name` parameter from the `context`:"
270
      ]
271
    },
272
    {
273
      "cell_type": "code",
274
      "execution_count": 13,
275
      "metadata": {
276
        "id": "_oqkWp9lAmXw"
277
      },
278
      "outputs": [],
279
      "source": [
280
        "messages = [\n",
281
        "    {\"role\": \"context\", \"content\": \"\"},\n",
282
        "    {\"role\": \"assistant\",\n",
283
        "     \"content\": \"Hi! How are you? Is there anything I can help with?\"},\n",
284
        "    {\"role\": \"user\", \"content\": \"Hey there!\"}\n",
285
        "]"
286
      ]
287
    },
288
    {
289
      "cell_type": "code",
290
      "execution_count": 14,
291
      "metadata": {
292
        "id": "SE_RaWj18TqJ"
293
      },
294
      "outputs": [],
295
      "source": [
296
        "# initialize rails config\n",
297
        "config = RailsConfig.from_content(\n",
298
        "    colang_content=colang_content,\n",
299
        "    yaml_content=yaml_content\n",
300
        ")\n",
301
        "# create rails\n",
302
        "rails = LLMRails(config)"
303
      ]
304
    },
305
    {
306
      "cell_type": "code",
307
      "execution_count": 15,
308
      "metadata": {
309
        "colab": {
310
          "base_uri": "https://localhost:8080/"
311
        },
312
        "id": "hNZvCksv8WAX",
313
        "outputId": "ff0f20cb-cef2-4205-b76c-b950008ce7e5"
314
      },
315
      "outputs": [
316
        {
317
          "data": {
318
            "text/plain": [
319
              "{'role': 'assistant', 'content': \"Hi there! What's your name?\"}"
320
            ]
321
          },
322
          "execution_count": 15,
323
          "metadata": {},
324
          "output_type": "execute_result"
325
        }
326
      ],
327
      "source": [
328
        "res = await rails.generate_async(messages=messages)\n",
329
        "res"
330
      ]
331
    },
332
    {
333
      "cell_type": "code",
334
      "execution_count": 16,
335
      "metadata": {
336
        "colab": {
337
          "base_uri": "https://localhost:8080/"
338
        },
339
        "id": "mYXnob8w8bNs",
340
        "outputId": "4b65c3bc-a85b-46cd-a989-ee3f8c62062d"
341
      },
342
      "outputs": [
343
        {
344
          "data": {
345
            "text/plain": [
346
              "{'role': 'assistant', 'content': 'Hey James!'}"
347
            ]
348
          },
349
          "execution_count": 16,
350
          "metadata": {},
351
          "output_type": "execute_result"
352
        }
353
      ],
354
      "source": [
355
        "messages += [\n",
356
        "    res,\n",
357
        "    {\"role\": \"user\", \"content\": \"I'm James\"}\n",
358
        "]\n",
359
        "res = await rails.generate_async(messages=messages)\n",
360
        "res"
361
      ]
362
    },
363
    {
364
      "attachments": {},
365
      "cell_type": "markdown",
366
      "metadata": {
367
        "id": "rYh_FC-HmK8z"
368
      },
369
      "source": [
370
        "---"
371
      ]
372
    }
373
  ],
374
  "metadata": {
375
    "colab": {
376
      "provenance": []
377
    },
378
    "kernelspec": {
379
      "display_name": "Python 3",
380
      "name": "python3"
381
    },
382
    "language_info": {
383
      "name": "python"
384
    }
385
  },
386
  "nbformat": 4,
387
  "nbformat_minor": 0
388
}
389

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

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

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

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