Models: one for analyses and forecasts, one for answers; Opus 5.5 default and fallback
- 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:
@@ -1,7 +1,7 @@
|
||||
//! Answers to questions about a finished analysis (`questions` rows). The
|
||||
//! model of the analysis answers, seeing the chart data the analysis was
|
||||
//! written from, the analysis itself and the earlier questions about it.
|
||||
//! Retries and refunds work as for analyses.
|
||||
//! question model answers, seeing the chart data the analysis was written
|
||||
//! from, the analysis itself and the earlier questions about it. Retries,
|
||||
//! fallback and refunds work as for analyses.
|
||||
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
@@ -10,7 +10,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::openrouter::{Completion, LlmError};
|
||||
use super::prompt::{self, Kind};
|
||||
use super::{Analyzer, backoff};
|
||||
use super::{Analyzer, Next, active_model, next_step};
|
||||
|
||||
/// The earlier questions and answers given to the model, at most this many.
|
||||
const HISTORY: i64 = 6;
|
||||
@@ -25,6 +25,8 @@ pub struct QuestionJob {
|
||||
pub prompt_version: String,
|
||||
pub attempts: i32,
|
||||
pub created_at: time::OffsetDateTime,
|
||||
pub fallback_model: Option<String>,
|
||||
pub on_fallback: bool,
|
||||
}
|
||||
|
||||
impl Analyzer {
|
||||
@@ -34,7 +36,8 @@ impl Analyzer {
|
||||
"UPDATE questions SET status = 'running', started_at = now(), attempts = attempts + 1 \
|
||||
WHERE id = (SELECT id FROM questions WHERE status = 'pending' AND next_attempt_at <= now() \
|
||||
ORDER BY next_attempt_at LIMIT 1 FOR UPDATE SKIP LOCKED) \
|
||||
RETURNING id, analysis_id, question, language, model, prompt_version, attempts, created_at",
|
||||
RETURNING id, analysis_id, question, language, model, prompt_version, attempts, created_at, \
|
||||
fallback_model, on_fallback",
|
||||
)
|
||||
.fetch_optional(&self.db)
|
||||
.await?;
|
||||
@@ -89,10 +92,13 @@ impl Analyzer {
|
||||
pub async fn handle_question(&self, job: QuestionJob) {
|
||||
let started = Instant::now();
|
||||
let result = match self.question_prompt(&job).await {
|
||||
Ok(p) => self.llm.complete(&job.model, &p).await.map(|mut c| {
|
||||
prompt::polish(&job.language, &mut c.content);
|
||||
c
|
||||
}),
|
||||
Ok(p) => {
|
||||
let model = active_model(&job.model, job.fallback_model.as_deref(), job.on_fallback);
|
||||
self.llm.complete(model, &p).await.map(|mut c| {
|
||||
prompt::polish(&job.language, &mut c.content);
|
||||
c
|
||||
})
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
};
|
||||
if let Err(e) = self.finish_question(&job, result, started.elapsed()).await {
|
||||
@@ -128,22 +134,40 @@ impl Analyzer {
|
||||
secs = took.as_secs(), "answer done"
|
||||
);
|
||||
}
|
||||
Err(LlmError::Transient(message)) if job.attempts < self.max_attempts => {
|
||||
let wait = backoff(job.attempts);
|
||||
sqlx::query(
|
||||
"UPDATE questions SET status = 'pending', error = $2, \
|
||||
next_attempt_at = now() + make_interval(secs => $3) WHERE id = $1 AND status = 'running'",
|
||||
)
|
||||
.bind(job.id)
|
||||
.bind(&message)
|
||||
.bind(wait.as_secs_f64())
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
tracing::warn!(id = %job.id, attempts = job.attempts, error = %message, "answer will be retried");
|
||||
}
|
||||
Err(e) => {
|
||||
self.fail_question(job.id, e.message()).await?;
|
||||
tracing::error!(id = %job.id, attempts = job.attempts, error = %e.message(), "answer failed");
|
||||
let can_fall_back = !job.on_fallback && job.fallback_model.is_some();
|
||||
match next_step(&e, job.attempts, self.max_attempts, can_fall_back) {
|
||||
Next::Retry(wait) => {
|
||||
sqlx::query(
|
||||
"UPDATE questions SET status = 'pending', error = $2, \
|
||||
next_attempt_at = now() + make_interval(secs => $3) WHERE id = $1 AND status = 'running'",
|
||||
)
|
||||
.bind(job.id)
|
||||
.bind(e.message())
|
||||
.bind(wait.as_secs_f64())
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
tracing::warn!(id = %job.id, attempts = job.attempts, error = %e.message(), "answer will be retried");
|
||||
}
|
||||
Next::Fallback => {
|
||||
sqlx::query(
|
||||
"UPDATE questions SET status = 'pending', error = $2, on_fallback = true, attempts = 0, \
|
||||
next_attempt_at = now() WHERE id = $1 AND status = 'running'",
|
||||
)
|
||||
.bind(job.id)
|
||||
.bind(e.message())
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
tracing::warn!(
|
||||
id = %job.id, model = %job.model, error = %e.message(),
|
||||
"the chosen model failed, the default model answers"
|
||||
);
|
||||
}
|
||||
Next::Fail => {
|
||||
self.fail_question(job.id, e.message()).await?;
|
||||
tracing::error!(id = %job.id, attempts = job.attempts, error = %e.message(), "answer failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user