From 064885a681438a2290c60112ad6a5d49cee9b36c Mon Sep 17 00:00:00 2001 From: token <239573049@qq.com> Date: Sun, 18 Aug 2024 23:17:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E8=8C=83=E4=BD=BF=E7=94=A8=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E5=8F=82=E6=95=B0=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Directory.Build.props | 4 +-- .../Services/QdrantVectorStoresService.cs | 33 +++---------------- src/mem0.NET/Model/SearchHit.cs | 15 ++------- src/mem0.NET/Model/VectorData.cs | 14 ++------ src/mem0.NET/Services/MemoryService.cs | 2 +- src/mem0.NET/Services/MemoryToolService.cs | 15 ++------- .../VectorStores/IVectorStoreService.cs | 2 +- 7 files changed, 17 insertions(+), 68 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 58336f8..fd2330b 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 0.1.91 - 0.1.91 + 0.2.0 + 0.2.0 AIDotNet AIDotNet $(AssemblyName) diff --git a/src/mem0.NET.Qdrant/Services/QdrantVectorStoresService.cs b/src/mem0.NET.Qdrant/Services/QdrantVectorStoresService.cs index 5e4a4c1..262253a 100644 --- a/src/mem0.NET.Qdrant/Services/QdrantVectorStoresService.cs +++ b/src/mem0.NET.Qdrant/Services/QdrantVectorStoresService.cs @@ -103,7 +103,7 @@ public async Task> SearchAsync(string name, float[] query, ulong { Id = Guid.Parse(hit.Id.Uuid), Score = hit.Score, - Payload = hit.Payload.ToDictionary(x => x.Key, x => (object)x.Value), + Payload = hit.Payload.ToDictionary(x => x.Key, x => x.Value.StringValue), }).ToList(); } @@ -160,7 +160,7 @@ public async Task DeleteAsync(string name, Guid vectorId) } public async Task UpdateAsync(string name, Guid vectorId, List vector = null, - Dictionary payload = null) + Dictionary payload = null) { var point = new PointStruct() { @@ -172,30 +172,7 @@ public async Task UpdateAsync(string name, Guid vectorId, List vector = n { foreach (var item in payload) { - if (item.Value is string str) - { - point.Payload.Add(item.Key, str); - } - else if (item.Value is float f) - { - point.Payload.Add(item.Key, f); - } - else if (item.Value is int i) - { - point.Payload.Add(item.Key, i); - } - else if (item.Value is bool b) - { - point.Payload.Add(item.Key, b); - } - else if (item.Value is Color color) - { - point.Payload.Add(item.Key, color.ToArgb()); - } - else - { - point.Payload.Add(item.Key, JsonSerializer.Serialize(item.Value)); - } + point.Payload.Add(item.Key, item.Value); } } @@ -214,7 +191,7 @@ public async Task GetAsync(string name, Guid vectorId) { Id = new Guid(result.Id.Uuid), Vector = result.Vectors.Vector.Data.ToList(), - MetaData = result.Payload.ToDictionary(x => x.Key, x => (object)x.Value) + MetaData = result.Payload.ToDictionary(x => x.Key, x => x.Value.StringValue) }; } @@ -262,7 +239,7 @@ public async Task> GetListAsync(string name, Dictionary x.Key, x => (object)x.Value) + MetaData = hit.Payload.ToDictionary(x => x.Key, x => x.Value.StringValue) }).ToList(); } } \ No newline at end of file diff --git a/src/mem0.NET/Model/SearchHit.cs b/src/mem0.NET/Model/SearchHit.cs index fbe8be2..edd56fb 100644 --- a/src/mem0.NET/Model/SearchHit.cs +++ b/src/mem0.NET/Model/SearchHit.cs @@ -6,20 +6,9 @@ public class SearchHit { public Guid Id { get; set; } // 向量 ID public float Score { get; set; } // 相似度分数 - public Dictionary Payload { get; set; } // 有效载荷 + public Dictionary Payload { get; set; } // 有效载荷 public Dictionary MetaData { get; set; } - public string? Text - { - get - { - if (Payload.TryGetValue("data", out var data)) - { - return JsonSerializer.Deserialize(data.ToString()).stringValue; - } - - return string.Empty; - } - } + public string? Text => Payload["data"]; } \ No newline at end of file diff --git a/src/mem0.NET/Model/VectorData.cs b/src/mem0.NET/Model/VectorData.cs index f1052da..6ea02ef 100644 --- a/src/mem0.NET/Model/VectorData.cs +++ b/src/mem0.NET/Model/VectorData.cs @@ -11,21 +11,13 @@ public class VectorData public IReadOnlyList Vector { get; set; } - public Dictionary MetaData { get; set; } // 有效载荷 + public Dictionary MetaData { get; set; } // 有效载荷 public string? Text { get { - if (MetaData.TryGetValue("data", out var data)) - { - return JsonSerializer.Deserialize(data.ToString(),new JsonSerializerOptions() - { - PropertyNameCaseInsensitive = true - }).stringValue; - } - - return string.Empty; + return MetaData["data"]; } } // 文本 -} +} \ No newline at end of file diff --git a/src/mem0.NET/Services/MemoryService.cs b/src/mem0.NET/Services/MemoryService.cs index 15c4310..704c961 100644 --- a/src/mem0.NET/Services/MemoryService.cs +++ b/src/mem0.NET/Services/MemoryService.cs @@ -251,7 +251,7 @@ public async Task> SearchMemory(string query, string? userId, { Id = x.Id, Score = x.Score, - MetaData = x.Payload + MetaData = x.Payload.ToDictionary(y => y.Key, y => y.Value), }).ToList(); } diff --git a/src/mem0.NET/Services/MemoryToolService.cs b/src/mem0.NET/Services/MemoryToolService.cs index 4caff88..c2cb4bb 100644 --- a/src/mem0.NET/Services/MemoryToolService.cs +++ b/src/mem0.NET/Services/MemoryToolService.cs @@ -67,10 +67,10 @@ public async Task UpdateMemoryAsync(Guid memoryId, var existingMemory = await vectorStoreService.GetAsync(options.Value.CollectionName, memoryId); var prevValue = existingMemory.MetaData["data"]; - var newMetadata = new Dictionary + var newMetadata = new Dictionary { { "data", prevValue }, - { "updated_at", DateTime.Now }, + { "updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }, }; foreach (var o in existingMemory.MetaData) @@ -84,18 +84,9 @@ public async Task UpdateMemoryAsync(Guid memoryId, await vectorStoreService.UpdateAsync(options.Value.CollectionName, memoryId, [..existingMemory.Vector.ToArray()], newMetadata); - var memoryText = string.Empty; - try - { - memoryText = JsonSerializer.Deserialize(prevValue.ToString())?.stringValue; - } - catch - { - memoryText = prevValue.ToString(); - } await historyService.AddHistoryAsync(memoryId.ToString(), - memoryText, data, "update", false, ApplicationContext.HistoryUserId.Value, + prevValue, data, "update", false, ApplicationContext.HistoryUserId.Value, ApplicationContext.HistoryTrackId.Value); } diff --git a/src/mem0.NET/VectorStores/IVectorStoreService.cs b/src/mem0.NET/VectorStores/IVectorStoreService.cs index 2e5d5a2..4ff9291 100644 --- a/src/mem0.NET/VectorStores/IVectorStoreService.cs +++ b/src/mem0.NET/VectorStores/IVectorStoreService.cs @@ -45,7 +45,7 @@ public Task> SearchAsync(string name, float[] query, ulong limit /// /// public Task UpdateAsync(string name, Guid vectorId, List vector = null, - Dictionary payload = null); + Dictionary payload = null); /// /// Get vector