Skip to content

Commit e2028ff

Browse files
committed
Added IVectorTextResultItem.Similarity and marked IVectorTextResultItem.VectorComparison obsolete
1 parent d5647ff commit e2028ff

File tree

9 files changed

+134
-39
lines changed

9 files changed

+134
-39
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## vNext
9+
10+
Add:
11+
12+
- Added `IVectorTextResultItem.Similarity` and marked `IVectorTextResultItem.VectorComparison` obsolete. `VectorComparison` will be removed in the future.
13+
- Added more comment metadata to code
14+
815
## v2.1.1
916

1017
Add:

samples/genai-rag-onnx/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static async Task Main(string[] args)
131131
ragContext += result.Text + "\n\n";
132132
// Print the metadata, vector comparison, and text of the result to the console
133133
Console.WriteLine($"Document: {result.Metadata}");
134-
Console.WriteLine($"Vector Comparison: {result.VectorComparison}");
134+
Console.WriteLine($"Vector Comparison: {result.Similarity}");
135135
Console.WriteLine($"Text Chunk Length: {result.Text.Length}");
136136
Console.WriteLine("");
137137
}

src/Build5Nines.SharpVector/VectorCompare/CosineSimilarityVectorComparerAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public float Calculate(float[] vectorA, float[] vectorB)
5959

6060
public IEnumerable<IVectorTextResultItem<TId, TDocument, TMetadata>> Sort<TId, TDocument, TMetadata>(IEnumerable<IVectorTextResultItem<TId, TDocument, TMetadata>> results)
6161
{
62-
return results.OrderByDescending(s => s.VectorComparison);
62+
return results.OrderByDescending(s => s.Similarity);
6363
}
6464

6565
public async Task<IEnumerable<IVectorTextResultItem<TId, TDocument, TMetadata>>> SortAsync<TId, TDocument, TMetadata>(IEnumerable<IVectorTextResultItem<TId, TDocument, TMetadata>> results)

src/Build5Nines.SharpVector/VectorCompare/EuclideanDistanceVectorComparerAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public float Calculate(float[] vectorA, float[] vectorB)
4343

4444
public IEnumerable<IVectorTextResultItem<TId, TDocument, TMetadata>> Sort<TId, TDocument, TMetadata>(IEnumerable<IVectorTextResultItem<TId, TDocument, TMetadata>> results)
4545
{
46-
return results.OrderBy(s => s.VectorComparison);
46+
return results.OrderBy(s => s.Similarity);
4747
}
4848

4949
public async Task<IEnumerable<IVectorTextResultItem<TId, TDocument, TMetadata>>> SortAsync<TId, TDocument, TMetadata>(IEnumerable<IVectorTextResultItem<TId, TDocument, TMetadata>> results)

src/Build5Nines.SharpVector/VectorTextResult.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@ namespace Build5Nines.SharpVector;
55
using System.Linq;
66
using System.Runtime.InteropServices;
77

8+
/// <summary>
9+
/// Represents a result of a vector text search.
10+
/// </summary>
11+
/// <typeparam name="TId">The type of the identifier.</typeparam>
12+
/// <typeparam name="TDocument">The type of the document.</typeparam>
13+
/// <typeparam name="TMetadata">The type of the metadata.</typeparam>
814
public interface IVectorTextResult<TId, TDocument, TMetadata>
915
{
16+
/// <summary>
17+
/// The list of Texts found in the search results.
18+
/// </summary>
1019
IEnumerable<IVectorTextResultItem<TId, TDocument, TMetadata>> Texts { get; }
1120

1221
/// <summary>
@@ -30,10 +39,20 @@ public interface IVectorTextResult<TId, TDocument, TMetadata>
3039
public int TotalPages { get; }
3140
}
3241

42+
/// <summary>
43+
/// Represents a result of a vector text search.
44+
/// </summary>
45+
/// <typeparam name="TMetadata">The type of the metadata.</typeparam>
3346
public interface IVectorTextResult<TMetadata>
3447
: IVectorTextResult<int, string, TMetadata>
3548
{ }
3649

