Paid detailed analyses with per-kind units; example analysis page
- Credits become analysis units (migration 0006): a natal analysis uses one 'natal' unit, a synastry analysis one 'synastry' unit. No monthly free credits, no model tiers: ANALYSIS_MODEL (default Opus 5.5) writes every paid analysis. Admins grant units per kind; the worker refunds the right unit on failure. - Chart page: the detailed analysis card (offer with units left and a confirmation, progress while writing, the finished six sections). - Public /pelda page: a full Opus analysis of a made-up chart, with the free summary and a login call to action. - The worker sends the JSON schema with properties in 'required' order. serde_json sorted them, so the title came after ~9000 tokens of text and Opus sometimes left it empty (3 failed attempts in a live test); with the order fixed the same analysis succeeded at the first try. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -18,7 +18,7 @@ Háttérmunkák a közös PostgreSQL-adatbázis fölött: e-mail-küldés, szöv
|
||||
- el nem fogadott meghívók 30 nappal a lejáratuk után.
|
||||
|
||||
**Szöveges elemzések** (`analyses`, `pending` → `running` → `done`/`failed`)
|
||||
- A backend létrehozza a `pending` sort, és levonja a kreditet. Egy trigger (`NOTIFY analyses`) felébreszti a workert; egyszerre `ANALYSIS_CONCURRENCY` elemzés készül.
|
||||
- A backend létrehozza a `pending` sort, és levon egy elemzési egységet. Egy trigger (`NOTIFY analyses`) felébreszti a workert; egyszerre `ANALYSIS_CONCURRENCY` elemzés készül.
|
||||
- A prompt a tárolt képletből készül (`src/analysis/compact.rs`). Ez egy tömör szöveg, amely csak a pozíciókat tartalmazza: jegyek, fokok, házak, fényszögek, elemek. **Név, születési dátum, idő, hely és koordináta nem megy ki.** Ismeretlen születési időnél nincsenek házak, Aszcendens és MC.
|
||||
- A modell OpenRouteren keresztül válaszol, azzal a modellel, amely a sor `model` mezőjében áll. A válasznak kötött JSON-sémát kell követnie (`src/analysis/prompt.rs`, `PROMPT_VERSION = v2`):
|
||||
- `title`, `summary`;
|
||||
@@ -29,7 +29,7 @@ Háttérmunkák a közös PostgreSQL-adatbázis fölött: e-mail-küldés, szöv
|
||||
A worker a választ maga is ellenőrzi a sémán. Magyar szövegben az egyenes idézőjelpárokat („…”) alakra cseréli.
|
||||
- Eltárolja a ténylegesen válaszoló modellt (`model_used`; az `openrouter/auto` mögött ez más lehet), a tokenszámot és a költséget (`cost_usd`).
|
||||
- **Átmeneti hiba** (hálózat, 429, 5xx, elfogyott OpenRouter-egyenleg, hibás vagy csonka válasz): újrapróbálja 30 mp, 2 perc, majd 8 perc múlva.
|
||||
- **Végleges hiba** (például ismeretlen modell), vagy ha elfogyott a `MAX_ANALYSIS_ATTEMPTS` próbálkozás: az elemzés `failed` lesz, és a kredit visszajár (`refund`, elemzésenként legfeljebb egyszer).
|
||||
- **Végleges hiba** (például ismeretlen modell), vagy ha elfogyott a `MAX_ANALYSIS_ATTEMPTS` próbálkozás: az elemzés `failed` lesz, és az egység visszajár (`refund`, elemzésenként legfeljebb egyszer).
|
||||
- Ha a worker elemzés közben leáll, a félkész elemzést visszateszi a sorba. Ha összeomlik, az elemzés 15 perc múlva magától visszakerül.
|
||||
- Szinasztriánál a modell a két félnek együtt szól; ha meg kell különböztetnie őket, „első fél” és „második fél” néven említi.
|
||||
|
||||
@@ -37,10 +37,7 @@ Háttérmunkák a közös PostgreSQL-adatbázis fölött: e-mail-küldés, szöv
|
||||
|
||||
## Modellek összevetése
|
||||
|
||||
A 2026-11-15-i összevetésen két képletet hét modell írt meg; az egyiket a tulajdonos, a másikat egy hivatásos asztrológus pontozta. Ebből lettek a szintek (`LLM_MODELS`, backend):
|
||||
- **Ingyenes:** Claude Haiku 5.5, kb. 0,006 dollár elemzésenként.
|
||||
- **Fizetős:** GPT-5.6 Terra, kb. 0,05 dollár.
|
||||
- **Privát/VIP:** Claude Opus 5.5, kb. 0,16 dollár. Az asztrológus szerint egyértelműen ez a legjobb.
|
||||
A 2026-11-15-i összevetésen két képletet hét modell írt meg; az egyiket a tulajdonos, a másikat egy hivatásos asztrológus pontozta. Részletes elemzésenként: Claude Haiku 5.5 kb. 0,006 dollár, GPT-5.6 Terra kb. 0,05, Claude Opus 5.5 kb. 0,16 (szinasztria kb. 0,21). Az asztrológus szerint egyértelműen az Opus a legjobb, ezért a fizetős elemzés vele készül (`ANALYSIS_MODEL`, backend).
|
||||
|
||||
A MiniMax M3 olcsó, de az asztrológus szerint többször felcserélt dolgokat.
|
||||
|
||||
@@ -70,7 +67,7 @@ Ugyanazt a radix-elemzést több modellel is megírja, véletlenszerű betűkkel
|
||||
| `OPENROUTER_BASE_URL` | `https://openrouter.ai/api/v1` | |
|
||||
| `PUBLIC_BASE_URL` | – | `HTTP-Referer` fejléc az OpenRouter felé |
|
||||
| `ANALYSIS_CONCURRENCY` | `4` | egyszerre írt elemzések |
|
||||
| `MAX_ANALYSIS_ATTEMPTS` | `3` | ennyi próbálkozás után sikertelen az elemzés (a kredit visszajár) |
|
||||
| `MAX_ANALYSIS_ATTEMPTS` | `3` | ennyi próbálkozás után sikertelen az elemzés (az egység visszajár) |
|
||||
| `BIND_ADDR`, `LOG_FORMAT`, `RUST_LOG` | `0.0.0.0:8082`, `text`, `info` | |
|
||||
|
||||
## Fejlesztés: Mailpit
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//! (`running`), builds the prompt from the stored chart data, asks the model
|
||||
//! through OpenRouter and stores the structured answer (`done`). Temporary
|
||||
//! failures are retried with backoff; after the last attempt, or on a
|
||||
//! permanent error, the analysis is `failed` and the credits are refunded.
|
||||
//! permanent error, the analysis is `failed` and the unit is refunded.
|
||||
|
||||
pub mod compact;
|
||||
pub mod openrouter;
|
||||
@@ -165,27 +165,29 @@ impl Analyzer {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Marks the analysis failed and gives the credits back (once).
|
||||
/// Marks the analysis failed and gives its unit back (once).
|
||||
async fn fail(&self, id: Uuid, message: &str) -> Result<(), sqlx::Error> {
|
||||
let mut tx = self.db.begin().await?;
|
||||
let row: Option<(Option<Uuid>, i32)> = sqlx::query_as(
|
||||
let row: Option<(Option<Uuid>, i32, String)> = sqlx::query_as(
|
||||
"UPDATE analyses SET status = 'failed', error = $2, completed_at = now() \
|
||||
WHERE id = $1 AND status = 'running' RETURNING requested_by, credits_charged",
|
||||
WHERE id = $1 AND status = 'running' RETURNING requested_by, credits_charged, kind",
|
||||
)
|
||||
.bind(id)
|
||||
.bind(message)
|
||||
.fetch_optional(&mut *tx)
|
||||
.await?;
|
||||
if let Some((Some(user), credits)) = row
|
||||
&& credits > 0
|
||||
if let Some((Some(user), units, kind)) = row
|
||||
&& units > 0
|
||||
{
|
||||
let unit = if kind == "synastry" { "synastry" } else { "natal" };
|
||||
sqlx::query(
|
||||
"INSERT INTO credit_ledger (id, user_id, delta, reason, analysis_id, note) \
|
||||
VALUES ($1, $2, $3, 'refund', $4, 'analysis failed') ON CONFLICT DO NOTHING",
|
||||
"INSERT INTO credit_ledger (id, user_id, unit, delta, reason, analysis_id, note) \
|
||||
VALUES ($1, $2, $3, $4, 'refund', $5, 'analysis failed') ON CONFLICT DO NOTHING",
|
||||
)
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(user)
|
||||
.bind(credits)
|
||||
.bind(unit)
|
||||
.bind(units)
|
||||
.bind(id)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::ser::{Serialize, SerializeMap, Serializer};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::prompt::{Prompt, validate};
|
||||
@@ -44,6 +45,77 @@ impl LlmError {
|
||||
}
|
||||
}
|
||||
|
||||
/// A JSON schema serialized with every `properties` object in the order of
|
||||
/// its sibling `required` list. `serde_json` sorts object keys, and models
|
||||
/// write strict structured output in schema order: sorted, the title came
|
||||
/// after 9000 tokens of text and was sometimes left empty.
|
||||
pub struct Ordered<'a>(pub &'a Value);
|
||||
|
||||
impl Serialize for Ordered<'_> {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
match self.0 {
|
||||
Value::Object(map) => {
|
||||
let order: Vec<&str> =
|
||||
map.get("required").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str).collect();
|
||||
let mut out = serializer.serialize_map(Some(map.len()))?;
|
||||
for (key, value) in map {
|
||||
match (key.as_str(), value) {
|
||||
("properties", Value::Object(props)) => out.serialize_entry(key, &Properties(props, &order))?,
|
||||
_ => out.serialize_entry(key, &Ordered(value))?,
|
||||
}
|
||||
}
|
||||
out.end()
|
||||
}
|
||||
Value::Array(items) => serializer.collect_seq(items.iter().map(Ordered)),
|
||||
other => other.serialize(serializer),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The request body; a struct (not `json!`) so the schema keeps its order.
|
||||
#[derive(serde::Serialize)]
|
||||
struct Request<'a> {
|
||||
model: &'a str,
|
||||
messages: Value,
|
||||
response_format: ResponseFormat<'a>,
|
||||
provider: Value,
|
||||
max_tokens: u32,
|
||||
usage: Value,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct ResponseFormat<'a> {
|
||||
#[serde(rename = "type")]
|
||||
kind: &'static str,
|
||||
json_schema: JsonSchema<'a>,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct JsonSchema<'a> {
|
||||
name: &'static str,
|
||||
strict: bool,
|
||||
schema: Ordered<'a>,
|
||||
}
|
||||
|
||||
/// `properties` in `required` order (any others after them).
|
||||
struct Properties<'a>(&'a serde_json::Map<String, Value>, &'a [&'a str]);
|
||||
|
||||
impl Serialize for Properties<'_> {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
let (props, order) = (self.0, self.1);
|
||||
let mut out = serializer.serialize_map(Some(props.len()))?;
|
||||
for key in order {
|
||||
if let Some(value) = props.get(*key) {
|
||||
out.serialize_entry(key, &Ordered(value))?;
|
||||
}
|
||||
}
|
||||
for (key, value) in props.iter().filter(|(k, _)| !order.contains(&k.as_str())) {
|
||||
out.serialize_entry(key, &Ordered(value))?;
|
||||
}
|
||||
out.end()
|
||||
}
|
||||
}
|
||||
|
||||
/// The JSON object in a reply, also when wrapped in a ``` fence or prose.
|
||||
fn extract_json(text: &str) -> Option<Value> {
|
||||
let trimmed = text.trim();
|
||||
@@ -74,21 +146,21 @@ impl OpenRouter {
|
||||
}
|
||||
|
||||
pub async fn complete(&self, model: &str, prompt: &Prompt) -> Result<Completion, LlmError> {
|
||||
let body = json!({
|
||||
"model": model,
|
||||
"messages": [
|
||||
let body = Request {
|
||||
model,
|
||||
messages: json!([
|
||||
{"role": "system", "content": prompt.system},
|
||||
{"role": "user", "content": prompt.user},
|
||||
],
|
||||
"response_format": {
|
||||
"type": "json_schema",
|
||||
"json_schema": {"name": prompt.schema_name, "strict": true, "schema": prompt.schema},
|
||||
]),
|
||||
response_format: ResponseFormat {
|
||||
kind: "json_schema",
|
||||
json_schema: JsonSchema { name: prompt.schema_name, strict: true, schema: Ordered(&prompt.schema) },
|
||||
},
|
||||
// Only route to providers that honour the schema.
|
||||
"provider": {"require_parameters": true},
|
||||
"max_tokens": MAX_TOKENS,
|
||||
"usage": {"include": true},
|
||||
});
|
||||
provider: json!({"require_parameters": true}),
|
||||
max_tokens: MAX_TOKENS,
|
||||
usage: json!({"include": true}),
|
||||
};
|
||||
let mut request = self
|
||||
.http
|
||||
.post(format!("{}/chat/completions", self.base_url))
|
||||
@@ -145,6 +217,22 @@ impl OpenRouter {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn schema_properties_follow_required_order() {
|
||||
let schema = super::super::prompt::schema(super::super::prompt::Kind::Natal);
|
||||
let text = serde_json::to_string(&Ordered(&schema)).unwrap();
|
||||
let pos = |needle: &str| text.find(needle).unwrap();
|
||||
assert!(pos("\"title\":{") < pos("\"summary\":{"));
|
||||
assert!(pos("\"summary\":{") < pos("\"sections\":{"));
|
||||
assert!(pos("\"personality\":{") < pos("\"growth\":{"));
|
||||
assert!(pos("\"strengths\":{") < pos("\"advice\":{"));
|
||||
// inside a section the title comes before the body
|
||||
let section = &text[pos("\"personality\":{")..];
|
||||
assert!(section.find("\"title\":{").unwrap() < section.find("\"body\":{").unwrap());
|
||||
// the content is unchanged
|
||||
assert_eq!(serde_json::from_str::<Value>(&text).unwrap(), schema);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extracts_json_from_fences_and_prose() {
|
||||
assert_eq!(extract_json(r#"{"a":1}"#), Some(json!({"a": 1})));
|
||||
|
||||
@@ -324,11 +324,13 @@ mod analyses {
|
||||
}
|
||||
|
||||
async fn refunds(db: &PgPool, user: Uuid) -> Vec<i32> {
|
||||
sqlx::query_scalar("SELECT delta FROM credit_ledger WHERE user_id = $1 AND reason = 'refund'")
|
||||
.bind(user)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
.unwrap()
|
||||
sqlx::query_scalar(
|
||||
"SELECT delta FROM credit_ledger WHERE user_id = $1 AND reason = 'refund' AND unit = 'natal'",
|
||||
)
|
||||
.bind(user)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
async fn make_due(db: &PgPool, id: Uuid) {
|
||||
|
||||
Reference in New Issue
Block a user