Skip to content

Commit e39eb32

Browse files
authored
.Net: Add a mem0 component with integration tests (#11692)
### Motivation and Context #10100 #10712 ### Description - Add a new `mem0` conversation state part - Add integration tests for the new component ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
1 parent 50881e7 commit e39eb32

8 files changed

Lines changed: 563 additions & 2 deletions

File tree

‎dotnet/Directory.Packages.props‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="8.9.1" />
114114
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0" />
115115
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.1" />
116-
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
116+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.3" />
117117
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />
118118
<PackageVersion Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" />
119119
<PackageVersion Include="Microsoft.Extensions.Options.DataAnnotations" Version="8.0.0" />
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System;
4+
using System.Net.Http;
5+
using System.Net.Http.Headers;
6+
using System.Threading.Tasks;
7+
using Microsoft.Extensions.AI;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.SemanticKernel.Memory;
10+
using SemanticKernel.IntegrationTests.TestSettings;
11+
using Xunit;
12+
13+
namespace SemanticKernel.IntegrationTests.Memory;
14+
15+
/// <summary>
16+
/// Contains tests for the <see cref="Mem0MemoryComponent"/> class.
17+
/// </summary>
18+
public class Mem0MemoryComponentTests : IDisposable
19+
{
20+
// If null, all tests will be enabled
21+
private const string SkipReason = "Requires a Mem0 service configured";
22+
23+
private readonly HttpClient _httpClient;
24+
private bool _disposedValue;
25+
26+
public Mem0MemoryComponentTests()
27+
{
28+
IConfigurationRoot configuration = new ConfigurationBuilder()
29+
.AddJsonFile(path: "testsettings.json", optional: false, reloadOnChange: true)
30+
.AddJsonFile(path: "testsettings.development.json", optional: true, reloadOnChange: true)
31+
.AddEnvironmentVariables()
32+
.AddUserSecrets<Mem0MemoryComponentTests>()
33+
.Build();
34+
35+
var mem0Settings = configuration.GetRequiredSection("Mem0").Get<Mem0Configuration>()!;
36+
37+
this._httpClient = new HttpClient();
38+
this._httpClient.BaseAddress = new Uri(mem0Settings.ServiceUri);
39+
this._httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", mem0Settings.ApiKey);
40+
}
41+
42+
[Fact(Skip = SkipReason)]
43+
public async Task Mem0ComponentCanAddAndRetrieveMemoriesAsync()
44+
{
45+
// Arrange
46+
var question = new ChatMessage(ChatRole.User, "What is my name?");
47+
var input = new ChatMessage(ChatRole.User, "Hello, my name is Caoimhe.");
48+
49+
var sut = new Mem0MemoryComponent(this._httpClient, new() { ThreadId = "test-thread-id", UserId = "test-user-id", ScopeToPerOperationThreadId = true });
50+
51+
await sut.ClearStoredUserFactsAsync();
52+
var answerBeforeAdding = await sut.OnModelInvokeAsync([question]);
53+
Assert.DoesNotContain("Caoimhe", answerBeforeAdding);
54+
55+
// Act
56+
await sut.OnNewMessageAsync("test-thread-id", input);
57+
58+
await sut.OnNewMessageAsync("test-thread-id", question);
59+
var answerAfterAdding = await sut.OnModelInvokeAsync([question]);
60+
61+
await sut.ClearStoredUserFactsAsync();
62+
var answerAfterClearing = await sut.OnModelInvokeAsync([question]);
63+
64+
// Assert
65+
Assert.Contains("Caoimhe", answerAfterAdding);
66+
Assert.DoesNotContain("Caoimhe", answerAfterClearing);
67+
}
68+
69+
[Fact(Skip = SkipReason)]
70+
public async Task Mem0ComponentDoesNotLeakMessagesAcrossScopesAsync()
71+
{
72+
// Arrange
73+
var question = new ChatMessage(ChatRole.User, "What is your name?");
74+
var input = new ChatMessage(ChatRole.Assistant, "I'm an AI tutor with a personality. My name is Caoimhe.");
75+
76+
var sut1 = new Mem0MemoryComponent(this._httpClient, new() { AgentId = "test-agent-id-1" });
77+
var sut2 = new Mem0MemoryComponent(this._httpClient, new() { AgentId = "test-agent-id-2" });
78+
79+
await sut1.ClearStoredUserFactsAsync();
80+
await sut2.ClearStoredUserFactsAsync();
81+
82+
var answerBeforeAdding1 = await sut1.OnModelInvokeAsync([question]);
83+
var answerBeforeAdding2 = await sut2.OnModelInvokeAsync([question]);
84+
Assert.DoesNotContain("Caoimhe", answerBeforeAdding1);
85+
Assert.DoesNotContain("Caoimhe", answerBeforeAdding2);
86+
87+
// Act
88+
await sut1.OnNewMessageAsync("test-thread-id-1", input);
89+
var answerAfterAdding = await sut1.OnModelInvokeAsync([question]);
90+
91+
await sut2.OnNewMessageAsync("test-thread-id-2", question);
92+
var answerAfterAddingOnOtherScope = await sut2.OnModelInvokeAsync([question]);
93+
94+
// Assert
95+
Assert.Contains("Caoimhe", answerAfterAdding);
96+
Assert.DoesNotContain("Caoimhe", answerAfterAddingOnOtherScope);
97+
98+
// Cleanup.
99+
await sut1.ClearStoredUserFactsAsync();
100+
await sut2.ClearStoredUserFactsAsync();
101+
}
102+
103+
[Fact(Skip = SkipReason)]
104+
public async Task Mem0ComponentDoesNotWorkWithMultiplePerOperationThreadsAsync()
105+
{
106+
// Arrange
107+
var input = new ChatMessage(ChatRole.User, "Hello, my name is Caoimhe.");
108+
109+
var sut = new Mem0MemoryComponent(this._httpClient, new() { UserId = "test-user-id", ScopeToPerOperationThreadId = true });
110+
111+
await sut.ClearStoredUserFactsAsync();
112+
113+
// Act & Assert
114+
await sut.OnThreadCreatedAsync("test-thread-id-1");
115+
await Assert.ThrowsAsync<InvalidOperationException>(() => sut.OnThreadCreatedAsync("test-thread-id-2"));
116+
117+
await sut.OnNewMessageAsync("test-thread-id-1", input);
118+
await Assert.ThrowsAsync<InvalidOperationException>(() => sut.OnNewMessageAsync("test-thread-id-2", input));
119+
120+
// Cleanup
121+
await sut.ClearStoredUserFactsAsync();
122+
}
123+
124+
protected virtual void Dispose(bool disposing)
125+
{
126+
if (!this._disposedValue)
127+
{
128+
if (disposing)
129+
{
130+
this._httpClient.Dispose();
131+
}
132+
133+
this._disposedValue = true;
134+
}
135+
}
136+
137+
public void Dispose()
138+
{
139+
this.Dispose(disposing: true);
140+
GC.SuppressFinalize(this);
141+
}
142+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System.Diagnostics.CodeAnalysis;
4+
5+
namespace SemanticKernel.IntegrationTests.TestSettings;
6+
7+
[SuppressMessage("Performance", "CA1812:Internal class that is apparently never instantiated",
8+
Justification = "Configuration classes are instantiated through IConfiguration.")]
9+
internal sealed class Mem0Configuration
10+
{
11+
public string ServiceUri { get; init; } = string.Empty;
12+
public string ApiKey { get; init; } = string.Empty;
13+
}

‎dotnet/src/IntegrationTests/testsettings.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,9 @@
126126
"BedrockAgent": {
127127
"AgentResourceRoleArn": "",
128128
"FoundationModel": "anthropic.claude-3-haiku-20240307-v1:0"
129+
},
130+
"Mem0": {
131+
"ServiceUri": "https://api.mem0.ai",
132+
"ApiKey": ""
129133
}
130134
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Linq;
7+
using System.Net.Http;
8+
using System.Text;
9+
using System.Text.Json;
10+
using System.Text.Json.Serialization;
11+
using System.Threading.Tasks;
12+
13+
namespace Microsoft.SemanticKernel.Memory;
14+
15+
/// <summary>
16+
/// Client for the Mem0 memory service.
17+
/// </summary>
18+
[ExcludeFromCodeCoverage] // Tested via integration tests.
19+
internal sealed class Mem0Client
20+
{
21+
private static readonly Uri s_searchUri = new("/v1/memories/search/", UriKind.Relative);
22+
private static readonly Uri s_createMemoryUri = new("/v1/memories/", UriKind.Relative);
23+
24+
private readonly HttpClient _httpClient;
25+
26+
public Mem0Client(HttpClient httpClient)
27+
{
28+
Verify.NotNull(httpClient);
29+
30+
this._httpClient = httpClient;
31+
}
32+
33+
public async Task<IEnumerable<string>> SearchAsync(string? applicationId, string? agentId, string? threadId, string? userId, string? inputText)
34+
{
35+
if (string.IsNullOrWhiteSpace(applicationId)
36+
&& string.IsNullOrWhiteSpace(agentId)
37+
&& string.IsNullOrWhiteSpace(threadId)
38+
&& string.IsNullOrWhiteSpace(userId))
39+
{
40+
throw new InvalidOperationException("At least one of applicationId, agentId, threadId, or userId must be provided.");
41+
}
42+
43+
var searchRequest = new SearchRequest
44+
{
45+
AppId = applicationId,
46+
AgentId = agentId,
47+
RunId = threadId,
48+
UserId = userId,
49+
Query = inputText ?? string.Empty
50+
};
51+
52+
// Search.
53+
using var content = new StringContent(JsonSerializer.Serialize(searchRequest, Mem0SourceGenerationContext.Default.SearchRequest), Encoding.UTF8, "application/json");
54+
var responseMessage = await this._httpClient.PostAsync(s_searchUri, content).ConfigureAwait(false);
55+
responseMessage.EnsureSuccessStatusCode();
56+
57+
// Process response.
58+
var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
59+
var searchResponseItems = JsonSerializer.Deserialize<SearchResponseItem[]>(response, Mem0SourceGenerationContext.Default.SearchResponseItemArray);
60+
return searchResponseItems?.Select(item => item.Memory) ?? [];
61+
}
62+
63+
public async Task CreateMemoryAsync(string? applicationId, string? agentId, string? threadId, string? userId, string messageContent, string messageRole)
64+
{
65+
if (string.IsNullOrWhiteSpace(applicationId)
66+
&& string.IsNullOrWhiteSpace(agentId)
67+
&& string.IsNullOrWhiteSpace(threadId)
68+
&& string.IsNullOrWhiteSpace(userId))
69+
{
70+
throw new InvalidOperationException("At least one of applicationId, agentId, threadId, or userId must be provided.");
71+
}
72+
73+
var createMemoryRequest = new CreateMemoryRequest()
74+
{
75+
AppId = applicationId,
76+
AgentId = agentId,
77+
RunId = threadId,
78+
UserId = userId,
79+
Messages = new[]
80+
{
81+
new CreateMemoryMessage
82+
{
83+
Content = messageContent,
84+
Role = messageRole
85+
}
86+
}
87+
};
88+
89+
using var content = new StringContent(JsonSerializer.Serialize(createMemoryRequest, Mem0SourceGenerationContext.Default.CreateMemoryRequest), Encoding.UTF8, "application/json");
90+
var responseMessage = await this._httpClient.PostAsync(s_createMemoryUri, content).ConfigureAwait(false);
91+
responseMessage.EnsureSuccessStatusCode();
92+
}
93+
94+
public async Task ClearMemoryAsync(string? applicationId, string? agentId, string? threadId, string? userId)
95+
{
96+
string[] paramNames = ["app_id", "agent_id", "run_id", "user_id"];
97+
98+
// Build query string.
99+
var querystringParams = new string?[4] { applicationId, agentId, threadId, userId }
100+
.Select((param, index) => string.IsNullOrWhiteSpace(param) ? null : $"{paramNames[index]}={param}")
101+
.Where(x => x != null);
102+
var queryString = string.Join("&", querystringParams);
103+
var clearMemoryUrl = new Uri($"/v1/memories/?{queryString}", UriKind.Relative);
104+
105+
// Delete.
106+
var responseMessage = await this._httpClient.DeleteAsync(clearMemoryUrl).ConfigureAwait(false);
107+
responseMessage.EnsureSuccessStatusCode();
108+
}
109+
110+
internal sealed class CreateMemoryRequest
111+
{
112+
[JsonPropertyName("app_id")]
113+
public string? AppId { get; set; }
114+
[JsonPropertyName("agent_id")]
115+
public string? AgentId { get; set; }
116+
[JsonPropertyName("run_id")]
117+
public string? RunId { get; set; }
118+
[JsonPropertyName("user_id")]
119+
public string? UserId { get; set; }
120+
[JsonPropertyName("messages")]
121+
public CreateMemoryMessage[] Messages { get; set; } = [];
122+
}
123+
124+
internal sealed class CreateMemoryMessage
125+
{
126+
[JsonPropertyName("content")]
127+
public string Content { get; set; } = string.Empty;
128+
[JsonPropertyName("role")]
129+
public string Role { get; set; } = string.Empty;
130+
}
131+
132+
internal sealed class SearchRequest
133+
{
134+
[JsonPropertyName("app_id")]
135+
public string? AppId { get; set; }
136+
[JsonPropertyName("agent_id")]
137+
public string? AgentId { get; set; } = null;
138+
[JsonPropertyName("run_id")]
139+
public string? RunId { get; set; } = null;
140+
[JsonPropertyName("user_id")]
141+
public string? UserId { get; set; } = null;
142+
[JsonPropertyName("query")]
143+
public string Query { get; set; } = string.Empty;
144+
}
145+
146+
internal sealed class SearchResponseItem
147+
{
148+
[JsonPropertyName("id")]
149+
public string Id { get; set; } = string.Empty;
150+
[JsonPropertyName("memory")]
151+
public string Memory { get; set; } = string.Empty;
152+
[JsonPropertyName("hash")]
153+
public string Hash { get; set; } = string.Empty;
154+
[JsonPropertyName("metadata")]
155+
public object? Metadata { get; set; }
156+
[JsonPropertyName("score")]
157+
public double Score { get; set; }
158+
[JsonPropertyName("created_at")]
159+
public DateTime CreatedAt { get; set; }
160+
[JsonPropertyName("updated_at")]
161+
public DateTime? UpdatedAt { get; set; }
162+
[JsonPropertyName("user_id")]
163+
public string UserId { get; set; } = string.Empty;
164+
[JsonPropertyName("app_id")]
165+
public string? AppId { get; set; }
166+
[JsonPropertyName("agent_id")]
167+
public string AgentId { get; set; } = string.Empty;
168+
[JsonPropertyName("session_id")]
169+
public string RunId { get; set; } = string.Empty;
170+
}
171+
}
172+
173+
[JsonSourceGenerationOptions(JsonSerializerDefaults.General,
174+
UseStringEnumConverter = false,
175+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
176+
WriteIndented = false)]
177+
[JsonSerializable(typeof(Mem0Client.CreateMemoryRequest))]
178+
[JsonSerializable(typeof(Mem0Client.SearchRequest))]
179+
[JsonSerializable(typeof(Mem0Client.SearchResponseItem[]))]
180+
internal partial class Mem0SourceGenerationContext : JsonSerializerContext
181+
{
182+
}

0 commit comments

Comments
 (0)