50+
/// <summary>
51+
/// Represents a result of a vector text search.
52+
/// </summary>
53+
/// <typeparam name="TId">The type of the identifier.</typeparam>
54+
/// <typeparam name="TDocument">The type of the document.</typeparam>
55+
/// <typeparam name="TMetadata">The type of the metadata.</typeparam>
3756
public class VectorTextResult<TId, TDocument, TMetadata>
3857
: IVectorTextResult<TId, TDocument, TMetadata>
3958
{
@@ -50,6 +69,9 @@ public VectorTextResult(int totalCount, int pageIndex, int totalPages, IEnumerab
5069
/// </summary>
5170
public IEnumerable<IVectorTextResultItem<TId, TDocument, TMetadata>> Texts { get; private set; }
5271

72+
/// <summary>
73+
/// Returns true if the search returned no results.
74+
/// </summary>
5375
public bool IsEmpty { get => Texts == null || !Texts.Any(); }
5476

5577
/// <summary>
@@ -68,6 +90,10 @@ public VectorTextResult(int totalCount, int pageIndex, int totalPages, IEnumerab
6890
public int TotalPages { get; private set; }
6991
}
7092

93+
/// <summary>
94+
/// Represents a result of a vector text search.
95+
/// </summary>
96+
/// <typeparam name="TMetadata">The type of the metadata.</typeparam>
7197
public class VectorTextResult<TMetadata>
7298
: VectorTextResult<int, string, TMetadata>, IVectorTextResult<TMetadata>
7399
{

src/Build5Nines.SharpVector/VectorTextResultItem.cs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,106 @@
33

44
namespace Build5Nines.SharpVector;
55

6+
/// <summary>
7+
/// Represents a result item from a semantic search on a vector database.
8+
/// </summary>
9+
/// <typeparam name="TDocument">The type of the document.</typeparam>
10+
/// <typeparam name="TMetadata">The type of the metadata.</typeparam>
611
public interface IVectorTextResultItem<TDocument, TMetadata>
712
{
13+
/// <summary>
14+
/// The string of text that was vectorized.
15+
/// </summary>
816
TDocument Text{ get; }
17+
18+
/// <summary>
19+
/// The metadata associated with the text.
20+
/// </summary>
921
TMetadata? Metadata { get; }
1022

23+
/// <summary>
24+
/// The vector similarity score between the query and the text. (This is deprecated, use 'Similarity' instead)
25+
/// </summary>
26+
[Obsolete("Use 'Similarity' instead")]
1127
float VectorComparison { get; }
28+
29+
/// <summary>
30+
/// The vector similarity score between the query and the text.
31+
/// </summary>
32+
float Similarity { get; }
1233
}
1334

35+
/// <summary>
36+
/// Represents a result item from a semantic search on a vector database.
37+
/// </summary>
38+
/// <typeparam name="TId">The type of the ID.</typeparam>
39+
/// <typeparam name="TDocument">The type of the document.</typeparam>
40+
/// <typeparam name="TMetadata">The type of the metadata.</typeparam>
1441
public interface IVectorTextResultItem<TId, TDocument, TMetadata>
1542
: IVectorTextResultItem<TDocument, TMetadata>
1643
{
1744
TId Id { get; }
1845
}
1946

47+
/// <summary>
48+
/// Represents a result item from a semantic search on a vector database.
49+
/// </summary>
50+
/// <typeparam name="TMetadata">The type of the metadata.</typeparam>
2051
public interface IVectorTextResultItem<TMetadata>
2152
: IVectorTextResultItem<string, TMetadata>, IVectorTextResultItem<int, string, TMetadata>
2253
{ }
2354

55+
/// <summary>
56+
/// Represents a result item from a semantic search on a vector database.
57+
/// </summary>
58+
/// <typeparam name="TId">The type of the ID.</typeparam>
59+
/// <typeparam name="TDocument">The type of the document.</typeparam>
60+
/// <typeparam name="TMetadata">The type of the metadata.</typeparam>
2461
public class VectorTextResultItem<TId, TDocument, TMetadata>
2562
: IVectorTextResultItem<TDocument, TMetadata>, IVectorTextResultItem<TId, TDocument, TMetadata>
2663
{
2764
private IVectorTextItem<TDocument, TMetadata> _item;
2865
private TId _id;
2966

30-
public VectorTextResultItem(TId id, IVectorTextItem<TDocument, TMetadata> item, float vectorComparison)
67+
public VectorTextResultItem(TId id, IVectorTextItem<TDocument, TMetadata> item, float similarity)
3168
{
3269
_id = id;
3370
_item = item;
34-
VectorComparison = vectorComparison;
71+
Similarity = similarity;
3572
}
3673

74+
/// <summary>
75+
/// The string of text that was vectorized.
76+
/// </summary>
3777
public TDocument Text { get => _item.Text; }
78+
79+
/// <summary>
80+
/// The metadata associated with the text.
81+
/// </summary>
3882
public TMetadata? Metadata { get => _item.Metadata; }
3983
public TId Id { get => _id; }
4084

85+
/// <summary>
86+
/// The vector representation / embeddings of the text.
87+
/// </summary>
4188
public ImmutableArray<float> Vectors { get => ImmutableArray.Create(_item.Vector); }
4289

43-
public float VectorComparison { get; private set; }
90+
/// <summary>
91+
/// The vector similarity score between the query and the text.
92+
/// </summary>
93+
public float Similarity { get; private set; }
94+
95+
/// <summary>
96+
/// The vector similarity score between the query and the text. (This is deprecated, use 'Similarity' instead)
97+
/// </summary>
98+
[Obsolete("Use 'Similarity' instead")]
99+
public float VectorComparison { get => Similarity; }
44100
}
45101

102+
/// <summary>
103+
/// Represents a result item from a semantic search on a vector database.
104+
/// </summary>
105+
/// <typeparam name="TMetadata">The type of the metadata.</typeparam>
46106
public class VectorTextResultItem<TMetadata>
47107
: VectorTextResultItem<int, string, TMetadata>, IVectorTextResultItem<TMetadata>
48108
{

src/SharpVectorTest/Data/TextDataLoaderAsyncTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ public async Task TextDataLoaderAsync_Paragraphs_01()
4646
Assert.AreEqual(1, results.Texts.Count());
4747
Assert.AreEqual("The Lion King is a 1994 Disney animated film about a young lion cub named Simba who is the heir to the throne of an African savanna. ", results.Texts.First().Text);
4848
Assert.AreEqual("{ chuckSize: \"133\" }", results.Texts.First().Metadata);
49-
Assert.AreEqual(0.3396831452846527, results.Texts.First().VectorComparison);
49+
Assert.AreEqual(0.3396831452846527, results.Texts.First().Similarity);
5050
}
5151
}

src/SharpVectorTest/Regression/RegressionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void VectorDatabaseVersion_2_0_2_001()
2525
Assert.AreEqual(1, results.Texts.Count());
2626
Assert.IsTrue(results.Texts.First().Text.Contains("Lion King"));
2727
Assert.AreEqual("{ value: \"JSON Metadata Value\" }", results.Texts.First().Metadata);
28-
Assert.AreEqual(0.3396831452846527, results.Texts.First().VectorComparison);
28+
Assert.AreEqual(0.3396831452846527, results.Texts.First().Similarity);
2929
}
3030

3131
[TestMethod]

0 commit comments

Comments
 (0)