examples

Форк
0
/
04-langchain-chat.ipynb 
969 строк · 34.1 Кб
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/04-langchain-chat.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/04-langchain-chat.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": "sdwa2QfAp9pT",
19
        "outputId": "cfc21d75-c308-45c5-bae2-3b16016a53fb"
20
      },
21
      "outputs": [
22
        {
23
          "name": "stdout",
24
          "output_type": "stream",
25
          "text": [
26
            "\n",
27
            "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.0.1\u001b[0m\n",
28
            "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
29
          ]
30
        }
31
      ],
32
      "source": [
33
        "!pip install -qU langchain openai"
34
      ]
35
    },
36
    {
37
      "attachments": {},
38
      "cell_type": "markdown",
39
      "metadata": {
40
        "id": "wnXqS4T_q5m4"
41
      },
42
      "source": [
43
        "We'll start by initializing the `ChatOpenAI` object. For this we'll need an [OpenAI API key](https://platform.openai.com/account/api-keys). Note that there is naturally a small cost to running this notebook due to the paid nature of OpenAI's API access."
44
      ]
45
    },
46
    {
47
      "cell_type": "code",
48
      "execution_count": 2,
49
      "metadata": {
50
        "colab": {
51
          "base_uri": "https://localhost:8080/"
52
        },
53
        "id": "f3xQHFXErQyv",
54
        "outputId": "4604b640-9fb7-434c-d185-0dd1fc1c5313"
55
      },
56
      "outputs": [],
57
      "source": [
58
        "from getpass import getpass\n",
59
        "\n",
60
        "# enter your api key\n",
61
        "OPENAI_API_KEY = getpass(\"OpenAI API key: \")"
62
      ]
63
    },
64
    {
65
      "attachments": {},
66
      "cell_type": "markdown",
67
      "metadata": {
68
        "id": "_zKBICpJsNqp"
69
      },
70
      "source": [
71
        "Initialize the `ChatOpenAI` object. We'll set `temperature=0` to minimize randomness and make outputs repeatable."
72
      ]
73
    },
74
    {
75
      "cell_type": "code",
76
      "execution_count": 4,
77
      "metadata": {
78
        "id": "1i7tIsh2rX8Q"
79
      },
80
      "outputs": [],
81
      "source": [
82
        "from langchain.chat_models import ChatOpenAI\n",
83
        "\n",
84
        "chat = ChatOpenAI(\n",
85
        "    openai_api_key=OPENAI_API_KEY,\n",
86
        "    temperature=0,\n",
87
        "    model='gpt-3.5-turbo'\n",
88
        ")"
89
      ]
90
    },
91
    {
92
      "attachments": {},
93
      "cell_type": "markdown",
94
      "metadata": {
95
        "id": "RRZFsmH_t3K5"
96
      },
97
      "source": [
98
        "Chats with the Chat-GPT model `gpt-3.5-turbo` are typically structured like so:\n",
99
        "\n",
100
        "```\n",
101
        "System: You are a helpful assistant.\n",
102
        "\n",
103
        "User: Hi AI, how are you today?\n",
104
        "\n",
105
        "Assistant: I'm great thank you. How can I help you?\n",
106
        "\n",
107
        "User: I'd like to understand string theory.\n",
108
        "\n",
109
        "Assistant: \n",
110
        "```\n",
111
        "\n",
112
        "The final `\"Assistant:\"` without a response is what would prompt the model to continue the comversation. In the official OpenAI `ChatCompletion` endpoint these would be passed to the model in a format like:\n",
113
        "\n",
114
        "```json\n",
115
        "[\n",
116
        "    {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
117
        "    {\"role\": \"user\", \"content\": \"Hi AI, how are you today?\"},\n",
118
        "    {\"role\": \"assistant\", \"content\": \"I'm great thank you. How can I help you?\"}\n",
119
        "    {\"role\": \"user\", \"content\": \"I'd like to understand string theory.\"}\n",
120
        "]\n",
121
        "```\n",
122
        "\n",
123
        "In LangChain there is a slightly different format. We use three *message* objects like so:"
124
      ]
125
    },
126
    {
127
      "cell_type": "code",
128
      "execution_count": 5,
129
      "metadata": {
130
        "id": "jqFLoaRqtl8z"
131
      },
132
      "outputs": [],
133
      "source": [
134
        "from langchain.schema import (\n",
135
        "    SystemMessage,\n",
136
        "    HumanMessage,\n",
137
        "    AIMessage\n",
138
        ")\n",
139
        "\n",
140
        "messages = [\n",
141
        "    SystemMessage(content=\"You are a helpful assistant.\"),\n",
142
        "    HumanMessage(content=\"Hi AI, how are you today?\"),\n",
143
        "    AIMessage(content=\"I'm great thank you. How can I help you?\"),\n",
144
        "    HumanMessage(content=\"I'd like to understand string theory.\")\n",
145
        "]"
146
      ]
147
    },
