Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<PackageVersion Include="CommunityToolkit.VectorData.Qdrant" Version="1.0.0" />
<!-- Agent SDKs -->
<PackageVersion Include="GitHub.Copilot.SDK" Version="1.0.5" />
<PackageVersion Include="Mem0Sharp" Version="0.2.1" />
<PackageVersion Include="ResponsibleAI.AgentHooks" Version="0.1.0-alpha.4" />
<PackageVersion Include="Microsoft.Agents.CopilotStudio.Client" Version="1.3.171-beta" />
<!-- M365 Agents SDK -->
Expand Down
2 changes: 1 addition & 1 deletion dotnet/agent-framework-dotnet.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
<Project Path="samples/02-agents/AgentWithMemory/AgentWithMemory_Step06_MemoryUsingAgentMemory/AgentWithMemory_Step06_MemoryUsingAgentMemory.csproj" />
<Project Path="samples/02-agents/AgentWithMemory/AgentWithMemory_Step07_FileMemoryProvider/AgentWithMemory_Step07_FileMemoryProvider.csproj" />
<Project Path="samples/02-agents/AgentWithMemory/AgentWithMemory_Step08_MemoryUsingCosmosNoSql/AgentWithMemory_Step08_MemoryUsingCosmosNoSql.csproj" />
<Project Path="samples/02-agents/AgentWithMemory/AgentWithMemory_Step09_MemoryUsingMem0Sharp/AgentWithMemory_Step09_MemoryUsingMem0Sharp.csproj" />
</Folder>
<Folder Name="/Samples/02-agents/AgentProviders/openai/">
<File Path="samples/02-agents/AgentProviders/openai/README.md" />
Expand Down Expand Up @@ -677,4 +678,3 @@
<Project Path="tests/Microsoft.Agents.AI.Workflows.UnitTests/Microsoft.Agents.AI.Workflows.UnitTests.csproj" />
</Folder>
</Solution>

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
Comment thread
jihadkhawaja marked this conversation as resolved.
Resolved

<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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft. All rights reserved.
Comment thread
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}"))}",
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft. All rights reserved.
Comment thread
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));
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Agent with Memory Using Mem0Sharp
Comment thread
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.
1 change: 1 addition & 0 deletions dotnet/samples/02-agents/AgentWithMemory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ These samples show how to create an agent with the Agent Framework that uses Mem
|[Memory Using AgentMemory](./AgentWithMemory_Step06_MemoryUsingAgentMemory/)|This sample demonstrates a retail shopping assistant built with [`AgentMemory`](https://www.nuget.org/packages/AgentMemory), an unofficial .NET port of the Neo4j Labs graph-memory provider, to learn customer preferences and recommend products via graph traversal.|
|[File Based Memory](./AgentWithMemory_Step07_FileMemoryProvider/)|This sample demonstrates how to use the `FileMemoryProvider` to give an agent tools for storing and recalling memories as files, and how to configure the folder that those memory files are written to.|
|[Memory with Azure Cosmos DB for NoSQL](./AgentWithMemory_Step08_MemoryUsingCosmosNoSql/)|This sample demonstrates how to persist and retrieve chat history across sessions with Azure Cosmos DB for NoSQL.|
|[Memory with Mem0Sharp](./AgentWithMemory_Step09_MemoryUsingMem0Sharp/)|This sample demonstrates how to use the `Mem0Sharp` NuGet package as an in-memory store for an agent.|

> **See also**: [Memory Search with Foundry Agents](../AgentProviders/foundry/Agent_Step22_MemorySearch/) - demonstrates using the built-in Memory Search tool with Microsoft Foundry agents.

Loading