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

Qdrant: don't throw on 404 when getting nearest match #698

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ namespace Microsoft.SemanticKernel.Connectors.Memory.Qdrant;
/// </remarks>
public class QdrantMemoryStore : IMemoryStore
{
/// <summary>
/// The Qdrant Vector database memory store logger.
/// </summary>
private readonly ILogger? _logger;

/// <summary>
/// Constructor for a memory store backed by a Qdrant Vector database instance.
/// </summary>
Expand All @@ -31,6 +36,7 @@ public class QdrantMemoryStore : IMemoryStore
/// <param name="logger"></param>
public QdrantMemoryStore(string host, int port, int vectorSize, ILogger? logger = null)
{
this._logger = logger;
this._qdrantClient = new QdrantVectorDbClient(endpoint: host, port: port, vectorSize: vectorSize, log: logger);
}

Expand Down Expand Up @@ -322,22 +328,47 @@ public async Task RemoveWithPointIdBatchAsync(string collectionName, IEnumerable
bool withEmbeddings = false,
[EnumeratorCancellation] CancellationToken cancel = default)
{
var results = this._qdrantClient.FindNearestInCollectionAsync(
IAsyncEnumerator<(QdrantVectorRecord, double)> enumerator = this._qdrantClient.FindNearestInCollectionAsync(
collectionName: collectionName,
target: embedding.Vector,
threshold: minRelevanceScore,
top: limit,
withVectors: withEmbeddings,
cancel: cancel);
cancel: cancel)
.GetAsyncEnumerator(cancel);

await foreach ((QdrantVectorRecord, double) result in results)
// Workaround for https://github.com/dotnet/csharplang/issues/2949: Yielding in catch blocks not supported in async iterators
(QdrantVectorRecord, double)? result = null;
bool hasResult = true;
do
{
yield return (
MemoryRecord.FromJsonMetadata(
json: result.Item1.GetSerializedPayload(),
embedding: new Embedding<float>(result.Item1.Embedding)),
result.Item2);
}
try
{
hasResult = await enumerator.MoveNextAsync().ConfigureAwait(false);
if (hasResult)
{
result = enumerator.Current;
}
else
{
result = null;
}
}
catch (HttpRequestException ex) when (ex.Message.Contains("404"))
{
this._logger?.LogWarning("NotFound when calling {0}::FindNearestInCollectionAsync - the collection '{1}' may not exist yet.", nameof(QdrantMemoryStore), collectionName);
hasResult = false;
}

if (result != null)
{
yield return (
MemoryRecord.FromJsonMetadata(
json: result.Value.Item1.GetSerializedPayload(),
embedding: new Embedding<float>(result.Value.Item1.Embedding)),
result.Value.Item2);
}
} while (hasResult);
}

/// <inheritdoc/>
Expand Down