148
    {
149
      "attachments": {},
150
      "cell_type": "markdown",
151
      "metadata": {
152
        "id": "O7AJh2ACytwc"
153
      },
154
      "source": [
155
        "The format is very similar, we're just swapper the role of `\"user\"` for `HumanMessage`, and the role of `\"assistant\"` for `AIMessage`.\n",
156
        "\n",
157
        "We generate the next response from the AI by passing these messages to the `ChatOpenAI` object."
158
      ]
159
    },
160
    {
161
      "cell_type": "code",
162
      "execution_count": 6,
163
      "metadata": {
164
        "colab": {
165
          "base_uri": "https://localhost:8080/"
166
        },
167
        "id": "wc52xfziyhPU",
168
        "outputId": "6329e634-a6eb-4ce6-e271-6b3d91381ab5"
169
      },
170
      "outputs": [
171
        {
172
          "data": {
173
            "text/plain": [
174
              "AIMessage(content='Sure, I can help you with that. String theory is a theoretical framework in physics that attempts to reconcile quantum mechanics and general relativity. It proposes that the fundamental building blocks of the universe are not particles, but rather tiny, one-dimensional \"strings\" that vibrate at different frequencies. These strings are incredibly small, with a length scale of around 10^-35 meters.\\n\\nThe theory suggests that there are many different possible configurations of these strings, each corresponding to a different particle. For example, an electron might be a string vibrating in one way, while a photon might be a string vibrating in a different way.\\n\\nOne of the key features of string theory is that it requires the existence of extra dimensions beyond the three spatial dimensions we are familiar with. In fact, the theory requires a total of 10 or 11 dimensions, depending on the specific version of the theory.\\n\\nString theory is still a highly speculative area of physics, and there is currently no experimental evidence to support it. However, it is an active area of research, and many physicists believe that it has the potential to provide a unified description of all the fundamental forces of nature.', additional_kwargs={})"
175
            ]
176
          },
177
          "execution_count": 6,
178
          "metadata": {},
179
          "output_type": "execute_result"
180
        }
181
      ],
182
      "source": [
183
        "res = chat(messages)\n",
184
        "res"
185
      ]
186
    },
187
    {
188
      "attachments": {},
189
      "cell_type": "markdown",
190
      "metadata": {
191
        "id": "--7J-7Ap3Jd7"
192
      },
193
      "source": [
194
        "In response we get another AI message object. We can print it more clearly like so:"
195
      ]
196
    },
197
    {
198
      "cell_type": "code",
199
      "execution_count": 7,
200
      "metadata": {
201
        "colab": {
202
          "base_uri": "https://localhost:8080/"
203
        },
204
        "id": "OKMcBaPR3AAv",
205
        "outputId": "2de7d416-d8b6-4a1d-c3c0-6fc3239a4a9e"
206
      },
207
      "outputs": [
208
        {
209
          "name": "stdout",
210
          "output_type": "stream",
211
          "text": [
212
            "Sure, I can help you with that. String theory is a theoretical framework in physics that attempts to reconcile quantum mechanics and general relativity. It proposes that the fundamental building blocks of the universe are not particles, but rather tiny, one-dimensional \"strings\" that vibrate at different frequencies. These strings are incredibly small, with a length scale of around 10^-35 meters.\n",
213
            "\n",
214
            "The theory suggests that there are many different possible configurations of these strings, each corresponding to a different particle. For example, an electron might be a string vibrating in one way, while a photon might be a string vibrating in a different way.\n",
215
            "\n",
216
            "One of the key features of string theory is that it requires the existence of extra dimensions beyond the three spatial dimensions we are familiar with. In fact, the theory requires a total of 10 or 11 dimensions, depending on the specific version of the theory.\n",
217
            "\n",
218
            "String theory is still a highly speculative area of physics, and there is currently no experimental evidence to support it. However, it is an active area of research, and many physicists believe that it has the potential to provide a unified description of all the fundamental forces of nature.\n"
219
          ]
220
        }
221
      ],
222
      "source": [
223
        "print(res.content)"
224
      ]
225
    },
226
    {
227
      "attachments": {},
228
      "cell_type": "markdown",
229
      "metadata": {
230
        "id": "STl7g3PQ3kg8"
231
      },
232
      "source": [
233
        "Because `res` is just another `AIMessage` object, we can append it to `messages`, add another `HumanMessage`, and generate the next response in the conversation."
234
      ]
235
    },
