Skip to content

Commit 1cd09c1

Browse files
Refactor and improve Qdrant DotNet code
Summary: This commit applies various refactoring and formatting changes to the Qdrant DotNet code, which is a wrapper for the Qdrant vector database. The changes include: - Breaking long lines into multiple lines for better readability and consistency - Adding or removing blank lines and spaces where needed - Using expression-bodied properties and constructors, null coalescing operator, string interpolation, var keyword, and discard variable where possible - Simplifying some boolean expressions and null checks - Renaming some variables and methods for clarity - Adding some validation checks and error messages - Adding missing namespace and XML documentation - Fixing a bug in the FetchVectorsResponse class, where the Json parsing was not using the System.Text.Json library - Fixing a bug in the DeleteCollectionAsync method, where the response status code was not checked - Refactoring the SearchVectorBySimilarityAsync method to take a DataEntry<VectorRecordData<float>> parameter instead of a VectorRecordData<float> parameter, to be consistent with the FindClosestAsync method - Fixing the indentation in the project file to use four spaces instead of two
1 parent 962c437 commit 1cd09c1

16 files changed

Lines changed: 126 additions & 137 deletions

‎dotnet/src/SemanticKernel.Skills/Skills.Memory.VectorDB/Qdrant/DotNet/IVectorDbCollection.cs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,24 @@ public interface IVectorDbCollection
2121

2222
public static class VectorDbCollectionExtensions
2323
{
24-
public static Task UpsertVectorAsync(this IVectorDbCollection collection, string id, float[] data, IEnumerable<string>? tags = null, Dictionary<string, object>? payload = null)
24+
public static Task UpsertVectorAsync(this IVectorDbCollection collection, string id, float[] data, IEnumerable<string>? tags = null,
25+
Dictionary<string, object>? payload = null)
2526
{
2627
return collection.UpsertVectorAsync(
2728
new DataEntry<VectorRecordData<float>>(key: id,
28-
value: new VectorRecordData<float>(
29-
new Embedding<float>(data),
30-
payload ?? new(),
31-
tags?.ToList() ?? new List<string>(0))));
29+
value: new VectorRecordData<float>(
30+
new Embedding<float>(data),
31+
payload ?? new(),
32+
tags?.ToList() ?? new List<string>(0))));
3233
}
3334

3435
public static Task DeleteVectorAsync(this IVectorDbCollection collection, DataEntry<VectorRecordData<float>> record)
3536
{
3637
return collection.DeleteVectorAsync(record.Key);
3738
}
3839

39-
public static IAsyncEnumerable<KeyValuePair<DataEntry<VectorRecordData<float>>, double>> FindClosestAsync(this IVectorDbCollection collection, DataEntry<VectorRecordData<float>> record, int top = 1, string[]? requiredTags = null)
40+
public static IAsyncEnumerable<KeyValuePair<DataEntry<VectorRecordData<float>>, double>> FindClosestAsync(this IVectorDbCollection collection,
41+
DataEntry<VectorRecordData<float>> record, int top = 1, string[]? requiredTags = null)
4042
{
4143
return collection.FindClosestAsync(record.Value!.Embedding.Vector.ToArray(), top, requiredTags);
4244
}

‎dotnet/src/SemanticKernel.Skills/Skills.Memory.VectorDB/Qdrant/DotNet/Internal/Diagnostics/ConsoleLogger.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static ILogger Create(string categoryName, Action<SimpleConsoleFormatterO
2727
}
2828

2929
private static ILoggerFactory? s_loggerFactory = null;
30+
3031
public static ILoggerFactory Factory
3132
{
3233
get

‎dotnet/src/SemanticKernel.Skills/Skills.Memory.VectorDB/Qdrant/DotNet/Internal/Diagnostics/Verify.cs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ internal static void NotNullOrEmpty<T>(IList<T> list, string message)
9696
}
9797
}
9898

99-
10099
public static void Equals<T>(T value, T test, string message)
101100
where T : IComparable<T>
102101
{
@@ -107,7 +106,6 @@ public static void Equals<T>(T value, T test, string message)
107106
}
108107
}
109108

110-
111109
// internal static void Contains<T>(IDictionary<string, T> dictionary, string key, string message)
112110
// {
113111
// if (dictionary.ContainsKey(key)) return;

