a digital person for bluesky
at main 4.0 kB view raw
1""" 2Letta Multi-Agent Financial Analysis Team Example 3================================================ 4 5This example demonstrates how to create a team of autonomous AI agents that work together 6to analyze financial markets, each with their own specialized knowledge and shared data. 7 8Key Concepts: 9------------ 101. **Memory Blocks**: Letta agents use "memory blocks" to store and access information. 11 Think of these as persistent knowledge bases that agents can read and write to. 12 132. **Shared vs Private Memory**: 14 - Shared blocks (like market data) are attached to multiple agents 15 - Private blocks (like proprietary models) are only attached to specific agents 16 173. **Agent Communication**: Agents can send messages to each other using tools, 18 allowing them to share findings and coordinate analysis. 19 20Architecture: 21------------ 22- 3 Quant Agents: Each analyzes markets using different strategies 23 - Momentum Quant: Uses price momentum signals 24 - Value Quant: Uses fundamental value metrics 25 - ML Quant: Uses machine learning predictions 26 27- 1 Portfolio Manager: Receives and consolidates findings from all quants 28 29Memory Design: 30------------- 31- Global Shared Memory: "market-data" - All agents see the same market data 32- Local Private Memory: Each quant has their own model/strategy block 33- PM Aggregation Memory: "aggregated-signals" - Stores consolidated findings 34 35Workflow: 36-------- 371. All agents read shared market data 382. Each quant analyzes using their unique approach 393. Quants send findings to the Portfolio Manager 404. PM aggregates insights and produces final report 41""" 42 43from letta_client import Letta 44import os 45 46client = Letta(token=os.environ["LETTA_API_KEY"]) 47project_id = "your-project-id" 48 49# 1. Create shared memory blocks (global data) 50market_data = client.blocks.create( 51 project_id=project_id, 52 label="market-data", 53 value="S&P500: 4800, VIX: 15.2, DXY: 102.5...", 54 description="Shared financial market data" 55) 56 57# 2. Create individual quant memory blocks 58quant1_model = client.blocks.create( 59 project_id=project_id, 60 label="quant1-momentum-model", 61 value="Momentum factor model: 12-month return signals...", 62 description="Quant 1's proprietary momentum model" 63) 64 65quant2_model = client.blocks.create( 66 project_id=project_id, 67 label="quant2-value-model", 68 value="Value factor model: P/E ratios, book values...", 69 description="Quant 2's value investing model" 70) 71 72quant3_model = client.blocks.create( 73 project_id=project_id, 74 label="quant3-ml-model", 75 value="LSTM predictions, feature importance weights...", 76 description="Quant 3's ML model" 77) 78 79# 3. Create agents with mixed memory (shared + individual) 80quant1 = client.agents.create( 81 project_id=project_id, 82 name="quant-momentum", 83 block_ids=[market_data.id, quant1_model.id] # Shared + Individual 84) 85 86quant2 = client.agents.create( 87 project_id=project_id, 88 name="quant-value", 89 block_ids=[market_data.id, quant2_model.id] 90) 91 92quant3 = client.agents.create( 93 project_id=project_id, 94 name="quant-ml", 95 block_ids=[market_data.id, quant3_model.id] 96) 97 98# 4. Create portfolio manager with access to all findings 99pm_findings = client.blocks.create( 100 project_id=project_id, 101 label="aggregated-signals", 102 value="", # Will be populated by quant reports 103 description="Consolidated findings from all quants" 104) 105 106portfolio_manager = client.agents.create( 107 project_id=project_id, 108 name="portfolio-manager", 109 block_ids=[market_data.id, pm_findings.id], 110 tags=["pm"] # Tag for message routing 111) 112 113# 5. Give quants ability to send findings to PM 114send_to_pm_tool = client.tools.list(name="send_message_to_agents_matching_tags")[0] 115for quant in [quant1, quant2, quant3]: 116 client.agents.tools.attach(agent_id=quant.id, tool_id=send_to_pm_tool.id) 117 118# 6. Run analysis 119for quant in [quant1, quant2, quant3]: 120 response = client.agents.messages.create( 121 agent_id=quant.id, 122 messages=[{"role": "user", "content": "Analyze market and send findings to PM (tag: pm)"}] 123 )