AzureDocumentDB: CosineDistance returns similarity scores, and ScoreThreshold filters the wrong way
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 45/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- azure, csharp, docker, mongodb
- 领域
- databases, testing-qa
调研方向
Read MEVD/src/AzureDocumentDB/DocumentDBCollectionCreateMapping.cs, MEVD/src/AzureDocumentDB/DocumentDBCollectionSearchMapping.cs, and MEVD/src/AzureDocumentDB/DocumentDBCollection.cs to compare the provider’s cosine score and threshold semantics with InMemory. Run the supplied dotnet run repro.cs, then inspect MEVD/test/AzureDocumentDB.ConformanceTests/DocumentDBDistanceFunctionTests.cs for the current score assertions. Completion requires agreeing whether to normalize similarity into distance, adjusting the threshold comparison, and updating conformance coverage without silently changing existing behavior.
由索引模型根据 Issue 内容生成。
描述
With CosineDistance, the default, AzureDocumentDB returns the cosine similarity as Score, and ScoreThreshold keeps scores >= the threshold. InMemory returns the distance and keeps scores <= it.
Repro: with Docker running, dotnet run repro.cs.
#:package [email protected]
#:package [email protected]
#:package [email protected]
#:property PublishAot=false
using System.Security.Authentication;
using CommunityToolkit.VectorData.AzureDocumentDB;
using CommunityToolkit.VectorData.InMemory;
using DotNet.Testcontainers.Builders;
using Microsoft.Extensions.VectorData;
using MongoDB.Driver;
// Starts DocumentDB local in Docker, then compares InMemory and AzureDocumentDB with CosineDistance.
await using var container = new ContainerBuilder("ghcr.io/microsoft/documentdb/documentdb-local:latest")
.WithEnvironment("USERNAME", "user")
.WithEnvironment("PASSWORD", "Pass123!")
.WithPortBinding(10260, true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilInternalTcpPortIsAvailable(10260))
.Build();
await container.StartAsync();
var settings = MongoClientSettings.FromConnectionString($"mongodb://user:Pass123!@{container.Hostname}:{container.GetMappedPublicPort(10260)}/?tls=true");
settings.SslSettings = new SslSettings { EnabledSslProtocols = SslProtocols.Tls12, ServerCertificateValidationCallback = (_, _, _, _) => true };
var documentDb = new DocumentDBVectorStore(new MongoClient(settings).GetDatabase("repro"));
var inMemory = new InMemoryVectorStore();
Hotel[] hotels =
[
new() { Id = "same", Vector = new float[] { 1, 0, 0 } },
new() { Id = "orthogonal", Vector = new float[] { 0, 1, 0 } },
new() { Id = "opposite", Vector = new float[] { -1, 0, 0 } },
];
ReadOnlyMemory<float> query = new float[] { 1, 0, 0 };
foreach (var (name, store) in new (string, VectorStore)[] { ("InMemory", inMemory), ("DocumentDB", documentDb) })
{
var collection = store.GetCollection<string, Hotel>("hotels");
await collection.EnsureCollectionDeletedAsync();
await collection.EnsureCollectionExistsAsync();
await collection.UpsertAsync(hotels);
await Task.Delay(1000);
Console.WriteLine($"{name} CosineDistance");
await foreach (var r in collection.SearchAsync(query, 3))
Console.WriteLine($" {r.Record.Id,-10} score {r.Score:0.##}");
var within = await collection.SearchAsync(query, 3, new() { ScoreThreshold = 1.5 }).Select(r => r.Record.Id).ToListAsync();
Console.WriteLine($" ScoreThreshold = 1.5 -> [{string.Join(", ", within)}]");
}
public sealed class Hotel
{
[VectorStoreKey] public string Id { get; set; } = "";
[VectorStoreVector(3, DistanceFunction = DistanceFunction.CosineDistance)] public ReadOnlyMemory<float> Vector { get; set; }
}
InMemory CosineDistance
same score 0
orthogonal score 1
opposite score 2
ScoreThreshold = 1.5 -> [same, orthogonal]
DocumentDB CosineDistance
same score 1
orthogonal score 0
opposite score -1
ScoreThreshold = 1.5 -> []
Cause:
CosineDistancemaps toCOS(DocumentDBCollectionCreateMapping.cs#L123), which returns a similarity.- The threshold uses
$gte(DocumentDBCollectionSearchMapping.cs#L98-L110). - The score is returned as is (DocumentDBCollection.cs#L575-L578).
AssertScores = falsein the conformance tests hides it (DocumentDBDistanceFunctionTests.cs#L24).
Returning 1 - similarity and filtering with $lte would match the other providers, but it changes the scores existing users get. Which way do you prefer? I can send the PR.
- 主要语言
- C#
- 星标
- 8
- 派生
- 8
- 平均合并
- 1 天 8 小时
- 30 天内合并 PR
- 10
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
CommunityToolkit/AI 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
CommunityToolkit/AI#19 ·
维护者通常 1 天内回复
-
enhancement
难度 5/5 一周以上 新手友好度 35/100
CommunityToolkit/AI#61 ·
维护者通常 1 天内回复
-
enhancement
难度 5/5 一周以上 新手友好度 35/100
CommunityToolkit/AI#59 ·
维护者通常 1 天内回复
-
Report all Cosmos DB emulator issues可能重新可做 @adamsitnik 于 103 天前认领,目前没有进行中的 PR。 未关闭
CommunityToolkit/AI#23 · 已指派 1 人 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 45/100
CommunityToolkit/AI#20 ·
维护者通常 1 天内回复
查看 CommunityToolkit/AI 的全部 Issue
相似的 Issue
-
status:waiting-for-triage type:bug
难度 1/5 1 小时以内 新手友好度 84/100
microsoftgraph/msgraph-sdk-dotnet#3194 ·
维护者通常 1 天内回复
-
Backend enhancement
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复
-
UI: money shows the minus sign next to the number, not before the currency (IL -3.0m, not -IL 3.0m)未关闭area: sim area: unity enhancement
难度 2/5 1-3 小时 新手友好度 78/100
eyalgolan/statecraft-israel#1107 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 83/100
dimitar-radenkov/Pointframe#271 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100
qew21/Genshin-Subtitles#45 ·