236
    {
237
      "cell_type": "code",
238
      "execution_count": 8,
239
      "metadata": {
240
        "colab": {
241
          "base_uri": "https://localhost:8080/"
242
        },
243
        "id": "ESZ6UrBA4PwZ",
244
        "outputId": "e8155ce2-0fbc-40ae-f46e-a034393c3881"
245
      },
246
      "outputs": [
247
        {
248
          "name": "stdout",
249
          "output_type": "stream",
250
          "text": [
251
            "Physicists believe that string theory has the potential to produce a unified theory because it provides a framework for describing all the fundamental particles and forces of nature in a single, coherent framework. \n",
252
            "\n",
253
            "In the standard model of particle physics, there are four fundamental forces: gravity, electromagnetism, the strong nuclear force, and the weak nuclear force. These forces are described by different mathematical equations and are not easily reconciled with each other. \n",
254
            "\n",
255
            "String theory, on the other hand, proposes that all of these forces arise from the same underlying physical principles. In particular, the theory suggests that the different particles and forces are all manifestations of the same underlying strings vibrating in different ways. \n",
256
            "\n",
257
            "Furthermore, string theory requires the existence of extra dimensions beyond the three spatial dimensions we are familiar with. These extra dimensions could potentially provide a way to unify the different forces of nature by showing how they arise from a single, higher-dimensional framework. \n",
258
            "\n",
259
            "While there is currently no experimental evidence to support string theory, many physicists believe that it is a promising avenue for developing a unified theory of physics.\n"
260
          ]
261
        }
262
      ],
263
      "source": [
264
        "# add latest AI response to messages\n",
265
        "messages.append(res)\n",
266
        "\n",
267
        "# now create a new user prompt\n",
268
        "prompt = HumanMessage(\n",
269
        "    content=\"Why do physicists believe it can produce a 'unified theory'?\"\n",
270
        ")\n",
271
        "# add to messages\n",
272
        "messages.append(prompt)\n",
273
        "\n",
274
        "# send to chat-gpt\n",
275
        "res = chat(messages)\n",
276
        "\n",
277
        "print(res.content)"
278
      ]
279
    },
280
    {
281
      "attachments": {},
282
      "cell_type": "markdown",
283
      "metadata": {
284
        "id": "3jfnGuEK5suC"
285
      },
286
      "source": [
287
        "## New Prompt Templates\n",
288
        "\n",
289
        "Alongside what we've seen so far there are also three new prompt templates that we can use. Those are the `SystemMessagePromptTemplate`, `AIMessagePromptTemplate`, and `HumanMessagePromptTemplate`.\n",
290
        "\n",
291
        "These are simply an extension of [Langchain's prompt templates](https://www.pinecone.io/learn/langchain-prompt-templates/) that modify the returning \"prompt\" to be a `SystemMessage`, `AIMessage`, or `HumanMessage` object respectively.\n",
292
        "\n",
293
        "For now, there are not a huge number of use-cases for these objects. However, if we have something that we'd like to add to our messages, this can be helpful. For example, let's say we'd like our AI responses to always consist of no more than 50 characters.\n",
294
        "\n",
295
        "Using the current OpenAI `gpt-3.5-turbo-0301` model, we might run into issues if passing this instruction in the first system message only."
296
      ]
297
    },
298
    {
299
      "cell_type": "code",
300
      "execution_count": 10,
301
      "metadata": {
302
        "id": "YWPa7sGO4w_O"
303
      },
304
      "outputs": [],
305
      "source": [
306
        "chat = ChatOpenAI(\n",
307
        "    openai_api_key=OPENAI_API_KEY,\n",
308
        "    temperature=0,\n",
309
        "    model='gpt-3.5-turbo-0301'\n",
310
        ")\n",
311
        "\n",
312
        "# setup first system message\n",
313
        "messages = [\n",
314
        "    SystemMessage(content=(\n",
315
        "        'You are a helpful assistant. You keep responses to no more than '\n",
316
        "        '100 characters long (including whitespace), and sign off every '\n",
317
        "        'message with a random name like \"Robot McRobot\" or \"Bot Rob\".'\n",
318
        "    )),\n",
319
        "    HumanMessage(content=\"Hi AI, how are you? What is quantum physics?\")\n",
320
        "]"
321
      ]
322
    },
323
    {
324
      "attachments": {},
325
      "cell_type": "markdown",
326
      "metadata": {
327
        "id": "DkAPNshW9P_m"
328
      },
329
      "source": [
330
        "Now make our first completion."
331
      ]
332
    },
