Extract core logic from qdrant and make it available as a library.

Overview

Qdrant lib

Why?

Qdrant is a vector search engine known for its speed, scalability, and user-friendliness. While it excels in its domain, it currently lacks a library interface for direct embedding into applications. This is fine for constructing a web service geared towards semantic search, as Qdrant can be used directly. However, for desktop or mobile applications, integrating a separate service is not an ideal approach. This library has been developed to address this specific challenge.

What?

Qdrant offers both GRPC and RESTful APIs. This library is designed to mirror the functionality of Qdrant's RESTful APIs. As it is intended for embedding within applications, it supports only the following core APIs:

  • collections & aliases
  • points
  • search
  • recommend
  • snapshot

However, the following service/cluster-related APIs will not be included in the supported features:

  • cluster
  • discovery
  • shard

How?

Qdrant's core architecture comprises components such as collection, memory, segment, and storage. The primary data structure we need to initialize is TableOfContent. Once we establish a method to create this structure, our next step is to integrate its functionality with the public-facing client APIs. However, since TableOfContent initiates multiple Tokio runtimes for indexing and searching, it cannot be operated directly under a standard #[tokio::main] application. To resolve this, we instantiate TableOfContent in a dedicated thread and facilitate communication through Tokio mpsc channels. All public-facing APIs function by internally dispatching messages to the dedicated thread and then awaiting the response.

arch

Users can initiate a Qdrant instance in the following manner:

let client = QdrantInstance::start(None)?;

This process results in the creation of an Arc<QdrantClient>, which is based on the following data structure:

#[derive(Debug)]
pub struct QdrantClient {
    tx: ManuallyDrop<mpsc::Sender<QdrantMsg>>,
    terminated_rx: oneshot::Receiver<()>,
    #[allow(dead_code)]
    handle: JoinHandle<Result<(), QdrantError>>,
}

It's crucial to ensure that when the QdrantClient is disposed of, the TableOfContent is also appropriately dropped before the main thread terminates. To achieve this, we have implemented the Drop trait for QdrantClient:

impl Drop for QdrantClient {
    fn drop(&mut self) {
        // drop the tx channel to terminate the qdrant thread
        unsafe {
            ManuallyDrop::drop(&mut self.tx);
        }
        while let Err(TryRecvError::Empty) = self.terminated_rx.try_recv() {
            warn!("Waiting for qdrant to terminate");
            thread::sleep(std::time::Duration::from_millis(100));
        }
    }
}

This approach is designed to pause until a termination message is received from the thread maintaining the TableOfContent:

loop {
    match Arc::try_unwrap(toc_arc) {
        Ok(toc) => {
            drop(toc);
            if let Err(e) = terminated_tx.send(()) {
                warn!("Failed to send termination signal: {:?}", e);
            }
            break;
        }
        Err(toc) => {
            toc_arc = toc;
            warn!("Waiting for ToC to be gracefully dropped");
            thread::sleep(Duration::from_millis(300));
        }
    }
}

How to use?

The library is currently in active development. To use it, simply add the following line to your Cargo.toml file:

qdrant-lib = { git = "https://github.com/tyrchen/qdrant-lib", tag = "v0.x.y" }

Then you could use it in your code:

let client = QdrantInstance::start(None)?;
let collection_name = "test_collection2";
match client
    .create_collection(collection_name, Default::default())
    .await
{
    Ok(v) => println!("Collection created: {:?}", v),
    Err(QdrantError::Storage(StorageError::BadInput { description })) => {
        println!("{description}");
    }
    Err(e) => panic!("Unexpected error: {:?}", e),
}

let collections = client.list_collections().await?;
println!("Collections: {:?}", collections);

For more detailed usage, refer to the examples folder. It includes a straightforward example demonstrating how to index a Wikipedia dataset and perform searches on it.

Future plan

  • Provide a set of high-level APIs for common use cases
  • Better documentation for the public APIs
  • Add unit/integration tests
  • Add performance benchmarks
  • Add python bindings
  • Add nodejs bindings

Caveats