‎dotnet/src/SemanticKernel.Skills/Skills.Memory.VectorDB/Qdrant/DotNet/Internal/Http/Specs/CreateCollectionRequest.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public void Validate()
7272
{
7373
Verify.True(this.Size > 0, "The vector size must be greater than zero");
7474
Verify.NotNull(this.DistanceType, "The distance type has not been defined");
75-
Verify.True(this.DistanceType is VectorDistanceType.Cosine or VectorDistanceType.DotProduct or VectorDistanceType.Euclidean or VectorDistanceType.Manhattan,
75+
Verify.True(
76+
this.DistanceType is VectorDistanceType.Cosine or VectorDistanceType.DotProduct or VectorDistanceType.Euclidean or VectorDistanceType.Manhattan,
7677
$"Distance type {this.DistanceType:G} not supported.");
7778
}
7879

‎dotnet/src/SemanticKernel.Skills/Skills.Memory.VectorDB/Qdrant/DotNet/Internal/Http/Specs/FetchVectorsRequest.cs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static FetchVectorsRequest Fetch(string collectionName)
2828
{
2929
return new FetchVectorsRequest(collectionName);
3030
}
31+
3132
public void Validate()
3233
{
3334
Verify.NotNullOrEmpty(this._collectionName, "The collection name is empty");
@@ -41,8 +42,6 @@ public HttpRequestMessage Build()
4142
$"collections/{this._collectionName}/points/scroll");
4243
}
4344

44-
45-
4645
#region private ================================================================================
4746

4847
private readonly string _collectionName;
@@ -53,7 +52,6 @@ private FetchVectorsRequest(string collectionName)
5352
this.Filters = new Filter();
5453
this.WithPayload = true;
5554
this.WithVector = true;
56-
5755
}
5856

5957
private class Filter : IValidatable
@@ -123,6 +121,6 @@ public void Validate()
123121
}
124122
}
125123
}
126-
#endregion
127124

128-
}
125+
#endregion
126+
}

‎dotnet/src/SemanticKernel.Skills/Skills.Memory.VectorDB/Qdrant/DotNet/Internal/Http/Specs/FetchVectorsResponse.cs‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,33 @@
99

1010
namespace Qdrant.DotNet.Internal.Http.Specs;
1111

12-
internal class FetchVectorsResponse
13-
{
12+
internal class FetchVectorsResponse
13+
{
1414
internal class Points
15-
{
15+
{
1616
[JsonPropertyName("id")]
17-
internal string? VectorId {get; set; }
17+
internal string? VectorId { get; set; }
1818

1919
[JsonPropertyName("payload")]
2020
internal Dictionary<string, object>? Payload { get; set; }
21-
21+
2222
[JsonPropertyName("vector")]
2323
internal float[]? Vector { get; set; }
2424

2525
internal Points()
26-
{}
26+
{
27+
}
2728
}
28-
29+
2930
internal string Status { get; set; }
3031
internal List<Points> VectorPointCollection { get; set; }
3132

3233
internal void ConvertFromJson(string json)
3334
{
34-
try
35+
try
3536
{
36-
JsonNode jsondata = JsonNode.Parse(json)!;
37-
37+
JsonNode jsondata = JsonNode.Parse(json)!;
38+
3839
this.Status = jsondata.Root["status"]!.GetValue<string>()!;
3940
JsonNode result = jsondata.Root["result"]!;
4041

@@ -49,7 +50,6 @@ internal void ConvertFromJson(string json)
4950
{
5051
throw new JsonException("Error in parsing Json from retrieving vectors", ex);
5152
}
52-
5353
}
5454

5555
internal FetchVectorsResponse(string json) : this()
@@ -66,4 +66,4 @@ private FetchVectorsResponse()
6666
}
6767

6868
#endregion
69-
}
69+
}

‎dotnet/src/SemanticKernel.Skills/Skills.Memory.VectorDB/Qdrant/DotNet/Internal/Http/Specs/SearchVectorsRequest.cs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace Qdrant.DotNet.Internal.Http.Specs;
1010

1111
internal class SearchVectorsRequest : IValidatable
1212
{
13-
1413
[JsonPropertyName("vector")]
1514
private float[] StartingVector { get; set; }
1615

@@ -112,7 +111,6 @@ public HttpRequestMessage Build()
112111
payload: this);
113112
}
114113

115-
116114
#region private ================================================================================
117115

118116
private readonly string _collectionName;

‎dotnet/src/SemanticKernel.Skills/Skills.Memory.VectorDB/Qdrant/DotNet/Internal/Http/Specs/SearchVectorsResponse.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ internal VectorFound()
3030

