Memory
Memory plays a crucial role in building effective AI agents, allowing them to maintain context, persist information across interactions, and retrieve relevant knowledge when needed.
Memory System Components
LocalMemory
Enabling a single agent use local memory to retain and utilize knowledge.
SharedMemory
Enabling Agents in a team session to retain and utilize knowledge.
PersistentMemory
Enabling Agents to Build and Refine Knowledge Over Time.
Implementing Memory in your AgentOpera
LocalMemory
LocalMemory allows agents to temporarily store user preferences or recent context during a single conversation or task.
In this example, the assistant remembers the user prefers metric units for temperature and will use this info when generating responses — even if the user doesn’t repeat it.
Short-Term Memory is:
Session-based.
Useful for holding temporary context.
Cleared after the conversation ends.
from agentopera.chatflow.agents import AssistantAgent
from agentopera.memory import LocalMemory, MemoryContent, MemoryMimeType
from agentopera.models.openai import OpenAIChatCompletionClient
# Initialize user memory
user_memory = LocalMemory()
# Add user preferences
await user_memory.add(MemoryContent(
content="The user prefers temperatures in metric units",
mime_type=MemoryMimeType.TEXT
))
# Create assistant with memory
assistant = AssistantAgent(
name="assistant",
model_client=OpenAIChatCompletionClient(model="gpt-4"),
memory=[user_memory], # You can provide multiple memory instances
)
# The memory will automatically be used to provide context
response = await assistant.run(task="What's the weather in New York?")SharedMemory (Shared within Teams)
SharedMemory allows multiple agents in a team to share real-time context during collaboration.
In this example, agents store user actions and suggestions into a shared memory space — making it easy for any agent to access the latest team insights when handling a task.
Key points:
Shared across agents.
Context expires after a set time (expiration_time).
Perfect for team collaboration and live coordination.
PersistentMemory
PersistentMemory allows agents to store and recall long-term knowledge across sessions, making it ideal for building personalized user experiences.
In this example, the assistant stores user preferences (like writing style and study interests) with unique keys and IDs. This allows the assistant to recall and use this information in future interactions, even after the session ends.
Key points:
Persistent: Memory remains across sessions.
Personalized: Enables tailored responses based on stored user knowledge.
Efficient: Uses unique key and id for easy memory retrieval.
Last updated