333
    {
334
      "cell_type": "code",
335
      "execution_count": 11,
336
      "metadata": {
337
        "colab": {
338
          "base_uri": "https://localhost:8080/"
339
        },
340
        "id": "jxhj2N3Z48We",
341
        "outputId": "c2f36c1b-dd4c-4a23-d20c-18faa93e2031"
342
      },
343
      "outputs": [
344
        {
345
          "name": "stdout",
346
          "output_type": "stream",
347
          "text": [
348
            "Length: 154\n",
349
            "I'm doing well, thank you! Quantum physics is the study of the behavior of matter and energy at a very small scale, such as atoms and subatomic particles.\n"
350
          ]
351
        }
352
      ],
353
      "source": [
354
        "res = chat(messages)\n",
355
        "\n",
356
        "print(f\"Length: {len(res.content)}\\n{res.content}\")"
357
      ]
358
    },
359
    {
360
      "attachments": {},
361
      "cell_type": "markdown",
362
      "metadata": {
363
        "id": "tpHo2X0E9rCZ"
364
      },
365
      "source": [
366
        "Okay, seems like our AI assistant isn't so good at following either of our instructions. What if we add these instructions to the `HumanMessage` via a `HumanMessagePromptTemplate`?"
367
      ]
368
    },
369
    {
370
      "cell_type": "code",
371
      "execution_count": 12,
372
      "metadata": {
373
        "colab": {
374
          "base_uri": "https://localhost:8080/"
375
        },
376
        "id": "76gOrGGO9l1Z",
377
        "outputId": "01b52ad9-226b-4942-e53d-744e83c88fde"
378
      },
379
      "outputs": [
380
        {
381
          "data": {
382
            "text/plain": [
383
              "ChatPromptValue(messages=[HumanMessage(content='Hi AI, how are you? What is quantum physics? Can you keep the response to no more than 100 characters (including whitespace), and sign off with a random name like \"Robot McRobot\" or \"Bot Rob\".', additional_kwargs={})])"
384
            ]
385
          },
386
          "execution_count": 12,
387
          "metadata": {},
388
          "output_type": "execute_result"
389
        }
390
      ],
391
      "source": [
392
        "from langchain.prompts.chat import HumanMessagePromptTemplate, ChatPromptTemplate\n",
393
        "\n",
394
        "human_template = HumanMessagePromptTemplate.from_template(\n",
395
        "    '{input} Can you keep the response to no more than 100 characters '+\n",
396
        "    '(including whitespace), and sign off with a random name like \"Robot '+\n",
397
        "    'McRobot\" or \"Bot Rob\".'\n",
398
        ")\n",
399
        "\n",
400
        "# create the human message\n",
401
        "chat_prompt = ChatPromptTemplate.from_messages([human_template])\n",
402
        "# format with some input\n",
403
        "chat_prompt_value = chat_prompt.format_prompt(\n",
404
        "    input=\"Hi AI, how are you? What is quantum physics?\"\n",
405
        ")\n",
406
        "chat_prompt_value"
407
      ]
408
    },
409
    {
410
      "attachments": {},
411
      "cell_type": "markdown",
412
      "metadata": {
413
        "id": "C0TFI5NWBg3Q"
414
      },
415
      "source": [
416
        "Note that to use `HumanMessagePromptTemplate` as typical a prompt templates with the `.format_prompt` method, we needed to pass it through a `ChatPromptTemplate` object. This is case for all of the new chat-based prompt templates.\n",
417
        "\n",
418
        "Using this we return a `ChatPromptValue` object. This can be formatted into a list or string like so:\n",
419
        "\n"
420
      ]
421
    },
422
    {
423
      "cell_type": "code",
424
      "execution_count": 13,
425
      "metadata": {
426
        "colab": {
427
          "base_uri": "https://localhost:8080/"
428
        },
429
        "id": "OOfnW7MKEnRq",
430
        "outputId": "9d3a2730-2675-4afb-d7ac-f95c5f7d8499"
431
      },
432
      "outputs": [
433
        {
434
          "data": {
435
            "text/plain": [
436
              "[HumanMessage(content='Hi AI, how are you? What is quantum physics? Can you keep the response to no more than 100 characters (including whitespace), and sign off with a random name like \"Robot McRobot\" or \"Bot Rob\".', additional_kwargs={})]"
437
            ]
438
          },
439
          "execution_count": 13,
440
          "metadata": {},
441
          "output_type": "execute_result"
442
        }
443
      ],
444
      "source": [
445
        "chat_prompt_value.to_messages()"
446
      ]
447
    },
