Skip to main content

openai_protocol/
embedding.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use super::common::{GenerationRequest, UsageInfo};
5
6// ============================================================================
7// Embedding API
8// ============================================================================
9
10#[serde_with::skip_serializing_none]
11#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
12pub struct EmbeddingRequest {
13    /// ID of the model to use
14    pub model: String,
15
16    /// Input can be a string, array of strings, tokens, or batch inputs
17    pub input: Value,
18
19    /// Optional encoding format (e.g., "float", "base64")
20    pub encoding_format: Option<String>,
21
22    /// Optional user identifier
23    pub user: Option<String>,
24
25    /// Optional number of dimensions for the embedding
26    pub dimensions: Option<u32>,
27
28    /// SGLang extension: request id for tracking
29    pub rid: Option<String>,
30}
31
32impl GenerationRequest for EmbeddingRequest {
33    fn is_stream(&self) -> bool {
34        // Embeddings are non-streaming
35        false
36    }
37
38    fn get_model(&self) -> Option<&str> {
39        Some(&self.model)
40    }
41
42    fn extract_text_for_routing(&self) -> String {
43        // Best effort: extract text content for routing decisions
44        match &self.input {
45            Value::String(s) => s.clone(),
46            Value::Array(arr) => arr
47                .iter()
48                .filter_map(|v| v.as_str())
49                .collect::<Vec<_>>()
50                .join(" "),
51            _ => String::new(),
52        }
53    }
54}
55
56#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
57pub struct EmbeddingObject {
58    pub object: String, // "embedding"
59    pub embedding: Vec<f32>,
60    pub index: u32,
61}
62
63#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
64pub struct EmbeddingResponse {
65    pub object: String, // "list"
66    pub data: Vec<EmbeddingObject>,
67    pub model: String,
68    pub usage: UsageInfo,
69}