Rig-rs

Rig 是一个 Rust 库,用于构建可伸缩、模块化和符合人体工程学的 LLM 支持的应用程序。它完全支持 LLM 完成和嵌入工作流,且模板代码极少。

Rig 支持 Qdrant 作为向量存储来语义地摄取和搜索文档。

安装

cargo add rig-core rig-qdrant qdrant-client

用法

这是使用 Rig 和 Qdrant 进行摄取和检索流程的示例。

use qdrant_client::{
    qdrant::{PointStruct, QueryPointsBuilder, UpsertPointsBuilder},
    Payload, Qdrant,
};
use rig::{
    embeddings::EmbeddingsBuilder,
    providers::openai::{Client, TEXT_EMBEDDING_3_SMALL},
    vector_store::VectorStoreIndex,
};
use rig_qdrant::QdrantVectorStore;
use serde_json::json;

const COLLECTION_NAME: &str = "rig-collection";

// Initialize Qdrant client.
let client = Qdrant::from_url("http://localhost:6334").build()?;
// Initialize OpenAI client.
let openai_client = Client::new("<OPENAI_API_KEY>");
let model = openai_client.embedding_model(TEXT_EMBEDDING_3_SMALL);

let documents = EmbeddingsBuilder::new(model.clone())
    .simple_document("0981d983-a5f8-49eb-89ea-f7d3b2196d2e", "Definition of a *flurbo*: A flurbo is a green alien that lives on cold planets")
    .simple_document("62a36d43-80b6-4fd6-990c-f75bb02287d1", "Definition of a *glarb-glarb*: A glarb-glarb is a ancient tool used by the ancestors of the inhabitants of planet Jiro to farm the land.")
    .simple_document("f9e17d59-32e5-440c-be02-b2759a654824", "Definition of a *linglingdong*: A term used by inhabitants of the far side of the moon to describe humans.")
    .build()
    .await?;

let points: Vec<PointStruct> = documents
    .into_iter()
    .map(|d| {
        let vec: Vec<f32> = d.embeddings[0].vec.iter().map(|&x| x as f32).collect();
        PointStruct::new(
            d.id,
            vec,
            Payload::try_from(json!({
                "document": d.document,
            }))
            .unwrap(),
        )
    })
    .collect();

client
    .upsert_points(UpsertPointsBuilder::new(COLLECTION_NAME, points))
    .await?;

let query_params = QueryPointsBuilder::new(COLLECTION_NAME).with_payload(true);
let vector_store = QdrantVectorStore::new(client, model, query_params.build());

let results = vector_store
    .top_n::<serde_json::Value>("Define a glarb-glarb?", 1)
    .await?;

println!("Results: {:?}", results);

延伸阅读

此页面是否有用?

感谢您的反馈!🙏

抱歉让您失望了。😔 您可以在 GitHub 上编辑此页面,或创建一个 GitHub Issue。