111 lines
4.1 KiB
Rust
111 lines
4.1 KiB
Rust
//! Admin: which model writes which kind of analysis, chosen from the models
|
|||
|
|
//! OpenRouter offers. A change applies to analyses requested after it.
|
||
|
|
|
||
|
|
use analysis_prompt::Kind;
|
||
|
|
use axum::Json;
|
||
|
|
use axum::extract::{Path, State};
|
||
|
|
use axum_extra::extract::WithRejection;
|
||
|
|
use serde::Deserialize;
|
||
|
|
use serde_json::{Value, json};
|
||
|
|
use time::OffsetDateTime;
|
||
|
|
|
||
|
|
use super::{Body, St};
|
||
|
|
use crate::AppState;
|
||
|
|
use crate::auth::AdminUser;
|
||
|
|
use crate::error::{ApiError, ApiResult};
|
||
|
|
use crate::models::default_model;
|
||
|
|
use crate::views::rfc3339;
|
||
|
|
|
||
|
|
fn catalog_error(e: String) -> ApiError {
|
||
|
|
ApiError::Conflict {
|
||
|
|
code: "model_list_unavailable",
|
||
|
|
message: format!("Az OpenRouter modell-listája most nem érhető el: {e}"),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async fn entry(state: &AppState, kind: Kind) -> Result<Value, sqlx::Error> {
|
||
|
|
let chosen: Option<(String, OffsetDateTime, Option<String>)> = sqlx::query_as(
|
||
|
|
"SELECT m.model, m.updated_at, u.email FROM model_settings m LEFT JOIN users u ON u.id = m.updated_by \
|
||
|
|
WHERE m.kind = $1",
|
||
|
|
)
|
||
|
|
.bind(kind.as_str())
|
||
|
|
.fetch_optional(&state.db)
|
||
|
|
.await?;
|
||
|
|
// Typical size of this kind of analysis, from the latest ones: for the cost estimate.
|
||
|
|
let (input, output, samples): (Option<f64>, Option<f64>, i64) = sqlx::query_as(
|
||
|
|
"SELECT avg(input_tokens)::float8, avg(output_tokens)::float8, count(*) FROM ( \
|
||
|
|
SELECT input_tokens, output_tokens FROM analyses \
|
||
|
|
WHERE kind = $1 AND status = 'done' AND input_tokens IS NOT NULL AND output_tokens IS NOT NULL \
|
||
|
|
ORDER BY completed_at DESC LIMIT 50) recent",
|
||
|
|
)
|
||
|
|
.bind(kind.as_str())
|
||
|
|
.fetch_one(&state.db)
|
||
|
|
.await?;
|
||
|
|
let default = default_model(state, kind);
|
||
|
|
Ok(json!({
|
||
|
|
"kind": kind.as_str(),
|
||
|
|
"model": chosen.as_ref().map_or(default, |c| c.0.as_str()),
|
||
|
|
"default": default,
|
||
|
|
"custom": chosen.is_some(),
|
||
|
|
"updated_at": chosen.as_ref().map(|c| rfc3339(c.1)),
|
||
|
|
"updated_by": chosen.as_ref().and_then(|c| c.2.clone()),
|
||
|
|
"avg_input_tokens": input,
|
||
|
|
"avg_output_tokens": output,
|
||
|
|
"samples": samples,
|
||
|
|
}))
|
||
|
|
}
|
||
|
|
|
||
|
|
pub async fn list(State(state): St, _admin: AdminUser) -> ApiResult<Json<Value>> {
|
||
|
|
let mut functions = Vec::new();
|
||
|
|
for kind in Kind::ALL {
|
||
|
|
functions.push(entry(&state, kind).await?);
|
||
|
|
}
|
||
|
|
Ok(Json(json!({ "functions": functions })))
|
||
|
|
}
|
||
|
|
|
||
|
|
pub async fn catalog(State(state): St, _admin: AdminUser) -> ApiResult<Json<Value>> {
|
||
|
|
let models = state.models.models().await.map_err(catalog_error)?;
|
||
|
|
Ok(Json(json!({ "models": *models })))
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Deserialize)]
|
||
|
|
#[serde(deny_unknown_fields)]
|
||
|
|
pub struct SaveModel {
|
||
|
|
/// An OpenRouter model id; `null` restores the default.
|
||
|
|
model: Option<String>,
|
||
|
|
}
|
||
|
|
|
||
|
|
pub async fn save(
|
||
|
|
State(state): St,
|
||
|
|
AdminUser(admin): AdminUser,
|
||
|
|
WithRejection(Path(kind), _): WithRejection<Path<String>, ApiError>,
|
||
|
|
WithRejection(Json(req), _): Body<SaveModel>,
|
||
|
|
) -> ApiResult<Json<Value>> {
|
||
|
|
let kind = Kind::parse(&kind).ok_or(ApiError::NotFound("function"))?;
|
||
|
|
match req.model.as_deref().map(str::trim) {
|
||
|
|
Some(model) if model != default_model(&state, kind) => {
|
||
|
|
let models = state.models.models().await.map_err(catalog_error)?;
|
||
|
|
if !models.iter().any(|m| m.id == model) {
|
||
|
|
return Err(ApiError::BadRequest(format!(
|
||
|
|
"unknown model, or it cannot give structured answers: {model}"
|
||
|
|
)));
|
||
|
|
}
|
||
|
|
sqlx::query(
|
||
|
|
"INSERT INTO model_settings (kind, model, updated_by) VALUES ($1, $2, $3) \
|
||
|
|
ON CONFLICT (kind) DO UPDATE SET model = EXCLUDED.model, updated_by = EXCLUDED.updated_by, \
|
||
|
|
updated_at = now()",
|
||
|
|
)
|
||
|
|
.bind(kind.as_str())
|
||
|
|
.bind(model)
|
||
|
|
.bind(admin.id())
|
||
|
|
.execute(&state.db)
|
||
|
|
.await?;
|
||
|
|
}
|
||
|
|
_ => {
|
||
|
|
sqlx::query("DELETE FROM model_settings WHERE kind = $1").bind(kind.as_str()).execute(&state.db).await?;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
tracing::info!(admin = %admin.id(), kind = kind.as_str(), "analysis model changed");
|
||
|
|
Ok(Json(entry(&state, kind).await?))
|
||
|
|
}
|