Features: - Multi-platform bot (Slack, Telegram) - Memory system with SQLite FTS - Tool use capabilities (file ops, commands) - Scheduled tasks system - Dynamic model switching (/sonnet, /haiku) - Prompt caching for cost optimization Optimizations: - Default to Haiku 4.5 (12x cheaper) - Reduced context: 3 messages, 2 memory results - Optimized SOUL.md (48% smaller) - Automatic caching when using Sonnet (90% savings) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
197 lines
5.1 KiB
Python
197 lines
5.1 KiB
Python
"""Example: Using the Memory System with SOUL and User files."""
|
|
|
|
from memory_system import MemorySystem
|
|
|
|
|
|
def main() -> None:
|
|
print("=" * 60)
|
|
print("Memory System - SOUL + User Files Example")
|
|
print("=" * 60)
|
|
|
|
memory = MemorySystem()
|
|
|
|
# 1. SOUL - Define agent personality
|
|
print("\n[1] Updating SOUL (Agent Personality)...")
|
|
memory.update_soul(
|
|
"""
|
|
## Additional Traits
|
|
- I remember user preferences and adapt
|
|
- I maintain context across conversations
|
|
- I learn from corrections and feedback
|
|
|
|
## Goals
|
|
- Help users be more productive
|
|
- Provide accurate, helpful information
|
|
- Build long-term relationships through memory
|
|
""",
|
|
append=True,
|
|
)
|
|
|
|
# 2. Create user profiles
|
|
print("\n[2] Creating user profiles...")
|
|
|
|
memory.update_user(
|
|
"alice",
|
|
"""
|
|
## Personal Info
|
|
- Name: Alice Johnson
|
|
- Role: Senior Python Developer
|
|
- Timezone: America/New_York (EST)
|
|
- Active hours: 9 AM - 6 PM EST
|
|
|
|
## Preferences
|
|
- Communication: Detailed technical explanations
|
|
- Code style: PEP 8, type hints, docstrings
|
|
- Favorite tools: VS Code, pytest, black
|
|
|
|
## Current Projects
|
|
- Building a microservices architecture
|
|
- Learning Kubernetes
|
|
- Migrating legacy Django app
|
|
|
|
## Recent Conversations
|
|
- 2026-02-12: Discussed SQLite full-text search implementation
|
|
- 2026-02-12: Asked about memory system design patterns
|
|
""",
|
|
)
|
|
|
|
memory.update_user(
|
|
"bob",
|
|
"""
|
|
## Personal Info
|
|
- Name: Bob Smith
|
|
- Role: Frontend Developer
|
|
- Timezone: America/Los_Angeles (PST)
|
|
- Active hours: 11 AM - 8 PM PST
|
|
|
|
## Preferences
|
|
- Communication: Concise, bullet points
|
|
- Code style: ESLint, Prettier, React best practices
|
|
- Favorite tools: WebStorm, Vite, TailwindCSS
|
|
|
|
## Current Projects
|
|
- React dashboard redesign
|
|
- Learning TypeScript
|
|
- Performance optimization work
|
|
|
|
## Recent Conversations
|
|
- 2026-02-11: Asked about React optimization techniques
|
|
- 2026-02-12: Discussed Vite configuration
|
|
""",
|
|
)
|
|
|
|
# 3. Add long-term memory
|
|
print("\n[3] Adding long-term memory...")
|
|
memory.write_memory(
|
|
"""
|
|
# System Architecture Decisions
|
|
|
|
## Memory System Design
|
|
- **Date**: 2026-02-12
|
|
- **Decision**: Use SQLite + Markdown for memory
|
|
- **Rationale**: Simple, fast, no external dependencies
|
|
- **Files**: SOUL.md for personality, users/*.md for user context
|
|
|
|
## Search Strategy
|
|
- FTS5 for keyword search (fast, built-in)
|
|
- No vector embeddings (keep it simple)
|
|
- Per-user search capability for privacy
|
|
""",
|
|
daily=False,
|
|
)
|
|
|
|
# 4. Add daily log
|
|
print("\n[4] Adding today's notes...")
|
|
memory.write_memory(
|
|
"""
|
|
## Conversations
|
|
|
|
### Alice (10:30 AM)
|
|
- Discussed memory system implementation
|
|
- Showed interest in SQLite FTS5 features
|
|
- Plans to integrate into her microservices project
|
|
|
|
### Bob (2:45 PM)
|
|
- Quick question about React performance
|
|
- Mentioned working late tonight on dashboard
|
|
- Prefers short, actionable answers
|
|
""",
|
|
daily=True,
|
|
)
|
|
|
|
# 5. Perform searches
|
|
print("\n[5] Searching memory...")
|
|
|
|
print("\n -> Global search for 'python':")
|
|
results = memory.search("python", max_results=3)
|
|
for r in results:
|
|
print(f" {r['path']}:{r['start_line']} - {r['snippet']}")
|
|
|
|
print("\n -> Alice's memory for 'project':")
|
|
alice_results = memory.search_user(
|
|
"alice", "project", max_results=2
|
|
)
|
|
for r in alice_results:
|
|
print(f" {r['path']}:{r['start_line']} - {r['snippet']}")
|
|
|
|
print("\n -> Bob's memory for 'React':")
|
|
bob_results = memory.search_user("bob", "React", max_results=2)
|
|
for r in bob_results:
|
|
print(f" {r['path']}:{r['start_line']} - {r['snippet']}")
|
|
|
|
# 6. Retrieve specific content
|
|
print("\n[6] Retrieving specific content...")
|
|
|
|
soul = memory.get_soul()
|
|
print(f"\n SOUL.md ({len(soul)} chars):")
|
|
print(" " + "\n ".join(soul.split("\n")[:5]))
|
|
print(" ...")
|
|
|
|
alice_context = memory.get_user("alice")
|
|
print(f"\n Alice's profile ({len(alice_context)} chars):")
|
|
print(" " + "\n ".join(alice_context.split("\n")[:5]))
|
|
print(" ...")
|
|
|
|
# 7. Show system status
|
|
print("\n[7] System Status:")
|
|
status = memory.status()
|
|
for key, value in status.items():
|
|
print(f" {key}: {value}")
|
|
|
|
print(f"\n Users: {', '.join(memory.list_users())}")
|
|
|
|
# 8. Demonstrate contextual response
|
|
print("\n" + "=" * 60)
|
|
print("CONTEXTUAL RESPONSE EXAMPLE")
|
|
print("=" * 60)
|
|
|
|
def get_context_for_user(username: str) -> str:
|
|
"""Build context for an AI response."""
|
|
user_soul = memory.get_soul()
|
|
user_prefs = memory.get_user(username)
|
|
recent_memory = memory.search_user(
|
|
username, "recent", max_results=2
|
|
)
|
|
|
|
recent_snippet = (
|
|
recent_memory[0]["snippet"]
|
|
if recent_memory
|
|
else "No recent activity"
|
|
)
|
|
|
|
return (
|
|
f"\n=== SOUL ===\n{user_soul[:200]}...\n\n"
|
|
f"=== USER: {username} ===\n{user_prefs[:200]}...\n\n"
|
|
f"=== RECENT CONTEXT ===\n{recent_snippet}\n"
|
|
)
|
|
|
|
print("\nContext for Alice:")
|
|
print(get_context_for_user("alice"))
|
|
|
|
memory.close()
|
|
print("\nMemory system closed")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|