Skip to content

Commit 26e9a24

Browse files
authored
Versioned Memory Store for AI Agents using ProllyTree and the Rig framework (#58)
* Versioned Memory Store for AI Agents using ProllyTree and the Rig framework What Was Built 1. Complete Memory Architecture - Short-term Memory: Stores conversation context - Long-term Memory: Stores learned facts and preferences - Episodic Memory: Stores experiences and outcomes 2. Core Features Implemented - Versioned Storage: Every memory operation creates a new version - Memory Branching: Create experimental branches without affecting main memory - Rollback Capability: Revert to any previous memory state - Audit Trail: Track all memory access and decisions 3. Demo Scenarios - Learning & Rollback: Shows how agents learn preferences and can rollback mistakes - Memory Branching: Demonstrates safe experimentation with different behaviors - Audit Trail: Shows decision tracking and memory access logging - Episodic Learning: Demonstrates learning from experiences - Interactive Chat: Full conversational agent with versioned memory 1. Setup: cd examples/rig_versioned_memory export OPENAI_API_KEY="your-key-here" cargo build 2. Run Demos: # Interactive chat (default) cargo run # Specific demos cargo run -- learning # Memory learning & rollback cargo run -- branching # Memory branching cargo run -- audit # Audit trail demo cargo run -- all # Run all demos 3. Interactive Commands: - /quit - Exit - /new - New conversation - /version - Show current version - /learn <concept> <fact> - Teach the agent * change ci build behavior * fix fmt * fix fmt issue * add examples to the top cargo file
1 parent f82f74b commit 26e9a24

17 files changed

Lines changed: 1410 additions & 2 deletions

File tree

.github/workflows/ci.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: CI
22

33
on:
44
pull_request:
5-
types: [ready_for_review]
5+
types: [ready_for_review, synchronize]
66
push:
77
branches:
88
- main
@@ -34,3 +34,19 @@ jobs:
3434

3535
- name: docs
3636
run: cargo doc --document-private-items --no-deps
37+
38+
- name: fmt rig_versioned_memory example
39+
run: cargo fmt --all -- --check
40+
working-directory: examples/rig_versioned_memory
41+
42+
- name: build rig_versioned_memory example
43+
run: cargo build --verbose
44+
working-directory: examples/rig_versioned_memory
45+
46+
- name: test rig_versioned_memory example
47+
run: cargo test --verbose
48+
working-directory: examples/rig_versioned_memory
49+
50+
- name: clippy rig_versioned_memory example
51+
run: cargo clippy
52+
working-directory: examples/rig_versioned_memory

.github/workflows/python.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ name: Build Python Package
22

33
on:
44
pull_request:
5-
types: [ready_for_review]
5+
types: [ready_for_review, synchronize]
6+
paths:
7+
- 'python/**'
68
push:
79
branches:
810
- main
11+
paths:
12+
- 'python/**'
913

1014
jobs:
1115
build-wheels:

Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,25 @@ required-features = ["sql"]
7777
name = "git_prolly_bench"
7878
harness = false
7979
required-features = ["git", "sql"]
80+
81+
[[example]]
82+
name = "proof_visualization"
83+
path = "examples/proof_visualization.rs"
84+
85+
[[example]]
86+
name = "git_sql"
87+
path = "examples/git_sql.rs"
88+
required-features = ["sql"]
89+
90+
[[example]]
91+
name = "git_diff"
92+
path = "examples/git_diff.rs"
93+
required-features = ["git"]
94+
95+
[[example]]
96+
name = "git_merge"
97+
path = "examples/git_merge.rs"
98+
required-features = ["git"]
99+
100+
[workspace]
101+
members = ["examples/rig_versioned_memory"]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# OpenAI API Key
2+
OPENAI_API_KEY=your-api-key-here
3+
4+
# Optional: Override default model
5+
# LLM_MODEL=gpt-4o-mini
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Environment variables
2+
.env
3+
4+
# Demo agent memory storage
5+
demo_agent_memory/
6+
7+
# Rust build artifacts
8+
target/
9+
Cargo.lock
10+
11+
# IDE files
12+
.vscode/
13+
.idea/
14+
15+
# OS files
16+
.DS_Store
17+
Thumbs.db
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "rig_versioned_memory"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
rig-core = "0.15"
8+
prollytree = { path = "../..", features = ["sql", "git"] }
9+
tokio = { version = "1.0", features = ["full"] }
10+
serde = { version = "1.0", features = ["derive"] }
11+
serde_json = "1.0"
12+
uuid = { version = "1.0", features = ["v4", "serde"] }
13+
chrono = { version = "0.4", features = ["serde"] }
14+
anyhow = "1.0"
15+
gluesql-core = "0.15"
16+
async-trait = "0.1"
17+
clap = { version = "4.0", features = ["derive"] }
18+
dotenv = "0.15"
19+
colored = "2.0"
20+
21+
[[bin]]
22+
name = "rig-memory-demo"
23+
path = "src/main.rs"
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# ProllyTree Versioned Memory for AI Agents
2+
3+
This example demonstrates how to use ProllyTree as a versioned memory backend for AI agents using the Rig framework. It showcases time-travel debugging, memory branching, and complete audit trails for reproducible AI behavior.
4+
5+
## Features
6+
7+
- **Versioned Memory**: Every interaction creates a new version, enabling rollback to any previous state
8+
- **Memory Types**: Short-term (conversation), long-term (facts), and episodic (experiences) memory
9+
- **Memory Branching**: Experiment with different agent behaviors without affecting the main memory
10+
- **Audit Trails**: Track every decision and memory access for debugging and compliance
11+
- **Rig Integration**: Seamless integration with Rig's LLM completion API
12+
13+
## Prerequisites
14+
15+
1. Rust (latest stable version)
16+
2. OpenAI API key
17+
3. ProllyTree library (included as local dependency)
18+
19+
## Setup
20+
21+
1. Set your OpenAI API key:
22+
```bash
23+
export OPENAI_API_KEY="your-api-key-here"
24+
```
25+
26+
Or create a `.env` file:
27+
```
28+
OPENAI_API_KEY=your-api-key-here
29+
```
30+
31+
2. Build the project:
32+
```bash
33+
cd examples/rig_versioned_memory
34+
cargo build
35+
```
36+
37+
## Running the Demo
38+
39+
### Interactive Chat Mode (Default)
40+
```bash
41+
cargo run
42+
```
43+
44+
### Custom Storage Location
45+
```bash
46+
# Use custom storage directory
47+
cargo run -- --storage ./my_agent_memory
48+
49+
# Use absolute path
50+
cargo run -- --storage /tmp/agent_data
51+
52+
# Short form
53+
cargo run -- -s ./custom_location
54+
```
55+
56+
### Specific Demos
57+
58+
1. **Memory Learning & Rollback**:
59+
```bash
60+
cargo run -- learning
61+
cargo run -- --storage ./custom_path learning
62+
```
63+
Shows how the agent learns preferences and can rollback to previous states.
64+
65+
2. **Memory Branching**:
66+
```bash
67+
cargo run -- branching
68+
```
69+
Demonstrates experimental memory branches for safe behavior testing.
70+
71+
3. **Audit Trail**:
72+
```bash
73+
cargo run -- audit
74+
```
75+
Shows decision tracking and memory access logging.
76+
77+
4. **Episodic Learning**:
78+
```bash
79+
cargo run -- episodic
80+
```
81+
Demonstrates learning from experiences and outcomes.
82+
83+
5. **Run All Demos**:
84+
```bash
85+
cargo run -- all
86+
```
87+
88+
## Interactive Mode Commands
89+
90+
- `/quit` - Exit interactive mode
91+
- `/new` - Start a new conversation (clears session memory)
92+
- `/version` - Show current memory version
93+
- `/learn <concept> <fact>` - Teach the agent a new fact
94+
95+
## Architecture
96+
97+
### Memory Types
98+
99+
1. **Short-term Memory**: Current conversation context
100+
- Stores user inputs and agent responses
101+
- Session-based storage
102+
- Used for maintaining conversation flow
103+
104+
2. **Long-term Memory**: Learned facts and preferences
105+
- Persistent across sessions
106+
- Concept-based organization
107+
- Access count tracking for relevance
108+
109+
3. **Episodic Memory**: Past experiences and outcomes
110+
- Records actions and their results
111+
- Includes reward signals for reinforcement
112+
- Used for learning from experience
113+
114+
### Key Components
115+
116+
- `VersionedMemoryStore`: Core storage backend using ProllyTree
117+
- `VersionedAgent`: Rig-based agent with memory integration
118+
- `Memory`: Data structure for storing memories with metadata
119+
- `MemoryContext`: Retrieved memories for context building
120+
121+
## Example Usage
122+
123+
```rust
124+
// Initialize agent with versioned memory
125+
let mut agent = VersionedAgent::new(api_key, "./agent_memory").await?;
126+
127+
// Process a message (automatically stores in memory)
128+
let (response, version) = agent.process_message("Hello!").await?;
129+
130+
// Learn a fact
131+
agent.learn_fact("user_preference", "Likes concise responses").await?;
132+
133+
// Create a memory branch for experimentation
134+
agent.create_memory_branch("experiment_1").await?;
135+
136+
// Rollback to a previous version
137+
agent.rollback_to_version(&version).await?;
138+
```
139+
140+
## Memory Storage
141+
142+
### Storage Location
143+
By default, the agent stores memory in `./demo_agent_memory/`. You can customize this with:
144+
```bash
145+
cargo run -- --storage /path/to/your/storage
146+
```
147+
148+
### Storage Structure
149+
The storage directory contains:
150+
- `.git/` - Git repository for version control
151+
- `.git-prolly/` - ProllyTree metadata and configuration
152+
- SQL database files with the following tables:
153+
- `short_term_memory`: Conversation history
154+
- `long_term_memory`: Learned facts and knowledge
155+
- `episodic_memory`: Experiences and outcomes
156+
- `memory_links`: Relationships between memories
157+
158+
### Storage Options
159+
- **Relative paths**: `./my_memory`, `../shared_memory`
160+
- **Absolute paths**: `/tmp/agent_data`, `/Users/name/agents/memory`
161+
- **Different agents**: Use different storage paths for separate agent instances
162+
163+
## Benefits
164+
165+
1. **Reproducibility**: Replay agent behavior from any historical state
166+
2. **Debugging**: Complete audit trail of decisions and memory access
167+
3. **Experimentation**: Safe testing with memory branches
168+
4. **Compliance**: Maintain required audit logs and data lineage
169+
5. **Learning**: Agents can learn and improve from experiences
170+
171+
## Future Enhancements
172+
173+
- Embedding-based semantic search
174+
- Distributed memory sharing between agents
175+
- Memory compression for old conversations
176+
- Advanced attention mechanisms for memory retrieval

0 commit comments

Comments
 (0)