Models: one for analyses and forecasts, one for answers; Opus 5.5 default and fallback
CI / test (push) Successful in 1m1s
CI / build (push) Successful in 4m56s
CI / deploy (push) Skipped

- Admin Modellek: summary, analyses + forecast (shared), question-answer (own model)
- Every default is Opus 5.5; when the chosen model fails twice (permanent: once),
  the default model takes over with fresh attempts before the unit is refunded
- The page shows how often the fallback was needed in the last 30 days

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-18 21:11:08 +00:00
co-authored by Claude Opus 5.5
parent 5af08ab689
commit 769b4791ae
18 changed files with 370 additions and 138 deletions
+21 -18
View File
@@ -1,7 +1,7 @@
//! Questions about a finished detailed analysis: a natal chart analysis or a
//! yearly forecast. Each question uses a `question` unit (admins ask for
//! free). The worker answers with the model that wrote the analysis, seeing
//! the chart data, the analysis and the earlier questions about it.
//! free). The worker answers with the question model of the admin page,
//! seeing the chart data, the analysis and the earlier questions about it.
use analysis_prompt::{Kind, QUESTION_MAX_CHARS};
use axum::Json;
@@ -64,21 +64,19 @@ async fn find_question(db: &PgPool, id: Uuid) -> Result<Option<QuestionRow>, sql
sqlx::query_as(&format!("SELECT {QUESTION_COLUMNS} FROM questions WHERE id = $1")).bind(id).fetch_optional(db).await
}
/// The model of the analysis (questions are answered by the same model) and
/// the prompt version of the question prompt in force.
/// The question model (and its fallback) and the version of the question
/// prompt in force.
async fn model_and_version(
state: &crate::AppState,
analysis_id: Uuid,
language: &str,
) -> Result<(String, String), sqlx::Error> {
let model: String =
sqlx::query_scalar("SELECT model FROM analyses WHERE id = $1").bind(analysis_id).fetch_one(&state.db).await?;
) -> Result<(String, Option<String>, String), sqlx::Error> {
let (model, fallback) = crate::models::models_for(state, Kind::Question).await?;
let base = &state.config.prompt_version;
let version = match super::prompts::current_revision(&state.db, Kind::Question.as_str(), language).await? {
Some(id) => format!("{base}-r{id}"),
None => base.clone(),
};
Ok((model, version))
Ok((model, fallback, version))
}
pub async fn list(State(state): St, auth: AuthUser, WithRejection(Path(id), _): IdPath) -> ApiResult<Json<Value>> {
@@ -141,12 +139,13 @@ pub async fn ask(
}
let admin = auth.user.has_role(Role::Admin);
let (model, prompt_version) = model_and_version(&state, id, &analysis.language).await?;
let (model, fallback, prompt_version) = model_and_version(&state, &analysis.language).await?;
let question_id = Uuid::now_v7();
let mut tx = state.db.begin().await?;
let row: QuestionRow = sqlx::query_as(&format!(
"INSERT INTO questions (id, analysis_id, asked_by, question, language, model, prompt_version, credits_charged) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING {QUESTION_COLUMNS}"
"INSERT INTO questions (id, analysis_id, asked_by, question, language, model, prompt_version, credits_charged, \
fallback_model) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING {QUESTION_COLUMNS}"
))
.bind(question_id)
.bind(id)
@@ -156,6 +155,7 @@ pub async fn ask(
.bind(&model)
.bind(&prompt_version)
.bind(i32::from(!admin))
.bind(&fallback)
.fetch_one(&mut *tx)
.await?;
if !admin {
@@ -175,26 +175,29 @@ pub async fn get_one(State(state): St, auth: AuthUser, WithRejection(Path(id), _
}
/// Admin: answers a question again in place, with the current question prompt
/// and the analysis's model. Nobody is charged.
/// and model. Nobody is charged.
pub async fn regenerate(
State(state): St,
_admin: AdminUser,
WithRejection(Path(id), _): IdPath,
) -> ApiResult<Json<Value>> {
let row = find_question(&state.db, id).await?.ok_or(ApiError::NotFound("question"))?;
let language: String =
sqlx::query_scalar("SELECT language FROM questions WHERE id = $1").bind(id).fetch_one(&state.db).await?;
let (model, prompt_version) = model_and_version(&state, row.analysis_id, &language).await?;
let language: String = sqlx::query_scalar("SELECT language FROM questions WHERE id = $1")
.bind(id)
.fetch_optional(&state.db)
.await?
.ok_or(ApiError::NotFound("question"))?;
let (model, fallback, prompt_version) = model_and_version(&state, &language).await?;
let mut tx = state.db.begin().await?;
let row: QuestionRow = sqlx::query_as(&format!(
"UPDATE questions SET status = 'pending', answer = NULL, error = NULL, model_used = NULL, input_tokens = NULL, \
output_tokens = NULL, cost_usd = NULL, attempts = 0, next_attempt_at = now(), started_at = NULL, \
completed_at = NULL, model = $2, prompt_version = $3 \
completed_at = NULL, model = $2, prompt_version = $3, fallback_model = $4, on_fallback = false \
WHERE id = $1 AND status IN ('done', 'failed') RETURNING {QUESTION_COLUMNS}"
))
.bind(id)
.bind(&model)
.bind(&prompt_version)
.bind(&fallback)
.fetch_optional(&mut *tx)
.await?
.ok_or_else(|| ApiError::conflict("question_in_progress", "Erre a kérdésre éppen készül a válasz."))?;