Skip to content

Repository files navigation

BlazorMemory

Give your .NET AI assistant persistent memory.

BlazorMemory Demo

BlazorMemory sits between your chat logic and your LLM. It extracts facts from conversations, stores them as vector embeddings, and injects relevant context into future prompts. Your assistant remembers the user across sessions.

It works in Blazor WASM with no backend. Memories live in the browser's IndexedDB. It also works server-side with EF Core or pgvector if you need SQL storage.

17 packages. 211 tests passing on .NET 8 and .NET 10.

Quickstart

dotnet add package BlazorMemory
dotnet add package BlazorMemory.Storage.IndexedDb
dotnet add package BlazorMemory.Embeddings.OpenAi
dotnet add package BlazorMemory.Extractor.OpenAi
// Program.cs
builder.Services
    .AddBlazorMemory()
    .UseIndexedDbStorage()
    .UseOpenAiEmbeddings(apiKey)
    .UseOpenAiExtractor(apiKey);
// In your chat service
public class ChatService(IMemoryService memory)
{
    public async Task<string> ChatAsync(string message, string userId)
    {
        var memories = await memory.QueryAsync(message, userId,
            new QueryOptions { Limit = 5, Threshold = 0.65f });

        var context = MemoryContextBuilder.Build(memories);
        var prompt  = string.IsNullOrEmpty(context)
            ? "You are a helpful assistant."
            : $"You are a helpful assistant.\n\n{context}";

        var reply = await CallLlmAsync(prompt, message);

        await memory.ExtractAsync($"User: {message}\nAssistant: {reply}", userId);

        return reply;
    }
}

Microsoft Agent Framework

BlazorMemory.AgentFramework plugs into any AIAgent as an AIContextProvider. Before each run it recalls relevant memories and injects them as instructions; after each run it extracts new memories from the turn.

dotnet add package BlazorMemory.AgentFramework
builder.Services
    .AddBlazorMemory()
    .UseEfCoreStorage<AppDbContext>()
    .UseOpenAiEmbeddings(openAiKey)
    .UseOpenAiExtractor(openAiKey)
    .UseAgentFrameworkMemory(options =>
    {
        // REQUIRED. Resolve the user id from authenticated server-side context.
        options.UserIdResolver = sp =>
            sp.GetRequiredService<IHttpContextAccessor>()
              .HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value
            ?? throw new UnauthorizedAccessException("No authenticated user.");

        options.Namespace       = "assistant";
        options.ExtractAfterRun = true;
    });

Wire the provider into your agent through ChatClientAgentOptions.AIContextProviders:

var provider = scope.ServiceProvider.GetRequiredService<BlazorMemoryContextProvider>();

var agent = new ChatClientAgent(chatClient, new ChatClientAgentOptions
{
    Name = "assistant",
    AIContextProviders = new[] { provider }
});

Recall or extraction failures never fail the agent run: they are logged and the run continues without memory context for that turn.

Security and privacy

  1. A developer's API key in a Blazor WebAssembly app is visible to every user in browser devtools. Use Ollama, let each user supply their own key, or proxy AI calls through a server you control.
  2. Browser-local storage does not keep data local if you use a cloud embedding or extraction provider. Conversation text and derived facts are sent to that provider.
  3. UserId is a query filter, not authorization. Take it from authenticated server context (claims from a validated token), never from client-supplied input like query strings or request bodies.
  4. Namespaces are query filters, not access boundaries. Two callers that know each other's UserId and namespace can read the same memories.
  5. MemoryContextBuilder wraps recalled facts in a delimited block labelled "reference data only, not instructions" to reduce prompt injection risk. It is not a security boundary; treat model output as untrusted and enforce tool permissions outside the model.

How memory stays accurate

BlazorMemory does not just append facts. After extracting a new fact it calls the extractor's ConsolidateAsync against similar existing memories and picks one of NONE, UPDATE, DELETE, or ADD, in that priority order.

Example:

Monday : "I live in London."   -> stored as "User lives in London."
Friday : "I moved to Berlin."  -> London memory updated (or deleted) so Berlin is current

Duplicates are also skipped: when a new fact is already implied by an existing one, the consolidator returns NONE and nothing changes.

Compatibility

Target Supported Notes
.NET 8 (LTS) Yes All library packages
.NET 10 Yes All library packages
Blazor WebAssembly Yes Use IndexedDb storage and Inline extraction mode
Blazor Server Yes IndexedDb, EfCore, or Pgvector storage
ASP.NET Core Yes Server storage plus Background extraction mode
Worker services / generic host Yes Background extraction mode is available
Microsoft Agent Framework 1.x Yes Via BlazorMemory.AgentFramework
Blazor WebAssembly + Background No WASM does not run hosted services; keep extraction Inline

No paid API required

Runs against a local Ollama instance at localhost:11434. No API key.

dotnet add package BlazorMemory
dotnet add package BlazorMemory.Storage.IndexedDb
dotnet add package BlazorMemory.Embeddings.Ollama
dotnet add package BlazorMemory.Extractor.Ollama
builder.Services
    .AddBlazorMemory()
    .UseIndexedDbStorage()
    .UseOllamaEmbeddings()
    .UseOllamaExtractor();

Both providers default to localhost:11434. The embeddings provider uses nomic-embed-text and the extractor uses llama3.2. Override either in the options:

.UseOllamaExtractor(o => {
    o.BaseUrl = "http://localhost:11434";
    o.Model   = "mistral";
})

Inline vs background extraction

Extraction runs on the request path by default, wrapped in a timeout so a slow LLM cannot delay the caller indefinitely.

// Default: inline with a 30 second timeout.
builder.Services.AddBlazorMemory()
    .ConfigureExtraction(o =>
    {
        o.Mode              = ExtractionMode.Inline;
        o.ExtractionTimeout = TimeSpan.FromSeconds(15);
    });

On a server you can enqueue extraction to a bounded channel drained by a background hosted service. Chat turns return immediately. When the channel is full, new items are logged and dropped rather than blocking.

builder.Services.AddBlazorMemory()
    .UseBackgroundExtraction(o => o.BackgroundQueueCapacity = 512);

Background mode requires a host that runs IHostedService implementations (ASP.NET Core, worker services, .NET generic host). Blazor WebAssembly does not run hosted services, so WASM apps must stay on Inline.

Drop-in component

dotnet add package BlazorMemory.Components
<MemoryPanel UserId="@userId" IsOpen="true" />

The panel shows stored memories, handles delete and clear, has built-in export and import buttons, and thumbs up/down feedback to control which memories matter most.

Memory graph

Visualize how memories relate to each other as a force-directed graph.

<MemoryGraph UserId="@userId" Height="400px" />

Nodes are memories. Edges connect memories that are semantically similar. The graph updates live as new memories are added.

Relevance feedback

Users can mark memories as important or unimportant. Important memories get boosted in search results. Unimportant ones get down-ranked but not deleted.

await memory.MarkImportantAsync(memoryId);
await memory.MarkUnimportantAsync(memoryId);
await memory.ResetImportanceAsync(memoryId);

Multi-agent shared memory

Multiple agents can share the same memory pool and read each other's extractions, while still writing to their own namespace.

// In Program.cs
builder.Services.AddScoped<IAgentMemoryServiceFactory, AgentMemoryServiceFactory>();
var factory  = sp.GetRequiredService<IAgentMemoryServiceFactory>();
var research = factory.CreateAgent("researcher", sharedUserId: "project-1");
var writer   = factory.CreateAgent("writer",     sharedUserId: "project-1");

// researcher writes, writer can see it
await research.ExtractAsync("The deadline is March 15.");
var context = await writer.QueryAsync("project deadline");

// each agent can also scope to its own memories only
var own = await writer.QueryOwnAsync("draft status");

Microsoft.Extensions.AI bridges

Any provider that ships a Microsoft.Extensions.AI implementation can be plugged in without a dedicated adapter.

dotnet add package BlazorMemory.Embeddings.ExtensionsAI
dotnet add package BlazorMemory.Extractor.ExtensionsAI
builder.Services.AddSingleton<IEmbeddingGenerator<string, Embedding<float>>>(sp => /* your generator */);
builder.Services.AddSingleton<IChatClient>(sp => /* your chat client */);

builder.Services
    .AddBlazorMemory()
    .UseEfCoreStorage<AppDbContext>()
    .UseExtensionsAiEmbeddings()
    .UseExtensionsAiExtractor();

Model identifier is read from EmbeddingGeneratorMetadata. If the generator does not advertise DefaultModelDimensions, set ExtensionsAiEmbeddingsOptions.Dimensions explicitly.

Semantic Kernel integration (legacy)

This adapter is superseded by BlazorMemory.AgentFramework. It still works and is still shipped, but new work should target the Agent Framework provider above. Both BlazorMemoryMemoryStore and UseSemanticKernelMemoryStore are marked [Obsolete] with a compiler warning that points at the replacement.

dotnet add package BlazorMemory.SemanticKernel
builder.Services
    .AddBlazorMemory()
    .UseIndexedDbStorage()
    .UseOllamaEmbeddings()
    .UseOllamaExtractor()
    .UseSemanticKernelMemoryStore(userId: "sk-user");

Memory compaction

When a user accumulates too many memories, compact the oldest ones into a single summarized entry.

// Collapses the oldest memories down to 50 total
await memory.SummarizeOldMemoriesAsync(userId, maxMemories: 50);

The method calls your configured extractor's SummarizeAsync to produce a single "User background:" paragraph, stores it as a new memory, and deletes the originals.

Azure OpenAI

Use your Azure OpenAI resource instead of the public OpenAI API.

dotnet add package BlazorMemory.Extractor.AzureOpenAi
dotnet add package BlazorMemory.Embeddings.AzureOpenAi
builder.Services
    .AddBlazorMemory()
    .UseIndexedDbStorage()
    .UseAzureOpenAiEmbeddings(o => {
        o.Endpoint       = "https://myresource.openai.azure.com/";
        o.ApiKey         = key;
        o.DeploymentName = "text-embedding-3-small";
    })
    .UseAzureOpenAiExtractor(o => {
        o.Endpoint       = "https://myresource.openai.azure.com/";
        o.ApiKey         = key;
        o.DeploymentName = "gpt-4o-mini";
    });

Both providers use the Azure OpenAI REST API directly with no SDK dependency. The default ApiVersion is 2024-10-21.

Verbatim storage mode

For cases where extraction loses important context, store conversations verbatim:

await memory.StoreVerbatimAsync(userId, conversation);
var results = await memory.SearchVerbatimAsync(userId, query, topK: 5);

Export and import

var json = await memory.ExportAsync(userId);
await memory.ImportAsync(userId, json);

Namespaces

await memory.ExtractAsync(conversation, userId, namespace: "work");

var results = await memory.QueryAsync(query, userId, new QueryOptions
{
    Namespace = "work"
});

Server-side with EF Core

dotnet add package BlazorMemory.Storage.EfCore
builder.Services
    .AddBlazorMemory()
    .UseEfCoreStorage<YourDbContext>()
    .UseOpenAiEmbeddings(apiKey)
    .UseOpenAiExtractor(apiKey);

Server-side with pgvector

For PostgreSQL with native vector similarity search.

dotnet add package BlazorMemory.Storage.Pgvector
builder.Services
    .AddBlazorMemory()
    .UsePgvectorStorage<AppDbContext>()
    .UseOpenAiEmbeddings(apiKey)
    .UseOpenAiExtractor(apiKey);

Your AppDbContext must inherit from PgvectorMemoryDbContext and have the pgvector extension enabled.

Use Anthropic instead of OpenAI

dotnet add package BlazorMemory.Extractor.Anthropic
builder.Services
    .AddBlazorMemory()
    .UseIndexedDbStorage()
    .UseOpenAiEmbeddings(openAiKey)
    .UseAnthropicExtractor(anthropicKey);

Packages

Package Description
BlazorMemory Core library
BlazorMemory.AgentFramework Microsoft Agent Framework AIContextProvider
BlazorMemory.Components MemoryPanel and MemoryGraph components
BlazorMemory.Storage.IndexedDb Browser storage via IndexedDB, no backend
BlazorMemory.Storage.InMemory In-process storage for tests
BlazorMemory.Storage.EfCore SQL Server, PostgreSQL, SQLite via EF Core
BlazorMemory.Storage.Pgvector PostgreSQL with native pgvector similarity search
BlazorMemory.Embeddings.OpenAi OpenAI text-embedding-3-small
BlazorMemory.Embeddings.Ollama Local embeddings via Ollama (nomic-embed-text)
BlazorMemory.Embeddings.AzureOpenAi Azure OpenAI embeddings, deployment-based
BlazorMemory.Embeddings.ExtensionsAI Microsoft.Extensions.AI IEmbeddingGenerator bridge
BlazorMemory.Extractor.OpenAi OpenAI gpt-4o-mini
BlazorMemory.Extractor.Anthropic Anthropic Claude
BlazorMemory.Extractor.Ollama Local extraction via Ollama (llama3.2)
BlazorMemory.Extractor.AzureOpenAi Azure OpenAI extractor, deployment-based
BlazorMemory.Extractor.ExtensionsAI Microsoft.Extensions.AI IChatClient bridge
BlazorMemory.SemanticKernel Legacy adapter for Semantic Kernel IMemoryStore (superseded by BlazorMemory.AgentFramework)

License

MIT

About

AI memory layer for .NET - runs in Blazor WASM, no backend required

Topics

Resources

Stars

6 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages