Skip to content

A programming framework for agentic AI

Memory component base (#5380)

Eitan Yarmush172a16a6150d
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py
index 4d3aecf72..b3dda7175 100644
--- a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py
+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py
@@ -62,6 +62,7 @@ class AssistantAgentConfig(BaseModel):
tools: List[ComponentModel] | None
handoffs: List[HandoffBase | str] | None = None
model_context: ComponentModel | None = None
+ memory: List[ComponentModel] | None = None
description: str
system_message: str | None = None
model_client_stream: bool = False
@@ -591,6 +592,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):
tools=[tool.dump_component() for tool in self._tools],
handoffs=list(self._handoffs.values()),
model_context=self._model_context.dump_component(),
+ memory=[memory.dump_component() for memory in self._memory] if self._memory else None,
description=self.description,
system_message=self._system_messages[0].content
if self._system_messages and isinstance(self._system_messages[0].content, str)
@@ -609,6 +611,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):
tools=[BaseTool.load_component(tool) for tool in config.tools] if config.tools else None,
handoffs=config.handoffs,
model_context=None,
+ memory=[Memory.load_component(memory) for memory in config.memory] if config.memory else None,
description=config.description,
system_message=config.system_message,
model_client_stream=config.model_client_stream,
diff --git a/python/packages/autogen-agentchat/tests/test_assistant_agent.py b/python/packages/autogen-agentchat/tests/test_assistant_agent.py
index 3db992436..040f3e23e 100644
--- a/python/packages/autogen-agentchat/tests/test_assistant_agent.py
+++ b/python/packages/autogen-agentchat/tests/test_assistant_agent.py
@@ -18,7 +18,7 @@ from autogen_agentchat.messages import (
ToolCallRequestEvent,
ToolCallSummaryMessage,
)
-from autogen_core import FunctionCall, Image
+from autogen_core import ComponentModel, FunctionCall, Image
from autogen_core.memory import ListMemory, Memory, MemoryContent, MemoryMimeType, MemoryQueryResult
from autogen_core.model_context import BufferedChatCompletionContext
from autogen_core.models import (
@@ -754,7 +754,12 @@ async def test_run_with_memory(monkeypatch: pytest.MonkeyPatch) -> None:
"test_agent", model_client=OpenAIChatCompletionClient(model=model, api_key=""), memory=[memory2]
)
- result = await agent.run(task="test task")
+ # Test dump and load component with memory
+ agent_config: ComponentModel = agent.dump_component()
+ assert agent_config.provider == "autogen_agentchat.agents.AssistantAgent"
+ agent2 = AssistantAgent.load_component(agent_config)
+
+ result = await agent2.run(task="test task")
assert len(result.messages) > 0
memory_event = next((msg for msg in result.messages if isinstance(msg, MemoryQueryEvent)), None)
assert memory_event is not None
@@ -795,9 +800,10 @@ async def test_assistant_agent_declarative(monkeypatch: pytest.MonkeyPatch) -> N
"test_agent",
model_client=OpenAIChatCompletionClient(model=model, api_key=""),
model_context=model_context,
+ memory=[ListMemory(name="test_memory")],
)
- agent_config = agent.dump_component()
+ agent_config: ComponentModel = agent.dump_component()
assert agent_config.provider == "autogen_agentchat.agents.AssistantAgent"
agent2 = AssistantAgent.load_component(agent_config)
diff --git a/python/packages/autogen-core/src/autogen_core/memory/_base_memory.py b/python/packages/autogen-core/src/autogen_core/memory/_base_memory.py
index 2ae79b410..577c23fda 100644
--- a/python/packages/autogen-core/src/autogen_core/memory/_base_memory.py
+++ b/python/packages/autogen-core/src/autogen_core/memory/_base_memory.py
@@ -5,6 +5,7 @@ from typing import Any, Dict, List, Union
from pydantic import BaseModel, ConfigDict
from .._cancellation_token import CancellationToken
+from .._component_config import ComponentBase
from .._image import Image
from ..model_context import ChatCompletionContext
@@ -49,7 +50,7 @@ class UpdateContextResult(BaseModel):
memories: MemoryQueryResult
-class Memory(ABC):
+class Memory(ABC, ComponentBase[BaseModel]):
"""Protocol defining the interface for memory implementations.
A memory is the storage for data that can be used to enrich or modify the model context.
@@ -64,6 +65,8 @@ class Memory(ABC):
See :class:`~autogen_core.memory.ListMemory` for an example implementation.
"""
+ component_type = "memory"
+
@abstractmethod
async def update_context(
self,
diff --git a/python/packages/autogen-core/src/autogen_core/memory/_list_memory.py b/python/packages/autogen-core/src/autogen_core/memory/_list_memory.py
index f28bab048..5ad086550 100644
--- a/python/packages/autogen-core/src/autogen_core/memory/_list_memory.py
+++ b/python/packages/autogen-core/src/autogen_core/memory/_list_memory.py
@@ -1,12 +1,25 @@
from typing import Any, List
+from pydantic import BaseModel
+from typing_extensions import Self
+
from .._cancellation_token import CancellationToken
+from .._component_config import Component
from ..model_context import ChatCompletionContext
from ..models import SystemMessage
from ._base_memory import Memory, MemoryContent, MemoryQueryResult, UpdateContextResult
-class ListMemory(Memory):
+class ListMemoryConfig(BaseModel):
+ """Configuration for ListMemory component."""
+
+ name: str | None = None
+ """Optional identifier for this memory instance."""
+ memory_contents: List[MemoryContent] = []
+ """List of memory contents stored in this memory instance."""
+
+
+class ListMemory(Memory, Component[ListMemoryConfig]):
"""Simple chronological list-based memory implementation.
This memory implementation stores contents in a list and retrieves them in
@@ -53,9 +66,13 @@ class ListMemory(Memory):
"""
- def __init__(self, name: str | None = None) -> None:
+ component_type = "memory"
+ component_provider_override = "autogen_core.memory.ListMemory"
+ component_config_schema = ListMemoryConfig
+
+ def __init__(self, name: str | None = None, memory_contents: List[MemoryContent] | None = None) -> None:
self._name = name or "default_list_memory"
- self._contents: List[MemoryContent] = []
+ self._contents: List[MemoryContent] = memory_contents if memory_contents is not None else []
@property
def name(self) -> str:
@@ -146,3 +163,10 @@ class ListMemory(Memory):
async def close(self) -> None:
"""Cleanup resources if needed."""
pass
+
+ @classmethod
+ def _from_config(cls, config: ListMemoryConfig) -> Self:
+ return cls(name=config.name, memory_contents=config.memory_contents)
+
+ def _to_config(self) -> ListMemoryConfig:
+ return ListMemoryConfig(name=self.name, memory_contents=self._contents)
diff --git a/python/packages/autogen-core/tests/test_memory.py b/python/packages/autogen-core/tests/test_memory.py
index 04054e1b2..ce98aaffb 100644
--- a/python/packages/autogen-core/tests/test_memory.py
+++ b/python/packages/autogen-core/tests/test_memory.py
@@ -1,7 +1,7 @@
from typing import Any
import pytest
-from autogen_core import CancellationToken
+from autogen_core import CancellationToken, ComponentModel
from autogen_core.memory import (
ListMemory,
Memory,
@@ -23,6 +23,34 @@ def test_memory_protocol_attributes() -> None:
assert hasattr(Memory, "close")
+def test_memory_component_load_config_from_base_model() -> None:
+ """Test that Memory component can be loaded from a BaseModel."""
+ config = ComponentModel(
+ provider="autogen_core.memory.ListMemory",
+ config={
+ "name": "test_memory",
+ "memory_contents": [MemoryContent(content="test", mime_type=MemoryMimeType.TEXT)],
+ },
+ )
+ memory = Memory.load_component(config)
+ assert isinstance(memory, ListMemory)
+ assert memory.name == "test_memory"
+ assert len(memory.content) == 1
+
+
+def test_memory_component_dump_config_to_base_model() -> None:
+ """Test that Memory component can be dumped to a BaseModel."""
+ memory = ListMemory(
+ name="test_memory", memory_contents=[MemoryContent(content="test", mime_type=MemoryMimeType.TEXT)]
+ )
+ config = memory.dump_component()
+ assert isinstance(config, ComponentModel)
+ assert config.provider == "autogen_core.memory.ListMemory"
+ assert config.component_type == "memory"
+ assert config.config["name"] == "test_memory"
+ assert len(config.config["memory_contents"]) == 1
+
+
def test_memory_abc_implementation() -> None:
"""Test that Memory ABC is properly implemented."""