Skip to content

Commit

Permalink
优化接口入参
Browse files Browse the repository at this point in the history
  • Loading branch information
239573049 committed Jul 30, 2024
1 parent aa3a408 commit 7326162
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<Version>0.1.2</Version>
<AssemblyVersion>0.1.2</AssemblyVersion>
<Version>0.1.3</Version>
<AssemblyVersion>0.1.3</AssemblyVersion>
<Authors>AIDotNet</Authors>
<Company>AIDotNet</Company>
<PackageId>$(AssemblyName)</PackageId>
Expand Down
5 changes: 4 additions & 1 deletion mem0.NET.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue">&lt;AssemblyExplorer&gt;&#xD;
&lt;Assembly Path="C:\Users\23957\.nuget\packages\qdrant.client\1.10.0\lib\net6.0\Qdrant.Client.dll" /&gt;&#xD;
&lt;/AssemblyExplorer&gt;</s:String></wpf:ResourceDictionary>
&lt;/AssemblyExplorer&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=a085a8f0_002D382e_002D4664_002Db2b6_002D52d7b0a5573e/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="解决方案中的所有测试" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Solution /&gt;&#xD;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
60 changes: 60 additions & 0 deletions src/mem0.Core/VectorStores/IVectorStoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,81 @@ public interface IVectorStoreService
{
public Task CreateCol(string name, ulong vectorSize, Distance distance = Distance.Cosine);

/// <summary>
/// Insert vectors
/// </summary>
/// <param name="name"></param>
/// <param name="vectors"></param>
/// <param name="payloads"></param>
/// <param name="ids"></param>
/// <returns></returns>
public Task Insert(string name, List<List<float>> vectors, List<Dictionary<string, object>> payloads = null,
List<object> ids = null);

/// <summary>
/// Search for vectors
/// </summary>
/// <param name="name"></param>
/// <param name="query"></param>
/// <param name="limit"></param>
/// <param name="filters"></param>
/// <returns></returns>
public Task<List<SearchHit>> Search(string name, float[] query, ulong limit = 5UL,
Dictionary<string, object> filters = null);

/// <summary>
/// Delete vector
/// </summary>
/// <param name="name"></param>
/// <param name="vectorId"></param>
/// <returns></returns>
public Task Delete(string name, object vectorId);

/// <summary>
/// Update vector
/// </summary>
/// <param name="name"></param>
/// <param name="vectorId"></param>
/// <param name="vector"></param>
/// <param name="payload"></param>
/// <returns></returns>
public Task Update(string name, object vectorId, List<float> vector = null,
Dictionary<string, object> payload = null);

/// <summary>
/// Get vector
/// </summary>
/// <param name="name"></param>
/// <param name="vectorId"></param>
/// <returns></returns>
public Task<VectorData> Get(string name, object vectorId);

/// <summary>
/// List collections
/// </summary>
/// <returns></returns>
public Task<IReadOnlyList<string>> ListCols();

/// <summary>
/// Delete collection
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public Task DeleteCol(string name);

/// <summary>
/// Get collection info
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public Task<VectorInfo> ColInfo(string name);

/// <summary>
/// List vectors in a collection
/// </summary>
/// <param name="name"></param>
/// <param name="filters"></param>
/// <param name="limit"></param>
/// <returns></returns>
public Task<List<VectorData>> List(string name, Dictionary<string, object> filters = null, uint limit = 100U);
}
1 change: 0 additions & 1 deletion src/mem0.NET.Qdrant/Services/QdrantVectorStoresService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
40 changes: 15 additions & 25 deletions src/mem0.NET.Service/Services/MemoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("搜索记忆")
Expand All @@ -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("记忆");
Expand All @@ -104,7 +94,7 @@ await memoryService.Reset(vectorStoreService, mem0DotNetOptions, historyService)
.WithDescription("重置记忆")
.WithDisplayName("重置记忆")
.WithTags("记忆");

return app;
}
}
97 changes: 83 additions & 14 deletions src/mem0.NET/Services/MemoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// <summary>
/// Memory service.
/// </summary>
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.
Expand Down Expand Up @@ -49,11 +59,13 @@ public class MemoryService
""";


/// <summary>
/// 创建记忆
/// </summary>
/// <param name="input"></param>
/// <param name="mem0DotNetOptions"></param>
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);

Expand Down Expand Up @@ -117,7 +129,6 @@ await vectorStoreService.Search(mem0DotNetOptions.CollectionName, embeddings.ToA
ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,
}, kernel);

Console.WriteLine(content.Content);
}

private List<ChatMessageContent> get_update_memory_messages(object serializedExistingMemories,
Expand All @@ -136,9 +147,13 @@ private List<ChatMessageContent> get_update_memory_messages(object serializedExi
};
}

/// <summary>
/// 创建记忆工具
/// </summary>
/// <param name="input"></param>
/// <param name="mem0DotNetOptions"></param>
public async Task CreateMemoryToolAsync(CreateMemoryToolInput input,
ITextEmbeddingGenerationService textEmbeddingGenerationService,
Mem0DotNetOptions mem0DotNetOptions, IVectorStoreService vectorStoreService)
Mem0DotNetOptions mem0DotNetOptions)
{
var embeddings = await textEmbeddingGenerationService.GenerateEmbeddingAsync(input.Data);

Expand All @@ -151,7 +166,13 @@ await vectorStoreService.Insert(mem0DotNetOptions.CollectionName, [[..embeddings
[memoryId]);
}

public async Task<VectorData> GetMemory(string memoryId, IVectorStoreService vectorStoreService,
/// <summary>
/// 获取缓存
/// </summary>
/// <param name="memoryId"></param>
/// <param name="options"></param>
/// <returns></returns>
public async Task<VectorData> GetMemory(string memoryId,
Mem0DotNetOptions options)
{
var memory = await vectorStoreService.Get(options.CollectionName, memoryId);
Expand All @@ -164,7 +185,16 @@ public async Task<VectorData> GetMemory(string memoryId, IVectorStoreService vec
return memory;
}

public async Task<List<VectorData>> GetMemoryAll(IVectorStoreService vectorStoreService, Mem0DotNetOptions options,
/// <summary>
/// 获取所有记忆
/// </summary>
/// <param name="options"></param>
/// <param name="userId"></param>
/// <param name="agentId"></param>
/// <param name="runId"></param>
/// <param name="limit"></param>
/// <returns></returns>
public async Task<List<VectorData>> GetMemoryAll(Mem0DotNetOptions options,
string? userId,
string? agentId, string? runId, uint limit = 100)
{
Expand All @@ -189,8 +219,17 @@ public async Task<List<VectorData>> GetMemoryAll(IVectorStoreService vectorStore
return memories;
}

public async Task<List<VectorData>> SearchMemory(IVectorStoreService vectorStoreService,
ITextEmbeddingGenerationService textEmbeddingGenerationService, Mem0DotNetOptions options,
/// <summary>
/// 搜索记忆
/// </summary>
/// <param name="options"></param>
/// <param name="query"></param>
/// <param name="userId"></param>
/// <param name="agentId"></param>
/// <param name="runId"></param>
/// <param name="limit"></param>
/// <returns></returns>
public async Task<List<VectorData>> SearchMemory(Mem0DotNetOptions options,
string query, string? userId,
string? agentId, string? runId, uint limit = 100)
{
Expand Down Expand Up @@ -222,17 +261,36 @@ public async Task<List<VectorData>> SearchMemory(IVectorStoreService vectorStore
}).ToList();
}

/// <summary>
/// 更新记忆
/// </summary>
/// <param name="input"></param>
/// <param name="memoryTool"></param>
public async Task Update(UpdateMemoryInput input, MemoryTool memoryTool)
{
await memoryTool.UpdateMemory(input.MemoryId, input.Data);
}

/// <summary>
/// 删除指定记忆
/// </summary>
/// <param name="memoryId"></param>
/// <param name="memoryTool"></param>
public async Task Delete(string memoryId, MemoryTool memoryTool)
{
await memoryTool.DeleteMemory(memoryId);
}

public async Task DeleteAll(string? userId, string? agentId, string? runId, IVectorStoreService vectorStoreService,
/// <summary>
/// 删除所有记忆
/// </summary>
/// <param name="userId"></param>
/// <param name="agentId"></param>
/// <param name="runId"></param>
/// <param name="options"></param>
/// <param name="memoryTool"></param>
/// <exception cref="Exception"></exception>
public async Task DeleteAll(string? userId, string? agentId, string? runId,
Mem0DotNetOptions options, MemoryTool memoryTool)
{
var filters = new Dictionary<string, object>();
Expand Down Expand Up @@ -265,13 +323,24 @@ public async Task DeleteAll(string? userId, string? agentId, string? runId, IVec
}
}

public async Task<List<History>> GetHistory(string memoryId, IHistoryService historyService)
/// <summary>
/// 获取历史
/// </summary>
/// <param name="memoryId"></param>
/// <returns></returns>
public async Task<List<History>> GetHistory(string memoryId)
{
var histories = await historyService.GetHistories(memoryId);

return histories;
}

/// <summary>
/// 重置记忆
/// </summary>
/// <param name="vectorStoreService"></param>
/// <param name="mem0DotNetOptions"></param>
/// <param name="historyService"></param>
public async Task Reset(IVectorStoreService vectorStoreService, Mem0DotNetOptions mem0DotNetOptions,
IHistoryService historyService)
{
Expand Down

0 comments on commit 7326162

Please sign in to comment.