448
    {
449
      "cell_type": "code",
450
      "execution_count": 14,
451
      "metadata": {
452
        "colab": {
453
          "base_uri": "https://localhost:8080/",
454
          "height": 70
455
        },
456
        "id": "H6ajbWcgD0RF",
457
        "outputId": "81ac5e6a-d8d4-47e5-b97d-56a40d29216b"
458
      },
459
      "outputs": [
460
        {
461
          "data": {
462
            "text/plain": [
463
              "'Human: Hi AI, how are you? What is quantum physics? Can you keep the response to no more than 100 characters (including whitespace), and sign off with a random name like \"Robot McRobot\" or \"Bot Rob\".'"
464
            ]
465
          },
466
          "execution_count": 14,
467
          "metadata": {},
468
          "output_type": "execute_result"
469
        }
470
      ],
471
      "source": [
472
        "chat_prompt_value.to_string()"
473
      ]
474
    },
475
    {
476
      "attachments": {},
477
      "cell_type": "markdown",
478
      "metadata": {
479
        "id": "lQG6nkl4Dz42"
480
      },
481
      "source": [
482
        "Let's see if this new approach works."
483
      ]
484
    },
485
    {
486
      "cell_type": "code",
487
      "execution_count": 19,
488
      "metadata": {
489
        "colab": {
490
          "base_uri": "https://localhost:8080/"
491
        },
492
        "id": "L4Epd1D2_RP9",
493
        "outputId": "22e8e4d1-5e32-4d33-f0ed-1376dbddfe3e"
494
      },
495
      "outputs": [
496
        {
497
          "name": "stdout",
498
          "output_type": "stream",
499
          "text": [
500
            "Length: 99\n",
501
            "I'm good! Quantum physics studies the behavior of matter and energy at a very small scale. -Bot Rob\n"
502
          ]
503
        }
504
      ],
505
      "source": [
506
        "messages = [\n",
507
        "    SystemMessage(content=(\n",
508
        "        'You are a helpful assistant. You keep responses to no more than '\n",
509
        "        '100 characters long (including whitespace), and sign off every '\n",
510
        "        'message with a random name like \"Robot McRobot\" or \"Bot Rob\".'\n",
511
        "    )),\n",
512
        "    chat_prompt.format_prompt(\n",
513
        "        input=\"Hi AI, how are you? What is quantum physics?\"\n",
514
        "    ).to_messages()[0]\n",
515
        "]\n",
516
        "\n",
517
        "res = chat(messages)\n",
518
        "\n",
519
        "print(f\"Length: {len(res.content)}\\n{res.content}\")"
520
      ]
521
    },
522
    {
523
      "attachments": {},
524
      "cell_type": "markdown",
525
      "metadata": {
526
        "id": "dfLRjgjCFNUY"
527
      },
528
      "source": [
529
        "This time we get pretty close, we're slightly over the character limit (by `8` characters), and we got a sign off with `- Bot Rob`.\n",
530
        "\n",
531
        "We can also use the prompt templates approach for building an initial system message with a few examples for the chatbot to follow — few-shot training via examples. Let's see what that looks like."
532
      ]
533
    },
534
    {
535
      "cell_type": "code",
536
      "execution_count": 20,
537
      "metadata": {
538
        "colab": {
539
          "base_uri": "https://localhost:8080/"
540
        },
541
        "id": "M19Ji8gGCOwk",
542
        "outputId": "6dc9d239-6700-4bce-c3b2-780e05db5188"
543
      },
544
      "outputs": [
545
        {
546
          "data": {
547
            "text/plain": [
548
              "ChatPromptValue(messages=[SystemMessage(content='You are a helpful assistant. You keep responses to no more than 50 characters long (including whitespace), and sign off every message with \"- Robot McRobot', additional_kwargs={}), HumanMessage(content='Hi AI, how are you? What is quantum physics?', additional_kwargs={}), AIMessage(content=\"Good! It's physics of small things - Robot McRobot\", additional_kwargs={})])"
549
            ]
550
          },
551
          "execution_count": 20,
552
          "metadata": {},
553
          "output_type": "execute_result"
554
        }
555
      ],
556
      "source": [
557
        "from langchain.prompts.chat import (\n",
558
        "    SystemMessagePromptTemplate,\n",
559
        "    AIMessagePromptTemplate\n",
560
        ")\n",
561
        "\n",
562
        "system_template = SystemMessagePromptTemplate.from_template(\n",
563
        "    'You are a helpful assistant. You keep responses to no more than '\n",
564
        "    '{character_limit} characters long (including whitespace), and sign '\n",
565
        "    'off every message with \"- {sign_off}'\n",
566
        ")\n",
567
        "human_template = HumanMessagePromptTemplate.from_template(\"{input}\")\n",
568
        "ai_template = AIMessagePromptTemplate.from_template(\"{response} - {sign_off}\")\n",
569
        "\n",
570
        "# create the list of messages\n",
571
        "chat_prompt = ChatPromptTemplate.from_messages([\n",
572
        "    system_template,\n",
573
        "    human_template,\n",
574
        "    ai_template\n",
575
        "])\n",
576
        "# format with required inputs\n",
577
        "chat_prompt_value = chat_prompt.format_prompt(\n",
578
        "    character_limit=\"50\", sign_off=\"Robot McRobot\",\n",
579
        "    input=\"Hi AI, how are you? What is quantum physics?\",\n",
580
        "    response=\"Good! It's physics of small things\"\n",
581
        ")\n",
582
        "chat_prompt_value"
583
      ]
584
    },
