Class AgentMemory
Provides persistent memory storage and retrieval capabilities for conversational agents using retrieval-augmented generation (RAG).
public sealed class AgentMemory : ISerializableData
- Inheritance
-
AgentMemory
- Implements
- Inherited Members
Examples
Example: Creating an agent with persistent memory
This example demonstrates building an agent that remembers user preferences and previous interactions.
using LMKit.Agents;
using LMKit.Model;
// Load models
using var chatModel = new LM("path/to/chat-model.gguf");
using var embeddingModel = new LM("path/to/embedding-model.gguf");
// Create memory with the embedding model
var memory = new AgentMemory(embeddingModel);
// Store user preferences
await memory.SaveInformationAsync(
dataSourceIdentifier: "user_preferences",
text: "User prefers detailed technical explanations with code examples.",
sectionIdentifier: "communication_style");
await memory.SaveInformationAsync(
dataSourceIdentifier: "user_preferences",
text: "User is an experienced C# developer working on .NET applications.",
sectionIdentifier: "background");
// Create an agent with this memory
var agent = Agent.CreateBuilder()
.WithModel(chatModel)
.WithPersona("TechAssistant")
.WithMemory(memory)
.Build();
// The agent will automatically recall relevant preferences when responding
var result = await agent.RunAsync("How do I implement dependency injection?");
Console.WriteLine(result.Content);
Example: Persisting and loading memory across sessions
using LMKit.Agents;
using LMKit.Model;
const string MemoryPath = "agent_memory.bin";
using var embeddingModel = new LM("path/to/embedding-model.gguf");
AgentMemory memory;
// Load existing memory or create new
if (File.Exists(MemoryPath))
{
memory = AgentMemory.Deserialize(MemoryPath, embeddingModel);
Console.WriteLine($"Loaded memory with {memory.DataSources.Count} data sources.");
}
else
{
memory = new AgentMemory(embeddingModel);
Console.WriteLine("Created new memory store.");
}
// Use the memory with an agent...
// Save memory before application exits
memory.Serialize(MemoryPath);
Console.WriteLine("Memory saved successfully.");
Remarks
AgentMemory enables agents to store and recall information across conversations by leveraging a semantic search engine. When an agent processes a message, it can automatically retrieve the most relevant stored information to enhance its responses with historical context or domain knowledge.
The memory system supports three types of memory, modeled after human cognitive memory:
- SemanticGeneral knowledge and facts (e.g., "The capital of France is Paris").
- EpisodicPersonal experiences and specific events (e.g., "User mentioned their birthday is in March").
- ProceduralProcesses and how-to knowledge (e.g., "Steps to deploy the application").
Persistence
Memory can be serialized to disk and reloaded across application sessions using the
Serialize(string) and Deserialize(string, LM) methods.
Embedding Model
The memory system requires an embedding model to convert text into vector representations
for semantic search. Use a model optimized for embeddings (e.g., sentence transformers)
for best results.
Constructors
- AgentMemory(LM)
Initializes a new instance of the AgentMemory class with the specified embedding model.
Properties
- DataSources
Gets the collection of data sources currently stored in the agent memory.
- Filter
Gets or sets the filtering criteria used during memory retrieval operations.
Methods
- AddDataSource(DataSource)
Adds a data source to the agent memory.
- AddDataSources(IEnumerable<DataSource>)
Adds multiple data sources to the agent memory.
- Clear()
Removes all stored data from the memory.
- Deserialize(byte[], LM)
Deserializes memory from a byte array.
- Deserialize(Stream, LM)
Deserializes memory from a stream.
- Deserialize(string, LM)
Deserializes memory from a file at the specified path.
- GetMemoryType(DataSource)
Retrieves the memory type associated with the specified data source.
- IsEmpty()
Determines whether the memory contains any stored data.
- RemoveDataSource(DataSource)
Removes a specific data source from the memory.
- RemoveDataSource(string)
Removes a data source from the memory by its identifier.
- SaveInformation(string, string, string, MemoryType, MetadataCollection, CancellationToken)
Saves text information into a designated memory data source with a specified memory type.
- SaveInformation(string, string, string, MetadataCollection, CancellationToken)
Saves text information into a designated memory data source using semantic memory type.
- SaveInformationAsync(string, string, string, MemoryType, MetadataCollection, CancellationToken)
Asynchronously saves text information with a specified memory type.
- SaveInformationAsync(string, string, string, MetadataCollection, CancellationToken)
Asynchronously saves text information into a designated memory data source.
- Serialize()
Serializes the memory to a byte array.
- Serialize(Stream)
Serializes the memory to the specified stream.
- Serialize(string)
Serializes the memory to a file at the specified path.