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

.Net: Bug: Qdrant CreateCollectionIfNotExistsAsync fails for the first time #9799

Open
panicoenlaxbox opened this issue Nov 23, 2024 · 0 comments
Labels
bug Something isn't working .NET Issue or Pull requests regarding .NET code triage

Comments

@panicoenlaxbox
Copy link

Describe the bug
An error occurs when creating a Qdrant collection for the first time. However, the collection is created. A subsequent run works fine.
The problem is in await collection.CreateCollectionIfNotExistsAsync();

System.Collections.Generic.KeyNotFoundException
  HResult=0x80131577
  Message=The given key 'System.String[]' was not present in the dictionary.
  Source=System.Private.CoreLib
  StackTrace:
   at System.ThrowHelper.ThrowKeyNotFoundException[T](T key)
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Microsoft.SemanticKernel.Connectors.Qdrant.QdrantVectorStoreRecordCollection`1.<CreateCollectionAsync>d__15.MoveNext()
   at Microsoft.SemanticKernel.Connectors.Qdrant.QdrantVectorStoreRecordCollection`1.<CreateCollectionIfNotExistsAsync>d__16.MoveNext()
   at Program.<<Main>$>d__0.MoveNext() in ...

To Reproduce

#pragma warning disable SKEXP0010
#pragma warning disable SKEXP0001
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.Qdrant;
using Microsoft.SemanticKernel.Embeddings;
using Qdrant.Client;

var configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", optional: true)
    .Build();

var services = new ServiceCollection();
services
    .AddTransient<IConfiguration>(_ => configuration)
    .AddLogging(configure => configure
        .AddConfiguration(configuration.GetSection("Logging"))
        .AddConsole());


var endpoint = "YOUR_ENDPOINT";
var apiKey = "YOUR_API_KEY";

services.AddAzureOpenAITextEmbeddingGeneration("text-embedding-ada-002", endpoint, apiKey);

services.AddTransient((serviceProvider) =>
{
    return new Kernel(serviceProvider);
});

using var scope = services.BuildServiceProvider().CreateScope();

var kernel = scope.ServiceProvider.GetRequiredService<Kernel>();

var vectorStore = new QdrantVectorStore(new QdrantClient("localhost"));
var collection = vectorStore.GetCollection<ulong, Customer>("skcustomers");
await collection.CreateCollectionIfNotExistsAsync();  // Here it's the error

async Task<ReadOnlyMemory<float>> GenerateEmbeddingAsync(string textToVectorize)
{
    var textEmbeddingGenerationService = scope.ServiceProvider.GetRequiredService<ITextEmbeddingGenerationService>();
    ReadOnlyMemory<float> embedding = await textEmbeddingGenerationService.GenerateEmbeddingAsync(textToVectorize);
    return embedding;
}

const ulong customerId = 1;
const string description = "It's an amazing company that helps you build new, quality applications with an agile approach.";

await collection.UpsertAsync(new Customer
{
    Id = customerId,
    Name = "BitAttack",
    Email = "[email protected]",
    Description = description,
    Tags = ["development", "software", "agile"],
    DescriptionEmbedding = await GenerateEmbeddingAsync(description),    
});

Customer? retrievedCustomer = await collection.GetAsync(customerId);
Console.WriteLine(retrievedCustomer);

public class Customer
{
    [VectorStoreRecordKey]
    public ulong Id { get; set; }

    [VectorStoreRecordData(IsFilterable = true)]
    public string Name { get; set; }

    [VectorStoreRecordData]
    public string Email { get; set; }

    [VectorStoreRecordData(IsFullTextSearchable = true)]
    public string Description { get; set; }

    [VectorStoreRecordData(IsFilterable = true)]
    public string[] Tags { get; set; }

    [VectorStoreRecordVector(Dimensions: 1536, DistanceFunction.CosineSimilarity, IndexKind.Hnsw)]
    public ReadOnlyMemory<float>? DescriptionEmbedding { get; set; }

    public override string ToString() => $"Id: {Id}, Name: {Name}, Email: {Email}, Description: {Description}, Tags: {string.Join(", ", Tags)}";
}

Expected behavior
Don't fail the first time

Screenshots
If applicable, add screenshots to help explain your problem.

Platform
Relevant packages used (I think):

	<PackageReference Include="Microsoft.Extensions.VectorData.Abstractions" Version="9.0.0-preview.1.24523.1" />
	<PackageReference Include="Microsoft.SemanticKernel" Version="1.30.0" />
	<PackageReference Include="Microsoft.SemanticKernel.Connectors.Qdrant" Version="1.30.0-preview" />

Additional context
For now, I can go ahead with this:

try
{
    await collection.CreateCollectionIfNotExistsAsync();
}
catch (KeyNotFoundException)
{
}
@panicoenlaxbox panicoenlaxbox added the bug Something isn't working label Nov 23, 2024
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code triage labels Nov 23, 2024
@github-actions github-actions bot changed the title Bug: Qdrant CreateCollectionIfNotExistsAsync fails for the first time .Net: Bug: Qdrant CreateCollectionIfNotExistsAsync fails for the first time Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working .NET Issue or Pull requests regarding .NET code triage
Projects
None yet
Development

No branches or pull requests

2 participants