turbospark-server 0.3.0

OpenAI Chat Completions and Anthropic Messages server on loopback: request/response envelopes, SSE streaming framing, and the axum router, wired to the raw-completion loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! OpenAI and Ollama-compatible embedding endpoints.
//!
//! - `POST /v1/embeddings`: OpenAI-compatible embeddings
//! - `POST /api/embeddings`: Ollama legacy embeddings
//! - `POST /api/embed`: Ollama batch embeddings

use axum::extract::State;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use serde::{Deserialize, Serialize};

use crate::handler::{error_response, resolve_embedding_backend};
use crate::model::ChatModel;
use crate::observe::RequestTag;
use crate::ServerState;

/// `encoding_format`'s two OpenAI-documented spellings. Anything else is
/// refused rather than silently treated as `float`: a caller who sent a
/// typo'd or future value has no way to learn their request was not
/// honoured otherwise.
enum EncodingFormat {
    Float,
    Base64,
}

fn parse_encoding_format(value: Option<&str>) -> Result<EncodingFormat, String> {
    match value {
        None | Some("float") => Ok(EncodingFormat::Float),
        Some("base64") => Ok(EncodingFormat::Base64),
        Some(other) => Err(format!(
            "encoding_format must be float or base64, not {other:?}"
        )),
    }
}

const BASE64_ALPHABET: &[u8; 64] =
    b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/// Standard base64 with padding, the mirror of `vision::base64_decode`.
/// Hand-rolled for the same reason that one is: it is a dozen lines, and
/// this is the only encoder this workspace needs.
fn base64_encode(bytes: &[u8]) -> String {
    let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
    for chunk in bytes.chunks(3) {
        let b0 = chunk[0] as u32;
        let b1 = *chunk.get(1).unwrap_or(&0) as u32;
        let b2 = *chunk.get(2).unwrap_or(&0) as u32;
        let n = (b0 << 16) | (b1 << 8) | b2;
        out.push(BASE64_ALPHABET[(n >> 18 & 0x3F) as usize] as char);
        out.push(BASE64_ALPHABET[(n >> 12 & 0x3F) as usize] as char);
        out.push(if chunk.len() > 1 {
            BASE64_ALPHABET[(n >> 6 & 0x3F) as usize] as char
        } else {
            '='
        });
        out.push(if chunk.len() > 2 {
            BASE64_ALPHABET[(n & 0x3F) as usize] as char
        } else {
            '='
        });
    }
    out
}

/// A vector as OpenAI's own base64 shape: the raw little-endian `f32` bytes,
/// standard-base64-encoded -- what the stock Python SDK sends
/// `encoding_format` as by default and base64-decodes on the way back.
fn embedding_value(v: &[f32], format: &EncodingFormat) -> serde_json::Value {
    match format {
        EncodingFormat::Float => serde_json::json!(v),
        EncodingFormat::Base64 => {
            let mut bytes = Vec::with_capacity(v.len() * 4);
            for x in v {
                bytes.extend_from_slice(&x.to_le_bytes());
            }
            serde_json::json!(base64_encode(&bytes))
        }
    }
}

/// Above this many inputs (OpenAI's own documented ceiling) or this many
/// total bytes of text, a request is refused before it ever reaches the
/// encoder. `spawn_blocking` (below) keeps a real forward pass off the
/// async runtime, but nothing here streams partial results or cancels a
/// batch mid-flight, so an unbounded one would still tie up a blocking
/// thread for an unreasonable span on nothing but request size.
const MAX_EMBEDDING_INPUTS: usize = 2048;
const MAX_EMBEDDING_BYTES: usize = 1 << 20;

fn check_embedding_input_size(texts: &[String]) -> Result<(), String> {
    if texts.len() > MAX_EMBEDDING_INPUTS {
        return Err(format!(
            "too many inputs: {} exceeds the {MAX_EMBEDDING_INPUTS}-item limit",
            texts.len()
        ));
    }
    let total_bytes: usize = texts.iter().map(String::len).sum();
    if total_bytes > MAX_EMBEDDING_BYTES {
        return Err(format!(
            "input too large: {total_bytes} bytes exceeds the {MAX_EMBEDDING_BYTES}-byte limit"
        ));
    }
    Ok(())
}

