6 个版本

0.1.1 2024 年 5 月 16 日
0.1.0 2024 年 4 月 29 日
0.0.4 2024 年 4 月 2 日
0.0.3 2024 年 3 月 8 日
0.0.1 2024 年 1 月 28 日

#142 in 机器学习

Download history 74/week @ 2024-04-16 226/week @ 2024-04-23 144/week @ 2024-04-30 73/week @ 2024-05-07 284/week @ 2024-05-14 274/week @ 2024-05-21 107/week @ 2024-05-28 78/week @ 2024-06-04 112/week @ 2024-06-11 143/week @ 2024-06-18 174/week @ 2024-06-25 125/week @ 2024-07-02 125/week @ 2024-07-09 155/week @ 2024-07-16 122/week @ 2024-07-23 72/week @ 2024-07-30

每月 484 次下载
用于 llama-core

Apache-2.0

18KB
313

轻量级的 Qdrant 客户端库,适用于 Rust

此非官方客户端的关键要求有两点

  • 编译成 Wasm 并在 WasmEdge 运行时下运行。
  • 支持向量集合和点的基本 CRUD 操作。
  • 支持 TLS 以远程安装 Qdrant 数据库。

快速入门

安装 WasmEdge 和 Rust 工具。

curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash
source $HOME/.wasmedge/env

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup target add wasm32-wasi

使用快速入门指南在 Docker 中启动 Qdrant 实例。

mkdir qdrant_storage

docker run -p 6333:6333 -p 6334:6334 \
    -v $(pwd)/qdrant_storage:/qdrant/storage:z \
    qdrant/qdrant

构建并运行此仓库中的 examples

cd examples
RUSTFLAGS="--cfg wasmedge --cfg tokio_unstable" cargo build --target wasm32-wasi --release
wasmedge target/wasm32-wasi/release/qdrant_examples.wasm

示例

以下是来自 examples/src/main.rs 的代码,展示如何执行 CRUD 操作。

    // Create
    let r = client.create_collection("my_test", 4).await;

    // Insert / Update
    let mut points = Vec::<Point>::new();
    points.push(Point{
        id: PointId::Num(1), vector: vec!(0.05, 0.61, 0.76, 0.74), payload: json!({"city": "Berlin"}).as_object().map(|m| m.to_owned())
    });
    points.push(Point{
        id: PointId::Num(2), vector: vec!(0.19, 0.81, 0.75, 0.11), payload: json!({"city": "London"}).as_object().map(|m| m.to_owned())
    });
    points.push(Point{
        id: PointId::Num(3), vector: vec!(0.36, 0.55, 0.47, 0.94), payload: json!({"city": "Moscow"}).as_object().map(|m| m.to_owned())
    });
    points.push(Point{
        id: PointId::Num(4), vector: vec!(0.18, 0.01, 0.85, 0.80), payload: json!({"city": "New York"}).as_object().map(|m| m.to_owned())
    });
    points.push(Point{
        id: PointId::Num(5), vector: vec!(0.24, 0.18, 0.22, 0.44), payload: json!({"city": "Beijing"}).as_object().map(|m| m.to_owned())
    });
    points.push(Point{
        id: PointId::Num(6), vector: vec!(0.35, 0.08, 0.11, 0.44), payload: json!({"city": "Mumbai"}).as_object().map(|m| m.to_owned())
    });
    let r = client.upsert_points("my_test", points).await;
    println!("The collection size is {}", client.collection_info("my_test").await);

    // Retrieve #1
    let ps = client.get_points("my_test", vec!(1, 2, 3, 4, 5, 6)).await;
    println!("The 1-6 points are {:?}", ps);

    // Retrieve Search
    let q = vec![0.2, 0.1, 0.9, 0.7];
    let r = client.search_points("my_test", q, 2).await;
    println!("Search result points are {:?}", r);

    // Delete
    let r = client.delete_points("my_test", vec!(1, 4)).await;
    println!("Delete points result is {:?}", r);
    println!("The collection size is {}", client.collection_info("my_test").await);

编写代码

将以下补丁添加到 cargo.toml,然后您可以将 qdrant_rest_clienttokio 作为常规依赖项使用。

[patch.crates-io]
socket2 = { git = "https://github.com/second-state/socket2.git", branch = "v0.5.x" }
reqwest = { git = "https://github.com/second-state/wasi_reqwest.git", branch = "0.11.x" }
hyper = { git = "https://github.com/second-state/wasi_hyper.git", branch = "v0.14.x" }
tokio = { git = "https://github.com/second-state/wasi_tokio.git", branch = "v1.36.x" }

[dependencies]
anyhow = "1.0"
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
url = "2.3"
tokio = { version = "1", features = [
    "io-util",
    "fs",
    "net",
    "time",
    "rt",
    "macros",
] }
qdrant_rest_client = "0.1"

要构建它,您需要像上面的快速入门一样传递 Rust 编译器的标志。

RUSTFLAGS="--cfg wasmedge --cfg tokio_unstable" cargo build --target wasm32-wasi --release

或者,您可以将它添加到 .cargo/config.toml 文件中。

[build]
target = "wasm32-wasi"
rustflags = ["--cfg", "wasmedge", "--cfg", "tokio_unstable"]

[target.wasm32-wasi]
runner = "wasmedge"

之后,您只需使用 cargo buildcargo run 来构建和运行应用程序。

依赖关系

~6–18MB
~268K SLoC