Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/server/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pub struct EmbedRequest {
pub text: String,
}

const MAX_BATCH_SIZE: usize = 100;
const MAX_TEXT_LENGTH: usize = 8192; // Common token limit for many models

#[derive(Debug, Deserialize)]
pub struct EmbedBatchRequest {
pub texts: Vec<String>,
Expand Down Expand Up @@ -303,6 +306,16 @@ async fn embed(
State(state): State<SharedState>,
Json(req): Json<EmbedRequest>,
) -> impl IntoResponse {
if req.text.len() > MAX_TEXT_LENGTH {
return (
StatusCode::BAD_REQUEST,
Json(serde_json::json!({
"error": format!("Text exceeds length limit of {}", MAX_TEXT_LENGTH)
})),
)
.into_response();
}

let engine = match state.embedding_engine.lock() {
Ok(e) => e,
Err(e) => {
Expand Down Expand Up @@ -339,6 +352,28 @@ async fn embed_batch(
State(state): State<SharedState>,
Json(req): Json<EmbedBatchRequest>,
) -> impl IntoResponse {
if req.texts.len() > MAX_BATCH_SIZE {
return (
StatusCode::BAD_REQUEST,
Json(serde_json::json!({
"error": format!("Batch size exceeds limit of {}", MAX_BATCH_SIZE)
})),
)
.into_response();
}

for (i, text) in req.texts.iter().enumerate() {
if text.len() > MAX_TEXT_LENGTH {
return (
StatusCode::BAD_REQUEST,
Json(serde_json::json!({
"error": format!("Text at index {} exceeds length limit of {}", i, MAX_TEXT_LENGTH)
})),
)
.into_response();
}
}

let engine = match state.embedding_engine.lock() {
Ok(e) => e,
Err(e) => {
Expand Down
Loading