Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:0xranx OpenContext EmbeddingClient

From Leeroopedia
Knowledge Sources
Domains NLP, Embedding
Last Updated 2026-10-24 12:00 GMT

Overview

Concrete tool for generating text embeddings via the OpenAI-compatible embedding API, implemented in Rust as part of the OpenContext core search pipeline.

Description

The EmbeddingClient struct is a Rust HTTP client that sends batched embedding requests to an OpenAI-compatible API endpoint. It uses `reqwest` for HTTP communication and supports configurable API base URLs, models, dimensions, and batch sizes.

Key features:

  • Batched processing: Texts are sent in batches (configurable `batch_size`) to optimize API throughput.
  • Text truncation: Input texts are truncated to ~2000 characters to stay within embedding model token limits.
  • Dimension auto-detection: The actual embedding dimension is detected from the first API response. If it differs from the configured dimension, the system re-initializes the vector store.
  • Configurable: API key, base URL, model, dimensions, and batch size are all configurable via `EmbeddingConfig`.
  • Error handling: API errors (rate limits, invalid keys, network failures) are surfaced as `SearchError` variants.

Usage

  • Index build: Used by `Indexer` during the embedding phase to generate vectors for all chunks.
  • Query embedding: Used by `Searcher` to embed search queries for vector search.
  • Configuration: Embedding config is managed via `SearchConfig.embedding` which reads from config files and environment variables.

Code Reference

Source Location

  • Repository: OpenContext
  • File: crates/opencontext-core/src/search/embedding.rs
  • File (config): crates/opencontext-core/src/search/config.rs

Signature

pub struct EmbeddingClient {
    config: EmbeddingConfig,
    client: reqwest::Client,
    actual_dimensions: std::sync::atomic::AtomicUsize,
}

impl EmbeddingClient {
    /// Create a new embedding client
    pub fn new(config: EmbeddingConfig) -> SearchResult<Self>;

    /// Get embedding dimensions (returns actual detected dimensions if available)
    pub fn dimensions(&self) -> usize;

    /// Get actual dimensions detected from API (0 if not yet detected)
    pub fn actual_dimensions(&self) -> usize;

    /// Generate embeddings for multiple texts
    pub async fn embed(&self, texts: Vec<String>) -> SearchResult<Vec<Vec<f32>>>;

    /// Generate embedding for a single text
    pub async fn embed_one(&self, text: &str) -> SearchResult<Vec<f32>>;
}

Import

use opencontext_core::search::embedding::EmbeddingClient;

I/O Contract

Inputs (embed)

Name Type Required Description
texts Vec<String> Yes Texts to embed (truncated to ~2000 chars each)

Outputs (embed)

Name Type Description
returns Vec<Vec<f32>> Embedding vectors, one per input text

Configuration (EmbeddingConfig)

Name Type Default Description
api_key String — API key for embedding provider
api_base String https://api.openai.com/v1 API base URL
model String text-embedding-3-small Embedding model name
dimensions usize 1536 Expected embedding dimensions
batch_size usize from config Batch size for API calls

Usage Examples

Basic Embedding

use opencontext_core::search::embedding::EmbeddingClient;
use opencontext_core::search::config::EmbeddingConfig;

let config = EmbeddingConfig {
    api_key: "sk-...".to_string(),
    api_base: "https://api.openai.com/v1".to_string(),
    model: "text-embedding-3-small".to_string(),
    dimensions: 1536,
    batch_size: 100,
};

let client = EmbeddingClient::new(config)?;

// Embed multiple texts
let texts = vec!["Hello world".to_string(), "Goodbye world".to_string()];
let embeddings = client.embed(texts).await?;

println!("Got {} embeddings, dim: {}", embeddings.len(), embeddings[0].len());

Single Text Embedding (for queries)

let query_embedding = client.embed_one("how to load models").await?;
// Use this vector for similarity search

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment