diff --git a/Directory.Build.props b/Directory.Build.props index b817536..8a706b5 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 0.1.2 - 0.1.2 + 0.1.3 + 0.1.3 AIDotNet AIDotNet $(AssemblyName) diff --git a/mem0.NET.sln.DotSettings.user b/mem0.NET.sln.DotSettings.user index 1b1d9e2..52a5f35 100644 --- a/mem0.NET.sln.DotSettings.user +++ b/mem0.NET.sln.DotSettings.user @@ -1,4 +1,7 @@  <AssemblyExplorer> <Assembly Path="C:\Users\23957\.nuget\packages\qdrant.client\1.10.0\lib\net6.0\Qdrant.Client.dll" /> -</AssemblyExplorer> \ No newline at end of file +</AssemblyExplorer> + <SessionState ContinuousTestingMode="0" IsActive="True" Name="解决方案中的所有测试" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <Solution /> +</SessionState> \ No newline at end of file diff --git a/src/mem0.Core/VectorStores/IVectorStoreService.cs b/src/mem0.Core/VectorStores/IVectorStoreService.cs index bab9eb7..f970da6 100644 --- a/src/mem0.Core/VectorStores/IVectorStoreService.cs +++ b/src/mem0.Core/VectorStores/IVectorStoreService.cs @@ -6,21 +6,81 @@ public interface IVectorStoreService { public Task CreateCol(string name, ulong vectorSize, Distance distance = Distance.Cosine); + /// + /// Insert vectors + /// + /// + /// + /// + /// + /// public Task Insert(string name, List> vectors, List> payloads = null, List ids = null); + /// + /// Search for vectors + /// + /// + /// + /// + /// + /// public Task> Search(string name, float[] query, ulong limit = 5UL, Dictionary filters = null); + /// + /// Delete vector + /// + /// + /// + /// public Task Delete(string name, object vectorId); + /// + /// Update vector + /// + /// + /// + /// + /// + /// public Task Update(string name, object vectorId, List vector = null, Dictionary payload = null); + /// + /// Get vector + /// + /// + /// + /// public Task Get(string name, object vectorId); + + /// + /// List collections + /// + /// public Task> ListCols(); + + /// + /// Delete collection + /// + /// + /// public Task DeleteCol(string name); + + /// + /// Get collection info + /// + /// + /// public Task ColInfo(string name); + /// + /// List vectors in a collection + /// + /// + /// + /// + /// public Task> List(string name, Dictionary filters = null, uint limit = 100U); } \ No newline at end of file diff --git a/src/mem0.NET.Qdrant/Services/QdrantVectorStoresService.cs b/src/mem0.NET.Qdrant/Services/QdrantVectorStoresService.cs index c23e646..f6cc2ea 100644 --- a/src/mem0.NET.Qdrant/Services/QdrantVectorStoresService.cs +++ b/src/mem0.NET.Qdrant/Services/QdrantVectorStoresService.cs @@ -7,7 +7,6 @@ using Distance = Qdrant.Client.Grpc.Distance; using MDistance = mem0.Core.Distance; using OptimizerStatus = mem0.Core.Model.OptimizerStatus; -using Range = Qdrant.Client.Grpc.Range; namespace mem0.NET.Qdrabt.Services; diff --git a/src/mem0.NET.Service/Services/MemoryService.cs b/src/mem0.NET.Service/Services/MemoryService.cs index 8bdcd06..425ccda 100644 --- a/src/mem0.NET.Service/Services/MemoryService.cs +++ b/src/mem0.NET.Service/Services/MemoryService.cs @@ -22,56 +22,46 @@ public static WebApplication MapMemoryService(this WebApplication app) memoryService.MapPost("/memory", async (MemoryService memoryService, CreateMemoryInput input, - ITextEmbeddingGenerationService textEmbeddingGenerationService, - IChatCompletionService chatCompletionService, - Kernel kernel, - Mem0DotNetOptions mem0DotNetOptions, - IVectorStoreService vectorStoreService) => + Mem0DotNetOptions mem0DotNetOptions) => { - await memoryService.CreateMemoryAsync(input, textEmbeddingGenerationService, chatCompletionService, - mem0DotNetOptions, vectorStoreService, kernel); + await memoryService.CreateMemoryAsync(input, + mem0DotNetOptions); }).WithDescription("创建记忆").WithDisplayName("创建记忆").WithTags("记忆").WithName("CreateMemory"); memoryService.MapPost("memory_tool", async (MemoryService memoryService, CreateMemoryToolInput input, - ITextEmbeddingGenerationService textEmbeddingGenerationService, - Mem0DotNetOptions mem0DotNetOptions, - IVectorStoreService vectorStoreService) => + Mem0DotNetOptions mem0DotNetOptions) => { - await memoryService.CreateMemoryToolAsync(input, textEmbeddingGenerationService, - mem0DotNetOptions, vectorStoreService); + await memoryService.CreateMemoryToolAsync(input, + mem0DotNetOptions); }).WithDescription("创建记忆工具").WithDisplayName("创建记忆工具").WithTags("记忆"); - memoryService.MapGet("history/{memoryId}", async (MemoryService memoryService, string memoryId, - IHistoryService historyService) => await memoryService.GetHistory(memoryId, historyService)) + memoryService.MapGet("history/{memoryId}", + async (MemoryService memoryService, string memoryId) => await memoryService.GetHistory(memoryId)) .WithDescription("获取历史").WithDisplayName("获取历史").WithTags("记忆"); memoryService.MapGet("memory/{memoryId}", async (MemoryService memoryService, string memoryId, - Mem0DotNetOptions options, - IVectorStoreService vectorStoreService) => - await memoryService.GetMemory(memoryId, vectorStoreService, options)) + Mem0DotNetOptions options) => + await memoryService.GetMemory(memoryId, options)) .WithDescription("获取记忆") .WithDisplayName("获取记忆") .WithTags("记忆"); memoryService.MapGet("memory", async (MemoryService memoryService, - IVectorStoreService vectorStoreService, Mem0DotNetOptions options, string? userId, string? agentId, string? runId, uint limit) => - await memoryService.GetMemoryAll(vectorStoreService, options, userId, agentId, runId, limit)) + await memoryService.GetMemoryAll(options, userId, agentId, runId, limit)) .WithDescription("获取所有记忆") .WithDisplayName("获取所有记忆") .WithTags("记忆"); memoryService.MapGet("search", async (MemoryService memoryService, - IVectorStoreService vectorStoreService, - ITextEmbeddingGenerationService textEmbeddingGenerationService, Mem0DotNetOptions options, string query, string? userId, string? agentId, string? runId, uint limit) => - await memoryService.SearchMemory(vectorStoreService, textEmbeddingGenerationService, options, query, + await memoryService.SearchMemory(options, query, userId, agentId, runId, limit)) .WithDescription("搜索记忆") @@ -91,9 +81,9 @@ await memoryService.SearchMemory(vectorStoreService, textEmbeddingGenerationServ .WithTags("记忆"); memoryService.MapDelete("memory", async (MemoryService memoryService, string? userId, - string? agentId, string? runId, IVectorStoreService vectorStoreService, + string? agentId, string? runId, Mem0DotNetOptions options, MemoryTool memoryTool) => - await memoryService.DeleteAll(userId, agentId, runId, vectorStoreService, options, memoryTool)) + await memoryService.DeleteAll(userId, agentId, runId, options, memoryTool)) .WithDescription("删除所有记忆") .WithDisplayName("删除所有记忆") .WithTags("记忆"); @@ -104,7 +94,7 @@ await memoryService.Reset(vectorStoreService, mem0DotNetOptions, historyService) .WithDescription("重置记忆") .WithDisplayName("重置记忆") .WithTags("记忆"); - + return app; } } \ No newline at end of file diff --git a/src/mem0.NET/Services/MemoryService.cs b/src/mem0.NET/Services/MemoryService.cs index c09c71a..6eb34ac 100644 --- a/src/mem0.NET/Services/MemoryService.cs +++ b/src/mem0.NET/Services/MemoryService.cs @@ -10,11 +10,21 @@ using Microsoft.SemanticKernel.Connectors.OpenAI; using Microsoft.SemanticKernel.Embeddings; +#pragma warning disable SKEXP0050 + #pragma warning disable SKEXP0001 namespace mem0.NET.Services; -public class MemoryService +/// +/// Memory service. +/// +public class MemoryService( + IVectorStoreService vectorStoreService, + Kernel kernel, + ITextEmbeddingGenerationService textEmbeddingGenerationService, + IChatCompletionService chatCompletionService, + IHistoryService historyService) { private const string MemoryDeductionPrompt = """ Deduce the facts, preferences, and memories from the provided text. @@ -49,11 +59,13 @@ public class MemoryService """; + /// + /// 创建记忆 + /// + /// + /// public async Task CreateMemoryAsync(CreateMemoryInput input, - ITextEmbeddingGenerationService textEmbeddingGenerationService, - IChatCompletionService chatCompletionService, - Mem0DotNetOptions mem0DotNetOptions, - IVectorStoreService vectorStoreService, Kernel kernel) + Mem0DotNetOptions mem0DotNetOptions) { var embeddings = await textEmbeddingGenerationService.GenerateEmbeddingAsync(input.Data); @@ -117,7 +129,6 @@ await vectorStoreService.Search(mem0DotNetOptions.CollectionName, embeddings.ToA ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions, }, kernel); - Console.WriteLine(content.Content); } private List get_update_memory_messages(object serializedExistingMemories, @@ -136,9 +147,13 @@ private List get_update_memory_messages(object serializedExi }; } + /// + /// 创建记忆工具 + /// + /// + /// public async Task CreateMemoryToolAsync(CreateMemoryToolInput input, - ITextEmbeddingGenerationService textEmbeddingGenerationService, - Mem0DotNetOptions mem0DotNetOptions, IVectorStoreService vectorStoreService) + Mem0DotNetOptions mem0DotNetOptions) { var embeddings = await textEmbeddingGenerationService.GenerateEmbeddingAsync(input.Data); @@ -151,7 +166,13 @@ await vectorStoreService.Insert(mem0DotNetOptions.CollectionName, [[..embeddings [memoryId]); } - public async Task GetMemory(string memoryId, IVectorStoreService vectorStoreService, + /// + /// 获取缓存 + /// + /// + /// + /// + public async Task GetMemory(string memoryId, Mem0DotNetOptions options) { var memory = await vectorStoreService.Get(options.CollectionName, memoryId); @@ -164,7 +185,16 @@ public async Task GetMemory(string memoryId, IVectorStoreService vec return memory; } - public async Task> GetMemoryAll(IVectorStoreService vectorStoreService, Mem0DotNetOptions options, + /// + /// 获取所有记忆 + /// + /// + /// + /// + /// + /// + /// + public async Task> GetMemoryAll(Mem0DotNetOptions options, string? userId, string? agentId, string? runId, uint limit = 100) { @@ -189,8 +219,17 @@ public async Task> GetMemoryAll(IVectorStoreService vectorStore return memories; } - public async Task> SearchMemory(IVectorStoreService vectorStoreService, - ITextEmbeddingGenerationService textEmbeddingGenerationService, Mem0DotNetOptions options, + /// + /// 搜索记忆 + /// + /// + /// + /// + /// + /// + /// + /// + public async Task> SearchMemory(Mem0DotNetOptions options, string query, string? userId, string? agentId, string? runId, uint limit = 100) { @@ -222,17 +261,36 @@ public async Task> SearchMemory(IVectorStoreService vectorStore }).ToList(); } + /// + /// 更新记忆 + /// + /// + /// public async Task Update(UpdateMemoryInput input, MemoryTool memoryTool) { await memoryTool.UpdateMemory(input.MemoryId, input.Data); } + /// + /// 删除指定记忆 + /// + /// + /// public async Task Delete(string memoryId, MemoryTool memoryTool) { await memoryTool.DeleteMemory(memoryId); } - public async Task DeleteAll(string? userId, string? agentId, string? runId, IVectorStoreService vectorStoreService, + /// + /// 删除所有记忆 + /// + /// + /// + /// + /// + /// + /// + public async Task DeleteAll(string? userId, string? agentId, string? runId, Mem0DotNetOptions options, MemoryTool memoryTool) { var filters = new Dictionary(); @@ -265,13 +323,24 @@ public async Task DeleteAll(string? userId, string? agentId, string? runId, IVec } } - public async Task> GetHistory(string memoryId, IHistoryService historyService) + /// + /// 获取历史 + /// + /// + /// + public async Task> GetHistory(string memoryId) { var histories = await historyService.GetHistories(memoryId); return histories; } + /// + /// 重置记忆 + /// + /// + /// + /// public async Task Reset(IVectorStoreService vectorStoreService, Mem0DotNetOptions mem0DotNetOptions, IHistoryService historyService) {