Module response

Module response 

Source
Expand description

OpenAI Embeddings API Response Types

This module provides data structures for deserializing responses from the OpenAI Embeddings API. It includes all necessary types to handle complete API responses, including embedding vectors, metadata, and usage statistics.

§Response Structure

The main response structure contains:

  • object: Always “list” for embedding responses
  • data: Array of embedding objects with vectors and indices
  • model: The model used for generating embeddings
  • usage: Token usage information

§Examples

§Parsing a Simple Embedding Response

use openai_tools::embedding::response::Response;

let json = r#"{
    "object": "list",
    "data": [{
        "object": "embedding",
        "embedding": [0.1, 0.2, 0.3, 0.4, 0.5],
        "index": 0
    }],
    "model": "text-embedding-3-small",
    "usage": {
        "prompt_tokens": 5,
        "total_tokens": 5
    }
}"#;

let response: Response = serde_json::from_str(json).unwrap();
assert_eq!(response.object, "list");
assert_eq!(response.data.len(), 1);

let embedding = response.data[0].embedding.as_1d().unwrap();
assert_eq!(embedding.len(), 5);

§Working with Batch Embeddings

use openai_tools::embedding::response::Response;

let json = r#"{
    "object": "list",
    "data": [
        {"object": "embedding", "embedding": [0.1, 0.2], "index": 0},
        {"object": "embedding", "embedding": [0.3, 0.4], "index": 1},
        {"object": "embedding", "embedding": [0.5, 0.6], "index": 2}
    ],
    "model": "text-embedding-3-small",
    "usage": {"prompt_tokens": 15, "total_tokens": 15}
}"#;

let response: Response = serde_json::from_str(json).unwrap();

for data in &response.data {
    let vector = data.embedding.as_1d().unwrap();
    println!("Index {}: {:?}", data.index, vector);
}

§Embedding Dimensions

The Embedding enum supports multiple dimensionalities:

  • 1D: Standard embedding vector (most common)
  • 2D: Matrix of embeddings
  • 3D: Tensor of embeddings

Use the is_*d() and as_*d() methods to check and access the appropriate dimension.

Structs§

EmbeddingData
Single embedding data item from the API response.
EmbeddingUsage
Token usage statistics for the embedding request.
Response
Complete response from the OpenAI Embeddings API.

Enums§

Embedding
Embedding vector that supports multiple dimensionalities.