/// Runs `model.encode` on a blocking thread, matching every generation
/// path's own `spawn_blocking` (`handler::exec::run_full`,
/// `completions::run_full`): a real embedding model's forward pass is
/// synchronous CPU/GPU work, not I/O, and running it inline here parked a
/// tokio worker for its whole duration with no cancellation and no
/// `CancelGuard` -- enough concurrent embedding requests could starve
/// `/health` the same way an un-`spawn_blocking`ed generation would
/// (Gotcha 22). `texts` is threaded through and back so the caller keeps
/// ownership after the `.await` for its own token-count accounting.
async fn encode_blocking(
    model: std::sync::Arc<dyn ChatModel>,
    texts: Vec<String>,
) -> (Vec<String>, Result<Vec<Vec<f32>>, String>) {
    // The FIFO gate's acquisition point for embeddings: the encoder holds
    // the same runner a generation would (`RealEncoderModel` lends through
    // `with_producer`), so an embedding burst must queue behind generations
    // rather than barge past waiters the gate exists to order. There is no
    // cancellation on this path, so the flag the gate reads is one nobody
    // can set -- admission waits, and a queued request cannot be skipped
    // for a disconnect the protocol has no way to observe.
    let gate = model.generation_queue();
    let never = crate::cancel::new_cancel();
    match crate::queue::run_gated(gate, &never, move || {
        let text_refs: Vec<&str> = texts.iter().map(String::as_str).collect();
        let result = model.encode(&text_refs);
        (texts, result)
    })
    .await
    {
        Some(Ok(pair)) => pair,
        Some(Err(e)) => (Vec::new(), Err(format!("embedding task panicked: {e}"))),
        // Unreachable in practice: nothing can fire `never`.
        None => (Vec::new(), Err("embedding admission cancelled".to_string())),
    }
}

/// Input can be a single string or an array of strings.
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub enum EmbeddingInput {
    Single(String),
    Multiple(Vec<String>),
}

impl EmbeddingInput {
    pub fn into_vec(self) -> Vec<String> {
        match self {
            EmbeddingInput::Single(s) => vec![s],
            EmbeddingInput::Multiple(v) => v,
        }
    }
}

/// OpenAI embedding request shape.
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub struct EmbeddingRequest {
    pub input: EmbeddingInput,
    #[serde(default)]
    pub model: Option<String>,
    #[serde(default)]
    pub encoding_format: Option<String>,
    #[serde(default)]
    pub dimensions: Option<usize>,
    #[serde(default)]
    pub user: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct EmbeddingData {
    pub object: &'static str,
    pub index: usize,
    /// An array of `f32` for `encoding_format: "float"` (the default), or a
    /// base64 string for `"base64"` -- one field, two shapes, matching
    /// OpenAI's own wire contract.
    pub embedding: serde_json::Value,
}

#[derive(Debug, Serialize)]
pub struct EmbeddingUsage {
    pub prompt_tokens: usize,
    pub total_tokens: usize,
}

#[derive(Debug, Serialize)]
pub struct EmbeddingResponse {
    pub object: &'static str,
    pub data: Vec<EmbeddingData>,
    pub model: String,
    pub usage: EmbeddingUsage,
}

/// `POST /v1/embeddings`.
pub async fn embeddings(
    State(state): State<ServerState>,
    tag: Option<axum::Extension<RequestTag>>,
    lease_slot: axum::Extension<crate::handler::RequestLeaseSlot>,
    Json(req): Json<EmbeddingRequest>,
) -> Response {
    let model = match resolve_embedding_backend(
        &state,
        &lease_slot.0,
        tag.map(|t| t.0),
        req.model.as_deref(),
    )
    .await
    {
        Ok(m) => m,
        Err(response) => return response,
    };
    let format = match parse_encoding_format(req.encoding_format.as_deref()) {
        Ok(f) => f,
        Err(e) => return error_response(StatusCode::BAD_REQUEST, e),
    };

    let texts = req.input.into_vec();
    if texts.is_empty() {
        return error_response(
            StatusCode::BAD_REQUEST,
            "input must not be empty".to_string(),
        );
    }
    if let Err(e) = check_embedding_input_size(&texts) {
        return error_response(StatusCode::BAD_REQUEST, e);
    }
    if req.dimensions == Some(0) {
        return error_response(
            StatusCode::BAD_REQUEST,
            "dimensions must be greater than 0".to_string(),
        );
    }

    let (texts, encoded) = encode_blocking(model.clone(), texts).await;
    let raw_embeddings = match encoded {
        Ok(embs) => embs,
        Err(err) => return error_response(StatusCode::BAD_REQUEST, err),
    };
    // The backend's own contract (`ChatModel::encode`'s doc) is one vector
    // per input text, in order. A backend that returned a different count
    // would otherwise panic on `texts[index]` below -- a bug in a backend
    // this crate does not control (any FFI host implementing the trait),
    // reported as a 500 rather than crashing the request.
    if raw_embeddings.len() != texts.len() {
        return error_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!(
                "embedding backend returned {} vectors for {} inputs",
                raw_embeddings.len(),
                texts.len()
            ),
        );
    }
    if let Some(dim) = req.dimensions {
        if let Some(width) = raw_embeddings.first().map(Vec::len) {
            if dim > width {
                return error_response(
                    StatusCode::BAD_REQUEST,
                    format!("dimensions {dim} exceeds this model's own width of {width}"),
                );
            }
        }
    }

    let mut data = Vec::with_capacity(raw_embeddings.len());
    let mut total_tokens = 0;
    for (index, mut emb) in raw_embeddings.into_iter().enumerate() {
        // The real tokenizer this server already holds, not a character-count
        // estimate: `usage.prompt_tokens` is what a caller bills against.
        total_tokens += model.tokenizer().encode(&texts[index], false).len();

        if let Some(dim) = req.dimensions {
            if dim < emb.len() {
                emb.truncate(dim);
                let norm: f32 = emb.iter().map(|v| v * v).sum::<f32>().sqrt().max(1e-12);
                for x in &mut emb {
                    *x /= norm;
                }
            }
        }

        data.push(EmbeddingData {
            object: "embedding",
            index,
            embedding: embedding_value(&emb, &format),
        });
    }

    let response = EmbeddingResponse {
        object: "list",
        data,
        model: model.model_id().to_string(),
        usage: EmbeddingUsage {
            prompt_tokens: total_tokens,
            total_tokens,
        },
    };

    Json(response).into_response()
}

