中文文档 | English
A .NET client library for Mem0, providing easy-to-use APIs for memory management in AI applications.
- 🚀 Easy Integration - Simple and intuitive API design
- 📦 Multiple .NET Versions - Supports .NET 6.0, 7.0, 8.0, and 9.0
- 🔧 Dependency Injection - Built-in support for Microsoft.Extensions.DependencyInjection
- 🌐 HTTP Client Management - Flexible HTTP client configuration
- 📝 Full API Coverage - Complete implementation of Mem0 API
- ⚡ Async/Await Support - Modern asynchronous programming patterns
Install the package via NuGet Package Manager:
dotnet add package Mem0.NETOr via Package Manager Console:
Install-Package Mem0.NETusing Mem0.NET;
// Initialize the client
var client = new Mem0Client("https://api.mem0.ai", "your-api-key");
// Create a memory
var createRequest = new MemoryCreateRequest
{
Messages = new List<Message>
{
new Message { Role = "user", Content = "I love playing basketball" }
},
UserId = "user123"
};
var memories = await client.CreateMemoryAsync(createRequest);
// Search memories
var searchRequest = new SearchRequest
{
Query = "What sports does the user like?",
UserId = "user123"
};
var results = await client.SearchMemoriesAsync(searchRequest);
// Get all memories for a user
var userMemories = await client.GetAllMemoriesAsync(userId: "user123");using Mem0.NET.Extensions;
// In your Program.cs or Startup.cs
services.AddMem0Client(options =>
{
options.BaseUrl = "https://api.mem0.ai";
options.ApiKey = "your-api-key";
});
// Use in your service
public class MyService
{
private readonly Mem0Client _mem0Client;
public MyService(Mem0Client mem0Client)
{
_mem0Client = mem0Client;
}
public async Task DoSomethingAsync()
{
var memories = await _mem0Client.GetAllMemoriesAsync();
// Your logic here
}
}CreateMemoryAsync(MemoryCreateRequest request)- Create new memoriesGetAllMemoriesAsync(string? userId, string? runId, string? agentId)- Retrieve all memoriesGetMemoryAsync(string memoryId)- Get a specific memory by IDUpdateMemoryAsync(string memoryId, Dictionary<string, object> updatedMemory)- Update a memoryDeleteMemoryAsync(string memoryId)- Delete a specific memoryDeleteAllMemoriesAsync(string? userId, string? runId, string? agentId)- Delete all memories
SearchMemoriesAsync(SearchRequest request)- Search memories with queriesGetMemoryHistoryAsync(string memoryId)- Get memory history
ConfigureAsync(Dictionary<string, object> config)- Configure Mem0 settingsResetMemoryAsync()- Reset all memories
public class MemoryCreateRequest
{
public List<Message> Messages { get; set; }
public string? UserId { get; set; }
public string? AgentId { get; set; }
public string? RunId { get; set; }
public Dictionary<string, object>? Metadata { get; set; }
}public class SearchRequest
{
public string Query { get; set; }
public string? UserId { get; set; }
public string? RunId { get; set; }
public string? AgentId { get; set; }
public Dictionary<string, object>? Filters { get; set; }
}public class Memory
{
public string Id { get; set; }
public string Content { get; set; }
public string? UserId { get; set; }
public string? AgentId { get; set; }
public string? RunId { get; set; }
public Dictionary<string, object>? Metadata { get; set; }
public DateTime? CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
}var createRequest = new MemoryCreateRequest
{
Messages = new List<Message>
{
new Message { Role = "user", Content = "I prefer working in the morning" },
new Message { Role = "assistant", Content = "I'll remember your morning preference" }
},
UserId = "user123",
Metadata = new Dictionary<string, object>
{
["category"] = "preference",
["priority"] = "high"
}
};
var memories = await client.CreateMemoryAsync(createRequest);var searchRequest = new SearchRequest
{
Query = "user preferences",
UserId = "user123",
Filters = new Dictionary<string, object>
{
["category"] = "preference",
["priority"] = "high"
}
};
var results = await client.SearchMemoriesAsync(searchRequest);var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(30);
var client = new Mem0Client("https://api.mem0.ai", "your-api-key", httpClient);The client throws exceptions for HTTP errors. Always wrap your calls in try-catch blocks:
try
{
var memories = await client.GetAllMemoriesAsync();
}
catch (HttpRequestException ex)
{
// Handle HTTP errors
Console.WriteLine($"HTTP Error: {ex.Message}");
}
catch (Exception ex)
{
// Handle other errors
Console.WriteLine($"Error: {ex.Message}");
}We welcome contributions! Please see our Contributing Guidelines for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for your changes
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
See CHANGELOG.md for a list of changes and updates.
Made with ❤️ by AIDotNet