rust-client

Rust client for Qdrant vector search engine.

Crates.io Apache 2.0 licensed

Installation

bash cargo add qdrant-client

Package is available in crates.io

Dependencies

The client uses gRPC via the Tonic library.

To change anything in the protocol buffer definitions, you need the protoc Protocol Buffers compiler, along with Protocol Buffers resource files.

Refer to the Tonic installation guide for more details.

Usage

Run Qdrant with enabled gRPC interface:

```bash

With env variable

docker run -p 6333:6333 -p 6334:6334 \ -e QDRANTSERVICEGRPC_PORT="6334" \ qdrant/qdrant ```

Or by updating the configuration file:

yaml service: grpc_port: 6334

More info about gRPC in documentation.

Making requests

Add necessary dependencies:

bash cargo add qdrant-client anyhow tonic tokio --features tokio/rt-multi-thread

Add search example from examples/search.rs to your src/main.rs:

```rust use anyhow::Result; use qdrantclient::prelude::*; use qdrantclient::qdrant::vectorsconfig::Config; use qdrantclient::qdrant::{CreateCollection, SearchPoints, VectorParams, VectorsConfig}; use serde_json::json;

[tokio::main]

async fn main() -> Result<()> { // Example of top level client // You may also use tonic-generated client from src/qdrant.rs let config = QdrantClientConfig::from_url("http://localhost:6334"); let client = QdrantClient::new(Some(config)).await?;

let collections_list = client.list_collections().await?;
dbg!(collections_list);
// collections_list = ListCollectionsResponse {
//     collections: [
//         CollectionDescription {
//             name: "test",
//         },
//     ],
//     time: 1.78e-6,
// }

let collection_name = "test";
client.delete_collection(collection_name).await?;

client
    .create_collection(&CreateCollection {
        collection_name: collection_name.into(),
        vectors_config: Some(VectorsConfig {
            config: Some(Config::Params(VectorParams {
                size: 10,
                distance: Distance::Cosine.into(),
                hnsw_config: None,
                quantization_config: None,
            })),
        }),
        ..Default::default()
    })
    .await?;

let collection_info = client.collection_info(collection_name).await?;
dbg!(collection_info);

let payload: Payload = json!(
    {
        "foo": "Bar",
        "bar": 12,
    }
).try_into().unwrap();

let points = vec![PointStruct::new(0, vec![12.; 10], payload)];
client
    .upsert_points_blocking(collection_name, points, None)
    .await?;

let search_result = client
    .search_points(&SearchPoints {
        collection_name: collection_name.into(),
        vector: vec![11.; 10],
        filter: None,
        limit: 10,
        with_vectors: None,
        with_payload: None,
        params: None,
        score_threshold: None,
        offset: None,
        ..Default::default()
    })
    .await?;
dbg!(search_result);
// search_result = SearchResponse {
//     result: [
//         ScoredPoint {
//             id: Some(
//                 PointId {
//                     point_id_options: Some(
//                         Num(
//                             0,
//                         ),
//                     ),
//                 },
//             ),
//             payload: {},
//             score: 1.0000001,
//             version: 0,
//             vectors: None,
//         },
//     ],
//     time: 5.312e-5,
// }

Ok(())

} ```

Or run the example from this project directly:

bash cargo run --example search

Qdrant Cloud

Qdrant Cloud is a managed service for Qdrant.

The client needs to be configured properly to access the service.

```rust async fn makeclient() -> Result { let mut config = QdrantClientConfig::fromurl("http://xxxxxxxxxx.eu-central.aws.cloud.qdrant.io:6334"); // using an env variable for the API KEY for example let apikey = std::env::var("QDRANTAPI_KEY").ok();

if let Some(api_key) = api_key {
    config.set_api_key(&api_key);
}
let client = QdrantClient::new(Some(config)).await?;
Ok(client)

} ```