Qdrant .NET SDK

repository·main·Indexed 42 days ago

https://github.com/qdrant/qdrant-dotnet

A high-level .NET client for the Qdrant vector database. The SDK provides an interface for managing collections, upserting vectors with payloads, and performing similarity searches with filtering via the Qdrant.Client NuGet package.

Tokens
1.5K
Snippets
9
Records
9
Agent score
3%

What's inside Qdrant

  1. Configure Qdrant client for .NET Framework

    main

    Because .NET Framework has limited support for gRPC over HTTP/2, you must manually configure the client to use TLS and WinHttpHandler.

    Requirements:

    • Use HTTPS.
    • Reference System.Net.Http.WinHttpHandler version 6.0.1 or later.
    • Configure WinHttpHandler as the inner handler for GrpcChannelOptions.
    • Manually add the api-key to the metadata via a call invoker interceptor, as the automatic User-Agent token is not added automatically on .NET Framework.
    var channel = GrpcChannel.ForAddress($"https://localhost:6334", new GrpcChannelOptions
    {
      HttpHandler = new WinHttpHandler
      {
        ServerCertificateValidationCallback = 
          CertificateValidation.Thumbprint("<certificate thumbprint>")
      }
    });
    var callInvoker = channel.Intercept(metadata =>
    {
      metadata.Add("api-key", "<api key>");
      return metadata;
    });
    
    var grpcClient = new QdrantGrpcClient(callInvoker);
    var client = new QdrantClient(grpcClient);
  2. Search for similar vectors

    main

    Perform a vector similarity search using SearchAsync. You provide the collection name, the query vector, and an optional limit to specify how many results to return.

    var queryVector = Enumerable.Range(1, 100).Select(_ => (float)random.NextDouble()).ToArray();
    
    // return the 5 closest points
    var points = await client.SearchAsync(
      "my_collection",
      queryVector,
      limit: 5);
  3. Search with filtering conditions

    main

    Enhance your vector search by applying filters using the filter parameter in SearchAsync. You can use the Qdrant.Client.Grpc.Conditions namespace to build complex filtering logic, such as Range queries.

    // static import Conditions to easily build filtering
    using static Qdrant.Client.Grpc.Conditions;
    
    // return the 5 closest points where rand_number >= 3
    var points = await client.SearchAsync(
      "my_collection",
      queryVector,
      filter: Range("rand_number", new Range { Gte = 3 }),
      limit: 5);
  4. Create a collection

    main

    Use CreateCollectionAsync to initialize a new collection in Qdrant. You must specify VectorParams, including the vector Size and the Distance metric (e.g., Distance.Cosine).

    await client.CreateCollectionAsync("my_collection", 
        new VectorParams { Size = 100, Distance = Distance.Cosine });
  5. Upsert vectors into a collection

    main

    Insert or update points in a collection using UpsertAsync. Each point (PointStruct) requires an Id, a vector (array of floats), and an optional Payload (dictionary of values).

    // generate some vectors
    var random = new Random();
    var points = Enumerable.Range(1, 100).Select(i => new PointStruct
    {
      Id = (ulong)i,
      Vectors = Enumerable.Range(1, 100).Select(_ => (float)random.NextDouble()).ToArray(),
      Payload = 
      {
        ["color"] = "red", 
        ["rand_number"] = i % 10 
      }
    }).ToList();
    
    var updateResult = await client.UpsertAsync("my_collection", points);
  6. Create a QdrantClient

    main

    The QdrantClient is the high-level entry point for interacting with Qdrant.

    For a simple local setup, you can instantiate it with just a hostname, which defaults to http://localhost:6334:

    var client = new QdrantClient("localhost");

    For more advanced configurations involving TLS, certificate thumbprint validation, and API key authentication, use QdrantChannel.ForAddress with ClientConfiguration to create a QdrantGrpcClient first.

    var channel = QdrantChannel.ForAddress("https://localhost:6334", new ClientConfiguration
    {
        ApiKey = "<api key>",
        CertificateThumbprint = "<certificate thumbprint>"
    });
    var grpcClient = new QdrantGrpcClient(channel);
    var client = new QdrantClient(grpcClient);