Conversation Memory
Overview
You can use Conversation Memory to give Griptape Structures the ability to keep track of the conversation across runs. All structures are created with ConversationMemory by default.
Example
from griptape.structures import Agent
agent = Agent()
agent.run("My favorite animal is a Liger.")
agent.run("What is my favorite animal?")
[09/19/23 14:21:07] INFO PromptTask 3e64ca5d5f634a11957cbf46adce251a
Input: My favorite animal is a Liger.
[09/19/23 14:21:13] INFO PromptTask 3e64ca5d5f634a11957cbf46adce251a
Output: That's fascinating! Ligers, a hybrid offspring of a male lion and a female tiger, are indeed unique and
interesting animals. They are known to be the largest of all big cats. Do you have a particular reason why you
like them so much?
INFO PromptTask 3e64ca5d5f634a11957cbf46adce251a
Input: What is my favorite animal?
[09/19/23 14:21:15] INFO PromptTask 3e64ca5d5f634a11957cbf46adce251a
Output: Your favorite animal is a Liger, as you previously mentioned.
You can disable conversation memory in any structure by setting it to None
:
Types of Memory
Griptape provides several types of Conversation Memory to fit various use-cases.
Conversation Memory
ConversationMemory will keep track of the full task input and output for all runs.
from griptape.memory.structure import ConversationMemory
from griptape.structures import Agent
agent = Agent(conversation_memory=ConversationMemory())
agent.run("Hello!")
print(agent.conversation_memory)
You can set the max_runs parameter to limit how many runs are kept in memory.
from griptape.memory.structure import ConversationMemory
from griptape.structures import Agent
conversation_memory = ConversationMemory(max_runs=2)
agent = Agent(conversation_memory=conversation_memory)
agent.run("Run 1")
agent.run("Run 2")
agent.run("Run 3")
agent.run("Run 4")
agent.run("Run 5")
print(conversation_memory.runs[0].input == "run4")
print(conversation_memory.runs[1].input == "run5")
Summary Conversation Memory
SummaryConversationMemory will progressively summarize task input and output of runs.
You can choose to offset which runs are summarized with the offset parameter.
from griptape.memory.structure import SummaryConversationMemory
from griptape.structures import Agent
from griptape.utils import Conversation
agent = Agent(conversation_memory=SummaryConversationMemory(offset=2))
agent.run("Hello my name is John?")
agent.run("What is my name?")
agent.run("My favorite color is blue.")
print(Conversation(agent.conversation_memory))