|
| 1 | +""" |
| 2 | +Multi-Memory Aggregation Example |
| 3 | +
|
| 4 | +Demonstrates concurrent fetching from multiple context sources. |
| 5 | +Inspired by CrewAI's ContextualMemory pattern. |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | +from praisonaiagents.context import ContextAggregator, create_aggregator_from_config |
| 10 | + |
| 11 | +# Example 1: Basic aggregator with mock sources |
| 12 | +def mock_memory_search(query: str) -> str: |
| 13 | + """Mock memory search - returns recent conversation context.""" |
| 14 | + return f"[Memory] User previously asked about: {query}. They prefer Python." |
| 15 | + |
| 16 | +def mock_knowledge_search(query: str) -> str: |
| 17 | + """Mock knowledge search - returns facts from knowledge base.""" |
| 18 | + return f"[Knowledge] Relevant facts: Python is a high-level programming language." |
| 19 | + |
| 20 | +def mock_rag_retrieve(query: str) -> str: |
| 21 | + """Mock RAG retrieval - returns document chunks.""" |
| 22 | + return f"[RAG] Retrieved: Documentation about {query} from the codebase." |
| 23 | + |
| 24 | +# Create aggregator |
| 25 | +aggregator = ContextAggregator( |
| 26 | + max_tokens=4000, |
| 27 | + separator="\n\n---\n\n", |
| 28 | + include_source_labels=True |
| 29 | +) |
| 30 | + |
| 31 | +# Register sources with priorities (lower = higher priority) |
| 32 | +aggregator.register_source("memory", mock_memory_search, priority=10) |
| 33 | +aggregator.register_source("knowledge", mock_knowledge_search, priority=20) |
| 34 | +aggregator.register_source("rag", mock_rag_retrieve, priority=30) |
| 35 | + |
| 36 | +async def demo_async(): |
| 37 | + """Demo async aggregation.""" |
| 38 | + print("=== Async Aggregation ===") |
| 39 | + result = await aggregator.aggregate("Python web development") |
| 40 | + print(f"Sources used: {result.sources_used}") |
| 41 | + print(f"Tokens used: {result.tokens_used}") |
| 42 | + print(f"Fetch times: {result.fetch_times}") |
| 43 | + print() |
| 44 | + print("Context:") |
| 45 | + print(result.context) |
| 46 | + return result |
| 47 | + |
| 48 | +def demo_sync(): |
| 49 | + """Demo sync aggregation.""" |
| 50 | + print("=== Sync Aggregation ===") |
| 51 | + result = aggregator.aggregate_sync("Flask API development") |
| 52 | + print(f"Sources used: {result.sources_used}") |
| 53 | + print(f"Tokens used: {result.tokens_used}") |
| 54 | + print() |
| 55 | + print("Context:") |
| 56 | + print(result.context) |
| 57 | + return result |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + print("=== Multi-Memory Aggregation Example ===") |
| 61 | + print() |
| 62 | + |
| 63 | + # Show registered sources |
| 64 | + print(f"Registered sources: {aggregator.sources}") |
| 65 | + print() |
| 66 | + |
| 67 | + # Demo sync aggregation |
| 68 | + demo_sync() |
| 69 | + print() |
| 70 | + |
| 71 | + # Demo async aggregation |
| 72 | + asyncio.run(demo_async()) |
| 73 | + print() |
| 74 | + |
| 75 | + # Demo selective sources |
| 76 | + print("=== Selective Sources ===") |
| 77 | + result = aggregator.aggregate_sync( |
| 78 | + "Find database info", |
| 79 | + sources=["memory", "knowledge"] # Only use these |
| 80 | + ) |
| 81 | + print(f"Sources used: {result.sources_used}") |
0 commit comments