feat: add chat completions route + test
CI / Rust CI (push) Successful in 4m23s
Publish & Deploy / Build and Push to Registry (push) Successful in 1m26s
Publish & Deploy / Deploy via SSH (push) Successful in 18s

This commit is contained in:
2026-04-10 11:47:56 +02:00
parent 70fe9bc4da
commit 6dc7231309
10 changed files with 521 additions and 429 deletions
+1
View File
@@ -8,5 +8,6 @@ pub fn router() -> Router<AppState> {
Router::new()
.route("/models", get(models::list_models))
.route("/completions", post(models::completions))
.route("/chat/completions", post(models::chat_completions))
.layer(middleware::from_fn(auth_middleware))
}
+19
View File
@@ -30,8 +30,27 @@ pub async fn completions(
axum::http::StatusCode::UNPROCESSABLE_ENTITY,
format!("model '{m}' is not available — run `ollama pull {m}` first"),
)),
Err(e) => Err((axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
pub async fn chat_completions(
State(state): State<AppState>,
Json(body): Json<Value>,
) -> Result<Json<Value>, (axum::http::StatusCode, String)> {
match state.ollama.chat_completions(body).await {
Ok(response) => Ok(Json(response)),
Err(OllamaError::MissingMessages) => Err((
axum::http::StatusCode::BAD_REQUEST,
"messages must be a non-empty array with at least one user message".to_string(),
)),
Err(OllamaError::ModelNotFound(m)) => Err((
axum::http::StatusCode::UNPROCESSABLE_ENTITY,
format!("model '{m}' is not available — run `ollama pull {m}` first"),
)),
Err(OllamaError::Http(e)) => {
Err((axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
}
Err(e) => Err((axum::http::StatusCode::BAD_REQUEST, e.to_string())),
}
}