3131
internal string Status { get; set; }
3232
internal List<VectorFound> Vectors { get; set; }
33+
3334
internal SearchVectorsResponse(string json) : this()
3435
{
3536
// TODO: Replace with a System.Text.Json implementation

‎dotnet/src/SemanticKernel.Skills/Skills.Memory.VectorDB/Qdrant/DotNet/Internal/QdrantCollection.cs‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.SemanticKernel.Memory.Storage;
1313
using Qdrant.DotNet.Internal.Diagnostics;
1414
using Qdrant.DotNet.Internal.Http.Specs;
15+
1516
namespace Qdrant.DotNet.Internal;
1617

1718
internal class QdrantCollection : IVectorDbCollection
@@ -30,17 +31,16 @@ public QdrantCollection(string id, QdrantHttpClientWrapper vectorHttp, ILogger l
3031
this._log = log ?? NullLogger.Instance;
3132
}
3233

33-
3434
public async Task<DataEntry<VectorRecordData<float>>?> GetVectorAsync(string id)
3535
{
3636
var point = await this.FindQdrantPointByExternalIdAsync(id);
3737
if (point == null) { return null; }
3838

3939
var result = new DataEntry<VectorRecordData<float>>(key: point.ExternalId,
40-
value: new VectorRecordData<float>(
41-
new Embedding<float>(point.Vector),
42-
point.ExternalPayload,
43-
point.ExternalTags));
40+
value: new VectorRecordData<float>(
41+
new Embedding<float>(point.Vector),
42+
point.ExternalPayload,
43+
point.ExternalTags));
4444

4545
return result;
4646
}
@@ -49,12 +49,12 @@ public async Task<IAsyncEnumerable<DataEntry<VectorRecordData<float>>>> GetAllVe
4949
{
5050
HttpRequestMessage vectorRequest = FetchVectorsRequest
5151
.Fetch(this.Name)
52-
.Build();
52+
.Build();
5353

5454
var (response, responseContent) = await this._vectorHttp.ExecuteHttpRequestAsync(vectorRequest);
5555
response.EnsureSuccessStatusCode();
56-
57-
return null;
56+
57+
return null;
5858
}
5959

6060
public Task UpsertVectorAsync(DataEntry<VectorRecordData<float>> record)
@@ -74,7 +74,8 @@ public async Task DeleteVectorAsync(string id)
7474
await this.DeleteVectorInternalAsync(qdrantId);
7575
}
7676

77-
public async IAsyncEnumerable<KeyValuePair<DataEntry<VectorRecordData<float>>, double>> FindClosestAsync(float[] target, int top = 1, string[]? requiredTags = null)
77+
public async IAsyncEnumerable<KeyValuePair<DataEntry<VectorRecordData<float>>, double>> FindClosestAsync(float[] target, int top = 1,
78+
string[]? requiredTags = null)
7879
{
7980
this._log.Debug("Searching top {0} closest vectors in {1}", top);
8081

@@ -106,10 +107,10 @@ public async IAsyncEnumerable<KeyValuePair<DataEntry<VectorRecordData<float>>, d
106107
foreach (var v in data.Vectors)
107108
{
108109
var record = new DataEntry<VectorRecordData<float>>(key: v.ExternalId,
109-
value: new VectorRecordData<float>(
110-
new Embedding<float>(v.Vector),
111-
v.ExternalPayload,
112-
v.ExternalTags));
110+
value: new VectorRecordData<float>(
111+
new Embedding<float>(v.Vector),
112+
v.ExternalPayload,
113+
v.ExternalTags));
113114

114115
result.Add(new KeyValuePair<DataEntry<VectorRecordData<float>>, double>(record, v.Score ?? 0));
115116
}
@@ -124,7 +125,8 @@ public async IAsyncEnumerable<KeyValuePair<DataEntry<VectorRecordData<float>>, d
124125

125126
#region private ================================================================================
126127

127-
private static List<KeyValuePair<DataEntry<VectorRecordData<float>>, double>> SortSearchResultByScore(List<KeyValuePair<DataEntry<VectorRecordData<float>>, double>> list)
128+
private static List<KeyValuePair<DataEntry<VectorRecordData<float>>, double>> SortSearchResultByScore(
129+
List<KeyValuePair<DataEntry<VectorRecordData<float>>, double>> list)
128130
{
129131
return (from entry in list orderby entry.Value descending select entry).ToList();
130132
}
@@ -215,7 +217,5 @@ private async Task DeleteVectorInternalAsync(string qdrantPointId)
215217
await this._vectorHttp.ExecuteHttpRequestAsync(request);
216218
}
217219

218-
219-
220220
#endregion
221221
}

‎dotnet/src/SemanticKernel.Skills/Skills.Memory.VectorDB/Qdrant/DotNet/Internal/QdrantHttpClientWrapper.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ private static Uri SanitizeEndpoint(string endpoint)
6767
private static Uri SanitizeEndpoint(string endpoint, int port)
6868
{
6969
UriBuilder builder = new UriBuilder(SanitizeEndpoint(endpoint))
70-
{ Port = port };
70+
{ Port = port };
7171
return builder.Uri;
72-
7372
}
7473

7574
#endregion

0 commit comments

Comments
 (0)