This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
pub mod models;
|
||||
|
||||
use crate::auth::middleware::auth_middleware;
|
||||
use crate::routes::v1::models::list_models;
|
||||
use crate::state::app_state::AppState;
|
||||
use axum::{Router, middleware, routing::get};
|
||||
use axum::{Router, middleware, routing::get, routing::post};
|
||||
|
||||
pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/models", get(list_models))
|
||||
.route("/models", get(models::list_models))
|
||||
.route("/completions", post(models::completions))
|
||||
.layer(middleware::from_fn(auth_middleware))
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use axum::{Json, extract::State};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::errors::OllamaError;
|
||||
use crate::state::app_state::AppState;
|
||||
|
||||
pub async fn list_models(
|
||||
@@ -14,3 +15,23 @@ pub async fn list_models(
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn completions(
|
||||
State(state): State<AppState>,
|
||||
Json(body): Json<Value>,
|
||||
) -> Result<Json<Value>, (axum::http::StatusCode, String)> {
|
||||
match state.ollama.completions(body).await {
|
||||
Ok(response) => Ok(Json(response)),
|
||||
Err(OllamaError::MissingPrompt) => Err((
|
||||
axum::http::StatusCode::BAD_REQUEST,
|
||||
"prompt is required and cannot be empty".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()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user