Conversation Memory
Overview
You can use Conversation Memory to give Gen AI Builder 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?")
[02/27/25 20:28:09] INFO PromptTask acc8b8a0a736413791238b1d7c320d4d Input: My favorite animal is a Liger. [02/27/25 20:28:11] INFO PromptTask acc8b8a0a736413791238b1d7c320d4d Output: That's interesting! Ligers are fascinating creatures. They are a hybrid offspring of a male lion and a female tiger. Ligers typically inherit physical and behavioral traits from both parents, often growing larger than either parent species. They are known for their unique appearance and are usually found in captivity, as lions and tigers do not share the same habitats in the wild. What do you find most interesting about ligers? INFO PromptTask acc8b8a0a736413791238b1d7c320d4d Input: What is my favorite animal? [02/27/25 20:28:12] INFO PromptTask acc8b8a0a736413791238b1d7c320d4d Output: Your favorite animal is a liger.
You can disable conversation memory in any structure by setting it to None
:
from griptape.structures import Agent Agent(conversation_memory=None)
Interaction With Structures
Per Structure
By default, Conversation Memory Runs are created for each run of the structure. Gen AI Builder takes the Structure's input_task's input and the output_task's output, storing them in the Run. Tasks that are neither the input task nor the output task are not stored in the Run.
from griptape.structures import Pipeline from griptape.tasks import PromptTask pipeline = Pipeline( tasks=[ PromptTask("Respond to this request: {{ args[0] }}", id="input"), PromptTask("Improve the writing of this: {{ parent_output }}", id="improve"), PromptTask("Output this as a pirate: {{ parent_output }}", id="output"), ] ) pipeline.run("My favorite animal is a Liger.") if pipeline.conversation_memory is not None: for run in pipeline.conversation_memory.runs: print("Input", run.input.value) print("Output", run.output.value) print("\n\n")
[02/27/25 20:35:33] INFO PromptTask input Input: Respond to this request: My favorite animal is a Liger. [02/27/25 20:35:34] INFO PromptTask input Output: That's a unique choice! Ligers are fascinating creatures, being hybrids between a male lion and a female tiger. They are known for their impressive size, often larger than both parent species, and they exhibit traits from both lions and tigers. While they don't occur naturally in the wild, ligers are often found in captivity. What do you find most interesting about ligers? INFO PromptTask improve Input: Improve the writing of this: That's a unique choice! Ligers are fascinating creatures, being hybrids between a male lion and a female tiger. They are known for their impressive size, often larger than both parent species, and they exhibit traits from both lions and tigers. While they don't occur naturally in the wild, ligers are often found in captivity. What do you find most interesting about ligers? [02/27/25 20:35:36] INFO PromptTask improve Output: That's an intriguing choice! Ligers are captivating creatures, being hybrids of a male lion and a female tiger. Known for their remarkable size, they often surpass both parent species in stature and display characteristics from both lions and tigers. Although they don't occur naturally in the wild, ligers are commonly found in captivity. What aspect of ligers do you find most fascinating? INFO PromptTask output Input: Output this as a pirate: That's an intriguing choice! Ligers are captivating creatures, being hybrids of a male lion and a female tiger. Known for their remarkable size, they often surpass both parent species in stature and display characteristics from both lions and tigers. Although they don't occur naturally in the wild, ligers are commonly found in captivity. What aspect of ligers do you find most fascinating? [02/27/25 20:35:37] INFO PromptTask output Output: Arrr, that's a curious pick, matey! Ligers be captivat'n beasts, they be, a mix of a lion buccaneer and a tigress lass. Known fer their grand size, they often outgrow both their lion and tiger kin, showin' traits from both sides of the family. Though ye won't find 'em roamin' the wild seas, these mighty creatures be mostly found in captivity. What part of these magnificent ligers be sparklin' the most in yer eye, eh? Input Respond to this request: My favorite animal is a Liger. Output Arrr, that's a curious pick, matey! Ligers be captivat'n beasts, they be, a mix of a lion buccaneer and a tigress lass. Known fer their grand size, they often outgrow both their lion and tiger kin, showin' traits from both sides of the family. Though ye won't find 'em roamin' the wild seas, these mighty creatures be mostly found in captivity. What part of these magnificent ligers be sparklin' the most in yer eye, eh?
In this example, the improve
Task is "forgotten" after the Structure's run is finished. This approach allows you to perform intermediary work within a Structure without it being stored in, and potentially cluttering, Conversation Memory.
Per Task
You can change when Conversation Memory Runs are created by modifying Structure.conversation_memory_strategy from the default per_structure
to per_task
.
from griptape.structures import Pipeline from griptape.tasks import PromptTask pipeline = Pipeline( conversation_memory_strategy="per_task", tasks=[ PromptTask("Respond to this request: {{ args[0] }}", id="input"), PromptTask("Improve the writing", id="improve"), PromptTask("Respond as a pirate", id="output"), ], ) pipeline.run("My favorite animal is a Liger.") if pipeline.conversation_memory is not None: for run in pipeline.conversation_memory.runs: print("Input", run.input.value) print("Output", run.output.value) print("\n\n")
[02/27/25 20:27:11] INFO PromptTask input Input: Respond to this request: My favorite animal is a Liger. [02/27/25 20:27:15] INFO PromptTask input Output: That's interesting! Ligers are fascinating creatures, being hybrids between a male lion and a female tiger. They are known for their impressive size, often larger than both parent species, and they exhibit a mix of traits from both lions and tigers. While they don't occur naturally in the wild, ligers are a testament to the diversity and complexity of the animal kingdom. What do you find most intriguing about ligers? INFO PromptTask improve Input: Improve the writing [02/27/25 20:27:16] INFO PromptTask improve Output: That's intriguing! Ligers are fascinating creatures, being hybrids of a male lion and a female tiger. Known for their impressive size, they often grow larger than either parent species and exhibit a blend of traits from both lions and tigers. Although they don't occur naturally in the wild, ligers showcase the diversity and complexity of the animal kingdom. What do you find most captivating about ligers? INFO PromptTask output Input: Respond as a pirate [02/27/25 20:27:18] INFO PromptTask output Output: Arrr, a liger, ye say? A mighty beast indeed, a mix of lion and tiger, like a treasure forged from the wildest parts of the animal kingdom! They be as rare as a mermaid's tear, and twice as fascinatin'. Tell me, matey, what be it about these grand creatures that captures yer adventurous spirit? Input Respond to this request: My favorite animal is a Liger. Output That's interesting! Ligers are fascinating creatures, being hybrids between a male lion and a female tiger. They are known for their impressive size, often larger than both parent species, and they exhibit a mix of traits from both lions and tigers. While they don't occur naturally in the wild, ligers are a testament to the diversity and complexity of the animal kingdom. What do you find most intriguing about ligers? Input Improve the writing Output That's intriguing! Ligers are fascinating creatures, being hybrids of a male lion and a female tiger. Known for their impressive size, they often grow larger than either parent species and exhibit a blend of traits from both lions and tigers. Although they don't occur naturally in the wild, ligers showcase the diversity and complexity of the animal kingdom. What do you find most captivating about ligers? Input Respond as a pirate Output Arrr, a liger, ye say? A mighty beast indeed, a mix of lion and tiger, like a treasure forged from the wildest parts of the animal kingdom! They be as rare as a mermaid's tear, and twice as fascinatin'. Tell me, matey, what be it about these grand creatures that captures yer adventurous spirit?
Now, each Task creates a Conversation Memory Run when it runs. This eliminates the need to feed the output of Tasks into each other using context variables like {{ parent_output }}
since the output of the previous Task is stored in Conversation Memory and loaded when the next Task runs.
To blend the two approaches, you can disable Conversation Memory on individual tasks by setting PromptTask.conversation_memory to None
.
from griptape.structures import Pipeline from griptape.tasks import PromptTask pipeline = Pipeline( conversation_memory_strategy="per_task", tasks=[ PromptTask("Respond to this request: {{ args[0] }}", id="input"), PromptTask("Improve the writing of this: {{ parent_output }}", id="improve", conversation_memory=None), PromptTask("Respond as a pirate", id="output"), ], ) pipeline.run("My favorite animal is a Liger.") if pipeline.conversation_memory is not None: for run in pipeline.conversation_memory.runs: print("Input", run.input.value) print("Output", run.output.value) print("\n\n")
[02/27/25 20:27:02] INFO PromptTask input Input: Respond to this request: My favorite animal is a Liger. [02/27/25 20:27:03] INFO PromptTask input Output: That's interesting! Ligers are fascinating creatures, being hybrids between a male lion and a female tiger. They are known for their impressive size, often larger than both parent species, and they exhibit a mix of traits from both lions and tigers. While they don't occur naturally in the wild, ligers have captured the imagination of many due to their unique characteristics. What do you find most interesting about ligers? INFO PromptTask improve Input: Improve the writing of this: That's interesting! Ligers are fascinating creatures, being hybrids between a male lion and a female tiger. They are known for their impressive size, often larger than both parent species, and they exhibit a mix of traits from both lions and tigers. While they don't occur naturally in the wild, ligers have captured the imagination of many due to their unique characteristics. What do you find most interesting about ligers? [02/27/25 20:27:04] INFO PromptTask improve Output: That's intriguing! Ligers are captivating creatures, resulting from the hybridization of a male lion and a female tiger. Renowned for their remarkable size, they often surpass both parent species in stature and display a blend of traits from lions and tigers. Although they do not occur naturally in the wild, ligers have fascinated many with their distinctive characteristics. What aspect of ligers do you find most fascinating? INFO PromptTask output Input: Respond as a pirate [02/27/25 20:27:06] INFO PromptTask output Output: Arrr, a liger, ye say? A mighty beast indeed, a mix of the king o' the jungle and the striped hunter of the wild! 'Tis a creature as rare as treasure on the high seas, with the strength of a lion and the stealth of a tiger. Aye, if I were to choose a beast to join me crew, a liger would make a fine matey! What be it about the mighty liger that captures yer fancy, me hearty? Input Respond to this request: My favorite animal is a Liger. Output That's interesting! Ligers are fascinating creatures, being hybrids between a male lion and a female tiger. They are known for their impressive size, often larger than both parent species, and they exhibit a mix of traits from both lions and tigers. While they don't occur naturally in the wild, ligers have captured the imagination of many due to their unique characteristics. What do you find most interesting about ligers? Input Respond as a pirate Output Arrr, a liger, ye say? A mighty beast indeed, a mix of the king o' the jungle and the striped hunter of the wild! 'Tis a creature as rare as treasure on the high seas, with the strength of a lion and the stealth of a tiger. Aye, if I were to choose a beast to join me crew, a liger would make a fine matey! What be it about the mighty liger that captures yer fancy, me hearty?
Types of Memory
Gen AI Builder 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)
[02/27/25 20:27:28] INFO PromptTask 3902de9d2ff5473e8f34398dfff5000e Input: Hello! INFO PromptTask 3902de9d2ff5473e8f34398dfff5000e Output: Hello! How can I assist you today? {"type": "ConversationMemory", "runs": [{"type": "Run", "id": "9616891ea5eb4cb2a16d7eac246b7e7d", "meta": null, "input": {"type": "TextArtifact", "id": "9fa4bcf1f54840519d5fe027b4670565", "reference": null, "meta": {}, "name": "9fa4bcf1f54840519d5fe027b4670565", "value": "Hello!"}, "output": {"type": "TextArtifact", "id": "9a827d3528b14690b8cf5b1636e694b5", "reference": null, "meta": {"is_react_prompt": false}, "name": "9a827d3528b14690b8cf5b1636e694b5", "value": "Hello! How can I assist you today?"}}], "meta": {}, "max_runs": null}
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")
[02/27/25 20:27:51] INFO PromptTask 2e9f9e5ff0664bd9882d14e9b20d2928 Input: Run 1 [02/27/25 20:27:52] INFO PromptTask 2e9f9e5ff0664bd9882d14e9b20d2928 Output: Could you please provide more context or details about what you mean by "Run 1"? Are you referring to a specific event, a program, a race, or something else? INFO PromptTask 2e9f9e5ff0664bd9882d14e9b20d2928 Input: Run 2 [02/27/25 20:27:53] INFO PromptTask 2e9f9e5ff0664bd9882d14e9b20d2928 Output: It seems like you're referencing something specific with "Run 1" and "Run 2," but without additional context, it's difficult to provide a meaningful response. Could you please provide more details or clarify what you're referring to? INFO PromptTask 2e9f9e5ff0664bd9882d14e9b20d2928 Input: Run 3 [02/27/25 20:27:54] INFO PromptTask 2e9f9e5ff0664bd9882d14e9b20d2928 Output: It appears you're mentioning "Run 1," "Run 2," and "Run 3," but without additional context, it's challenging to understand what you're referring to. Are these related to a sequence of events, a series of tests, a workout routine, or something else? Please provide more information so I can assist you better. INFO PromptTask 2e9f9e5ff0664bd9882d14e9b20d2928 Input: Run 4 [02/27/25 20:27:55] INFO PromptTask 2e9f9e5ff0664bd9882d14e9b20d2928 Output: It seems like you're referencing a sequence or series, but without additional context, it's difficult to determine what "Run 4" specifically refers to. It could be related to a variety of things such as a series of experiments, a sequence in a game, a workout routine, or something else entirely. Could you provide more details or clarify the context? INFO PromptTask 2e9f9e5ff0664bd9882d14e9b20d2928 Input: Run 5 [02/27/25 20:27:56] INFO PromptTask 2e9f9e5ff0664bd9882d14e9b20d2928 Output: It seems like you're continuing a sequence with "Run 3," "Run 4," and now "Run 5." However, without additional context, it's difficult to determine what this sequence pertains to. Could you provide more information or clarify the context so I can assist you more effectively? False False
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))
[02/27/25 20:26:57] INFO PromptTask e40be7118727403a9fecb47655d7a0c4 Input: Hello my name is John? INFO PromptTask e40be7118727403a9fecb47655d7a0c4 Output: Hello John! How can I assist you today? INFO PromptTask e40be7118727403a9fecb47655d7a0c4 Input: What is my name? [02/27/25 20:26:58] INFO PromptTask e40be7118727403a9fecb47655d7a0c4 Output: Your name is John. How can I help you further? INFO PromptTask e40be7118727403a9fecb47655d7a0c4 Input: My favorite color is blue. [02/27/25 20:26:59] INFO PromptTask e40be7118727403a9fecb47655d7a0c4 Output: Blue is a great color! It's often associated with calmness and serenity. Do you have any other favorites or anything else you'd like to share? Q: Hello my name is John? A: Hello John! How can I assist you today? Q: What is my name? A: Your name is John. How can I help you further? Q: My favorite color is blue. A: Blue is a great color! It's often associated with calmness and serenity. Do you have any other favorites or anything else you'd like to share? Summary: John introduces himself to the assistant, who then offers to help him.
- On this page
- Overview
- Types of Memory
Could this page be better? Report a problem or suggest an addition!