2026-08-05 12:21:38 +00:00
|
|
|
use std::sync::LazyLock;
|
|
|
|
|
use anyhow;
|
|
|
|
|
use rig_core;
|
|
|
|
|
use rig_core::client::{CompletionClient, Nothing};
|
|
|
|
|
use rig_core::providers::llamafile::LlamafileExt;
|
|
|
|
|
use rig_core::providers::openai::GenericCompletionModel;
|
|
|
|
|
use rig_core::providers::{llamafile, ollama};
|
|
|
|
|
use crate::models::Prompts;
|
|
|
|
|
|
|
|
|
|
mod models;
|
2026-08-05 13:07:42 +00:00
|
|
|
mod revise;
|
2026-08-05 12:45:44 +00:00
|
|
|
mod server;
|
2026-08-05 12:21:38 +00:00
|
|
|
|
|
|
|
|
static PROMPTS: LazyLock<Prompts> = LazyLock::new(|| {
|
|
|
|
|
toml::from_str(include_str!("prompts.toml")).expect("Could not parse prompts.toml")
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-31 08:21:31 +00:00
|
|
|
#[tokio::main]
|
2026-08-05 12:21:38 +00:00
|
|
|
async fn main() -> anyhow::Result<()> {
|
2026-08-05 13:23:30 +00:00
|
|
|
// Respects RUST_LOG if the shell sets one (e.g. `RUST_LOG=debug cargo run`),
|
|
|
|
|
// otherwise defaults to "info" — the level Rig's own completion spans use.
|
|
|
|
|
// `with_span_events(CLOSE)` is the part that actually makes anything print:
|
|
|
|
|
// Rig records fields (model, token usage, ...) onto the span itself rather
|
|
|
|
|
// than emitting log events, so without this, fmt's default event-only
|
|
|
|
|
// logging shows nothing even though tracing is "on".
|
|
|
|
|
// Logs go to stderr, not stdout — keeps stdout reserved for the actual
|
|
|
|
|
// result (the final `println!` below), so it stays pipeable/parseable
|
|
|
|
|
// without log lines mixed in.
|
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
|
.with_env_filter(
|
|
|
|
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
|
|
|
|
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
|
|
|
|
|
)
|
|
|
|
|
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE)
|
|
|
|
|
.with_writer(std::io::stderr)
|
|
|
|
|
.init();
|
|
|
|
|
|
2026-08-05 12:45:44 +00:00
|
|
|
// Independent setup steps (talk to unrelated backends, no data dependency) — run concurrently.
|
|
|
|
|
let (gemma, ()) = tokio::try_join!(wire_gemma_client(), server::ensure_running())?;
|
|
|
|
|
|
2026-08-05 12:21:38 +00:00
|
|
|
let shieldstral = wire_shieldstral().await?;
|
|
|
|
|
|
2026-08-05 13:07:42 +00:00
|
|
|
let result = revise::generate_below_threshold(&gemma, &shieldstral).await?;
|
2026-08-05 12:21:38 +00:00
|
|
|
|
2026-08-05 13:23:30 +00:00
|
|
|
tracing::info!("Final score (score={:.6})", result.score);
|
|
|
|
|
tracing::info!("Final text ({})", result.text);
|
2026-08-05 12:21:38 +00:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn wire_gemma_client() -> anyhow::Result<ollama::CompletionModel> {
|
|
|
|
|
let gemma_client = ollama::Client::new(Nothing)?;
|
|
|
|
|
|
|
|
|
|
let gemma = gemma_client.completion_model("gemma4-e4b:latest");
|
|
|
|
|
|
|
|
|
|
Ok(gemma)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn wire_shieldstral() -> anyhow::Result<GenericCompletionModel<LlamafileExt>> {
|
2026-08-05 12:45:44 +00:00
|
|
|
let client = llamafile::Client::from_url(&server::url())?;
|
2026-08-05 12:21:38 +00:00
|
|
|
|
|
|
|
|
// Name doesn't matter here, server just uses whatever is running on it.
|
|
|
|
|
let shieldstral = client.completion_model("shieldstral");
|
|
|
|
|
|
|
|
|
|
Ok(shieldstral)
|
|
|
|
|
}
|