Currently, the library employs a modified version of the original Qdrant code. As a standalone library, minimizing unnecessary dependencies is crucial. However, the original Qdrant codebase includes several dependencies that are redundant for the library's purposes. For instance, the collection crate depends on actix-web-validator, which in turn introduces the entire actix ecosystem into the library. To circumvent this, we've eliminated the actix-web-validator dependency, opting instead to integrate the pertinent code directly into the collection crate (code). While this is not an ideal solution, we plan to explore more optimal alternatives in the future.

Furthermore, we intend to remove certain dependencies, notably axum within the api crate, which was originally introduced by the tonic transport feature.

Update: Upon further review, we found that disabling the transport feature of tonic renders the api crate non-compilable. Additionally, crates like collection / storage are heavily dependent on the api crate. Therefore, we need to retain this feature for the time being.

License

For information regarding the licensing, please refer to the Qdrant License available on their GitHub repo. As of the current date, it is under the Apache 2.0 License.

You might also like...
Tricking shells into interactive mode when local PTY's are not available
Tricking shells into interactive mode when local PTY's are not available

Remote Pseudoterminals Remote Pseudoterminals or "RPTY" is a Rust library which intercepts calls to the Linux kernel's TTY/PTY-related libc functions

Core Fiberplane data models and methods for transforming them (templates, providers, markdown conversion)

fiberplane This repository is a monorepo for Rust code that is used throughout Fiberplane's product. Overview base64uuid - A utility for working with

Rust SDK for the core C2PA (Coalition for Content Provenance and Authenticity) specification

C2PA Rust SDK The Coalition for Content Provenance and Authenticity (C2PA) addresses the prevalence of misleading information online through the devel

Core lightning (CLN) plugin to watch channel health, gossip health and ping amboss for online status

vitality Core lightning (CLN) plugin to watch channel health, gossip health and ping amboss for online status Installation Building Usage Telegram Opt

Core Lightning plugin for sending zap (NIP-57) notes

Core Lightning plugin for sending zap events You can add the plugin by copying it to CLN's plugin directory or by adding the following line to your co

Neptune-core is the reference implementation for the Neptune protocol

Neptune Core Neptune-core is the reference implementation for the Neptune protocol. The implementation is not complete yet, but already supports many

CLI tool that make it easier to perform multiple lighthouse runs towards a single target and output the result in a plotable format.

Lighthouse Aggregator CLI tool that make it easier to perform multiple lighthouse runs towards a single target and output the result in a "plotable" f

A tiny crate to make it easy to share and apply Git hooks for Rust projects
A tiny crate to make it easy to share and apply Git hooks for Rust projects

Shareable git hooks for Rust project. Sloughi is a friend of Husky from North Africa! :algeria:

Workflows make it easy to browse, search, execute and share commands (or a series of commands)--without needing to leave your terminal.
Workflows make it easy to browse, search, execute and share commands (or a series of commands)--without needing to leave your terminal.

Workflows The repo for all public Workflows that appear within Warp and within commands.dev. To learn how to create local or repository workflows, see

Comments
  • fix: export more types

    fix: export more types

    Using qdrant_lib as a crate, I found that some types were not exported. I did not test all the interfaces. I just tried according to the example. Other interfaces should have similar problems. Currently, only these are fixed.

    opened by zackshen 0
  • Qdrant modules are private in examples

    Qdrant modules are private in examples

    For example VectorParams is a pub enum for the qdrant module.

    Are we intended to use the official qdrant libraries for these?

    Excited to find this project!

    opened by ShelbyJenkins 2
Releases(v0.1.4)
  • v0.1.4(Jan 1, 2024)

  • v0.1.3(Dec 27, 2023)

  • v0.1.2(Dec 17, 2023)


    0.1.2 - 2023-12-17

    Features

    • spawn a new task for message processing for maximum concurrency - (f8cfd94) - Tyr Chen

    Other

    • Merge pull request #1 from WSL0809/master

    Update README.md - (32caa14) - Tyr Chen

    • use cocogitto cliff config to properly show commit url - (37df61c) - Tyr Chen
    Source code(tar.gz)
    Source code(zip)
  • v0.1.1(Dec 16, 2023)

    [0.1.1] - 2023-12-16

    Bug Fixes

    • Fix pthread invalid argument issue. It is causd by qdrant is not terminated properly (95c5774 - 2023-12-15 by Tyr Chen)
    • Fix CI (ceef0f0 - 2023-12-16 by Tyr Chen)
    • Fix CI and prepare for release (ad53b23 - 2023-12-16 by Tyr Chen)

    Miscellaneous Tasks

    • Improve CI to generate changelog upon new git tag (f860688 - 2023-12-16 by Tyr Chen)

    Other

    • Prepare for git repo (765c8aa - 2023-12-15 by Tyr Chen)
    • Use thiserror for better error handling (efd6559 - 2023-12-15 by Tyr Chen)
    • Add the rest of the APIs and provide indexer/searcher examples (9ab234d - 2023-12-16 by Tyr Chen)
    • Remove qdrant/qdrant (156d360 - 2023-12-16 by Tyr Chen)
    • Remove actix-web-validator from lib/collection in tyrchen/qdrant. It brings in lots of actix dependencies which are not necessary (9bd6877 - 2023-12-16 by Tyr Chen)
    • Update readme (6e8aa8f - 2023-12-16 by Tyr Chen)
    • Add get points API and update CI & readme (963d2e2 - 2023-12-16 by Tyr Chen)
    • Small doc update (cfe2e1b - 2023-12-16 by Tyr Chen)
    Source code(tar.gz)
    Source code(zip)
Owner
Tyr Chen
father, programming enthusiast, author and entrepreneur.
Tyr Chen
Fetch and extract HTML's title and description by given link.

extd Fetch and extract HTML's title and description by given link. Usage in Cargo.toml: [dependencies] extd = "0.1.4" Example use extd::extract_td; f

null 4 Nov 4, 2022
Given a set of kmers (fasta format) and a set of sequences (fasta format), this tool will extract the sequences containing the kmers.

Kmer2sequences Description Given a set of kmers (fasta / fastq [.gz] format) and a set of sequences (fasta / fastq [.gz] format), this tool will extra

Pierre Peterlongo 22 Sep 16, 2023
The module graph logic for Deno CLI

deno_graph The module graph/dependency logic for the Deno CLI. This repository is a Rust crate which provides the foundational code to be able to buil

Deno Land 67 Dec 14, 2022
Compiler for an "extended" version of the Mindustry logic language

Minblur Compiler Minblur is a compiler for a superset of "logic" programming language in the game Mindustry. It helps reduce code-duplication, making

Binder News 15 May 2, 2022
bevy_scriptum is a a plugin for Bevy that allows you to write some of your game logic in a scripting language

bevy_scriptum is a a plugin for Bevy that allows you to write some of your game logic in a scripting language. Currently, only Rhai is supported, but more languages may be added in the future.

Jarosław Konik 7 Jun 24, 2023
httm prints the size, date and corresponding locations of available unique versions of files residing on ZFS snapshots

httm prints the size, date and corresponding locations of available unique versions of files residing on ZFS snapshots, as well as allowing their interactive viewing and restoration.

null 837 Dec 30, 2022
Command line tool to extract various data from Blender .blend files

blendtool Command line tool to extract various data from Blender .blend files. Currently supports dumping Eevee irradiance volumes to .dds, new featur

null 2 Sep 26, 2021
Extract subsets of ONT (Nanopore) reads based on time

ONTime Extract subsets of ONT (Nanopore) reads based on time Motivation Install Examples Usage Time range format Cite Motivation Some collaborators wa

Michael Hall 5 Jan 17, 2023
Periodically download a youtube playlist, extract audio, convert to mp3, move to directory (possibly synced using syncthing).

auto-dl Periodically download a youtube playlist, extract audio, convert to mp3, move to directory (possibly synced using syncthing). drop https://git

Paul Adenot 10 Jan 12, 2023
Extract patterns from unstructured log messages

logu logu is for extracting patterns from (streaming) unstructured log messages. For parsing unstructured logs, it uses the parser from Drain. In simp

null 78 Oct 21, 2024