use std::fmt;
use super::engine::{serial_prompt_limit_message, SERIAL_PROMPT_LIMIT_SENTINEL};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EngineRequestError {
PromptTooLong { prompt_tokens: usize, limit: usize },
ContextOverflow {
context_limit: usize,
needed: usize,
detail: String,
},
EmbeddingsUnsupported { family: &'static str },
UnsupportedCapability { message: String },
InvalidRequest { message: String },
}
impl fmt::Display for EngineRequestError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::PromptTooLong {
prompt_tokens,
limit,
} => write!(
f,
"{SERIAL_PROMPT_LIMIT_SENTINEL}: {}",
serial_prompt_limit_message(*prompt_tokens, *limit)
),
Self::ContextOverflow { detail, .. } => write!(f, "{detail}"),
Self::EmbeddingsUnsupported { family } => write!(
f,
"embeddings_unsupported: the loaded {family} runtime has no \
embedding path; /v1/embeddings cannot be served with this model"
),
Self::UnsupportedCapability { message } => write!(f, "{message}"),
Self::InvalidRequest { message } => write!(f, "invalid_request: {message}"),
}
}
}
impl std::error::Error for EngineRequestError {}
impl EngineRequestError {
pub fn into_anyhow(self) -> anyhow::Error {
anyhow::Error::new(self)
}
pub fn context_overflow(context_limit: usize, needed: usize, detail: String) -> Self {
Self::ContextOverflow {
context_limit,
needed,
detail,
}
}
pub fn invalid_request(message: impl Into<String>) -> Self {
Self::InvalidRequest {
message: message.into(),
}
}
}