langchain-rust 5.1.0

LangChain for Rust, the easiest way to write LLM-based programs in Rust
Documentation
#[cfg(feature = "fastembed")]
use langchain_rust::embedding::{
    onnx_environment, Embedder, EmbeddingModel, FastEmbed, TextEmbedding, TextInitOptions,
};

#[cfg(feature = "fastembed")]
#[tokio::main]
async fn main() {
    // With default model
    let fastembed = FastEmbed::try_new().unwrap();
    let embeddings = fastembed
        .embed_documents(&["hello world".to_string(), "foo bar".to_string()])
        .await
        .unwrap();

    println!("Len: {}", embeddings.len());
    println!("Embeddings: {:?}", embeddings);

    // With custom model
    let model = TextEmbedding::try_new(
        TextInitOptions::new(EmbeddingModel::AllMiniLML6V2)
            .with_show_download_progress(true)
            // Only one ONNX environment may exist per process; reuse the shared one.
            .with_environment(onnx_environment().unwrap()),
    )
    .unwrap();

    let fastembed = FastEmbed::from(model);

    fastembed
        .embed_documents(&["hello world".to_string(), "foo bar".to_string()])
        .await
        .unwrap();

    println!("Len: {:?}", embeddings.len());
}

#[cfg(not(feature = "fastembed"))]
fn main() {
    println!("This example requires the 'fastembed' feature to be enabled.");
    println!("Please run the command as follows:");
    println!("cargo run --example embedding_fastembed --features=fastembed");
}