Files
langchain/docs/getting_started/memory.ipynb
T
90e8ccc898 Harrison/update links (#450)
Co-authored-by: Sam Ching <samuelcwl@gmail.com>
Co-authored-by: Ikko Ashimine <eltociear@gmail.com>
2022-12-28 09:02:07 -05:00

6.3 KiB

Memory

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.

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.

ConversationChain with default memory

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).

In [1]:
from langchain import OpenAI, ConversationChain

llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm, verbose=True)

conversation.predict(input="Hi there!")
Out [1]:

> Entering new chain...
Prompt after formatting:
The 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.

Current conversation:

Human: Hi there!
AI:

> Finished chain.
' Hello! How are you today?'
In [2]:
conversation.predict(input="I'm doing well! Just having a conversation with an AI.")
Out [2]:

> Entering new chain...
Prompt after formatting:
The 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.

Current conversation:

Human: Hi there!
AI:  Hello! How are you today?
Human: I'm doing well! Just having a conversation with an AI.
AI:

> Finished chain.
" That's great! What would you like to talk about?"
In [3]:
conversation.predict(input="Tell me about yourself.")
Out [3]:

> Entering new chain...
Prompt after formatting:
The 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.

Current conversation:

Human: Hi there!
AI:  Hello! How are you today?
Human: I'm doing well! Just having a conversation with an AI.
AI:  That's great! What would you like to talk about?
Human: Tell me about yourself.
AI:

> Finished chain.
'  I am an AI created to provide information and support to humans. I enjoy learning and exploring new things.'

More Resources on Memory

This just scratches the surface of what you can do with memory.

For more concrete examples of conversational memory, please see this notebook

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 section.

For even more advanced ideas on memory (which will hopefully be included in LangChain soon!) see the MemPrompt paper.

In [ ]: