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