Skip to content

Commit

Permalink
规范使用字符串参数
Browse files Browse the repository at this point in the history
更新版本
  • Loading branch information
239573049 committed Aug 18, 2024
1 parent 96ec8a9 commit 064885a
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 68 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.91</Version>
<AssemblyVersion>0.1.91</AssemblyVersion>
<Version>0.2.0</Version>
<AssemblyVersion>0.2.0</AssemblyVersion>
<Authors>AIDotNet</Authors>
<Company>AIDotNet</Company>
<PackageId>$(AssemblyName)</PackageId>
Expand Down
33 changes: 5 additions & 28 deletions src/mem0.NET.Qdrant/Services/QdrantVectorStoresService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public async Task<List<SearchHit>> 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();
}

Expand Down Expand Up @@ -160,7 +160,7 @@ public async Task DeleteAsync(string name, Guid vectorId)
}

public async Task UpdateAsync(string name, Guid vectorId, List<float> vector = null,
Dictionary<string, object> payload = null)
Dictionary<string, string> payload = null)
{
var point = new PointStruct()
{
Expand All @@ -172,30 +172,7 @@ public async Task UpdateAsync(string name, Guid vectorId, List<float> 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);
}
}

Expand All @@ -214,7 +191,7 @@ public async Task<VectorData> 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)
};
}

Expand Down Expand Up @@ -262,7 +239,7 @@ public async Task<List<VectorData>> GetListAsync(string name, Dictionary<string,
{
Id = Guid.Parse(hit.Id.Uuid),
Vector = hit.Vectors.Vector.Data,
MetaData = hit.Payload.ToDictionary(x => x.Key, x => (object)x.Value)
MetaData = hit.Payload.ToDictionary(x => x.Key, x => x.Value.StringValue)
}).ToList();
}
}
15 changes: 2 additions & 13 deletions src/mem0.NET/Model/SearchHit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,9 @@ public class SearchHit
{
public Guid Id { get; set; } // 向量 ID
public float Score { get; set; } // 相似度分数
public Dictionary<string, object> Payload { get; set; } // 有效载荷
public Dictionary<string, string> Payload { get; set; } // 有效载荷

public Dictionary<string, object> MetaData { get; set; }

public string? Text
{
get
{
if (Payload.TryGetValue("data", out var data))
{
return JsonSerializer.Deserialize<VectorDataPayload>(data.ToString()).stringValue;
}

return string.Empty;
}
}
public string? Text => Payload["data"];
}
14 changes: 3 additions & 11 deletions src/mem0.NET/Model/VectorData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,13 @@ public class VectorData

public IReadOnlyList<float> Vector { get; set; }

public Dictionary<string, object> MetaData { get; set; } // 有效载荷
public Dictionary<string, string> MetaData { get; set; } // 有效载荷

public string? Text
{
get
{
if (MetaData.TryGetValue("data", out var data))
{
return JsonSerializer.Deserialize<VectorDataPayload>(data.ToString(),new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true
}).stringValue;
}

return string.Empty;
return MetaData["data"];
}
} // 文本
}
}
2 changes: 1 addition & 1 deletion src/mem0.NET/Services/MemoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public async Task<List<VectorData>> 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();
}

Expand Down
15 changes: 3 additions & 12 deletions src/mem0.NET/Services/MemoryToolService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>
var newMetadata = new Dictionary<string, string>
{
{ "data", prevValue },
{ "updated_at", DateTime.Now },
{ "updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
};

foreach (var o in existingMemory.MetaData)
Expand All @@ -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<VectorDataPayload>(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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/mem0.NET/VectorStores/IVectorStoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Task<List<SearchHit>> SearchAsync(string name, float[] query, ulong limit
/// <param name="payload"></param>
/// <returns></returns>
public Task UpdateAsync(string name, Guid vectorId, List<float> vector = null,
Dictionary<string, object> payload = null);
Dictionary<string, string> payload = null);

/// <summary>
/// Get vector
Expand Down

0 comments on commit 064885a

Please sign in to comment.