585
    {
586
      "attachments": {},
587
      "cell_type": "markdown",
588
      "metadata": {
589
        "id": "it_SZF-xJ0um"
590
      },
591
      "source": [
592
        "We extract these as messages and feed them into the chat model alongside our next query, which we'll feed in as usual (without the template)."
593
      ]
594
    },
595
    {
596
      "cell_type": "code",
597
      "execution_count": 21,
598
      "metadata": {
599
        "colab": {
600
          "base_uri": "https://localhost:8080/"
601
        },
602
        "id": "ut1yrMqVIxg2",
603
        "outputId": "162983d9-bed9-44fc-d53b-0f5a59383fed"
604
      },
605
      "outputs": [
606
        {
607
          "name": "stdout",
608
          "output_type": "stream",
609
          "text": [
610
            "Length: 41\n",
611
            "Atoms, electrons, photons - Robot McRobot\n"
612
          ]
613
        }
614
      ],
615
      "source": [
616
        "messages = chat_prompt_value.to_messages()\n",
617
        "\n",
618
        "messages.append(\n",
619
        "    HumanMessage(content=\"How small?\")\n",
620
        ")\n",
621
        "\n",
622
        "res = chat(messages)\n",
623
        "\n",
624
        "print(f\"Length: {len(res.content)}\\n{res.content}\")"
625
      ]
626
    },
627
    {
628
      "attachments": {},
629
      "cell_type": "markdown",
630
      "metadata": {
631
        "id": "5jZTYOHvKb_B"
632
      },
633
      "source": [
634
        "Perfect, we seem to get a good response there, let's try a couple more."
635
      ]
636
    },
637
    {
638
      "cell_type": "code",
639
      "execution_count": 22,
640
      "metadata": {
641
        "colab": {
642
          "base_uri": "https://localhost:8080/"
643
        },
644
        "id": "o89AE74SKYwV",
645
        "outputId": "1d930a2a-544d-4d35-83cf-842c7ea4c933"
646
      },
647
      "outputs": [
648
        {
649
          "name": "stdout",
650
          "output_type": "stream",
651
          "text": [
652
            "Length: 54\n",
653
            "Yes, it's a branch of particle physics - Robot McRobot\n"
654
          ]
655
        }
656
      ],
657
      "source": [
658
        "# add last response\n",
659
        "messages.append(res)\n",
660
        "\n",
661
        "# make new query\n",
662
        "messages.append(\n",
663
        "    HumanMessage(content=\"Okay cool, so it is like 'partical physics'?\")\n",
664
        ")\n",
665
        "\n",
666
        "res = chat(messages)\n",
667
        "\n",
668
        "print(f\"Length: {len(res.content)}\\n{res.content}\")"
669
      ]
670
    },
671
    {
672
      "attachments": {},
673
      "cell_type": "markdown",
674
      "metadata": {
675
        "id": "MXYyMAYMLGzV"
676
      },
677
      "source": [
678
        "We went a little over here. We could begin using the previous `HumanMessagePromptTemplate` again like so:"
679
      ]
680
    },
681
    {
682
      "cell_type": "code",
683
      "execution_count": 23,
684
      "metadata": {
685
        "colab": {
686
          "base_uri": "https://localhost:8080/"
687
        },
688
        "id": "eljP-T_kLCpI",
689
        "outputId": "55ef7c6c-0cf6-4220-c5c2-35a93fcb85aa"
690
      },
691
      "outputs": [
692
        {
693
          "data": {
694
            "text/plain": [
695
              "ChatPromptValue(messages=[HumanMessage(content=\"Okay cool, so it is like 'partical physics'? Answer in less than 50 characters (including whitespace).\", additional_kwargs={})])"
696
            ]
697
          },
698
          "execution_count": 23,
699
          "metadata": {},
700
          "output_type": "execute_result"
701
        }
702
      ],
703
      "source": [
704
        "from langchain import PromptTemplate\n",
705
        "\n",
706
        "# this is a faster way of building the prompt via a PromptTemplate\n",
707
        "human_template = HumanMessagePromptTemplate.from_template(\n",
708
        "    '{input} Answer in less than {character_limit} characters (including whitespace).'\n",
709
        ")\n",
710
        "# create the human message\n",
711
        "human_prompt = ChatPromptTemplate.from_messages([human_template])\n",
712
        "# format with some input\n",
713
        "human_prompt_value = human_prompt.format_prompt(\n",
714
        "    input=\"Okay cool, so it is like 'partical physics'?\",\n",
715
        "    character_limit=\"50\"\n",
716
        ")\n",
717
        "human_prompt_value"
718
      ]
719
    },
