{ "cells": [ { "cell_type": "markdown", "id": "d31df93e", "metadata": {}, "source": [ "# Memory\n", "So far, all the chains and agents we've gone through have been stateless. But often, you may want a chain or agent to have some concept of \"memory\" so that it may remember information about its previous interactions. The most clear and simple example of this is when designing a chatbot - you want it to remember previous messages so it can use context from that to have a better conversation. This would be a type of \"short-term memory\". On the more complex side, you could imagine a chain/agent remembering key pieces of information over time - this would be a form of \"long-term memory\".\n", "\n", "LangChain provides several specially created chains just for this purpose. This notebook walk throughs using one of those chains (the `ConversationChain`) with two different types of memory." ] }, { "cell_type": "markdown", "id": "d051c1da", "metadata": {}, "source": [ "### ConversationChain with default memory\n", "By default, the `ConversationChain` has a simple type of memory which remebers all previes inputs/outputs and adds them to the context that is passed. Let's take a look at using this chain (setting `verbose=True` so we can see the prompt)." ] }, { "cell_type": "code", "execution_count": 1, "id": "ae046bff", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n", "\n", "Current conversation:\n", "\n", "Human: Hi there!\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "' Hello! How are you today?'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain import OpenAI, ConversationChain\n", "\n", "llm = OpenAI(temperature=0)\n", "conversation = ConversationChain(llm=llm, verbose=True)\n", "\n", "conversation.predict(input=\"Hi there!\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "d8e2a6ff", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n", "\n", "Current conversation:\n", "\n", "Human: Hi there!\n", "AI: Hello! How are you today?\n", "Human: I'm doing well! Just having a conversation with an AI.\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\" That's great! What would you like to talk about?\"" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversation.predict(input=\"I'm doing well! Just having a conversation with an AI.\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "15eda316", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n", "\n", "Current conversation:\n", "\n", "Human: Hi there!\n", "AI: Hello! How are you today?\n", "Human: I'm doing well! Just having a conversation with an AI.\n", "AI: That's great! What would you like to talk about?\n", "Human: Tell me about yourself.\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "' I am an AI created to provide information and support to humans. I enjoy learning and exploring new things.'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversation.predict(input=\"Tell me about yourself.\")" ] }, { "cell_type": "markdown", "id": "4fad9448", "metadata": {}, "source": [ "### ConversationChain with ConversationSummaryMemory\n", "Now lets take a look at using a slightly more complex type of memory - `ConversationSummaryMemory`. This type of memory creates a summary of the conversation over time. This can be useful for condensing information from the conversation over time.\n", "\n", "Let's walk through an example, again setting `verbose=True` so we can see the prompt." ] }, { "cell_type": "code", "execution_count": 4, "id": "f60a2fe8", "metadata": {}, "outputs": [], "source": [ "from langchain.chains.conversation.memory import ConversationSummaryMemory" ] }, { "cell_type": "code", "execution_count": 5, "id": "b7274f2c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n", "\n", "Current conversation:\n", "\n", "Human: Hi, what's up?\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\"\\n\\nI'm doing well, thank you for asking. I'm currently working on a project that I'm really excited about.\"" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversation_with_summary = ConversationChain(llm=llm, memory=ConversationSummaryMemory(llm=OpenAI()), verbose=True)\n", "conversation_with_summary.predict(input=\"Hi, what's up?\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "a6b6b88f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n", "\n", "Current conversation:\n", "\n", "The human and artificial intelligence are talking. The human asked the AI what it is doing, and the AI said that it is working on a project that it is excited about.\n", "Human: Tell me more about it!\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\"\\n\\nI'm working on a project that I'm really excited about. It's a lot of work, but I think it's going to be really great when it's finished. I can't wait to show it to you!\"" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversation_with_summary.predict(input=\"Tell me more about it!\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "dad869fe", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n", "\n", "Current conversation:\n", "\n", "\n", "The human and artificial intelligence are talking. The human asked the AI what it is doing, and the AI said that it is working on a project that it is excited about. The AI said that the project is a lot of work, but it is going to be great when it is finished.\n", "Human: Very cool -- what is the scope of the project?\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "'\\n\\nThe project is quite large in scope. It involves a lot of data analysis and work with artificial intelligence algorithms.'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversation_with_summary.predict(input=\"Very cool -- what is the scope of the project?\")" ] }, { "cell_type": "code", "execution_count": null, "id": "0eb11bd0", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }