Repository navigation
.NET: added Mem0Sharp integration for in-memory storage in agent samples. #7792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Roger Barreto (rogerbarreto)
merged 7 commits into
microsoft:main
from
jihadkhawaja:dotnet-mem0sharp-long-term-memory-sample
Aug 25, 2026
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f693cd7
added Mem0Sharp integration for in-memory storage in agent samples.
jihadkhawaja 5b89914
formatting fix
jihadkhawaja 3dba398
Remove Mem0Sharp integration from agent memory samples and update REA…
jihadkhawaja c8e070c
Fix: Add missing newline at end of solution file
jihadkhawaja 6aedd29
Fix: Add missing newline at end of solution file
jihadkhawaja 3c7f1dd
Merge branch 'main' into dotnet-mem0sharp-long-term-memory-sample
jihadkhawaja d7993ec
Merge branch 'main' into dotnet-mem0sharp-long-term-memory-sample
jihadkhawaja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...WithMemory_Step09_MemoryUsingMem0Sharp/AgentWithMemory_Step09_MemoryUsingMem0Sharp.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFrameworks>net10.0</TargetFrameworks> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Azure.Identity" /> | ||
| <PackageReference Include="Mem0Sharp" /> | ||
| <ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.Foundry\Microsoft.Agents.AI.Foundry.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
37 changes: 37 additions & 0 deletions
37
...2-agents/AgentWithMemory/AgentWithMemory_Step09_MemoryUsingMem0Sharp/Mem0SharpProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
jihadkhawaja marked this conversation as resolved.
Resolved
|
||
|
|
||
| using Mem0Sharp; | ||
| using Microsoft.Agents.AI; | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| internal sealed class Mem0SharpProvider(MemoryService memory, string userId) : AIContextProvider | ||
| { | ||
| protected override async ValueTask StoreAIContextAsync( | ||
| InvokedContext context, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| foreach (ChatMessage message in context.RequestMessages.Where(message => message.Role == ChatRole.User && !string.IsNullOrWhiteSpace(message.Text))) | ||
| { | ||
| await memory.AddAsync( | ||
| message.Text!, | ||
| new MemoryAddOptions { UserId = userId, Infer = false }, | ||
| cancellationToken); | ||
| } | ||
| } | ||
|
|
||
| protected override async ValueTask<AIContext> ProvideAIContextAsync( | ||
| InvokingContext context, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| var memories = await memory.GetAllAsync( | ||
| new MemoryFilter(UserId: userId), | ||
| cancellationToken: cancellationToken); | ||
|
|
||
| return memories.Count == 0 | ||
| ? new AIContext() | ||
| : new AIContext | ||
| { | ||
| Instructions = $"Relevant memories for this user:\n{string.Join(Environment.NewLine, memories.Select(item => $"- {item.Text}"))}", | ||
| }; | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
.../samples/02-agents/AgentWithMemory/AgentWithMemory_Step09_MemoryUsingMem0Sharp/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
jihadkhawaja marked this conversation as resolved.
Resolved
|
||
|
|
||
| // This sample shows how to use Mem0Sharp's in-memory store with an Agent Framework agent. | ||
|
|
||
| using Azure.AI.Projects; | ||
| using Azure.Identity; | ||
| using Mem0Sharp; | ||
| using Microsoft.Agents.AI; | ||
|
|
||
| var endpoint = Environment.GetEnvironmentVariable("FOUNDRY_PROJECT_ENDPOINT") | ||
| ?? throw new InvalidOperationException("FOUNDRY_PROJECT_ENDPOINT is not set."); | ||
| var deploymentName = Environment.GetEnvironmentVariable("FOUNDRY_MODEL") ?? "gpt-5.4-mini"; | ||
|
|
||
| var memory = new MemoryService(); | ||
| AIProjectClient aiProjectClient = new(new Uri(endpoint), new DefaultAzureCredential()); | ||
|
|
||
| // WARNING: DefaultAzureCredential is convenient for development but requires careful consideration in production. | ||
| // In production, consider using a specific credential (e.g., ManagedIdentityCredential) to avoid | ||
| // latency issues, unintended credential probing, and potential security risks from fallback mechanisms. | ||
| AIAgent agent = aiProjectClient | ||
| .AsAIAgent(new ChatClientAgentOptions | ||
| { | ||
| ChatOptions = new() { ModelId = deploymentName, Instructions = "You are a helpful assistant. Use remembered preferences when relevant, and do not invent memories." }, | ||
| AIContextProviders = [new Mem0SharpProvider(memory, userId: "sample-user")], | ||
| }); | ||
|
|
||
| AgentSession session = await agent.CreateSessionAsync(); | ||
| Console.WriteLine(await agent.RunAsync("I prefer window seats when I fly.", session)); | ||
|
|
||
| Console.WriteLine("\n>> Start a new session that shares the same Mem0Sharp memory\n"); | ||
| AgentSession newSession = await agent.CreateSessionAsync(); | ||
| Console.WriteLine(await agent.RunAsync("Which seat should I book for my next flight?", newSession)); | ||
33 changes: 33 additions & 0 deletions
33
...02-agents/AgentWithMemory/AgentWithMemory_Step09_MemoryUsingMem0Sharp/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Agent with Memory Using Mem0Sharp | ||
|
jihadkhawaja marked this conversation as resolved.
Resolved
|
||
|
|
||
| This sample uses the [`Mem0Sharp`](https://www.nuget.org/packages/Mem0Sharp) NuGet package as a local, in-memory store for an Agent Framework agent. It stores a user preference and recalls it from a new agent session without requiring a memory service or database. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| 1. [.NET 10 SDK](https://dotnet.microsoft.com/download/dotnet/10.0) | ||
| 2. A Microsoft Foundry project with a chat model deployment | ||
| 3. Azure CLI authentication (`az login`) | ||
|
|
||
| ## Configuration | ||
|
|
||
| | Variable | Description | Default | | ||
| |---|---|---| | ||
| | `FOUNDRY_PROJECT_ENDPOINT` | Microsoft Foundry project endpoint | *(required)* | | ||
| | `FOUNDRY_MODEL` | Chat model deployment name | `gpt-5.4-mini` | | ||
|
|
||
| ## Run the Sample | ||
|
|
||
| ```bash | ||
| dotnet run | ||
| ``` | ||
|
|
||
| ## Storage Providers | ||
|
|
||
| This sample uses `MemoryService`'s default in-memory store, so memories do not survive application restarts. Mem0Sharp also supports: | ||
|
|
||
| - Qdrant through `QdrantMemoryStore`, included in the core `Mem0Sharp` package | ||
| - PostgreSQL with pgvector through the [`Mem0Sharp.PostgreSQL`](https://www.nuget.org/packages/Mem0Sharp.PostgreSQL) package | ||
| - SQLite through the [`Mem0Sharp.SQLite`](https://www.nuget.org/packages/Mem0Sharp.SQLite) package | ||
| - Custom providers by implementing `IMemoryStore` and, when needed, optional interfaces such as `IVectorMemoryStore` or `IMemoryHistoryStore` | ||
|
|
||
| Pass the configured store to `MemoryService` to replace the in-memory store. See the [Mem0Sharp providers and persistence guide](https://github.com/jihadkhawaja/mem0sharp/blob/main/docs/providers-and-persistence.md) for setup examples. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.