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
- ConsolidationSimilarityThreshold
Gets or sets the similarity threshold used to identify clusters of related memories during consolidation.
- DataSources
Gets the collection of data sources currently stored in the agent memory.
- DeduplicationThreshold
Gets or sets the similarity threshold for memory deduplication during extraction.
- EntryCount
Gets the total number of memory entries stored across all data sources.
- EvictionPolicy
Gets or sets the eviction strategy used when memory capacity is exceeded.
- ExtractionMode
Gets or sets the automatic memory extraction mode.
- ExtractionModel
Gets or sets the language model used for memory extraction.
- ExtractionPrompt
Gets or sets custom extraction guidance that controls what types of information the extractor identifies and stores.
- Filter
Gets or sets the filtering criteria used during memory retrieval operations.
- MaxConversationSummaries
Gets or sets the maximum number of episodic entries to create when summarizing a conversation.
- MaxExtractionCompletionTokens
Gets or sets the maximum number of completion tokens for the extraction LLM call.
- MaxExtractionsPerTurn
Gets or sets the maximum number of facts to extract per conversation turn.
- MaxMemoryEntries
Gets or sets the maximum number of memory entries (sections) allowed globally across all data sources.
- MinClusterSize
Gets or sets the minimum cluster size required for consolidation.
- MinScore
Gets or sets the minimum similarity score required for a partition to be considered relevant.
- RunExtractionSynchronously
Gets or sets a value indicating whether memory extraction should block until complete.
- TimeDecayHalfLife
Gets or sets the half-life for time-decay scoring during memory retrieval.
- TopK
Gets or sets the maximum number of matching partitions to retrieve during memory recall.
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.
- ConsolidateAsync(LM, CancellationToken)
Consolidates similar memory entries by merging clusters of semantically related entries into single, summarized entries using an LLM.
- 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.
- SummarizeConversationAsync(ChatHistory, LM, string, CancellationToken)
Summarizes a conversation into episodic memory entries that capture the key topics, decisions, and outcomes discussed.
Events
- BeforeMemoryConsolidated
Occurs before a cluster of similar memories is merged into a single consolidated entry.
- BeforeMemoryStored
Occurs after facts have been extracted and deduplicated, but before they are stored.
- MemoryEvicted
Occurs before a memory entry is evicted due to capacity enforcement.