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