Function single_embedding

Source
pub async fn single_embedding(
    text: &String,
    model: &str,
) -> Result<Vec<f32>, Box<dyn Error + Send + Sync>>
Expand description

Asynchronously generates a single embedding vector for the given text using a specified model.

This function creates an embedding for the input text by calling an external service (e.g., OpenAI’s API) with the specified model. It returns the embedding vector as a Vec<f32>.

§Parameters

  • text: A reference to a String containing the text to be embedded.
  • model: A string slice (&str) specifying the model to use for generating the embedding.

§Returns

A Result<Vec<f32>, Box<dyn std::error::Error + Send + Sync>>:

  • Ok(Vec<f32>) containing the embedding vector if the operation is successful.
  • Err(Box<dyn std::error::Error + Send + Sync>) if there is an error during the operation, including issues with creating the request, network errors, or if the response does not contain an embedding.

§Errors

This function can return an error in several cases, including:

  • Failure to build the embedding request.
  • Network or API errors when contacting the external service.
  • The response from the external service does not include an embedding vector.

§Example

use std::path::Path;
use your_module::{single_embedding, FuncEnumsError, Client, CreateEmbeddingRequestArgs};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let text = String::from("Your sample text here");
    let model = "your-model-name";
     
    let embedding = single_embedding(&text, model).await?;
    println!("Embedding vector: {:?}", embedding);

    Ok(())
}