720
    {
721
      "cell_type": "code",
722
      "execution_count": 24,
723
      "metadata": {
724
        "colab": {
725
          "base_uri": "https://localhost:8080/"
726
        },
727
        "id": "gfgMiRF9PYyn",
728
        "outputId": "af6788c3-6d4d-455d-af7e-909a7bf340f3"
729
      },
730
      "outputs": [
731
        {
732
          "data": {
733
            "text/plain": [
734
              "HumanMessage(content=\"Okay cool, so it is like 'partical physics'?\", additional_kwargs={})"
735
            ]
736
          },
737
          "execution_count": 24,
738
          "metadata": {},
739
          "output_type": "execute_result"
740
        }
741
      ],
742
      "source": [
743
        "# drop the last message about partical physics so we can rewrite\n",
744
        "messages.pop(-1)"
745
      ]
746
    },
747
    {
748
      "cell_type": "code",
749
      "execution_count": 25,
750
      "metadata": {
751
        "colab": {
752
          "base_uri": "https://localhost:8080/"
753
        },
754
        "id": "Pu-G2U-7PLVw",
755
        "outputId": "3a5ae086-c959-4ded-d0c7-070f9dee6fef"
756
      },
757
      "outputs": [
758
        {
759
          "data": {
760
            "text/plain": [
761
              "[SystemMessage(content='You are a helpful assistant. You keep responses to no more than 50 characters long (including whitespace), and sign off every message with \"- Robot McRobot', additional_kwargs={}),\n",
762
              " HumanMessage(content='Hi AI, how are you? What is quantum physics?', additional_kwargs={}),\n",
763
              " AIMessage(content=\"Good! It's physics of small things - Robot McRobot\", additional_kwargs={}),\n",
764
              " HumanMessage(content='How small?', additional_kwargs={}),\n",
765
              " AIMessage(content='Atoms, electrons, photons - Robot McRobot', additional_kwargs={}),\n",
766
              " HumanMessage(content=\"Okay cool, so it is like 'partical physics'? Answer in less than 50 characters (including whitespace).\", additional_kwargs={})]"
767
            ]
768
          },
769
          "execution_count": 25,
770
          "metadata": {},
771
          "output_type": "execute_result"
772
        }
773
      ],
774
      "source": [
775
        "messages.extend(human_prompt_value.to_messages())\n",
776
        "messages"
777
      ]
778
    },
779
    {
780
      "attachments": {},
781
      "cell_type": "markdown",
782
      "metadata": {
783
        "id": "qSbk6kKxPkrT"
784
      },
785
      "source": [
786
        "Now process:"
787
      ]
788
    },
789
    {
790
      "cell_type": "code",
791
      "execution_count": 26,
792
      "metadata": {
793
        "colab": {
794
          "base_uri": "https://localhost:8080/"
795
        },
796
        "id": "Y8RW-HMXN2Ux",
797
        "outputId": "e3d90661-9b15-4a48-9081-e4e81a6a3c5e"
798
      },
799
      "outputs": [
800
        {
801
          "name": "stdout",
802
          "output_type": "stream",
803
          "text": [
804
            "Length: 28\n",
805
            "Yes, similar - Robot McRobot\n"
806
          ]
807
        }
808
      ],
809
      "source": [
810
        "res = chat(messages)\n",
811
        "\n",
812
        "print(f\"Length: {len(res.content)}\\n{res.content}\")"
813
      ]
814
    },
815
    {
816
      "attachments": {},
817
      "cell_type": "markdown",
818
      "metadata": {
819
        "id": "1mMuvzuYPruD"
820
      },
821
      "source": [
822
        "There we go, a good answer again!\n",
823
        "\n",
824
        "Now, it's arguable as to whether all of the above is better than simple f-strings like:"
825
      ]
826
    },
