use std::path::PathBuf;
use base64::Engine as _;
use serde::{Deserialize, Serialize};
pub const SHM_LOGITS_CAPACITY: usize = 4 * 1024 * 1024;
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum Request {
Init {
library: PathBuf,
model: PathBuf,
n_ctx: u32,
shm: PathBuf,
},
Forward { pos: u32, n_tokens: u32 },
MediaPrefill { prompt: String, images: Vec<String> },
Reset,
Shutdown,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Response {
pub ok: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub vocab: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub media: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub n_past: Option<u32>,
}
impl Response {
pub fn ok() -> Self {
Self {
ok: true,
error: None,
vocab: None,
media: None,
n_past: None,
}
}
pub fn err(message: impl Into<String>) -> Self {
Self {
ok: false,
error: Some(message.into()),
vocab: None,
media: None,
n_past: None,
}
}
}
pub fn b64_encode(data: &[u8]) -> String {
base64::engine::general_purpose::STANDARD.encode(data)
}
pub fn b64_decode(s: &str) -> std::result::Result<Vec<u8>, String> {
base64::engine::general_purpose::STANDARD
.decode(s)
.map_err(|e| e.to_string())
}