Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add batching upsert support to Qdrant Memory #504

Merged
merged 5 commits into from
May 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions extensions/Qdrant/Qdrant/QdrantMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Microsoft.KernelMemory.MemoryDb.Qdrant;
/// * allow using more Qdrant specific filtering logic
/// </summary>
[Experimental("KMEXP03")]
public sealed class QdrantMemory : IMemoryDb
public sealed class QdrantMemory : IMemoryDb, IMemoryDbBatchUpsert
{
private readonly ITextEmbeddingGenerator _embeddingGenerator;
private readonly QdrantClient<DefaultQdrantPayload> _qdrantClient;
Expand Down Expand Up @@ -91,40 +91,60 @@ public async Task<string> UpsertAsync(
string index,
MemoryRecord record,
CancellationToken cancellationToken = default)
{
var result = this.BatchUpsertAsync(index, [record], cancellationToken);
var id = await result.SingleAsync(cancellationToken).ConfigureAwait(false);
return id;
}

/// <inheritdoc />
public async IAsyncEnumerable<string> BatchUpsertAsync(string index, IEnumerable<MemoryRecord> records, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
index = NormalizeIndexName(index);

QdrantPoint<DefaultQdrantPayload> qdrantPoint;
// Call ToList to avoid multiple enumerations (CA1851: Possible multiple enumerations of 'IEnumerable' collection. Consider using an implementation that avoids multiple enumerations).
var localRecords = records.ToList();

if (string.IsNullOrEmpty(record.Id))
var qdrantPoints = new List<QdrantPoint<DefaultQdrantPayload>>();
foreach (var record in localRecords)
{
record.Id = Guid.NewGuid().ToString("N");
qdrantPoint = QdrantPoint<DefaultQdrantPayload>.FromMemoryRecord(record);
qdrantPoint.Id = Guid.NewGuid();
QdrantPoint<DefaultQdrantPayload> qdrantPoint;

this._log.LogTrace("Generate new Qdrant point ID {0} and record ID {1}", qdrantPoint.Id, record.Id);
}
else
{
qdrantPoint = QdrantPoint<DefaultQdrantPayload>.FromMemoryRecord(record);
QdrantPoint<DefaultQdrantPayload>? existingPoint = await this._qdrantClient
.GetVectorByPayloadIdAsync(index, record.Id, cancellationToken: cancellationToken)
.ConfigureAwait(false);
if (existingPoint == null)
if (string.IsNullOrEmpty(record.Id))
{
record.Id = Guid.NewGuid().ToString("N");
qdrantPoint = QdrantPoint<DefaultQdrantPayload>.FromMemoryRecord(record);
qdrantPoint.Id = Guid.NewGuid();
this._log.LogTrace("No record with ID {0} found, generated a new point ID {1}", record.Id, qdrantPoint.Id);

this._log.LogTrace("Generate new Qdrant point ID {0} and record ID {1}", qdrantPoint.Id, record.Id);
}
else
{
qdrantPoint.Id = existingPoint.Id;
this._log.LogTrace("Point ID {0} found, updating...", qdrantPoint.Id);
qdrantPoint = QdrantPoint<DefaultQdrantPayload>.FromMemoryRecord(record);
QdrantPoint<DefaultQdrantPayload>? existingPoint = await this._qdrantClient
.GetVectorByPayloadIdAsync(index, record.Id, cancellationToken: cancellationToken)
.ConfigureAwait(false);
if (existingPoint == null)
{
qdrantPoint.Id = Guid.NewGuid();
this._log.LogTrace("No record with ID {0} found, generated a new point ID {1}", record.Id, qdrantPoint.Id);
}
else
{
qdrantPoint.Id = existingPoint.Id;
this._log.LogTrace("Point ID {0} found, updating...", qdrantPoint.Id);
}
}

qdrantPoints.Add(qdrantPoint);
}

await this._qdrantClient.UpsertVectorsAsync(index, new[] { qdrantPoint }, cancellationToken).ConfigureAwait(false);
await this._qdrantClient.UpsertVectorsAsync(index, qdrantPoints, cancellationToken).ConfigureAwait(false);

return record.Id;
foreach (var record in localRecords)
{
yield return record.Id;
}
}

/// <inheritdoc />
Expand Down
Loading