33import uuid
44from contextlib import redirect_stderr , redirect_stdout
55from datetime import datetime
6+ from enum import Enum
67from typing import Any , Dict , List , Optional , TypedDict , cast
78
8- from autogen_core import CancellationToken , Component , ComponentBase
9+ from autogen_core import CancellationToken , Component , ComponentBase , FunctionCall
910from autogen_core .memory import Memory , MemoryContent , MemoryQueryResult , UpdateContextResult
1011from autogen_core .model_context import ChatCompletionContext
11- from autogen_core .models import SystemMessage
12+ from autogen_core .models import AssistantMessage , FunctionExecutionResult , FunctionExecutionResultMessage , SystemMessage
1213from mem0 import Memory as Memory0
1314from mem0 import MemoryClient
1415from pydantic import BaseModel , Field
1819logging .getLogger ("chromadb" ).setLevel (logging .ERROR )
1920
2021
22+ class ContextInjectionMode (Enum ):
23+ """Enum for context injection modes."""
24+
25+ SYSTEM_MESSAGE = "system_message"
26+ FUNCTION_CALL = "function_call"
27+
28+
2129class Mem0MemoryConfig (BaseModel ):
2230 """Configuration for Mem0Memory component."""
2331
@@ -32,6 +40,10 @@ class Mem0MemoryConfig(BaseModel):
3240 config : Optional [Dict [str , Any ]] = Field (
3341 default = None , description = "Configuration dictionary for local Mem0 client. Required if is_cloud=False."
3442 )
43+ context_injection_mode : ContextInjectionMode = Field (
44+ default = ContextInjectionMode .SYSTEM_MESSAGE ,
45+ description = "Mode for injecting memories into context: 'system_message' or 'function_call'." ,
46+ )
3547
3648
3749class MemoryResult (TypedDict , total = False ):
@@ -68,15 +80,16 @@ class Mem0Memory(Memory, Component[Mem0MemoryConfig], ComponentBase[Mem0MemoryCo
6880 .. code-block:: python
6981
7082 import asyncio
71- from autogen_ext.memory.mem0 import Mem0Memory
83+ from autogen_ext.memory.mem0 import Mem0Memory, ContextInjectionMode
7284 from autogen_core.memory import MemoryContent
7385
7486
7587 async def main() -> None:
76- # Create a local Mem0Memory (no API key required)
88+ # Create a local Mem0Memory with function call injection mode
7789 memory = Mem0Memory(
7890 is_cloud=False,
7991 config={"path": ":memory:"}, # Use in-memory storage for testing
92+ context_injection_mode=ContextInjectionMode.FUNCTION_CALL,
8093 )
8194 print("Memory initialized successfully!")
8295
@@ -111,19 +124,20 @@ async def main() -> None:
111124 import asyncio
112125 from autogen_agentchat.agents import AssistantAgent
113126 from autogen_core.memory import MemoryContent
114- from autogen_ext.memory.mem0 import Mem0Memory
127+ from autogen_ext.memory.mem0 import Mem0Memory, ContextInjectionMode
115128 from autogen_ext.models.openai import OpenAIChatCompletionClient
116129
117130
118131 async def main() -> None:
119132 # Create a model client
120133 model_client = OpenAIChatCompletionClient(model="gpt-4.1")
121134
122- # Create a Mem0 memory instance
135+ # Create a Mem0 memory instance with system message injection (default)
123136 memory = Mem0Memory(
124137 user_id="user123",
125138 is_cloud=False,
126139 config={"path": ":memory:"}, # Use in-memory storage for testing
140+ context_injection_mode=ContextInjectionMode.SYSTEM_MESSAGE,
127141 )
128142
129143 # Add something to memory
@@ -157,6 +171,7 @@ async def main() -> None:
157171 is_cloud: Whether to use cloud Mem0 client (True) or local client (False).
158172 api_key: API key for cloud Mem0 client. It will read from the environment MEM0_API_KEY if not provided.
159173 config: Configuration dictionary for local Mem0 client. Required if is_cloud=False.
174+ context_injection_mode: Mode for injecting memories into context ('system_message' or 'function_call').
160175 """
161176
162177 component_type = "memory"
@@ -170,6 +185,7 @@ def __init__(
170185 is_cloud : bool = True ,
171186 api_key : Optional [str ] = None ,
172187 config : Optional [Dict [str , Any ]] = None ,
188+ context_injection_mode : ContextInjectionMode = ContextInjectionMode .SYSTEM_MESSAGE ,
173189 ) -> None :
174190 # Validate parameters
175191 if not is_cloud and config is None :
@@ -181,6 +197,7 @@ def __init__(
181197 self ._is_cloud = is_cloud
182198 self ._api_key = api_key
183199 self ._config = config
200+ self ._context_injection_mode = context_injection_mode
184201
185202 # Initialize client
186203 if self ._is_cloud :
@@ -210,6 +227,11 @@ def config(self) -> Optional[Dict[str, Any]]:
210227 """Get the configuration for the Mem0 client."""
211228 return self ._config
212229
230+ @property
231+ def context_injection_mode (self ) -> ContextInjectionMode :
232+ """Get the context injection mode."""
233+ return self ._context_injection_mode
234+
213235 async def add (
214236 self ,
215237 content : MemoryContent ,
@@ -366,7 +388,8 @@ async def update_context(
366388
367389 This method retrieves the conversation history from the model context,
368390 uses the last message as a query to find relevant memories, and then
369- adds those memories to the context as a system message.
391+ adds those memories to the context either as a system message or as
392+ function call messages based on the configured injection mode.
370393
371394 Args:
372395 model_context: The model context to update.
@@ -392,8 +415,40 @@ async def update_context(
392415 memory_strings = [f"{ i } . { str (memory .content )} " for i , memory in enumerate (query_results .results , 1 )]
393416 memory_context = "\n Relevant memories:\n " + "\n " .join (memory_strings )
394417
395- # Add as system message
396- await model_context .add_message (SystemMessage (content = memory_context ))
418+ if self ._context_injection_mode == ContextInjectionMode .SYSTEM_MESSAGE :
419+ # Add as system message (original behavior)
420+ await model_context .add_message (SystemMessage (content = memory_context ))
421+
422+ elif self ._context_injection_mode == ContextInjectionMode .FUNCTION_CALL :
423+ # Add as function call result messages
424+ # Generate a unique call ID
425+ call_id = f"call_{ uuid .uuid4 ().hex [:20 ]} "
426+
427+ # Create the function call
428+ function_call = FunctionCall (
429+ id = call_id ,
430+ name = "retrieve_mem0memory" ,
431+ arguments = "{}" , # No parameters as specified
432+ )
433+
434+ # Create AssistantMessage with the function call
435+ assistant_message = AssistantMessage (
436+ content = [function_call ], source = "memory_system" , type = "AssistantMessage"
437+ )
438+
439+ # Create the function execution result
440+ function_result = FunctionExecutionResult (
441+ content = memory_context , name = "retrieve_mem0memory" , call_id = call_id , is_error = False
442+ )
443+
444+ # Create FunctionExecutionResultMessage
445+ result_message = FunctionExecutionResultMessage (
446+ content = [function_result ], type = "FunctionExecutionResultMessage"
447+ )
448+
449+ # Add both messages to the context
450+ await model_context .add_message (assistant_message )
451+ await model_context .add_message (result_message )
397452
398453 return UpdateContextResult (memories = query_results )
399454
@@ -432,6 +487,7 @@ def _from_config(cls, config: Mem0MemoryConfig) -> Self:
432487 is_cloud = config .is_cloud ,
433488 api_key = config .api_key ,
434489 config = config .config ,
490+ context_injection_mode = config .context_injection_mode ,
435491 )
436492
437493 def _to_config (self ) -> Mem0MemoryConfig :
@@ -446,4 +502,5 @@ def _to_config(self) -> Mem0MemoryConfig:
446502 is_cloud = self ._is_cloud ,
447503 api_key = self ._api_key ,
448504 config = self ._config ,
505+ context_injection_mode = self ._context_injection_mode ,
449506 )
0 commit comments