mirror of
https://github.com/hwchase17/langchain
synced 2024-11-08 07:10:35 +00:00
87e502c6bc
Co-authored-by: jacoblee93 <jacoblee93@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
257 lines
6.1 KiB
Plaintext
257 lines
6.1 KiB
Plaintext
We will walk through the simplest form of memory: "buffer" memory, which just involves keeping a buffer of all prior messages. We will show how to use the modular utility functions here, then show how it can be used in a chain (both returning a string as well as a list of messages).
|
|
|
|
## ChatMessageHistory
|
|
One of the core utility classes underpinning most (if not all) memory modules is the `ChatMessageHistory` class. This is a super lightweight wrapper which exposes convenience methods for saving Human messages, AI messages, and then fetching them all.
|
|
|
|
You may want to use this class directly if you are managing memory outside of a chain.
|
|
|
|
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! Instead, edit the notebook w/the location & name as this file. -->
|
|
|
|
|
|
```python
|
|
from langchain.memory import ChatMessageHistory
|
|
|
|
history = ChatMessageHistory()
|
|
|
|
history.add_user_message("hi!")
|
|
|
|
history.add_ai_message("whats up?")
|
|
```
|
|
|
|
|
|
```python
|
|
history.messages
|
|
```
|
|
|
|
<CodeOutputBlock lang="python">
|
|
|
|
```
|
|
[HumanMessage(content='hi!', additional_kwargs={}),
|
|
AIMessage(content='whats up?', additional_kwargs={})]
|
|
```
|
|
|
|
</CodeOutputBlock>
|
|
|
|
## ConversationBufferMemory
|
|
|
|
We now show how to use this simple concept in a chain. We first showcase `ConversationBufferMemory` which is just a wrapper around ChatMessageHistory that extracts the messages in a variable.
|
|
|
|
We can first extract it as a string.
|
|
|
|
|
|
```python
|
|
from langchain.memory import ConversationBufferMemory
|
|
```
|
|
|
|
|
|
```python
|
|
memory = ConversationBufferMemory()
|
|
memory.chat_memory.add_user_message("hi!")
|
|
memory.chat_memory.add_ai_message("whats up?")
|
|
```
|
|
|
|
|
|
```python
|
|
memory.load_memory_variables({})
|
|
```
|
|
|
|
<CodeOutputBlock lang="python">
|
|
|
|
```
|
|
{'history': 'Human: hi!\nAI: whats up?'}
|
|
```
|
|
|
|
</CodeOutputBlock>
|
|
|
|
We can also get the history as a list of messages
|
|
|
|
|
|
```python
|
|
memory = ConversationBufferMemory(return_messages=True)
|
|
memory.chat_memory.add_user_message("hi!")
|
|
memory.chat_memory.add_ai_message("whats up?")
|
|
```
|
|
|
|
|
|
```python
|
|
memory.load_memory_variables({})
|
|
```
|
|
|
|
<CodeOutputBlock lang="python">
|
|
|
|
```
|
|
{'history': [HumanMessage(content='hi!', additional_kwargs={}),
|
|
AIMessage(content='whats up?', additional_kwargs={})]}
|
|
```
|
|
|
|
</CodeOutputBlock>
|
|
|
|
## Using in a chain
|
|
Finally, let's take a look at using this in a chain (setting `verbose=True` so we can see the prompt).
|
|
|
|
|
|
```python
|
|
from langchain.llms import OpenAI
|
|
from langchain.chains import ConversationChain
|
|
|
|
|
|
llm = OpenAI(temperature=0)
|
|
conversation = ConversationChain(
|
|
llm=llm,
|
|
verbose=True,
|
|
memory=ConversationBufferMemory()
|
|
)
|
|
```
|
|
|
|
|
|
```python
|
|
conversation.predict(input="Hi there!")
|
|
```
|
|
|
|
<CodeOutputBlock lang="python">
|
|
|
|
```
|
|
|
|
|
|
> Entering new ConversationChain 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.
|
|
|
|
|
|
|
|
|
|
|
|
" Hi there! It's nice to meet you. How can I help you today?"
|
|
```
|
|
|
|
</CodeOutputBlock>
|
|
|
|
|
|
```python
|
|
conversation.predict(input="I'm doing well! Just having a conversation with an AI.")
|
|
```
|
|
|
|
<CodeOutputBlock lang="python">
|
|
|
|
```
|
|
|
|
|
|
> Entering new ConversationChain 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: Hi there! It's nice to meet you. How can I help you today?
|
|
Human: I'm doing well! Just having a conversation with an AI.
|
|
AI:
|
|
|
|
> Finished chain.
|
|
|
|
|
|
|
|
|
|
|
|
" That's great! It's always nice to have a conversation with someone new. What would you like to talk about?"
|
|
```
|
|
|
|
</CodeOutputBlock>
|
|
|
|
|
|
```python
|
|
conversation.predict(input="Tell me about yourself.")
|
|
```
|
|
|
|
<CodeOutputBlock lang="python">
|
|
|
|
```
|
|
|
|
|
|
> Entering new ConversationChain 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: Hi there! It's nice to meet you. How can I help you today?
|
|
Human: I'm doing well! Just having a conversation with an AI.
|
|
AI: That's great! It's always nice to have a conversation with someone new. What would you like to talk about?
|
|
Human: Tell me about yourself.
|
|
AI:
|
|
|
|
> Finished chain.
|
|
|
|
|
|
|
|
|
|
|
|
" Sure! I'm an AI created to help people with their everyday tasks. I'm programmed to understand natural language and provide helpful information. I'm also constantly learning and updating my knowledge base so I can provide more accurate and helpful answers."
|
|
```
|
|
|
|
</CodeOutputBlock>
|
|
|
|
## Saving Message History
|
|
|
|
You may often have to save messages, and then load them to use again. This can be done easily by first converting the messages to normal python dictionaries, saving those (as json or something) and then loading those. Here is an example of doing that.
|
|
|
|
|
|
```python
|
|
import json
|
|
|
|
from langchain.memory import ChatMessageHistory
|
|
from langchain.schema import messages_from_dict, messages_to_dict
|
|
|
|
history = ChatMessageHistory()
|
|
|
|
history.add_user_message("hi!")
|
|
|
|
history.add_ai_message("whats up?")
|
|
```
|
|
|
|
|
|
```python
|
|
dicts = messages_to_dict(history.messages)
|
|
```
|
|
|
|
|
|
```python
|
|
dicts
|
|
```
|
|
|
|
<CodeOutputBlock lang="python">
|
|
|
|
```
|
|
[{'type': 'human', 'data': {'content': 'hi!', 'additional_kwargs': {}}},
|
|
{'type': 'ai', 'data': {'content': 'whats up?', 'additional_kwargs': {}}}]
|
|
```
|
|
|
|
</CodeOutputBlock>
|
|
|
|
|
|
```python
|
|
new_messages = messages_from_dict(dicts)
|
|
```
|
|
|
|
|
|
```python
|
|
new_messages
|
|
```
|
|
|
|
<CodeOutputBlock lang="python">
|
|
|
|
```
|
|
[HumanMessage(content='hi!', additional_kwargs={}),
|
|
AIMessage(content='whats up?', additional_kwargs={})]
|
|
```
|
|
|
|
</CodeOutputBlock>
|
|
|
|
And that's it for the getting started! There are plenty of different types of memory, check out our examples to see them all
|