52 lines
1.5 KiB
Rust
52 lines
1.5 KiB
Rust
//! `GET /v1/models` und `GET /v1/models/{id}`.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use axum::Json;
|
|
use axum::extract::{Extension, Path, State};
|
|
use serde_json::{Value, json};
|
|
|
|
use crate::access::{AccessSlot, clip};
|
|
use crate::auth::Authed;
|
|
use crate::error::ApiError;
|
|
use crate::state::AppState;
|
|
|
|
fn entry(name: &str) -> Value {
|
|
json!({ "id": name, "object": "model", "created": 0, "owned_by": "nexus" })
|
|
}
|
|
|
|
/// Auch Modelle, die gerade nicht geladen sind, erscheinen im Katalog.
|
|
pub async fn list(
|
|
State(state): State<Arc<AppState>>,
|
|
Authed(key): Authed,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
let models = state.store().list_models(true).map_err(|e| {
|
|
tracing::error!(fehler = %e, "Modellliste nicht lesbar");
|
|
ApiError::internal()
|
|
})?;
|
|
let data: Vec<Value> = models
|
|
.iter()
|
|
.filter(|m| key.rights.allows(&m.name))
|
|
.map(|m| entry(&m.name))
|
|
.collect();
|
|
Ok(Json(json!({ "object": "list", "data": data })))
|
|
}
|
|
|
|
pub async fn retrieve(
|
|
State(state): State<Arc<AppState>>,
|
|
Authed(key): Authed,
|
|
Extension(slot): Extension<AccessSlot>,
|
|
Path(id): Path<String>,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
slot.lock().unwrap_or_else(|e| e.into_inner()).model = Some(clip(&id));
|
|
let models = state.store().list_models(true).map_err(|e| {
|
|
tracing::error!(fehler = %e, "Modellliste nicht lesbar");
|
|
ApiError::internal()
|
|
})?;
|
|
if key.rights.allows(&id) && models.iter().any(|m| m.name == id) {
|
|
Ok(Json(entry(&id)))
|
|
} else {
|
|
Err(ApiError::model_not_found(&id))
|
|
}
|
|
}
|