/// Ollama legacy embedding request shape (`/api/embeddings`).
#[derive(Debug, Deserialize)]
pub struct OllamaEmbeddingRequest {
    pub model: Option<String>,
    pub prompt: String,
}

#[derive(Debug, Serialize)]
pub struct OllamaEmbeddingResponse {
    pub embedding: Vec<f32>,
}

/// `POST /api/embeddings`.
pub async fn ollama_embeddings(
    State(state): State<ServerState>,
    tag: Option<axum::Extension<RequestTag>>,
    lease_slot: axum::Extension<crate::handler::RequestLeaseSlot>,
    Json(req): Json<OllamaEmbeddingRequest>,
) -> Response {
    let model = match resolve_embedding_backend(
        &state,
        &lease_slot.0,
        tag.map(|t| t.0),
        req.model.as_deref(),
    )
    .await
    {
        Ok(m) => m,
        Err(response) => return response,
    };

    if let Err(e) = check_embedding_input_size(std::slice::from_ref(&req.prompt)) {
        return error_response(StatusCode::BAD_REQUEST, e);
    }
    let (_, encoded) = encode_blocking(model, vec![req.prompt]).await;
    match encoded {
        Ok(mut embs) => {
            let embedding = embs.pop().unwrap_or_default();
            Json(OllamaEmbeddingResponse { embedding }).into_response()
        }
        Err(err) => error_response(StatusCode::BAD_REQUEST, err),
    }
}

/// Ollama batch embedding request shape (`/api/embed`).
#[derive(Debug, Deserialize)]
pub struct OllamaEmbedRequest {
    pub model: Option<String>,
    pub input: EmbeddingInput,
}

#[derive(Debug, Serialize)]
pub struct OllamaEmbedResponse {
    pub model: String,
    pub embeddings: Vec<Vec<f32>>,
}

/// `POST /api/embed`.
pub async fn ollama_embed(
    State(state): State<ServerState>,
    tag: Option<axum::Extension<RequestTag>>,
    lease_slot: axum::Extension<crate::handler::RequestLeaseSlot>,
    Json(req): Json<OllamaEmbedRequest>,
) -> Response {
    let model = match resolve_embedding_backend(
        &state,
        &lease_slot.0,
        tag.map(|t| t.0),
        req.model.as_deref(),
    )
    .await
    {
        Ok(m) => m,
        Err(response) => return response,
    };

    let texts = req.input.into_vec();
    if let Err(e) = check_embedding_input_size(&texts) {
        return error_response(StatusCode::BAD_REQUEST, e);
    }

    let (texts, encoded) = encode_blocking(model.clone(), texts).await;
    let embeddings = match encoded {
        Ok(embs) => embs,
        Err(err) => return error_response(StatusCode::BAD_REQUEST, err),
    };
    if embeddings.len() != texts.len() {
        return error_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!(
                "embedding backend returned {} vectors for {} inputs",
                embeddings.len(),
                texts.len()
            ),
        );
    }
    Json(OllamaEmbedResponse {
        model: model.model_id().to_string(),
        embeddings,
    })
    .into_response()
}