|
| 1 | +""" |
| 2 | +Mem0 toolkit demo using SpoonReactAI. |
| 3 | +This demo requires spoon-toolkit to be installed |
| 4 | +""" |
| 5 | + |
| 6 | +import asyncio |
| 7 | +from typing import Any, Dict, List |
| 8 | + |
| 9 | +from pydantic import Field |
| 10 | + |
| 11 | +from spoon_ai.agents.spoon_react import SpoonReactAI |
| 12 | +from spoon_ai.chat import ChatBot |
| 13 | +from spoon_ai.tools.tool_manager import ToolManager |
| 14 | +from spoon_ai.tools.base import ToolResult |
| 15 | +from spoon_toolkits.memory import AddMemoryTool, SearchMemoryTool, GetAllMemoryTool |
| 16 | + |
| 17 | + |
| 18 | +USER_ID = "defi_user_002" |
| 19 | + |
| 20 | + |
| 21 | +class DeFiMemoryAgent(SpoonReactAI): |
| 22 | + """Brain from spoon-core + memory tools from spoon-toolkit.""" |
| 23 | + |
| 24 | + mem0_config: Dict[str, Any] = Field(default_factory=dict) |
| 25 | + available_tools: ToolManager = Field(default_factory=lambda: ToolManager([])) |
| 26 | + |
| 27 | + def model_post_init(self, __context: Any = None) -> None: |
| 28 | + super().model_post_init(__context) |
| 29 | + # Rebuild tools with the injected mem0_config for this agent |
| 30 | + memory_tools = [ |
| 31 | + AddMemoryTool(mem0_config=self.mem0_config), |
| 32 | + SearchMemoryTool(mem0_config=self.mem0_config), |
| 33 | + GetAllMemoryTool(mem0_config=self.mem0_config), |
| 34 | + ] |
| 35 | + self.available_tools = ToolManager(memory_tools) |
| 36 | + # Refresh prompts so SpoonReactAI lists the newly provided tools |
| 37 | + if hasattr(self, "_refresh_prompts"): |
| 38 | + self._refresh_prompts() |
| 39 | + |
| 40 | + |
| 41 | +def build_agent(mem0_cfg: Dict[str, Any]) -> DeFiMemoryAgent: |
| 42 | + return DeFiMemoryAgent( |
| 43 | + llm=ChatBot( |
| 44 | + llm_provider="openrouter", |
| 45 | + base_url="https://openrouter.ai/api/v1", |
| 46 | + model_name="anthropic/claude-3.5-sonnet", |
| 47 | + enable_long_term_memory=False, # memory comes from toolkit tools instead |
| 48 | + ), |
| 49 | + mem0_config=mem0_cfg, |
| 50 | + system_prompt=( |
| 51 | + "You are a DeFi investment advisor. Use the provided Mem0 tools to recall " |
| 52 | + "and update user preferences before answering." |
| 53 | + ), |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def print_memories(result: ToolResult, label: str) -> None: |
| 58 | + if not isinstance(result, ToolResult): |
| 59 | + print(f"[Mem0] {label}: error -> {result}") |
| 60 | + return |
| 61 | + memories: List[str] = result.output.get("memories", []) if result and result.output else [] |
| 62 | + print(f"[Mem0] {label}:") |
| 63 | + for m in memories: |
| 64 | + print(f" - {m}") |
| 65 | + |
| 66 | + |
| 67 | +async def phase_capture(agent: DeFiMemoryAgent) -> None: |
| 68 | + print("\n=== Phase 1: Capture high-risk Solana preferences ===") |
| 69 | + await agent.available_tools.execute( |
| 70 | + name="add_memory", |
| 71 | + tool_input={ |
| 72 | + "messages": [ |
| 73 | + { |
| 74 | + "role": "user", |
| 75 | + "content": ( |
| 76 | + "I am a high-risk degen trader. I exclusively trade meme coins on Solana " |
| 77 | + "and dislike Ethereum gas fees." |
| 78 | + ), |
| 79 | + } |
| 80 | + ] |
| 81 | + }, |
| 82 | + ) |
| 83 | + memories = await agent.available_tools.execute( |
| 84 | + name="search_memory", |
| 85 | + tool_input={"query": "Solana meme coins high risk"}, |
| 86 | + ) |
| 87 | + print_memories(memories, "After Phase 1 store") |
| 88 | + |
| 89 | + |
| 90 | +async def phase_recall(mem0_cfg: Dict[str, Any]) -> None: |
| 91 | + print("\n=== Phase 2: Recall with a fresh agent instance ===") |
| 92 | + agent = build_agent(mem0_cfg) |
| 93 | + memories = await agent.available_tools.execute( |
| 94 | + name="search_memory", |
| 95 | + tool_input={"query": "trading strategy solana meme"}, |
| 96 | + ) |
| 97 | + print_memories(memories, "Retrieved for Phase 2") |
| 98 | + |
| 99 | + |
| 100 | +async def phase_update(agent: DeFiMemoryAgent) -> None: |
| 101 | + print("\n=== Phase 3: Update preferences to safer Arbitrum yield ===") |
| 102 | + await agent.available_tools.execute( |
| 103 | + name="add_memory", |
| 104 | + tool_input={ |
| 105 | + "messages": [ |
| 106 | + { |
| 107 | + "role": "user", |
| 108 | + "content": ( |
| 109 | + "I lost too much money. I want to pivot to safe stablecoin yield farming on Arbitrum now." |
| 110 | + ), |
| 111 | + } |
| 112 | + ] |
| 113 | + }, |
| 114 | + ) |
| 115 | + memories = await agent.available_tools.execute( |
| 116 | + name="search_memory", |
| 117 | + tool_input={"query": "stablecoin yield chain choice"}, |
| 118 | + ) |
| 119 | + print_memories(memories, "Retrieved after update (Phase 3)") |
| 120 | + |
| 121 | + |
| 122 | +async def main() -> None: |
| 123 | + mem0_cfg = { |
| 124 | + "user_id": USER_ID, |
| 125 | + "metadata": {"project": "defi-investment-advisor"}, |
| 126 | + "async_mode": False, # synchronous writes so the next search sees new data |
| 127 | + } |
| 128 | + agent = build_agent(mem0_cfg) |
| 129 | + await phase_capture(agent) |
| 130 | + await phase_recall(mem0_cfg) |
| 131 | + await phase_update(agent) |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + asyncio.run(main()) |
0 commit comments