Skip to main content

Module request

Module request 

Source
Expand description

OpenAI Embeddings API Request Module

This module provides the functionality to build and send requests to the OpenAI Embeddings API. It offers a builder pattern for constructing embedding requests, allowing you to convert text into numerical vector representations that capture semantic meaning.

§Key Features

  • Builder Pattern: Fluent API for constructing embedding requests
  • Single & Batch Input: Support for single text or multiple texts at once
  • Encoding Formats: Support for float and base64 output formats
  • Error Handling: Robust error management and validation

§Quick Start

use openai_tools::embedding::request::Embedding;
use openai_tools::common::models::EmbeddingModel;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize the embedding client
    let mut embedding = Embedding::new()?;

    // Generate embedding for a single text
    let response = embedding
        .model(EmbeddingModel::TextEmbedding3Small)
        .input_text("Hello, world!")
        .embed()
        .await?;

    let vector = response.data[0].embedding.as_1d().unwrap();
    println!("Embedding dimension: {}", vector.len());
    Ok(())
}

§Batch Processing

use openai_tools::embedding::request::Embedding;
use openai_tools::common::models::EmbeddingModel;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut embedding = Embedding::new()?;

    // Embed multiple texts in a single request
    let texts = vec!["First text", "Second text", "Third text"];

    let response = embedding
        .model(EmbeddingModel::TextEmbedding3Small)
        .input_text_array(texts)
        .embed()
        .await?;

    for data in &response.data {
        println!("Index {}: {} dimensions",
                 data.index,
                 data.embedding.as_1d().unwrap().len());
    }
    Ok(())
}

Structs§

Embedding
Main struct for building and sending embedding requests to the OpenAI API.