From 38a49564cbe2a98171a038a78f514cfbb74a9ddb Mon Sep 17 00:00:00 2001
From: Jeremiah Lowin <153965+jlowin@users.noreply.github.com>
Date: Mon, 23 Sep 2024 22:39:12 -0400
Subject: [PATCH 1/2] Add memory example
---
docs/examples/features/memory.mdx | 96 +++++++++++++++++++++++++++++++
docs/mint.json | 3 +-
2 files changed, 98 insertions(+), 1 deletion(-)
create mode 100644 docs/examples/features/memory.mdx
diff --git a/docs/examples/features/memory.mdx b/docs/examples/features/memory.mdx
new file mode 100644
index 00000000..8784146c
--- /dev/null
+++ b/docs/examples/features/memory.mdx
@@ -0,0 +1,96 @@
+---
+title: Using Memory
+description: How to use memory to persist information across different conversations
+icon: brain
+---
+
+Memory in ControlFlow allows agents to store and retrieve information across different conversations or workflow executions. This is particularly useful for maintaining context over time or sharing information between separate interactions.
+
+## Setup
+
+In order to use memory, you'll need to configure a [memory provider](/patterns/memory#provider). For this example, we'll use the default Chroma provider. You'll need to `pip install chromadb` to install its dependencies.
+
+## Code
+
+In this example, we'll create a simple workflow that remembers a user's favorite color across different conversations. For simplicity, we'll demonstrate the memory by using two different flows, which represent two different threads.
+
+```python
+import controlflow as cf
+
+# Create a memory module for user preferences
+user_preferences = cf.Memory(
+ key="user_preferences",
+ instructions="Store and retrieve user preferences."
+)
+
+# Create an agent with access to the memory
+agent = cf.Agent(memories=[user_preferences])
+
+# Create a flow to ask for the user's favorite color
+@cf.flow
+def remember_color():
+ return cf.run(
+ "Ask the user for their favorite color and store it in memory",
+ agents=[agent],
+ interactive=True,
+ result_type=str
+ )
+
+# Create a flow to recall the user's favorite color
+@cf.flow
+def recall_color():
+ return cf.run(
+ "What is the user's favorite color?",
+ agents=[agent],
+ result_type=str
+ )
+
+```
+
+Ordinarily, running the flows above would result in two separate -- unconnected -- conversations. The agent in the `recall_color` flow would have no way of knowing about the information from the first flow, even though its the same agent, because the conversation histories are not shared.
+
+However, because we gave the agent a memory module and instructions for how to use it, the agent *will* be able to recall the information from the first flow.
+
+Run the first flow:
+
+```python First flow
+remember_color()
+```
+```text Result
+Agent: Hello! What is your favorite color?
+User: I really like a blue-purple shade.
+Agent: Great, thank you.
+```
+
+
+When we run the second flow, the agent correctly recalls the favorite color:
+
+```python Second flow
+result = recall_color()
+print(result)
+```
+```text Result
+The user's favorite color is a blue-purple shade.
+```
+
+
+## Key concepts
+
+1. **[Memory creation](/patterns/memory#creating-memory-modules)**: We create a `Memory` object with a unique key and instructions for its use.
+
+ ```python
+ user_preferences = cf.Memory(
+ key="user_preferences",
+ instructions="Store and retrieve user preferences."
+ )
+ ```
+
+2. **[Assigning memory to agents](/patterns/memory#assigning-memories)**: We assign the memory to an agent, allowing it to access and modify the stored information.
+
+ ```python
+ agent = cf.Agent(name="PreferenceAgent", memories=[user_preferences])
+ ```
+
+3. **[Using memory across flows](/patterns/memory#sharing-memories)**: By using the same memory in different flows, we can access information across separate conversations.
+
+This example demonstrates how ControlFlow's memory feature allows information to persist across different workflow executions, enabling more context-aware and personalized interactions.
diff --git a/docs/mint.json b/docs/mint.json
index 65fdea0f..21451762 100644
--- a/docs/mint.json
+++ b/docs/mint.json
@@ -78,7 +78,8 @@
"examples/features/dependent-tasks",
"examples/features/tools",
"examples/features/multi-llm",
- "examples/features/private-flows"
+ "examples/features/private-flows",
+ "examples/features/memory"
]
},
{
From c9b5dab119ea072a174c0ad622c38947e1dbcf44 Mon Sep 17 00:00:00 2001
From: Jeremiah Lowin <153965+jlowin@users.noreply.github.com>
Date: Mon, 23 Sep 2024 22:40:49 -0400
Subject: [PATCH 2/2] Add memory doc
---
docs/examples/features/memory.mdx | 4 ++++
docs/patterns/memory.mdx | 1 -
docs/style.css | 1 -
3 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/docs/examples/features/memory.mdx b/docs/examples/features/memory.mdx
index 8784146c..5eaef3be 100644
--- a/docs/examples/features/memory.mdx
+++ b/docs/examples/features/memory.mdx
@@ -3,6 +3,10 @@ title: Using Memory
description: How to use memory to persist information across different conversations
icon: brain
---
+import { VersionBadge } from '/snippets/version-badge.mdx'
+
+
+
Memory in ControlFlow allows agents to store and retrieve information across different conversations or workflow executions. This is particularly useful for maintaining context over time or sharing information between separate interactions.
diff --git a/docs/patterns/memory.mdx b/docs/patterns/memory.mdx
index 4942a5d9..6e4d675c 100644
--- a/docs/patterns/memory.mdx
+++ b/docs/patterns/memory.mdx
@@ -5,7 +5,6 @@ icon: bookmark
---
import { VersionBadge } from '/snippets/version-badge.mdx'
-
diff --git a/docs/style.css b/docs/style.css
index 90edfa88..06b6a616 100644
--- a/docs/style.css
+++ b/docs/style.css
@@ -8,7 +8,6 @@
border: 1px solid #f2a5f9;
border-radius: 4px;
vertical-align: middle;
- margin-left: 0.5em;
}
.dark .version-badge {