-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathMilvusMemoryStore.cs
590 lines (507 loc) · 25.9 KB
/
MilvusMemoryStore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel.Memory;
using Milvus.Client;
namespace Microsoft.SemanticKernel.Connectors.Milvus;
/// <summary>
/// An implementation of <see cref="IMemoryStore" /> for the Milvus vector database.
/// </summary>
public class MilvusMemoryStore : IMemoryStore, IDisposable
{
private readonly int _vectorSize;
private readonly SimilarityMetricType _metricType;
private readonly ConsistencyLevel _consistencyLevel;
private readonly bool _ownsMilvusClient;
private readonly string _indexName;
private const string DefaultIndexName = "default";
private const string IsReferenceFieldName = "is_reference";
private const string ExternalSourceNameFieldName = "external_source_name";
private const string IdFieldName = "id";
private const string DescriptionFieldName = "description";
private const string TextFieldName = "text";
private const string AdditionalMetadataFieldName = "additional_metadata";
private const string EmbeddingFieldName = "embedding";
private const string KeyFieldName = "key";
private const string TimestampFieldName = "timestamp";
private const int DefaultMilvusPort = 19530;
private const int DefaultVarcharLength = 65_535;
private readonly QueryParameters _queryParametersWithEmbedding;
private readonly QueryParameters _queryParametersWithoutEmbedding;
private readonly SearchParameters _searchParameters = new()
{
OutputFields = { IsReferenceFieldName, ExternalSourceNameFieldName, IdFieldName, DescriptionFieldName, TextFieldName, AdditionalMetadataFieldName, KeyFieldName, TimestampFieldName }
};
/// <summary>
/// Exposes the underlying <see cref="Client" /> used to communicate with Milvus. Can be used to execute operations not supported by the <see cref="IMemoryStore" /> abstraction.
/// </summary>
public MilvusClient Client { get; }
#region Constructors
/// <summary>
/// Creates a new <see cref="MilvusMemoryStore" />, connecting to the given hostname on the default Milvus port of 19530.
/// For more advanced configuration opens, construct a <see cref="MilvusClient" /> instance and pass it to
/// <see cref="MilvusMemoryStore(MilvusClient, string, int, SimilarityMetricType, ConsistencyLevel)" />.
/// </summary>
/// <param name="host">The hostname or IP address to connect to.</param>
/// <param name="port">The port to connect to. Defaults to 19530.</param>
/// <param name="ssl">Whether to use TLS/SSL. Defaults to <c>false</c>.</param>
/// <param name="database">The database to connect to. Defaults to the default Milvus database.</param>
/// <param name="indexName">The name of the index to use. Defaults to <see cref="DefaultIndexName" />.</param>
/// <param name="vectorSize">The size of the vectors used in Milvus. Defaults to 1536.</param>
/// <param name="metricType">The metric used to measure similarity between vectors. Defaults to <see cref="SimilarityMetricType.Ip" />.</param>
/// <param name="consistencyLevel">The consistency level to be used in the search. Defaults to <see cref="ConsistencyLevel.Session"/>.</param>
/// <param name="loggerFactory">An optional logger factory through which the Milvus client will log.</param>
public MilvusMemoryStore(
string host,
int port = DefaultMilvusPort,
bool ssl = false,
string? database = null,
string? indexName = null,
int vectorSize = 1536,
SimilarityMetricType metricType = SimilarityMetricType.Ip,
ConsistencyLevel consistencyLevel = ConsistencyLevel.Session,
ILoggerFactory? loggerFactory = null)
: this(
new MilvusClient(host, port, ssl, database, callOptions: default, loggerFactory),
indexName, vectorSize, metricType, consistencyLevel)
{
this._ownsMilvusClient = true;
}
/// <summary>
/// Creates a new <see cref="MilvusMemoryStore" />, connecting to the given hostname on the default Milvus port of 19530.
/// For more advanced configuration opens, construct a <see cref="MilvusClient" /> instance and pass it to
/// <see cref="MilvusMemoryStore(MilvusClient, string, int, SimilarityMetricType, ConsistencyLevel)" />.
/// </summary>
/// <param name="host">The hostname or IP address to connect to.</param>
/// <param name="username">The username to use for authentication.</param>
/// <param name="password">The password to use for authentication.</param>
/// <param name="port">The port to connect to. Defaults to 19530.</param>
/// <param name="ssl">Whether to use TLS/SSL. Defaults to <c>false</c>.</param>
/// <param name="database">The database to connect to. Defaults to the default Milvus database.</param>
/// <param name="indexName">The name of the index to use. Defaults to <see cref="DefaultIndexName" />.</param>
/// <param name="vectorSize">The size of the vectors used in Milvus. Defaults to 1536.</param>
/// <param name="metricType">The metric used to measure similarity between vectors. Defaults to <see cref="SimilarityMetricType.Ip" />.</param>
/// <param name="consistencyLevel">The consistency level to be used in the search. Defaults to <see cref="ConsistencyLevel.Session"/>.</param>
/// <param name="loggerFactory">An optional logger factory through which the Milvus client will log.</param>
public MilvusMemoryStore(
string host,
string username,
string password,
int port = DefaultMilvusPort,
bool ssl = false,
string? database = null,
string? indexName = null,
int vectorSize = 1536,
SimilarityMetricType metricType = SimilarityMetricType.Ip,
ConsistencyLevel consistencyLevel = ConsistencyLevel.Session,
ILoggerFactory? loggerFactory = null)
: this(
new MilvusClient(host, username, password, port, ssl, database, callOptions: default, loggerFactory),
indexName, vectorSize, metricType, consistencyLevel)
{
this._ownsMilvusClient = true;
}
/// <summary>
/// Creates a new <see cref="MilvusMemoryStore" />, connecting to the given hostname on the default Milvus port of 19530.
/// For more advanced configuration opens, construct a <see cref="MilvusClient" /> instance and pass it to
/// <see cref="MilvusMemoryStore(MilvusClient, string, int, SimilarityMetricType, ConsistencyLevel)" />.
/// </summary>
/// <param name="host">The hostname or IP address to connect to.</param>
/// <param name="apiKey">An API key to be used for authentication, instead of a username and password.</param>
/// <param name="port">The port to connect to. Defaults to 19530.</param>
/// <param name="ssl">Whether to use TLS/SSL. Defaults to <c>false</c>.</param>
/// <param name="database">The database to connect to. Defaults to the default Milvus database.</param>
/// <param name="indexName">The name of the index to use. Defaults to <see cref="DefaultIndexName" />.</param>
/// <param name="vectorSize">The size of the vectors used in Milvus. Defaults to 1536.</param>
/// <param name="metricType">The metric used to measure similarity between vectors. Defaults to <see cref="SimilarityMetricType.Ip" />.</param>
/// <param name="consistencyLevel">The consistency level to be used in the search. Defaults to <see cref="ConsistencyLevel.Session"/>.</param>
/// <param name="loggerFactory">An optional logger factory through which the Milvus client will log.</param>
public MilvusMemoryStore(
string host,
string apiKey,
int port = DefaultMilvusPort,
bool ssl = false,
string? database = null,
string? indexName = null,
int vectorSize = 1536,
SimilarityMetricType metricType = SimilarityMetricType.Ip,
ConsistencyLevel consistencyLevel = ConsistencyLevel.Session,
ILoggerFactory? loggerFactory = null)
: this(
new MilvusClient(host, apiKey, port, ssl, database, callOptions: default, loggerFactory),
indexName, vectorSize, metricType, consistencyLevel)
{
this._ownsMilvusClient = true;
}
/// <summary>
/// Initializes a new instance of <see cref="MilvusMemoryStore" /> over the given <see cref="MilvusClient" />.
/// </summary>
/// <param name="client">A <see cref="MilvusClient" /> configured with the necessary endpoint and authentication information.</param>
/// <param name="indexName">The name of the index to use. Defaults to <see cref="DefaultIndexName" />.</param>
/// <param name="vectorSize">The size of the vectors used in Milvus. Defaults to 1536.</param>
/// <param name="metricType">The metric used to measure similarity between vectors. Defaults to <see cref="SimilarityMetricType.Ip" />.</param>
/// <param name="consistencyLevel">The consistency level to be used in the search. Defaults to <see cref="ConsistencyLevel.Session"/>.</param>
public MilvusMemoryStore(
MilvusClient client,
string? indexName = null,
int vectorSize = 1536,
SimilarityMetricType metricType = SimilarityMetricType.Ip,
ConsistencyLevel consistencyLevel = ConsistencyLevel.Session)
: this(client, ownsMilvusClient: false, indexName, vectorSize, metricType, consistencyLevel)
{
}
private MilvusMemoryStore(
MilvusClient client,
bool ownsMilvusClient,
string? indexName,
int vectorSize,
SimilarityMetricType metricType,
ConsistencyLevel consistencyLevel)
{
this.Client = client;
this._indexName = indexName ?? DefaultIndexName;
this._vectorSize = vectorSize;
this._metricType = metricType;
this._ownsMilvusClient = ownsMilvusClient;
this._consistencyLevel = consistencyLevel;
this._queryParametersWithEmbedding = new()
{
OutputFields = { IsReferenceFieldName, ExternalSourceNameFieldName, IdFieldName, DescriptionFieldName, TextFieldName, AdditionalMetadataFieldName, EmbeddingFieldName, KeyFieldName, TimestampFieldName },
ConsistencyLevel = this._consistencyLevel
};
this._queryParametersWithoutEmbedding = new()
{
OutputFields = { IsReferenceFieldName, ExternalSourceNameFieldName, IdFieldName, DescriptionFieldName, TextFieldName, AdditionalMetadataFieldName, KeyFieldName, TimestampFieldName },
ConsistencyLevel = this._consistencyLevel
};
}
#endregion Constructors
/// <inheritdoc />
public async Task CreateCollectionAsync(string collectionName, CancellationToken cancellationToken = default)
{
var exists = await this.Client.HasCollectionAsync(collectionName, cancellationToken: cancellationToken).ConfigureAwait(false);
if (!exists)
{
CollectionSchema schema = new()
{
Fields =
{
FieldSchema.CreateVarchar(IdFieldName, maxLength: DefaultVarcharLength, isPrimaryKey: true, autoId: false),
FieldSchema.CreateFloatVector(EmbeddingFieldName, this._vectorSize)
},
EnableDynamicFields = true
};
MilvusCollection collection = await this.Client.CreateCollectionAsync(collectionName, schema, this._consistencyLevel, cancellationToken: cancellationToken).ConfigureAwait(false);
await collection.CreateIndexAsync(EmbeddingFieldName, metricType: this._metricType, indexName: this._indexName, cancellationToken: cancellationToken).ConfigureAwait(false);
await collection.WaitForIndexBuildAsync("float_vector", this._indexName, cancellationToken: cancellationToken).ConfigureAwait(false);
await collection.LoadAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
await collection.WaitForCollectionLoadAsync(waitingInterval: TimeSpan.FromMilliseconds(100), timeout: TimeSpan.FromMinutes(1), cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
/// <inheritdoc />
public async IAsyncEnumerable<string> GetCollectionsAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
{
foreach (MilvusCollectionInfo collection in await this.Client.ListCollectionsAsync(cancellationToken: cancellationToken).ConfigureAwait(false))
{
yield return collection.Name;
}
}
/// <inheritdoc />
public Task<bool> DoesCollectionExistAsync(string collectionName, CancellationToken cancellationToken = default)
=> this.Client.HasCollectionAsync(collectionName, cancellationToken: cancellationToken);
/// <inheritdoc />
public Task DeleteCollectionAsync(string collectionName, CancellationToken cancellationToken = default)
=> this.Client.GetCollection(collectionName).DropAsync(cancellationToken);
/// <inheritdoc />
public async Task<string> UpsertAsync(string collectionName, MemoryRecord record, CancellationToken cancellationToken = default)
{
MilvusCollection collection = this.Client.GetCollection(collectionName);
var metadata = record.Metadata;
List<FieldData> fieldData =
[
FieldData.Create(IdFieldName, [metadata.Id]),
FieldData.CreateFloatVector(EmbeddingFieldName, [record.Embedding]),
FieldData.Create(IsReferenceFieldName, [metadata.IsReference], isDynamic: true),
FieldData.Create(ExternalSourceNameFieldName, [metadata.ExternalSourceName], isDynamic: true),
FieldData.Create(DescriptionFieldName, [metadata.Description], isDynamic: true),
FieldData.Create(TextFieldName, [metadata.Text], isDynamic: true),
FieldData.Create(AdditionalMetadataFieldName, [metadata.AdditionalMetadata], isDynamic: true),
FieldData.Create(KeyFieldName, [record.Key], isDynamic: true),
FieldData.Create(TimestampFieldName, [record.Timestamp?.ToString(CultureInfo.InvariantCulture) ?? string.Empty], isDynamic: true)
];
MutationResult result = await collection.UpsertAsync(fieldData, cancellationToken: cancellationToken).ConfigureAwait(false);
return result.Ids.StringIds![0];
}
/// <inheritdoc />
public async IAsyncEnumerable<string> UpsertBatchAsync(
string collectionName,
IEnumerable<MemoryRecord> records,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
StringBuilder idString = new();
List<bool> isReferenceData = [];
List<string> externalSourceNameData = [];
List<string> idData = [];
List<string> descriptionData = [];
List<string> textData = [];
List<string> additionalMetadataData = [];
List<ReadOnlyMemory<float>> embeddingData = [];
List<string> keyData = [];
List<string> timestampData = [];
foreach (MemoryRecord record in records)
{
var metadata = record.Metadata;
if (idString.Length > 0)
{
idString.Append(',');
}
idString.Append('"').Append(metadata.Id).Append('"');
isReferenceData.Add(metadata.IsReference);
externalSourceNameData.Add(metadata.ExternalSourceName);
idData.Add(record.Metadata.Id);
descriptionData.Add(metadata.Description);
textData.Add(metadata.Text);
additionalMetadataData.Add(metadata.AdditionalMetadata);
embeddingData.Add(record.Embedding);
keyData.Add(record.Key);
timestampData.Add(record.Timestamp?.ToString(CultureInfo.InvariantCulture) ?? string.Empty);
}
MilvusCollection collection = this.Client.GetCollection(collectionName);
FieldData[] fieldData =
[
FieldData.Create(IdFieldName, idData),
FieldData.CreateFloatVector(EmbeddingFieldName, embeddingData),
FieldData.Create(IsReferenceFieldName, isReferenceData, isDynamic: true),
FieldData.Create(ExternalSourceNameFieldName, externalSourceNameData, isDynamic: true),
FieldData.Create(DescriptionFieldName, descriptionData, isDynamic: true),
FieldData.Create(TextFieldName, textData, isDynamic: true),
FieldData.Create(AdditionalMetadataFieldName, additionalMetadataData, isDynamic: true),
FieldData.Create(KeyFieldName, keyData, isDynamic: true),
FieldData.Create(TimestampFieldName, timestampData, isDynamic: true)
];
MutationResult result = await collection.UpsertAsync(fieldData, cancellationToken: cancellationToken).ConfigureAwait(false);
foreach (var id in result.Ids.StringIds!)
{
yield return id;
}
}
/// <inheritdoc />
public async Task<MemoryRecord?> GetAsync(
string collectionName,
string key,
bool withEmbedding = false,
CancellationToken cancellationToken = default)
{
await foreach (MemoryRecord record in this.GetBatchAsync(collectionName, [key], withEmbedding, cancellationToken).ConfigureAwait(false))
{
return record;
}
return null;
}
/// <inheritdoc />
public async IAsyncEnumerable<MemoryRecord> GetBatchAsync(
string collectionName,
IEnumerable<string> keys,
bool withEmbeddings = false,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
StringBuilder idString = new();
foreach (string key in keys)
{
if (idString.Length > 0)
{
idString.Append(',');
}
idString.Append('"').Append(key).Append('"');
}
IReadOnlyList<FieldData> fields = await this.Client
.GetCollection(collectionName)
.QueryAsync(
$"{IdFieldName} in [{idString}]",
withEmbeddings ? this._queryParametersWithEmbedding : this._queryParametersWithoutEmbedding,
cancellationToken: cancellationToken)
.ConfigureAwait(false);
var rowCount = fields[0].RowCount;
for (int rowNum = 0; rowNum < rowCount; rowNum++)
{
yield return this.ReadMemoryRecord(fields, rowNum);
}
}
/// <inheritdoc />
public Task RemoveAsync(string collectionName, string key, CancellationToken cancellationToken = default)
=> this.Client.GetCollection(collectionName)
.DeleteAsync($@"{IdFieldName} in [""{key}""]", cancellationToken: cancellationToken);
/// <inheritdoc />
public Task RemoveBatchAsync(string collectionName, IEnumerable<string> keys, CancellationToken cancellationToken = default)
{
StringBuilder idString = new();
idString.Append(IdFieldName).Append(" in [");
bool first = true;
foreach (string id in keys)
{
if (first)
{
first = false;
}
else
{
idString.Append(',');
}
idString.Append('"').Append(id).Append('"');
}
idString.Append(']');
return this.Client
.GetCollection(collectionName)
.DeleteAsync(idString.ToString(), cancellationToken: cancellationToken);
}
/// <inheritdoc />
public async Task<(MemoryRecord, double)?> GetNearestMatchAsync(
string collectionName,
ReadOnlyMemory<float> embedding,
double minRelevanceScore = 0,
bool withEmbedding = false,
CancellationToken cancellationToken = default)
{
await foreach ((MemoryRecord, double) result in this.GetNearestMatchesAsync(collectionName, embedding, limit: 1, minRelevanceScore, withEmbedding, cancellationToken).ConfigureAwait(false))
{
return result;
}
return null;
}
/// <inheritdoc />
public async IAsyncEnumerable<(MemoryRecord, double)> GetNearestMatchesAsync(
string collectionName,
ReadOnlyMemory<float> embedding,
int limit,
double minRelevanceScore = 0,
bool withEmbeddings = false,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
MilvusCollection collection = this.Client.GetCollection(collectionName);
SearchResults results = await collection
.SearchAsync(EmbeddingFieldName, [embedding], this._metricType, limit, this._searchParameters, cancellationToken)
.ConfigureAwait(false);
IReadOnlyList<string> ids = results.Ids.StringIds!;
int rowCount = ids.Count;
IReadOnlyList<FieldData> data = results.FieldsData;
// Since Milvus does not support fetching vectors via the Search API, we do an extra call to fetch the ids and embeddings using the Query API,
// using the IDs returned from the Search above, populating a map from the IDs to the embedding.
// TODO: There's some support for fetching vectors from Search in Milvus 2.3, check that out.
Dictionary<string, ReadOnlyMemory<float>>? embeddingMap = null;
if (withEmbeddings)
{
StringBuilder filter = new();
filter.Append(IdFieldName).Append(" in [");
for (int rowNum = 0; rowNum < ids.Count; rowNum++)
{
if (rowNum > 0)
{
filter.Append(',');
}
filter.Append('"').Append(ids[rowNum]).Append('"');
}
filter.Append(']');
IReadOnlyList<FieldData> fieldData = await collection.QueryAsync(
filter.ToString(),
new() { OutputFields = { EmbeddingFieldName } },
cancellationToken: cancellationToken)
.ConfigureAwait(false);
IReadOnlyList<string> idData = (fieldData[0] as FieldData<string> ?? fieldData[1] as FieldData<string>)!.Data;
IReadOnlyList<ReadOnlyMemory<float>> embeddingData = (fieldData[0] as FloatVectorFieldData ?? fieldData[1] as FloatVectorFieldData)!.Data;
embeddingMap = new Dictionary<string, ReadOnlyMemory<float>>(ids.Count);
for (int rowNum = 0; rowNum < ids.Count; rowNum++)
{
embeddingMap[idData[rowNum]] = embeddingData[rowNum];
}
}
for (int rowNum = 0; rowNum < rowCount; rowNum++)
{
// TODO: Milvus 2.3 has range search, which will move this to the server.
if (results.Scores[rowNum] >= minRelevanceScore)
{
yield return (
this.ReadMemoryRecord(data, rowNum, withEmbeddings ? embeddingMap![ids[rowNum]] : null),
results.Scores[rowNum]);
}
}
}
private MemoryRecord ReadMemoryRecord(IReadOnlyList<FieldData> data, int rowNum, ReadOnlyMemory<float>? externalEmbedding = null)
{
bool isReference = false;
string externalSourceName = string.Empty;
string id = string.Empty;
string description = string.Empty;
string text = string.Empty;
string additionalMetadata = string.Empty;
ReadOnlyMemory<float>? embedding = null;
string key = string.Empty;
DateTimeOffset? timestamp = null;
foreach (FieldData field in data)
{
switch (field.FieldName)
{
case IsReferenceFieldName when field is FieldData<bool> isReferenceField:
isReference = isReferenceField.Data[rowNum];
break;
case ExternalSourceNameFieldName when field is FieldData<string> externalSourceNameField:
externalSourceName = externalSourceNameField.Data[rowNum];
break;
case IdFieldName when field is FieldData<string> idField:
id = idField.Data[rowNum];
break;
case DescriptionFieldName when field is FieldData<string> descriptionField:
description = descriptionField.Data[rowNum];
break;
case TextFieldName when field is FieldData<string> textField:
text = textField.Data[rowNum];
break;
case AdditionalMetadataFieldName when field is FieldData<string> additionalMetadataField:
additionalMetadata = additionalMetadataField.Data[rowNum];
break;
case EmbeddingFieldName when field is FloatVectorFieldData embeddingField:
Debug.Assert(externalEmbedding is null);
embedding = embeddingField.Data[rowNum];
break;
case KeyFieldName when field is FieldData<string> keyField:
key = keyField.Data[rowNum];
break;
case TimestampFieldName when field is FieldData<string> timestampField:
string timestampString = timestampField.Data[rowNum];
timestamp = timestampString is { Length: > 0 }
? DateTimeOffset.Parse(timestampString, CultureInfo.InvariantCulture)
: null;
break;
default:
continue; // Unknown field - ignore
}
}
return new MemoryRecord(
new MemoryRecordMetadata(isReference, id, text, description, externalSourceName, additionalMetadata),
embedding ?? externalEmbedding ?? Array.Empty<float>(),
key,
timestamp);
}
/// <summary>
/// Implements the dispose pattern.
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (disposing && this._ownsMilvusClient)
{
this.Client.Dispose();
}
}
/// <inheritdoc />
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
}