180 lines
7.3 KiB
Rust
180 lines
7.3 KiB
Rust
//! 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.
|
||
|
|
|
||
|
|
use std::time::{Duration, Instant};
|
||
|
|
|
||
|
|
use serde_json::Value;
|
||
|
|
use uuid::Uuid;
|
||
|
|
|
||
|
|
use super::openrouter::{Completion, LlmError};
|
||
|
|
use super::prompt::{self, Kind};
|
||
|
|
use super::{Analyzer, backoff};
|
||
|
|
|
||
|
|
/// The earlier questions and answers given to the model, at most this many.
|
||
|
|
const HISTORY: i64 = 6;
|
||
|
|
|
||
|
|
#[derive(Debug, sqlx::FromRow)]
|
||
|
|
pub struct QuestionJob {
|
||
|
|
pub id: Uuid,
|
||
|
|
pub analysis_id: Uuid,
|
||
|
|
pub question: String,
|
||
|
|
pub language: String,
|
||
|
|
pub model: String,
|
||
|
|
pub prompt_version: String,
|
||
|
|
pub attempts: i32,
|
||
|
|
pub created_at: time::OffsetDateTime,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Analyzer {
|
||
|
|
/// Takes the next due question, if any.
|
||
|
|
pub async fn claim_question(&self) -> Result<Option<QuestionJob>, sqlx::Error> {
|
||
|
|
let job: Option<QuestionJob> = sqlx::query_as(
|
||
|
|
"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",
|
||
|
|
)
|
||
|
|
.fetch_optional(&self.db)
|
||
|
|
.await?;
|
||
|
|
if let Some(job) = &job {
|
||
|
|
self.running.lock().unwrap().insert(job.id);
|
||
|
|
}
|
||
|
|
Ok(job)
|
||
|
|
}
|
||
|
|
|
||
|
|
async fn question_prompt(&self, job: &QuestionJob) -> Result<prompt::Prompt, LlmError> {
|
||
|
|
let db_err = |e: sqlx::Error| LlmError::Transient(format!("database: {e}"));
|
||
|
|
let (kind, chart_id, forecast_id, content): (String, Option<Uuid>, Option<Uuid>, Option<Value>) =
|
||
|
|
sqlx::query_as("SELECT kind, chart_id, forecast_id, content FROM analyses WHERE id = $1")
|
||
|
|
.bind(job.analysis_id)
|
||
|
|
.fetch_optional(&self.db)
|
||
|
|
.await
|
||
|
|
.map_err(db_err)?
|
||
|
|
.ok_or_else(|| LlmError::Permanent("the analysis was deleted".into()))?;
|
||
|
|
let kind = Kind::parse(&kind).ok_or_else(|| LlmError::Permanent(format!("unknown kind '{kind}'")))?;
|
||
|
|
let content = content.ok_or_else(|| LlmError::Permanent("the analysis has no text".into()))?;
|
||
|
|
let chart_text = match kind {
|
||
|
|
Kind::Forecast => self.forecast_text(forecast_id).await?,
|
||
|
|
_ => self.natal_text(chart_id).await?,
|
||
|
|
};
|
||
|
|
let analysis = prompt::to_markdown(kind, &job.language, &content);
|
||
|
|
let mut history: Vec<(String, String)> = sqlx::query_as(
|
||
|
|
"SELECT question, answer FROM questions \
|
||
|
|
WHERE analysis_id = $1 AND status = 'done' AND answer IS NOT NULL AND created_at < $2 \
|
||
|
|
ORDER BY created_at DESC LIMIT $3",
|
||
|
|
)
|
||
|
|
.bind(job.analysis_id)
|
||
|
|
.bind(job.created_at)
|
||
|
|
.bind(HISTORY)
|
||
|
|
.fetch_all(&self.db)
|
||
|
|
.await
|
||
|
|
.map_err(db_err)?;
|
||
|
|
history.reverse();
|
||
|
|
let (_, revision) = prompt::split_version(&job.prompt_version);
|
||
|
|
let system: Option<String> = match revision {
|
||
|
|
Some(id) => sqlx::query_scalar("SELECT system FROM prompt_revisions WHERE id = $1")
|
||
|
|
.bind(id)
|
||
|
|
.fetch_optional(&self.db)
|
||
|
|
.await
|
||
|
|
.map_err(db_err)?
|
||
|
|
.flatten(),
|
||
|
|
None => None,
|
||
|
|
};
|
||
|
|
Ok(prompt::build_question(&job.language, &chart_text, &analysis, &history, &job.question, system.as_deref()))
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Answers one claimed question and records the outcome.
|
||
|
|
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
|
||
|
|
}),
|
||
|
|
Err(e) => Err(e),
|
||
|
|
};
|
||
|
|
if let Err(e) = self.finish_question(&job, result, started.elapsed()).await {
|
||
|
|
tracing::error!(id = %job.id, error = %e, "saving the answer failed");
|
||
|
|
}
|
||
|
|
self.running.lock().unwrap().remove(&job.id);
|
||
|
|
}
|
||
|
|
|
||
|
|
async fn finish_question(
|
||
|
|
&self,
|
||
|
|
job: &QuestionJob,
|
||
|
|
result: Result<Completion, LlmError>,
|
||
|
|
took: Duration,
|
||
|
|
) -> Result<(), sqlx::Error> {
|
||
|
|
match result {
|
||
|
|
Ok(c) => {
|
||
|
|
let answer = c.content["answer"].as_str().unwrap_or_default().trim().to_string();
|
||
|
|
sqlx::query(
|
||
|
|
"UPDATE questions SET status = 'done', answer = $2, model_used = $3, input_tokens = $4, \
|
||
|
|
output_tokens = $5, cost_usd = $6, error = NULL, completed_at = now() \
|
||
|
|
WHERE id = $1 AND status = 'running'",
|
||
|
|
)
|
||
|
|
.bind(job.id)
|
||
|
|
.bind(&answer)
|
||
|
|
.bind(&c.model)
|
||
|
|
.bind(c.input_tokens)
|
||
|
|
.bind(c.output_tokens)
|
||
|
|
.bind(c.cost_usd)
|
||
|
|
.execute(&self.db)
|
||
|
|
.await?;
|
||
|
|
tracing::info!(
|
||
|
|
id = %job.id, model = %c.model, cost_usd = c.cost_usd, words = answer.split_whitespace().count(),
|
||
|
|
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");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Marks the question failed and gives its unit back (once).
|
||
|
|
async fn fail_question(&self, id: Uuid, message: &str) -> Result<(), sqlx::Error> {
|
||
|
|
let mut tx = self.db.begin().await?;
|
||
|
|
let row: Option<(Option<Uuid>, i32)> = sqlx::query_as(
|
||
|
|
"UPDATE questions SET status = 'failed', error = $2, completed_at = now() \
|
||
|
|
WHERE id = $1 AND status = 'running' RETURNING asked_by, credits_charged",
|
||
|
|
)
|
||
|
|
.bind(id)
|
||
|
|
.bind(message)
|
||
|
|
.fetch_optional(&mut *tx)
|
||
|
|
.await?;
|
||
|
|
if let Some((Some(user), units)) = row
|
||
|
|
&& units > 0
|
||
|
|
{
|
||
|
|
sqlx::query(
|
||
|
|
"INSERT INTO credit_ledger (id, user_id, unit, delta, reason, question_id, note) \
|
||
|
|
VALUES ($1, $2, 'question', $3, 'refund', $4, 'answer failed') ON CONFLICT DO NOTHING",
|
||
|
|
)
|
||
|
|
.bind(Uuid::now_v7())
|
||
|
|
.bind(user)
|
||
|
|
.bind(units)
|
||
|
|
.bind(id)
|
||
|
|
.execute(&mut *tx)
|
||
|
|
.await?;
|
||
|
|
}
|
||
|
|
tx.commit().await
|
||
|
|
}
|
||
|
|
}
|