Skip to content

Commit 27e8457

Browse files
yorekwestey-m
andauthored
.Net: Fixed sorting for negative DOT product (#13071)
### Motivation and Context When using negative dot product scoring, the more negative the value, the closer the vector is to the query vector. ### Description Fixed the sorting used for negative dot product distance calculations ### Contribution Checklist - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: westey <[email protected]>
1 parent 6e4097b commit 27e8457

File tree

4 files changed

+4
-20
lines changed

4 files changed

+4
-20
lines changed

dotnet/src/VectorData/SqlServer/SqlServerCommandBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,8 @@ private static string Map(PropertyModel property)
657657
DistanceFunction.CosineDistance => ("COSINE", "ASC"),
658658
// A value of 0 indicates that the vectors are identical, while larger values indicate greater dissimilarity.
659659
DistanceFunction.EuclideanDistance => ("EUCLIDEAN", "ASC"),
660-
// A value closer to 0 indicates higher similarity, while more negative values indicate greater dissimilarity.
661-
DistanceFunction.NegativeDotProductSimilarity => ("DOT", "DESC"),
660+
// Smaller numbers indicate more similar vectors
661+
DistanceFunction.NegativeDotProductSimilarity => ("DOT", "ASC"),
662662
_ => throw new NotSupportedException($"Distance function {name} is not supported.")
663663
};
664664
}

dotnet/test/VectorData/SqlServer.ConformanceTests/Support/SqlServerTestEnvironment.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

33
using Microsoft.Extensions.Configuration;
4-
using Microsoft.SemanticKernel.Connectors.SqlServer;
54

65
namespace SqlServer.ConformanceTests.Support;
76

@@ -17,7 +16,7 @@ internal static class SqlServerTestEnvironment
1716
.AddJsonFile(path: "testsettings.json", optional: true)
1817
.AddJsonFile(path: "testsettings.development.json", optional: true)
1918
.AddEnvironmentVariables()
20-
.AddUserSecrets<SqlServerVectorStore>()
19+
.AddUserSecrets<SqlServerFixture>()
2120
.Build();
2221

2322
return configuration.GetSection("SqlServer")["ConnectionString"];

dotnet/test/VectorData/VectorData.ConformanceTests/VectorSearch/VectorSearchDistanceFunctionComplianceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public virtual Task DotProductSimilarity()
2424

2525
[ConditionalFact]
2626
public virtual Task NegativeDotProductSimilarity()
27-
=> this.SimpleSearch(DistanceFunction.NegativeDotProductSimilarity, -1, 1, 0, [1, 2, 0]);
27+
=> this.SimpleSearch(DistanceFunction.NegativeDotProductSimilarity, -1, 1, 0, [0, 2, 1]);
2828

2929
[ConditionalFact]
3030
public virtual Task EuclideanDistance()
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3-
using Microsoft.Extensions.VectorData;
43
using VectorData.ConformanceTests.VectorSearch;
54
using Weaviate.ConformanceTests.Support;
65
using Xunit;
@@ -15,13 +14,6 @@ public class WeaviateVectorSearchDistanceFunctionComplianceTests_NamedVectors(We
1514
public override Task DotProductSimilarity() => Assert.ThrowsAsync<NotSupportedException>(base.DotProductSimilarity);
1615

1716
public override Task EuclideanDistance() => Assert.ThrowsAsync<NotSupportedException>(base.EuclideanDistance);
18-
19-
/// <summary>
20-
/// Tests vector search using <see cref="DistanceFunction.NegativeDotProductSimilarity"/>, computing -(u · v) as a distance metric per Weaviate's convention.
21-
/// Expects scores of -1 (exact match), 1 (opposite), and 0 (orthogonal), sorted ascending ([0, 2, 1]), with lower scores indicating closer matches.
22-
/// <see href="https://weaviate.io/developers/weaviate/config-refs/distances#available-distance-metrics"/>.
23-
/// </summary>
24-
public override Task NegativeDotProductSimilarity() => this.SimpleSearch(DistanceFunction.NegativeDotProductSimilarity, -1, 1, 0, [0, 2, 1]);
2517
}
2618

2719
public class WeaviateVectorSearchDistanceFunctionComplianceTests_UnnamedVector(WeaviateDynamicDataModelNamedVectorsFixture fixture)
@@ -32,11 +24,4 @@ public class WeaviateVectorSearchDistanceFunctionComplianceTests_UnnamedVector(W
3224
public override Task DotProductSimilarity() => Assert.ThrowsAsync<NotSupportedException>(base.DotProductSimilarity);
3325

3426
public override Task EuclideanDistance() => Assert.ThrowsAsync<NotSupportedException>(base.EuclideanDistance);
35-
36-
/// <summary>
37-
/// Tests vector search using <see cref="DistanceFunction.NegativeDotProductSimilarity"/>, computing -(u · v) as a distance metric per Weaviate's convention.
38-
/// Expects scores of -1 (exact match), 1 (opposite), and 0 (orthogonal), sorted ascending ([0, 2, 1]), with lower scores indicating closer matches.
39-
/// <see href="https://weaviate.io/developers/weaviate/config-refs/distances#available-distance-metrics"/>.
40-
/// </summary>
41-
public override Task NegativeDotProductSimilarity() => this.SimpleSearch(DistanceFunction.NegativeDotProductSimilarity, -1, 1, 0, [0, 2, 1]);
4227
}

0 commit comments

Comments
 (0)