{ "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 clearest 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\". For more concrete ideas on the latter, see this [awesome paper](https://memprompt.com/).\n", "\n", "LangChain provides several specially created chains just for this purpose. This notebook walks through 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 that remembers all previous 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": "5c8735cc", "metadata": {}, "source": [ "### More Resources on Memory\n", "\n", "This just scratches the surface of what you can do with memory. \n", "\n", "For more concrete examples of conversational memory, please see [this notebook](../examples/memory/conversational_memory)\n", "\n", "For more examples on things like how to implement custom memory classes, how to add memory to a custom LLM chain and how to use memory with an agent, please see the [How-To: Memory](../examples/memoryrst) section. \n", "\n", "For even more advanced ideas on memory (which will hopefully be included in LangChain soon!) see the [MemPrompt](https://memprompt.com/) paper." ] }, { "cell_type": "code", "execution_count": null, "id": "436dda66", "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.10.8" } }, "nbformat": 4, "nbformat_minor": 5 }