mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 11:14:04 +00:00
add everyting for docs
This commit is contained in:
parent
de45a738ee
commit
0fe8799f94
1015 changed files with 185353 additions and 0 deletions
23
docs/snippets/modules/memory/chat_messages/get_started.mdx
Normal file
23
docs/snippets/modules/memory/chat_messages/get_started.mdx
Normal file
|
@ -0,0 +1,23 @@
|
|||
```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>
|
173
docs/snippets/modules/memory/get_started.mdx
Normal file
173
docs/snippets/modules/memory/get_started.mdx
Normal file
|
@ -0,0 +1,173 @@
|
|||
Let's take a look at how to use ConversationBufferMemory in chains.
|
||||
ConversationBufferMemory is an extremely simple form of memory that just keeps a list of chat messages in a buffer
|
||||
and passes those into the prompt template.
|
||||
|
||||
```python
|
||||
from langchain.memory import ConversationBufferMemory
|
||||
|
||||
memory = ConversationBufferMemory()
|
||||
memory.chat_memory.add_user_message("hi!")
|
||||
memory.chat_memory.add_ai_message("whats up?")
|
||||
```
|
||||
|
||||
When using memory in a chain, there are a few key concepts to understand.
|
||||
Note that here we cover general concepts that are useful for most types of memory.
|
||||
Each individual memory type may very well have its own parameters and concepts that are necessary to understand.
|
||||
|
||||
### What variables get returned from memory
|
||||
Before going into the chain, various variables are read from memory.
|
||||
This have specific names which need to align with the variables the chain expects.
|
||||
You can see what these variables are by calling `memory.load_memory_variables({})`.
|
||||
Note that the empty dictionary that we pass in is just a placeholder for real variables.
|
||||
If the memory type you are using is dependent upon the input variables, you may need to pass some in.
|
||||
|
||||
```python
|
||||
memory.load_memory_variables({})
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
{'history': "Human: hi!\nAI: whats up?"}
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
In this case, you can see that `load_memory_variables` returns a single key, `history`.
|
||||
This means that your chain (and likely your prompt) should expect and input named `history`.
|
||||
You can usually control this variable through parameters on the memory class.
|
||||
For example, if you want the memory variables to be returned in the key `chat_history` you can do:
|
||||
|
||||
```python
|
||||
memory = ConversationBufferMemory(memory_key="chat_history")
|
||||
memory.chat_memory.add_user_message("hi!")
|
||||
memory.chat_memory.add_ai_message("whats up?")
|
||||
```
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
{'chat_history': "Human: hi!\nAI: whats up?"}
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
The parameter name to control these keys may vary per memory type, but it's important to understand that (1) this is controllable, (2) how to control it.
|
||||
|
||||
### Whether memory is a string or a list of messages
|
||||
|
||||
One of the most common types of memory involves returning a list of chat messages.
|
||||
These can either be returned as a single string, all concatenated together (useful when they will be passed in LLMs)
|
||||
or a list of ChatMessages (useful when passed into ChatModels).
|
||||
|
||||
By default, they are returned as a single string.
|
||||
In order to return as a list of messages, you can set `return_messages=True`
|
||||
|
||||
```python
|
||||
memory = ConversationBufferMemory(return_messages=True)
|
||||
memory.chat_memory.add_user_message("hi!")
|
||||
memory.chat_memory.add_ai_message("whats up?")
|
||||
```
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
{'history': [HumanMessage(content='hi!', additional_kwargs={}, example=False),
|
||||
AIMessage(content='whats up?', additional_kwargs={}, example=False)]}
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
### What keys are saved to memory
|
||||
|
||||
Often times chains take in or return multiple input/output keys.
|
||||
In these cases, how can we know which keys we want to save to the chat message history?
|
||||
This is generally controllable by `input_key` and `output_key` parameters on the memory types.
|
||||
These default to None - and if there is only one input/output key it is known to just use that.
|
||||
However, if there are multiple input/output keys then you MUST specify the name of which one to use
|
||||
|
||||
### End to end example
|
||||
|
||||
Finally, let's take a look at using this in a chain.
|
||||
We'll use an LLMChain, and show working with both an LLM and a ChatModel.
|
||||
|
||||
#### Using an LLM
|
||||
|
||||
|
||||
```python
|
||||
from langchain.llms import OpenAI
|
||||
from langchain.prompts import PromptTemplate
|
||||
from langchain.chains import LLMChain
|
||||
from langchain.memory import ConversationBufferMemory
|
||||
|
||||
|
||||
llm = OpenAI(temperature=0)
|
||||
# Notice that "chat_history" is present in the prompt template
|
||||
template = """You are a nice chatbot having a conversation with a human.
|
||||
|
||||
Previous conversation:
|
||||
{chat_history}
|
||||
|
||||
New human question: {question}
|
||||
Response:"""
|
||||
prompt = PromptTemplate.from_template(template)
|
||||
# Notice that we need to align the `memory_key`
|
||||
memory = ConversationBufferMemory(memory_key="chat_history")
|
||||
conversation = LLMChain(
|
||||
llm=llm,
|
||||
prompt=prompt,
|
||||
verbose=True,
|
||||
memory=memory
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# Notice that we just pass in the `question` variables - `chat_history` gets populated by memory
|
||||
conversation({"question": "hi"})
|
||||
```
|
||||
|
||||
|
||||
#### Using a ChatModel
|
||||
|
||||
|
||||
```python
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
from langchain.prompts import (
|
||||
ChatPromptTemplate,
|
||||
MessagesPlaceholder,
|
||||
SystemMessagePromptTemplate,
|
||||
HumanMessagePromptTemplate,
|
||||
)
|
||||
from langchain.chains import LLMChain
|
||||
from langchain.memory import ConversationBufferMemory
|
||||
|
||||
|
||||
llm = ChatOpenAI()
|
||||
prompt = ChatPromptTemplate(
|
||||
messages=[
|
||||
SystemMessagePromptTemplate.from_template(
|
||||
"You are a nice chatbot having a conversation with a human."
|
||||
),
|
||||
# The `variable_name` here is what must align with memory
|
||||
MessagesPlaceholder(variable_name="chat_history"),
|
||||
HumanMessagePromptTemplate.from_template("{question}")
|
||||
]
|
||||
)
|
||||
# Notice that we `return_messages=True` to fit into the MessagesPlaceholder
|
||||
# Notice that `"chat_history"` aligns with the MessagesPlaceholder name.
|
||||
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
||||
conversation = LLMChain(
|
||||
llm=llm,
|
||||
prompt=prompt,
|
||||
verbose=True,
|
||||
memory=memory
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# Notice that we just pass in the `question` variables - `chat_history` gets populated by memory
|
||||
conversation({"question": "hi"})
|
||||
```
|
||||
|
||||
|
||||
|
157
docs/snippets/modules/memory/types/buffer.mdx
Normal file
157
docs/snippets/modules/memory/types/buffer.mdx
Normal file
|
@ -0,0 +1,157 @@
|
|||
```python
|
||||
from langchain.memory import ConversationBufferMemory
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
memory = ConversationBufferMemory()
|
||||
memory.save_context({"input": "hi"}, {"output": "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 (this is useful if you are using this with a chat model).
|
||||
|
||||
|
||||
```python
|
||||
memory = ConversationBufferMemory(return_messages=True)
|
||||
memory.save_context({"input": "hi"}, {"output": "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>
|
||||
|
||||
And that's it for the getting started! There are plenty of different types of memory, check out our examples to see them all
|
185
docs/snippets/modules/memory/types/buffer_window.mdx
Normal file
185
docs/snippets/modules/memory/types/buffer_window.mdx
Normal file
|
@ -0,0 +1,185 @@
|
|||
```python
|
||||
from langchain.memory import ConversationBufferWindowMemory
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
memory = ConversationBufferWindowMemory( k=1)
|
||||
memory.save_context({"input": "hi"}, {"output": "whats up"})
|
||||
memory.save_context({"input": "not much you"}, {"output": "not much"})
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
memory.load_memory_variables({})
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
{'history': 'Human: not much you\nAI: not much'}
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
We can also get the history as a list of messages (this is useful if you are using this with a chat model).
|
||||
|
||||
|
||||
```python
|
||||
memory = ConversationBufferWindowMemory( k=1, return_messages=True)
|
||||
memory.save_context({"input": "hi"}, {"output": "whats up"})
|
||||
memory.save_context({"input": "not much you"}, {"output": "not much"})
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
memory.load_memory_variables({})
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
{'history': [HumanMessage(content='not much you', additional_kwargs={}),
|
||||
AIMessage(content='not much', additional_kwargs={})]}
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
## Using in a chain
|
||||
Let's walk through an example, again setting `verbose=True` so we can see the prompt.
|
||||
|
||||
|
||||
```python
|
||||
from langchain.llms import OpenAI
|
||||
from langchain.chains import ConversationChain
|
||||
conversation_with_summary = ConversationChain(
|
||||
llm=OpenAI(temperature=0),
|
||||
# We set a low k=2, to only keep the last 2 interactions in memory
|
||||
memory=ConversationBufferWindowMemory(k=2),
|
||||
verbose=True
|
||||
)
|
||||
conversation_with_summary.predict(input="Hi, what's up?")
|
||||
```
|
||||
|
||||
<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, what's up?
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you?"
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
conversation_with_summary.predict(input="What's their issues?")
|
||||
```
|
||||
|
||||
<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, what's up?
|
||||
AI: Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you?
|
||||
Human: What's their issues?
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" The customer is having trouble connecting to their Wi-Fi network. I'm helping them troubleshoot the issue and get them connected."
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
conversation_with_summary.predict(input="Is it going well?")
|
||||
```
|
||||
|
||||
<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, what's up?
|
||||
AI: Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you?
|
||||
Human: What's their issues?
|
||||
AI: The customer is having trouble connecting to their Wi-Fi network. I'm helping them troubleshoot the issue and get them connected.
|
||||
Human: Is it going well?
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" Yes, it's going well so far. We've already identified the problem and are now working on a solution."
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
# Notice here that the first interaction does not appear.
|
||||
conversation_with_summary.predict(input="What's the solution?")
|
||||
```
|
||||
|
||||
<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: What's their issues?
|
||||
AI: The customer is having trouble connecting to their Wi-Fi network. I'm helping them troubleshoot the issue and get them connected.
|
||||
Human: Is it going well?
|
||||
AI: Yes, it's going well so far. We've already identified the problem and are now working on a solution.
|
||||
Human: What's the solution?
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" The solution is to reset the router and reconfigure the settings. We're currently in the process of doing that."
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
418
docs/snippets/modules/memory/types/entity_summary_memory.mdx
Normal file
418
docs/snippets/modules/memory/types/entity_summary_memory.mdx
Normal file
|
@ -0,0 +1,418 @@
|
|||
```python
|
||||
from langchain.llms import OpenAI
|
||||
from langchain.memory import ConversationEntityMemory
|
||||
llm = OpenAI(temperature=0)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
memory = ConversationEntityMemory(llm=llm)
|
||||
_input = {"input": "Deven & Sam are working on a hackathon project"}
|
||||
memory.load_memory_variables(_input)
|
||||
memory.save_context(
|
||||
_input,
|
||||
{"output": " That sounds like a great project! What kind of project are they working on?"}
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
memory.load_memory_variables({"input": 'who is Sam'})
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
{'history': 'Human: Deven & Sam are working on a hackathon project\nAI: That sounds like a great project! What kind of project are they working on?',
|
||||
'entities': {'Sam': 'Sam is working on a hackathon project with Deven.'}}
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
memory = ConversationEntityMemory(llm=llm, return_messages=True)
|
||||
_input = {"input": "Deven & Sam are working on a hackathon project"}
|
||||
memory.load_memory_variables(_input)
|
||||
memory.save_context(
|
||||
_input,
|
||||
{"output": " That sounds like a great project! What kind of project are they working on?"}
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
memory.load_memory_variables({"input": 'who is Sam'})
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
{'history': [HumanMessage(content='Deven & Sam are working on a hackathon project', additional_kwargs={}),
|
||||
AIMessage(content=' That sounds like a great project! What kind of project are they working on?', additional_kwargs={})],
|
||||
'entities': {'Sam': 'Sam is working on a hackathon project with Deven.'}}
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
## Using in a chain
|
||||
Let's now use it in a chain!
|
||||
|
||||
|
||||
```python
|
||||
from langchain.chains import ConversationChain
|
||||
from langchain.memory import ConversationEntityMemory
|
||||
from langchain.memory.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Dict, Any
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
conversation = ConversationChain(
|
||||
llm=llm,
|
||||
verbose=True,
|
||||
prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE,
|
||||
memory=ConversationEntityMemory(llm=llm)
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
conversation.predict(input="Deven & Sam are working on a hackathon project")
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
|
||||
|
||||
> Entering new ConversationChain chain...
|
||||
Prompt after formatting:
|
||||
You are an assistant to a human, powered by a large language model trained by OpenAI.
|
||||
|
||||
You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
|
||||
|
||||
You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.
|
||||
|
||||
Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.
|
||||
|
||||
Context:
|
||||
{'Deven': 'Deven is working on a hackathon project with Sam.', 'Sam': 'Sam is working on a hackathon project with Deven.'}
|
||||
|
||||
Current conversation:
|
||||
|
||||
Last line:
|
||||
Human: Deven & Sam are working on a hackathon project
|
||||
You:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
' That sounds like a great project! What kind of project are they working on?'
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
conversation.memory.entity_store.store
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
{'Deven': 'Deven is working on a hackathon project with Sam, which they are entering into a hackathon.',
|
||||
'Sam': 'Sam is working on a hackathon project with Deven.'}
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
conversation.predict(input="They are trying to add more complex memory structures to Langchain")
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
|
||||
|
||||
> Entering new ConversationChain chain...
|
||||
Prompt after formatting:
|
||||
You are an assistant to a human, powered by a large language model trained by OpenAI.
|
||||
|
||||
You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
|
||||
|
||||
You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.
|
||||
|
||||
Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.
|
||||
|
||||
Context:
|
||||
{'Deven': 'Deven is working on a hackathon project with Sam, which they are entering into a hackathon.', 'Sam': 'Sam is working on a hackathon project with Deven.', 'Langchain': ''}
|
||||
|
||||
Current conversation:
|
||||
Human: Deven & Sam are working on a hackathon project
|
||||
AI: That sounds like a great project! What kind of project are they working on?
|
||||
Last line:
|
||||
Human: They are trying to add more complex memory structures to Langchain
|
||||
You:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
' That sounds like an interesting project! What kind of memory structures are they trying to add?'
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
conversation.predict(input="They are adding in a key-value store for entities mentioned so far in the conversation.")
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
|
||||
|
||||
> Entering new ConversationChain chain...
|
||||
Prompt after formatting:
|
||||
You are an assistant to a human, powered by a large language model trained by OpenAI.
|
||||
|
||||
You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
|
||||
|
||||
You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.
|
||||
|
||||
Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.
|
||||
|
||||
Context:
|
||||
{'Deven': 'Deven is working on a hackathon project with Sam, which they are entering into a hackathon. They are trying to add more complex memory structures to Langchain.', 'Sam': 'Sam is working on a hackathon project with Deven, trying to add more complex memory structures to Langchain.', 'Langchain': 'Langchain is a project that is trying to add more complex memory structures.', 'Key-Value Store': ''}
|
||||
|
||||
Current conversation:
|
||||
Human: Deven & Sam are working on a hackathon project
|
||||
AI: That sounds like a great project! What kind of project are they working on?
|
||||
Human: They are trying to add more complex memory structures to Langchain
|
||||
AI: That sounds like an interesting project! What kind of memory structures are they trying to add?
|
||||
Last line:
|
||||
Human: They are adding in a key-value store for entities mentioned so far in the conversation.
|
||||
You:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
' That sounds like a great idea! How will the key-value store help with the project?'
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
conversation.predict(input="What do you know about Deven & Sam?")
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
|
||||
|
||||
> Entering new ConversationChain chain...
|
||||
Prompt after formatting:
|
||||
You are an assistant to a human, powered by a large language model trained by OpenAI.
|
||||
|
||||
You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
|
||||
|
||||
You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.
|
||||
|
||||
Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.
|
||||
|
||||
Context:
|
||||
{'Deven': 'Deven is working on a hackathon project with Sam, which they are entering into a hackathon. They are trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation.', 'Sam': 'Sam is working on a hackathon project with Deven, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation.'}
|
||||
|
||||
Current conversation:
|
||||
Human: Deven & Sam are working on a hackathon project
|
||||
AI: That sounds like a great project! What kind of project are they working on?
|
||||
Human: They are trying to add more complex memory structures to Langchain
|
||||
AI: That sounds like an interesting project! What kind of memory structures are they trying to add?
|
||||
Human: They are adding in a key-value store for entities mentioned so far in the conversation.
|
||||
AI: That sounds like a great idea! How will the key-value store help with the project?
|
||||
Last line:
|
||||
Human: What do you know about Deven & Sam?
|
||||
You:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
' Deven and Sam are working on a hackathon project together, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to be working hard on this project and have a great idea for how the key-value store can help.'
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
## Inspecting the memory store
|
||||
We can also inspect the memory store directly. In the following examples, we look at it directly, and then go through some examples of adding information and watch how it changes.
|
||||
|
||||
|
||||
```python
|
||||
from pprint import pprint
|
||||
pprint(conversation.memory.entity_store.store)
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
{'Daimon': 'Daimon is a company founded by Sam, a successful entrepreneur.',
|
||||
'Deven': 'Deven is working on a hackathon project with Sam, which they are '
|
||||
'entering into a hackathon. They are trying to add more complex '
|
||||
'memory structures to Langchain, including a key-value store for '
|
||||
'entities mentioned so far in the conversation, and seem to be '
|
||||
'working hard on this project with a great idea for how the '
|
||||
'key-value store can help.',
|
||||
'Key-Value Store': 'A key-value store is being added to the project to store '
|
||||
'entities mentioned in the conversation.',
|
||||
'Langchain': 'Langchain is a project that is trying to add more complex '
|
||||
'memory structures, including a key-value store for entities '
|
||||
'mentioned so far in the conversation.',
|
||||
'Sam': 'Sam is working on a hackathon project with Deven, trying to add more '
|
||||
'complex memory structures to Langchain, including a key-value store '
|
||||
'for entities mentioned so far in the conversation. They seem to have '
|
||||
'a great idea for how the key-value store can help, and Sam is also '
|
||||
'the founder of a company called Daimon.'}
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
conversation.predict(input="Sam is the founder of a company called Daimon.")
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
|
||||
|
||||
> Entering new ConversationChain chain...
|
||||
Prompt after formatting:
|
||||
You are an assistant to a human, powered by a large language model trained by OpenAI.
|
||||
|
||||
You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
|
||||
|
||||
You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.
|
||||
|
||||
Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.
|
||||
|
||||
Context:
|
||||
{'Daimon': 'Daimon is a company founded by Sam, a successful entrepreneur.', 'Sam': 'Sam is working on a hackathon project with Deven, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to have a great idea for how the key-value store can help, and Sam is also the founder of a company called Daimon.'}
|
||||
|
||||
Current conversation:
|
||||
Human: They are adding in a key-value store for entities mentioned so far in the conversation.
|
||||
AI: That sounds like a great idea! How will the key-value store help with the project?
|
||||
Human: What do you know about Deven & Sam?
|
||||
AI: Deven and Sam are working on a hackathon project together, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to be working hard on this project and have a great idea for how the key-value store can help.
|
||||
Human: Sam is the founder of a company called Daimon.
|
||||
AI:
|
||||
That's impressive! It sounds like Sam is a very successful entrepreneur. What kind of company is Daimon?
|
||||
Last line:
|
||||
Human: Sam is the founder of a company called Daimon.
|
||||
You:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" That's impressive! It sounds like Sam is a very successful entrepreneur. What kind of company is Daimon?"
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
from pprint import pprint
|
||||
pprint(conversation.memory.entity_store.store)
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
{'Daimon': 'Daimon is a company founded by Sam, a successful entrepreneur, who '
|
||||
'is working on a hackathon project with Deven to add more complex '
|
||||
'memory structures to Langchain.',
|
||||
'Deven': 'Deven is working on a hackathon project with Sam, which they are '
|
||||
'entering into a hackathon. They are trying to add more complex '
|
||||
'memory structures to Langchain, including a key-value store for '
|
||||
'entities mentioned so far in the conversation, and seem to be '
|
||||
'working hard on this project with a great idea for how the '
|
||||
'key-value store can help.',
|
||||
'Key-Value Store': 'A key-value store is being added to the project to store '
|
||||
'entities mentioned in the conversation.',
|
||||
'Langchain': 'Langchain is a project that is trying to add more complex '
|
||||
'memory structures, including a key-value store for entities '
|
||||
'mentioned so far in the conversation.',
|
||||
'Sam': 'Sam is working on a hackathon project with Deven, trying to add more '
|
||||
'complex memory structures to Langchain, including a key-value store '
|
||||
'for entities mentioned so far in the conversation. They seem to have '
|
||||
'a great idea for how the key-value store can help, and Sam is also '
|
||||
'the founder of a successful company called Daimon.'}
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
conversation.predict(input="What do you know about Sam?")
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
|
||||
|
||||
> Entering new ConversationChain chain...
|
||||
Prompt after formatting:
|
||||
You are an assistant to a human, powered by a large language model trained by OpenAI.
|
||||
|
||||
You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
|
||||
|
||||
You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.
|
||||
|
||||
Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.
|
||||
|
||||
Context:
|
||||
{'Deven': 'Deven is working on a hackathon project with Sam, which they are entering into a hackathon. They are trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation, and seem to be working hard on this project with a great idea for how the key-value store can help.', 'Sam': 'Sam is working on a hackathon project with Deven, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to have a great idea for how the key-value store can help, and Sam is also the founder of a successful company called Daimon.', 'Langchain': 'Langchain is a project that is trying to add more complex memory structures, including a key-value store for entities mentioned so far in the conversation.', 'Daimon': 'Daimon is a company founded by Sam, a successful entrepreneur, who is working on a hackathon project with Deven to add more complex memory structures to Langchain.'}
|
||||
|
||||
Current conversation:
|
||||
Human: What do you know about Deven & Sam?
|
||||
AI: Deven and Sam are working on a hackathon project together, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to be working hard on this project and have a great idea for how the key-value store can help.
|
||||
Human: Sam is the founder of a company called Daimon.
|
||||
AI:
|
||||
That's impressive! It sounds like Sam is a very successful entrepreneur. What kind of company is Daimon?
|
||||
Human: Sam is the founder of a company called Daimon.
|
||||
AI: That's impressive! It sounds like Sam is a very successful entrepreneur. What kind of company is Daimon?
|
||||
Last line:
|
||||
Human: What do you know about Sam?
|
||||
You:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
' Sam is the founder of a successful company called Daimon. He is also working on a hackathon project with Deven to add more complex memory structures to Langchain. They seem to have a great idea for how the key-value store can help.'
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
193
docs/snippets/modules/memory/types/summary.mdx
Normal file
193
docs/snippets/modules/memory/types/summary.mdx
Normal file
|
@ -0,0 +1,193 @@
|
|||
```python
|
||||
from langchain.memory import ConversationSummaryMemory, ChatMessageHistory
|
||||
from langchain.llms import OpenAI
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
memory = ConversationSummaryMemory(llm=OpenAI(temperature=0))
|
||||
memory.save_context({"input": "hi"}, {"output": "whats up"})
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
memory.load_memory_variables({})
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
{'history': '\nThe human greets the AI, to which the AI responds.'}
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
We can also get the history as a list of messages (this is useful if you are using this with a chat model).
|
||||
|
||||
|
||||
```python
|
||||
memory = ConversationSummaryMemory(llm=OpenAI(temperature=0), return_messages=True)
|
||||
memory.save_context({"input": "hi"}, {"output": "whats up"})
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
memory.load_memory_variables({})
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
{'history': [SystemMessage(content='\nThe human greets the AI, to which the AI responds.', additional_kwargs={})]}
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
We can also utilize the `predict_new_summary` method directly.
|
||||
|
||||
|
||||
```python
|
||||
messages = memory.chat_memory.messages
|
||||
previous_summary = ""
|
||||
memory.predict_new_summary(messages, previous_summary)
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
'\nThe human greets the AI, to which the AI responds.'
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
## Initializing with messages
|
||||
|
||||
If you have messages outside this class, you can easily initialize the class with ChatMessageHistory. During loading, a summary will be calculated.
|
||||
|
||||
|
||||
```python
|
||||
history = ChatMessageHistory()
|
||||
history.add_user_message("hi")
|
||||
history.add_ai_message("hi there!")
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
memory = ConversationSummaryMemory.from_messages(llm=OpenAI(temperature=0), chat_memory=history, return_messages=True)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
memory.buffer
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
'\nThe human greets the AI, to which the AI responds with a friendly greeting.'
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
## Using in a chain
|
||||
Let's walk through an example of using this in a chain, again 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_with_summary = ConversationChain(
|
||||
llm=llm,
|
||||
memory=ConversationSummaryMemory(llm=OpenAI()),
|
||||
verbose=True
|
||||
)
|
||||
conversation_with_summary.predict(input="Hi, what's up?")
|
||||
```
|
||||
|
||||
<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, what's up?
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you?"
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
conversation_with_summary.predict(input="Tell me more about it!")
|
||||
```
|
||||
|
||||
<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:
|
||||
|
||||
The human greeted the AI and asked how it was doing. The AI replied that it was doing great and was currently helping a customer with a technical issue.
|
||||
Human: Tell me more about it!
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" Sure! The customer is having trouble with their computer not connecting to the internet. I'm helping them troubleshoot the issue and figure out what the problem is. So far, we've tried resetting the router and checking the network settings, but the issue still persists. We're currently looking into other possible solutions."
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
conversation_with_summary.predict(input="Very cool -- what is the scope of the project?")
|
||||
```
|
||||
|
||||
<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:
|
||||
|
||||
The human greeted the AI and asked how it was doing. The AI replied that it was doing great and was currently helping a customer with a technical issue where their computer was not connecting to the internet. The AI was troubleshooting the issue and had already tried resetting the router and checking the network settings, but the issue still persisted and they were looking into other possible solutions.
|
||||
Human: Very cool -- what is the scope of the project?
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" The scope of the project is to troubleshoot the customer's computer issue and find a solution that will allow them to connect to the internet. We are currently exploring different possibilities and have already tried resetting the router and checking the network settings, but the issue still persists."
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
|
@ -0,0 +1,229 @@
|
|||
```python
|
||||
from datetime import datetime
|
||||
from langchain.embeddings.openai import OpenAIEmbeddings
|
||||
from langchain.llms import OpenAI
|
||||
from langchain.memory import VectorStoreRetrieverMemory
|
||||
from langchain.chains import ConversationChain
|
||||
from langchain.prompts import PromptTemplate
|
||||
```
|
||||
|
||||
### Initialize your VectorStore
|
||||
|
||||
Depending on the store you choose, this step may look different. Consult the relevant VectorStore documentation for more details.
|
||||
|
||||
|
||||
```python
|
||||
import faiss
|
||||
|
||||
from langchain.docstore import InMemoryDocstore
|
||||
from langchain.vectorstores import FAISS
|
||||
|
||||
|
||||
embedding_size = 1536 # Dimensions of the OpenAIEmbeddings
|
||||
index = faiss.IndexFlatL2(embedding_size)
|
||||
embedding_fn = OpenAIEmbeddings().embed_query
|
||||
vectorstore = FAISS(embedding_fn, index, InMemoryDocstore({}), {})
|
||||
```
|
||||
|
||||
### Create your the VectorStoreRetrieverMemory
|
||||
|
||||
The memory object is instantiated from any VectorStoreRetriever.
|
||||
|
||||
|
||||
```python
|
||||
# In actual usage, you would set `k` to be a higher value, but we use k=1 to show that
|
||||
# the vector lookup still returns the semantically relevant information
|
||||
retriever = vectorstore.as_retriever(search_kwargs=dict(k=1))
|
||||
memory = VectorStoreRetrieverMemory(retriever=retriever)
|
||||
|
||||
# When added to an agent, the memory object can save pertinent information from conversations or used tools
|
||||
memory.save_context({"input": "My favorite food is pizza"}, {"output": "that's good to know"})
|
||||
memory.save_context({"input": "My favorite sport is soccer"}, {"output": "..."})
|
||||
memory.save_context({"input": "I don't the Celtics"}, {"output": "ok"}) #
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# Notice the first result returned is the memory pertaining to tax help, which the language model deems more semantically relevant
|
||||
# to a 1099 than the other documents, despite them both containing numbers.
|
||||
print(memory.load_memory_variables({"prompt": "what sport should i watch?"})["history"])
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
input: My favorite sport is soccer
|
||||
output: ...
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
## Using in a chain
|
||||
Let's walk through an example, again setting `verbose=True` so we can see the prompt.
|
||||
|
||||
|
||||
```python
|
||||
llm = OpenAI(temperature=0) # Can be any valid LLM
|
||||
_DEFAULT_TEMPLATE = """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.
|
||||
|
||||
Relevant pieces of previous conversation:
|
||||
{history}
|
||||
|
||||
(You do not need to use these pieces of information if not relevant)
|
||||
|
||||
Current conversation:
|
||||
Human: {input}
|
||||
AI:"""
|
||||
PROMPT = PromptTemplate(
|
||||
input_variables=["history", "input"], template=_DEFAULT_TEMPLATE
|
||||
)
|
||||
conversation_with_summary = ConversationChain(
|
||||
llm=llm,
|
||||
prompt=PROMPT,
|
||||
# We set a very low max_token_limit for the purposes of testing.
|
||||
memory=memory,
|
||||
verbose=True
|
||||
)
|
||||
conversation_with_summary.predict(input="Hi, my name is Perry, what's up?")
|
||||
```
|
||||
|
||||
<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.
|
||||
|
||||
Relevant pieces of previous conversation:
|
||||
input: My favorite food is pizza
|
||||
output: that's good to know
|
||||
|
||||
(You do not need to use these pieces of information if not relevant)
|
||||
|
||||
Current conversation:
|
||||
Human: Hi, my name is Perry, what's up?
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" Hi Perry, I'm doing well. How about you?"
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
# Here, the basketball related content is surfaced
|
||||
conversation_with_summary.predict(input="what's my favorite sport?")
|
||||
```
|
||||
|
||||
<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.
|
||||
|
||||
Relevant pieces of previous conversation:
|
||||
input: My favorite sport is soccer
|
||||
output: ...
|
||||
|
||||
(You do not need to use these pieces of information if not relevant)
|
||||
|
||||
Current conversation:
|
||||
Human: what's my favorite sport?
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
' You told me earlier that your favorite sport is soccer.'
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
# Even though the language model is stateless, since relevant memory is fetched, it can "reason" about the time.
|
||||
# Timestamping memories and data is useful in general to let the agent determine temporal relevance
|
||||
conversation_with_summary.predict(input="Whats my favorite food")
|
||||
```
|
||||
|
||||
<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.
|
||||
|
||||
Relevant pieces of previous conversation:
|
||||
input: My favorite food is pizza
|
||||
output: that's good to know
|
||||
|
||||
(You do not need to use these pieces of information if not relevant)
|
||||
|
||||
Current conversation:
|
||||
Human: Whats my favorite food
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
' You said your favorite food is pizza.'
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
# The memories from the conversation are automatically stored,
|
||||
# since this query best matches the introduction chat above,
|
||||
# the agent is able to 'remember' the user's name.
|
||||
conversation_with_summary.predict(input="What's my name?")
|
||||
```
|
||||
|
||||
<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.
|
||||
|
||||
Relevant pieces of previous conversation:
|
||||
input: Hi, my name is Perry, what's up?
|
||||
response: Hi Perry, I'm doing well. How about you?
|
||||
|
||||
(You do not need to use these pieces of information if not relevant)
|
||||
|
||||
Current conversation:
|
||||
Human: What's my name?
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
' Your name is Perry.'
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
Loading…
Add table
Add a link
Reference in a new issue