worktree-fix-ollama-json-extraction #12
3 changed files with 44 additions and 17 deletions
|
|
@ -27,20 +27,17 @@ pub(crate) async fn research(topic: &str, show_progress: bool) -> anyhow::Result
|
|||
findings = gathered.findings;
|
||||
incomplete = gathered.incomplete;
|
||||
|
||||
// The researcher ran out of turns mid-investigation rather than
|
||||
// concluding on its own — another round would just repeat the same
|
||||
// dead end, so stop and write up whatever was gathered.
|
||||
// Prevent repeat iterations where researcher hits same dead end.
|
||||
if incomplete {
|
||||
tracing::info!(round, "researcher exhausted its turn budget");
|
||||
break;
|
||||
}
|
||||
|
||||
let review = review::review_findings(&client, topic, &findings, show_progress).await?;
|
||||
let approved = review.approved;
|
||||
|
||||
tracing::info!(round, approved, "review verdict");
|
||||
tracing::info!(round, review.approved, "review verdict");
|
||||
|
||||
if approved || round == MAX_RESEARCH_ROUNDS {
|
||||
if review.approved || round == MAX_RESEARCH_ROUNDS {
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use rig::client::AgentClientExt;
|
||||
use rig::completion::TypedPrompt;
|
||||
use rig::providers::ollama;
|
||||
use rig::schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
|
@ -25,10 +26,18 @@ pub(crate) struct Review {
|
|||
pub(crate) gaps: String,
|
||||
}
|
||||
|
||||
/// Uses rig's typed extractor — a forced tool call into a `submit(Review)`
|
||||
/// schema — rather than parsing free-text output, so the verdict and its
|
||||
/// two feedback fields always come back structured instead of relying on
|
||||
/// scanning prose for a trailing yes/no.
|
||||
/// Retries for a review that fails to come back as valid structured output
|
||||
/// (e.g. a transient network error), on top of the initial attempt.
|
||||
const REVIEW_RETRIES: usize = 2;
|
||||
|
||||
/// Uses rig's typed-prompt API — `Native` structured output constraining the
|
||||
/// model's own reply to the `Review` schema — rather than a forced tool call
|
||||
/// or parsing free-text output. A forced tool call needs `tool_choice`
|
||||
/// support, which Ollama doesn't have: the reviewer's small local model would
|
||||
/// just answer in prose and never call the tool, so extraction only ever
|
||||
/// exhausted its retries and errored out. `Native` mode instead uses Ollama's
|
||||
/// own `format` JSON-schema constraint, which is honored regardless of
|
||||
/// tool-calling ability.
|
||||
#[tracing::instrument(skip(client, findings), fields(gen_ai.agent.name = "reviewer"))]
|
||||
pub(crate) async fn review_findings(
|
||||
client: &ollama::Client,
|
||||
|
|
@ -37,7 +46,7 @@ pub(crate) async fn review_findings(
|
|||
show_progress: bool,
|
||||
) -> anyhow::Result<Review> {
|
||||
let reviewer = client
|
||||
.extractor::<Review>(REVIEWER_MODEL)
|
||||
.agent(REVIEWER_MODEL)
|
||||
.preamble(
|
||||
"You are a skeptical fact-checker reviewing another researcher's notes before they \
|
||||
get turned into a report. Approve only if every conclusion in the findings is \
|
||||
|
|
@ -47,20 +56,40 @@ pub(crate) async fn review_findings(
|
|||
Always separate the solid, well-supported findings from the gaps so a follow-up \
|
||||
research pass knows what to keep and what to dig into further.",
|
||||
)
|
||||
.retries(2)
|
||||
.build();
|
||||
|
||||
let spinner = Spinner::start(
|
||||
show_progress,
|
||||
format!("{REVIEW_EMOJI} Reviewing findings..."),
|
||||
);
|
||||
let review = reviewer
|
||||
.extract(format!(
|
||||
"Topic: {topic}\n\nResearch findings to review:\n{findings}"
|
||||
))
|
||||
.await?;
|
||||
let prompt = format!("Topic: {topic}\n\nResearch findings to review:\n{findings}");
|
||||
|
||||
let mut last_error = None;
|
||||
let mut review = None;
|
||||
for attempt in 0..=REVIEW_RETRIES {
|
||||
match reviewer.prompt_typed::<Review>(prompt.clone()).await {
|
||||
Ok(r) => {
|
||||
review = Some(r);
|
||||
break;
|
||||
}
|
||||
Err(e) => {
|
||||
let suffix = if attempt < REVIEW_RETRIES {
|
||||
" Retrying..."
|
||||
} else {
|
||||
""
|
||||
};
|
||||
tracing::warn!("Attempt {attempt} to extract JSON failed: {e:?}.{suffix}");
|
||||
last_error = Some(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
drop(spinner);
|
||||
|
||||
let review = match review {
|
||||
Some(review) => review,
|
||||
None => return Err(last_error.expect("loop always sets last_error on failure").into()),
|
||||
};
|
||||
|
||||
tracing::info!(approved = review.approved, gaps = %review.gaps, "review complete");
|
||||
|
||||
Ok(review)
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ pub(crate) async fn search_web(
|
|||
) -> Result<String, ToolExecutionError> {
|
||||
progress::set_activity(format!("{SEARCH_EMOJI} Searching: {query}"));
|
||||
|
||||
// TODO re-use same client object?
|
||||
let response = reqwest::Client::new()
|
||||
.get(format!("{}/search", searxng_base_url()))
|
||||
.query(&[("q", query.as_str()), ("format", "json")])
|
||||
|
|
|
|||
Loading…
Reference in a new issue