-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathlist_memory_example.py
More file actions
136 lines (107 loc) · 4.11 KB
/
list_memory_example.py
File metadata and controls
136 lines (107 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
Example: Application-managed memory with ListMemory.
Demonstrates how applications control memory storage and retrieval,
automatically injecting relevant context into agent prompts.
The developer calls memory.add() to store information, and the framework
automatically retrieves and injects context via memory.get_context().
The agent receives this context but does not control what gets stored.
"""
import asyncio
from picoagents import Agent
from picoagents.llm import AzureOpenAIChatCompletionClient
from picoagents.memory import ListMemory, MemoryContent
async def demo_list_memory():
"""
Demonstrate application-managed memory.
The application stores user preferences in memory, and the framework
automatically injects this context into the agent's prompts.
"""
# Initialize model client
client = AzureOpenAIChatCompletionClient(model="gpt-4.1-mini")
# Create memory - application manages storage
memory = ListMemory(max_memories=100)
# Application stores user preferences
await memory.add(
MemoryContent(
content="User prefers concise responses without verbose explanations",
mime_type="text/plain",
)
)
await memory.add(
MemoryContent(
content="User works as a Python developer, familiar with async/await",
mime_type="text/plain",
)
)
await memory.add(
MemoryContent(
content="User's timezone is PST (UTC-8)",
mime_type="text/plain",
)
)
# Create agent with memory - framework handles context injection
agent = Agent(
name="assistant",
description="Helpful programming assistant",
instructions="""You are a helpful programming assistant.
Use any relevant context from memory to personalize your responses.""",
model_client=client,
memory=memory,
)
print("=" * 60)
print("APPLICATION-MANAGED MEMORY DEMO")
print("=" * 60)
print()
print(f"Stored {len(memory.memories)} preferences in memory")
print()
# Agent automatically receives memory context
task = "Explain how to use asyncio.gather()"
print(f"User: {task}")
print()
response = await agent.run(task)
print(f"{agent.name}: {response.messages[-1].content}")
print()
print("=" * 60)
print("Notice how the agent's response reflects the stored preferences:")
print("- Concise (no verbose explanations)")
print("- Assumes Python/async knowledge")
print("- The agent did NOT call any memory tools")
print("- The framework automatically injected context")
print("=" * 60)
async def demo_memory_query():
"""Demonstrate querying memory for relevant context."""
print("\n" + "=" * 60)
print("MEMORY QUERY DEMO")
print("=" * 60)
print()
client = AzureOpenAIChatCompletionClient(model="gpt-4.1-mini")
memory = ListMemory(max_memories=100)
# Store various facts
facts = [
"Project deadline is March 15th",
"Team meeting every Monday at 10am PST",
"Code review checklist: tests, docs, type hints",
"Production deployment happens on Fridays",
"Database backup runs at 2am daily",
]
for fact in facts:
await memory.add(MemoryContent(content=fact))
print(f"Stored {len(memory.memories)} facts in memory")
print()
# Query for relevant memories
query = "when is the deadline"
results = await memory.query(query, limit=2)
print(f"Query: '{query}'")
print(f"Found {len(results.results)} relevant memories:")
for i, result in enumerate(results.results, 1):
print(f" {i}. {result.content}")
print()
# The framework uses similar logic internally when preparing prompts
print("(The framework uses similar querying when injecting context)")
if __name__ == "__main__":
print("📝 PicoAgents Application-Managed Memory Examples\n")
asyncio.run(demo_list_memory())
asyncio.run(demo_memory_query())
print("\n✨ Examples completed!")
print("\nNext: Try ChromaDB for semantic search with vector embeddings")
print("Install: pip install 'picoagents[rag]'")