|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# ConversationBufferWindowMemory\n", |
| 8 | + "\n", |
| 9 | + "`ConversationBufferWindowMemory` maintains a list of interactions over time.\n", |
| 10 | + "\n", |
| 11 | + "At this time, `ConversationBufferWindowMemory` uses only the **most recent K** interactions, not all conversation content.\n", |
| 12 | + "\n", |
| 13 | + "This can be useful for maintaining a sliding window of the most recent interactions so that the buffer does not become too large.\n", |
| 14 | + "\n" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 1, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "from langchain.memory import ConversationBufferWindowMemory\n", |
| 24 | + "\n", |
| 25 | + "memory = ConversationBufferWindowMemory(k=2, return_messages=True)\n", |
| 26 | + "\n", |
| 27 | + "memory.save_context(\n", |
| 28 | + " inputs={\n", |
| 29 | + " \"human\": \"Hello, I would like to open a bank account online. How should I start?\"\n", |
| 30 | + " },\n", |
| 31 | + " outputs={\n", |
| 32 | + " \"ai\": \"Hello! I'm glad you want to open an account. First, could you please prepare your ID for verification?\"\n", |
| 33 | + " },\n", |
| 34 | + ")\n", |
| 35 | + "memory.save_context(\n", |
| 36 | + " inputs={\"human\": \"Yes, I have prepared my ID. What should I do next?\"},\n", |
| 37 | + " outputs={\n", |
| 38 | + " \"ai\": \"Thank you. Please take a clear photo of the front and back of your ID and upload it. We will proceed with the verification process afterward.\"\n", |
| 39 | + " },\n", |
| 40 | + ")\n", |
| 41 | + "memory.save_context(\n", |
| 42 | + " inputs={\"human\": \"I have uploaded the photo. How is the verification done?\"},\n", |
| 43 | + " outputs={\n", |
| 44 | + " \"ai\": \"We have checked the photo you uploaded. Now, please proceed with the verification through your phone. Enter the verification code sent via text message.\"\n", |
| 45 | + " },\n", |
| 46 | + ")\n", |
| 47 | + "memory.save_context(\n", |
| 48 | + " inputs={\"human\": \"I have entered the verification code. How do I open the account now?\"},\n", |
| 49 | + " outputs={\n", |
| 50 | + " \"ai\": \"Your verification is complete. Now, please select the type of account you want and enter the necessary information. You can choose the type of deposit, currency, etc.\"\n", |
| 51 | + " },\n", |
| 52 | + ")\n", |
| 53 | + "memory.save_context(\n", |
| 54 | + " inputs={\"human\": \"I have entered all the information. What is the next step?\"},\n", |
| 55 | + " outputs={\n", |
| 56 | + " \"ai\": \"We have checked the information you entered. The account opening process is almost complete. Finally, please agree to the terms and conditions and confirm the account opening.\"\n", |
| 57 | + " },\n", |
| 58 | + ")\n", |
| 59 | + "memory.save_context(\n", |
| 60 | + " inputs={\"human\": \"I have completed all the procedures. Is the account opened?\"},\n", |
| 61 | + " outputs={\n", |
| 62 | + " \"ai\": \"Yes, the account has been opened. Your account number and related information have been sent to your registered email. If you need further assistance, please feel free to contact us. Thank you!\"\n", |
| 63 | + " },\n", |
| 64 | + ")" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "markdown", |
| 69 | + "metadata": {}, |
| 70 | + "source": [ |
| 71 | + "You can check the conversation history to see that it returns only the **most recent 2** messages.\n" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "code", |
| 76 | + "execution_count": 4, |
| 77 | + "metadata": {}, |
| 78 | + "outputs": [ |
| 79 | + { |
| 80 | + "data": { |
| 81 | + "text/plain": [ |
| 82 | + "[HumanMessage(content='I have entered all the information. What is the next step?', additional_kwargs={}, response_metadata={}),\n", |
| 83 | + " AIMessage(content='We have checked the information you entered. The account opening process is almost complete. Finally, please agree to the terms and conditions and confirm the account opening.', additional_kwargs={}, response_metadata={}),\n", |
| 84 | + " HumanMessage(content='I have completed all the procedures. Is the account opened?', additional_kwargs={}, response_metadata={}),\n", |
| 85 | + " AIMessage(content='Yes, the account has been opened. Your account number and related information have been sent to your registered email. If you need further assistance, please feel free to contact us. Thank you!', additional_kwargs={}, response_metadata={})]" |
| 86 | + ] |
| 87 | + }, |
| 88 | + "execution_count": 4, |
| 89 | + "metadata": {}, |
| 90 | + "output_type": "execute_result" |
| 91 | + } |
| 92 | + ], |
| 93 | + "source": [ |
| 94 | + "# Check the conversation history.\n", |
| 95 | + "memory.load_memory_variables({})[\"history\"]" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": null, |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [], |
| 103 | + "source": [] |
| 104 | + } |
| 105 | + ], |
| 106 | + "metadata": { |
| 107 | + "kernelspec": { |
| 108 | + "display_name": "py-test", |
| 109 | + "language": "python", |
| 110 | + "name": "python3" |
| 111 | + }, |
| 112 | + "language_info": { |
| 113 | + "codemirror_mode": { |
| 114 | + "name": "ipython", |
| 115 | + "version": 3 |
| 116 | + }, |
| 117 | + "file_extension": ".py", |
| 118 | + "mimetype": "text/x-python", |
| 119 | + "name": "python", |
| 120 | + "nbconvert_exporter": "python", |
| 121 | + "pygments_lexer": "ipython3", |
| 122 | + "version": "3.11.10" |
| 123 | + } |
| 124 | + }, |
| 125 | + "nbformat": 4, |
| 126 | + "nbformat_minor": 2 |
| 127 | +} |
0 commit comments