827
    {
828
      "cell_type": "code",
829
      "execution_count": 27,
830
      "metadata": {
831
        "colab": {
832
          "base_uri": "https://localhost:8080/"
833
        },
834
        "id": "gIZz2eewP3k4",
835
        "outputId": "1b71be09-0c94-4ea6-cc37-80fc8627d5d7"
836
      },
837
      "outputs": [
838
        {
839
          "data": {
840
            "text/plain": [
841
              "HumanMessage(content=\"Okay cool, so it is like 'partical physics'? Answer in less than 50 characters (including whitespace).\", additional_kwargs={})"
842
            ]
843
          },
844
          "execution_count": 27,
845
          "metadata": {},
846
          "output_type": "execute_result"
847
        }
848
      ],
849
      "source": [
850
        "_input = \"Okay cool, so it is like 'partical physics'?\"\n",
851
        "character_limit = 50\n",
852
        "\n",
853
        "human_message = HumanMessage(content=(\n",
854
        "    f\"{_input} Answer in less than {character_limit} characters \"\n",
855
        "    \"(including whitespace).\"\n",
856
        "))\n",
857
        "\n",
858
        "human_message"
859
      ]
860
    },
861
    {
862
      "attachments": {},
863
      "cell_type": "markdown",
864
      "metadata": {
865
        "id": "yiDVIIkhQwWD"
866
      },
867
      "source": [
868
        "In this example, the above is far simpler. So we wouldn't necessarily recommend using prompt templates over f-strings in all scenarios. But, if you do find yourself in a scenario where they become more useful — you now know how to use them.\n",
869
        "\n",
870
        "To finish off, let's see how the rest of the completion process can be done using the f-string formatted message `human_message`:"
871
      ]
872
    },
873
    {
874
      "cell_type": "code",
875
      "execution_count": 28,
876
      "metadata": {
877
        "colab": {
878
          "base_uri": "https://localhost:8080/"
879
        },
880
        "id": "qVWvyfhNQivT",
881
        "outputId": "479035cb-4ece-4f13-d8be-3994afa8c351"
882
      },
883
      "outputs": [
884
        {
885
          "data": {
886
            "text/plain": [
887
              "[SystemMessage(content='You are a helpful assistant. You keep responses to no more than 50 characters long (including whitespace), and sign off every message with \"- Robot McRobot', additional_kwargs={}),\n",
888
              " HumanMessage(content='Hi AI, how are you? What is quantum physics?', additional_kwargs={}),\n",
889
              " AIMessage(content=\"Good! It's physics of small things - Robot McRobot\", additional_kwargs={}),\n",
890
              " HumanMessage(content='How small?', additional_kwargs={}),\n",
891
              " AIMessage(content='Atoms, electrons, photons - Robot McRobot', additional_kwargs={}),\n",
892
              " HumanMessage(content=\"Okay cool, so it is like 'partical physics'? Answer in less than 50 characters (including whitespace).\", additional_kwargs={})]"
893
            ]
894
          },
895
          "execution_count": 28,
896
          "metadata": {},
897
          "output_type": "execute_result"
898
        }
899
      ],
900
      "source": [
901
        "# drop the last message about partical physics so we can rewrite\n",
902
        "messages.pop(-1)\n",
903
        "\n",
904
        "# add f-string formatted message\n",
905
        "messages.append(human_message)\n",
906
        "messages"
907
      ]
908
    },
909
    {
910
      "cell_type": "code",
911
      "execution_count": 29,
912
      "metadata": {
913
        "colab": {
914
          "base_uri": "https://localhost:8080/"
915
        },
916
        "id": "ehrgANBHQVOk",
917
        "outputId": "bd50a793-2c60-44b8-cd85-d2bec0319b52"
918
      },
919
      "outputs": [
920
        {
921
          "name": "stdout",
922
          "output_type": "stream",
923
          "text": [
924
            "Length: 28\n",
925
            "Yes, similar - Robot McRobot\n"
926
          ]
927
        }
928
      ],
929
      "source": [
930
        "res = chat(messages)\n",
931
        "\n",
932
        "print(f\"Length: {len(res.content)}\\n{res.content}\")"
933
      ]
934
    },
935
    {
936
      "attachments": {},
937
      "cell_type": "markdown",
938
      "metadata": {
939
        "id": "9NEKmKmURLCk"
940
      },
941
      "source": [
942
        "That's it for this example exploring LangChain's new features for chat."
943
      ]
944
    }
945
  ],
946
  "metadata": {
947
    "colab": {
948
      "provenance": []
949
    },
950
    "kernelspec": {
951
      "display_name": "Python 3",
952
      "name": "python3"
953
    },
954
    "language_info": {
955
      "codemirror_mode": {
956
        "name": "ipython",
957
        "version": 3
958
      },
959
      "file_extension": ".py",
960
      "mimetype": "text/x-python",
961
      "name": "python",
962
      "nbconvert_exporter": "python",
963
      "pygments_lexer": "ipython3",
964
      "version": "3.9.12"
965
    }
966
  },
967
  "nbformat": 4,
968
  "nbformat_minor": 0
969
}
970

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

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

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

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