Trait EmbeddingModel

Source
pub trait EmbeddingModel {
    // Required methods
    fn dim(&self) -> usize;
    fn embed(&self, text: &str) -> impl Future<Output = Result<Vec<f32>>> + Send;
}
Expand description

Converts text to vector representations.

This trait provides a unified interface for different embedding model implementations, allowing you to switch between providers (OpenAI, Cohere, Hugging Face, etc.) while maintaining the same API.

See the module documentation for more details on embeddings and their use cases.

§Implementation Requirements

  • The embed method must return vectors with length equal to dim
  • Embeddings should be normalized if the underlying model requires it
  • The implementation should handle errors gracefully (network issues, API limits, etc.)

§Example

use ai_types::EmbeddingModel;

struct MyEmbedding {
    api_key: String,
}

impl EmbeddingModel for MyEmbedding {
    fn dim(&self) -> usize {
        1536 // OpenAI text-embedding-ada-002 dimension
    }
     
    async fn embed(&self, text: &str) -> ai_types::Result<Vec<f32>> {
        // In a real implementation, this would call the embedding API
        Ok(vec![0.0; self.dim()])
    }
}

let model = MyEmbedding { api_key: "sk-...".to_string() };
let embedding = model.embed("The quick brown fox").await.unwrap();
assert_eq!(embedding.len(), 1536);

§Performance Considerations

  • Batch multiple texts when possible to reduce API calls
  • Consider caching embeddings for frequently used texts
  • Be aware of rate limits when using cloud-based embedding services

Required Methods§

Source

fn dim(&self) -> usize

Returns the embedding vector dimension.

This value determines the length of vectors returned by embed. Common dimensions include:

  • 384 (Sentence Transformers MiniLM)
  • 768 (BERT-base)
  • 1536 (OpenAI text-embedding-ada-002)
  • 3072 (OpenAI text-embedding-3-large)
Source

fn embed(&self, text: &str) -> impl Future<Output = Result<Vec<f32>>> + Send

Converts text to an embedding vector.

§Arguments
  • text - The input text to embed. Can be a word, sentence, paragraph, or document.
§Returns

A Vec<f32> with length equal to Self::dim. The vector represents the semantic meaning of the input text in high-dimensional space.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§