Orchard Core AI Memory
Configure persistent user memory
You are an Orchard Core expert. Generate configuration, recipes, and code that let the AI remember durable, non-sensitive facts about the signed-in user across conversations.
Guidelines
- Memory works only for authenticated users. Every read and write is filtered by the user's
ClaimTypes.NameIdentifier; anonymous users get no memory tools. CrestApps.OrchardCore.AI.MemoryisEnabledByDependencyOnly; it depends onCrestApps.OrchardCore.AI.Chat.CoreandOrchardCore.Indexing. Enable a provider feature and it comes along.- Each memory is a record in the tenant store (
AIMemoryEntry) plus one document in the configured master memory index (an index profile of typeAIMemory). - The memory index names an embedding deployment from the
embeddingslot, so the deployment must declaretextEmbedding. It is locked once the index exists. - Memory is for preferences, projects, recurring topics, and stable background. Never store passwords, keys, tokens, card or Social Security numbers, private keys, or connection strings; the save tool rejects obvious sensitive patterns.
- Read the options through
IOptionsMonitor<AIMemoryOptions>andIOptionsMonitor<ChatInteractionMemoryOptions>; saving the Memory settings no longer releases the tenant. - Install packages in the web/startup project.
- User memory is not contact memory. A note the AI keeps about a CRM contact across calls, texts and chats (
AIContactMemory, the profile's Contact memory card withUseMemoryandSaveMemory) is part of AI Services, needs no memory index, and works for anonymous callers. Seeorchardcore-ai-conversations.
Features
| Feature | Feature ID | Depends on |
|---|---|---|
| AI Memory | CrestApps.OrchardCore.AI.Memory | CrestApps.OrchardCore.AI.Chat.Core, OrchardCore.Indexing (dependency only) |
| AI Memory indexing using Azure AI Search | CrestApps.OrchardCore.AI.Memory.AzureAI | AI Memory, OrchardCore.Indexing, OrchardCore.AzureAI |
| AI Memory indexing using Elasticsearch | CrestApps.OrchardCore.AI.Memory.Elasticsearch | AI Memory, OrchardCore.Elasticsearch |
Provider details are in orchardcore-ai-memory-azureai and orchardcore-ai-memory-elasticsearch.
Enable memory with Elasticsearch
{
"steps": [
{
"name": "Feature",
"enable": [
"OrchardCore.Elasticsearch",
"CrestApps.OrchardCore.AI.Memory.Elasticsearch"
],
"disable": []
}
]
}
Use OrchardCore.AzureAI and CrestApps.OrchardCore.AI.Memory.AzureAI for Azure AI Search.
Setup
- Create an embedding deployment that declares
textEmbedding(seeorchardcore-ai-model-capabilities). - Go to Search > Indexing and add AI Memory (Elasticsearch) or AI Memory (Azure AI Search), then pick the embedding deployment.
- In the Memory section of Settings > Artificial Intelligence, select the Index profile and the Default top N (1 to 20, default 5).
- Turn on Enable user memory on the profiles that should remember (off by default).
- Optionally turn off Enable preemptive memory retrieval in the General section to keep the tools but skip the upfront memory search.
Settings and options
| Setting | Stored as | Read through | Guarded by |
|---|---|---|---|
| Memory index profile and top N | AIMemorySettings (IndexProfileName, TopN) | IOptionsMonitor<AIMemoryOptions> | ManageAIProfiles |
| Chat interaction memory | site MemoryMetadata (EnableUserMemory) | IOptionsMonitor<ChatInteractionMemoryOptions> | ManageChatInteractionSettings |
| Preemptive memory retrieval | GeneralAISettings.EnablePreemptiveMemoryRetrieval (default true) | IOptionsMonitor<GeneralAIOptions> | AI settings |
{
"steps": [
{
"name": "Settings",
"AIMemorySettings": {
"IndexProfileName": "AIMemory",
"TopN": 5
},
"MemoryMetadata": {
"EnableUserMemory": true
}
}
]
}
The chat interaction setting is added only when CrestApps.OrchardCore.AI.Chat.Interactions is enabled; the feature migration turns it on for a new tenant. Memory stays inactive until a valid memory index profile is selected.
Enable memory on a profile
Profiles and profile-source templates store the switch in MemoryMetadata (Enable user memory on the Knowledge tab):
{
"steps": [
{
"name": "AIProfile",
"profiles": [
{
"Name": "personal-assistant",
"DisplayText": "Personal Assistant",
"Type": "Chat",
"Properties": {
"AIProfileMetadata": {
"SystemMessage": "You are a helpful assistant. Remember durable user preferences."
},
"MemoryMetadata": {
"EnableUserMemory": true
}
}
}
]
}
]
}
In code, use the extensions in CrestApps.Core.AI.Memory:
public sealed class PersonalAssistantMigrations : DataMigration
{
private readonly IAIProfileManager _profileManager;
public PersonalAssistantMigrations(IAIProfileManager profileManager)
{
_profileManager = profileManager;
}
public async Task<int> CreateAsync()
{
var profile = await _profileManager.NewAsync();
profile.Name = "personal-assistant";
profile.DisplayText = "Personal Assistant";
profile.Type = AIProfileType.Chat;
profile.Put(new AIProfileMetadata
{
SystemMessage = "You are a helpful assistant. Remember durable user preferences.",
});
profile.AlterMemoryMetadata(memory => memory.EnableUserMemory = true);
var result = await _profileManager.ValidateAsync(profile);
if (result.Succeeded)
{
await _profileManager.CreateAsync(profile);
}
return 1;
}
}
profile.GetMemoryMetadata() reads the switch, and template.WithMemoryMetadata(...) sets it on an AIProfileTemplate. Legacy AIProfileMemorySettings and ChatInteractionMemorySettings keys are migrated to MemoryMetadata automatically.
Memory tools
| Tool | Purpose |
|---|---|
| save_user_memory | Create or update a named memory |
| search_user_memories | Semantic search over the user's memories |
| list_user_memories | List the user's memories |
| remove_user_memory | Forget a memory |
They are system tools, added to a request only when memory is enabled for that profile or interaction and the user is signed in. The orchestration prompt tells the model to save durable facts in the same turn before promising to remember them, and to search or list memory before saying it does not know.
Each memory has a stable name (for example preferred_name), a description (The user's preferred name.), and the content. The embedding is built from the name and description, because the raw content alone is often not meaningful for retrieval.
Indexing pipeline
AIMemoryEntryHandlercollects created, updated, and deleted entries and adds oneShellScopedeferred task per request.AIMemoryIndexingServiceloads the index named byAIMemoryOptions.IndexProfileName, embeds the memory with the index's deployment (UseUsageLabels(purpose: AIUsagePurposes.Indexing)), and builds anAIMemoryEntryIndexDocument.- The provider
IDocumentIndexHandlermaps it tomemoryId,userId,name,description,content,updatedUtc, andembedding, and the keyedIDocumentIndexManagerwrites it. - Synchronizing the memory index from Search > Indexing rebuilds every memory into it (
SyncByIndexProfileIdsAsync). - Retrieval uses the keyed
IMemoryVectorSearchService, always filtered to the current user.
Because indexing hangs off the store lifecycle, memories written by admin actions, tools, or custom code stay in sync. This pattern is the template for orchardcore-custom-indexing-elasticsearch and orchardcore-custom-indexing-azureai.
Clearing memory
With OrchardCore.Users enabled, the user editor gets an AI Memory section with a Danger zone and Clear saved AI memory button, shown only when the viewer is authorized and the user has memories. It uses the standard admin confirmation dialog, then removes the records and their index documents.
| Permission | Meaning |
|---|---|
| ClearAIMemory | Clear your own memory. Grant it to the roles whose users may clear their memory. |
| ClearAIMemoryForOthers | Clear another user's memory; implies ClearAIMemory. |
Both are given to Administrator by default. The UI and the clear endpoint check ClearAIMemory with the target user id as the resource; AIMemoryAuthorizationHandler then requires ClearAIMemory for your own id and ClearAIMemoryForOthers for anyone else. Use the same check in custom code:
var canClear = await _authorizationService.AuthorizeAsync(User, AIPermissions.ClearAIMemory, (object)targetUserId);
Upgrading from 2.x
- Replace
IOptions<AIMemoryOptions>andIOptions<ChatInteractionMemoryOptions>withIOptionsMonitor<T>.CurrentValue; settings changes are now pushed without a tenant release. - The memory index embedding picker lists deployments by the
embeddingslot. DeclaretextEmbeddingon new embedding deployments; legacyPurpose: "Embedding"records are projected automatically.
Troubleshooting
| Symptom | Check |
|---|---|
| No memory tools | The user is anonymous, the profile has Enable user memory off, or no memory index is selected. |
| Nothing is remembered between sessions | The memory index has no embedding deployment, or the provider feature is disabled. |
| Memory not injected up front | Enable preemptive memory retrieval is off; the tools still work. |
| Clear button missing | The viewer lacks ClearAIMemory (own memory) or ClearAIMemoryForOthers (another user), or the user has no memories. |
| Wrong vector size after a model change | Create a new memory index with the new deployment, select it, and synchronize it. |