From a5a96671c90b37d32bc379d449dc458e73662192 Mon Sep 17 00:00:00 2001 From: vrubelroman Date: Tue, 27 Oct 2026 23:14:46 +0300 Subject: [PATCH] Translation: low reasoning effort for ChatGPT, three requests at once Each request waited for the model's full default thinking, and the batches of a long game went one after another. A model that rejects the effort is asked again without it; the first error still stops the job. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 3 + apps/desktop/src/chatgpt.rs | 20 ++++- apps/desktop/src/translate.rs | 148 +++++++++++++++++++++++----------- 3 files changed, 122 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0861fc1..9010ad4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ All notable changes are listed here. Versions follow [Semantic Versioning](https remembered for the next start (a changed file is counted again). - Desktop, the play dialog: the strength rides above the slider's handle, where the eye is while it moves. +- Translation of annotations is faster: ChatGPT is asked to think little (translating + needs no long thought), and a long game goes in three requests at once instead of one + after another. - Desktop, a game against the computer: the buttons under the clocks are of one height; the moves and the board flip on one row, the game's own actions on the next. diff --git a/apps/desktop/src/chatgpt.rs b/apps/desktop/src/chatgpt.rs index a5384a6..0f69b51 100644 --- a/apps/desktop/src/chatgpt.rs +++ b/apps/desktop/src/chatgpt.rs @@ -601,14 +601,16 @@ pub fn respond( user: &str, cancel: &AtomicBool, ) -> Result { - let body = json!({ + let mut body = json!({ "model": model, "instructions": system, "input": [{"role": "user", "content": [{"type": "input_text", "text": user}]}], "store": false, "stream": true, - }) - .to_string(); + // Translating needs no long thought: at the default effort the model thinks for + // most of a minute over each request before it writes a word. + "reasoning": {"effort": "low"}, + }); let mut refreshed = false; with_waits(cancel, || { loop { @@ -618,7 +620,7 @@ pub fn respond( .header("Authorization", format!("Bearer {token}")) .header("Content-Type", "application/json") .header("Accept", "text/event-stream") - .send(&body) + .send(&body.to_string()) .map_err(|e| Failure::Offline(e.to_string()))?; let status = resp.status().as_u16(); if status == 401 && !refreshed { @@ -636,6 +638,16 @@ pub fn respond( if status != 200 { let mut resp = resp; let text = resp.body_mut().read_to_string().unwrap_or_default(); + // A model that does not think takes no effort: asked again without it. + if status == 400 + && text.contains("reasoning") + && body + .as_object_mut() + .and_then(|b| b.remove("reasoning")) + .is_some() + { + continue; + } return Err(failure(status, &text)); } return read_stream(BufReader::new(resp.into_body().into_reader()), cancel); diff --git a/apps/desktop/src/translate.rs b/apps/desktop/src/translate.rs index 0290f30..a4a0d2a 100644 --- a/apps/desktop/src/translate.rs +++ b/apps/desktop/src/translate.rs @@ -8,7 +8,7 @@ use std::collections::HashMap; use std::io::{BufRead, BufReader, Write as _}; use std::path::PathBuf; -use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::mpsc::{Receiver, Sender, channel}; use std::sync::{Arc, Mutex}; use std::time::Duration; @@ -1291,60 +1291,73 @@ fn run( let _ = tx.send(e); ctx.request_repaint(); }; - for batch in batches { - if cancel.load(Ordering::Relaxed) { - return; + // A few requests at once: each waits for its whole answer, so one after another + // a long game took minutes. The first error stops the rest. + let next = AtomicUsize::new(0); + let failed = AtomicBool::new(false); + let stopped = || cancel.load(Ordering::Relaxed) || failed.load(Ordering::Relaxed); + let fail = |e: Failure| { + if !cancel.load(Ordering::Relaxed) && !failed.swap(true, Ordering::Relaxed) { + send(Event::Failed(e)); } - let answer = match backend.complete(&system, &request(context, &batch), cancel) { - Ok(a) => a, - Err(e) => { - if !cancel.load(Ordering::Relaxed) { - send(Event::Failed(e)); - } + }; + let worker = || { + while let Some(batch) = batches.get(next.fetch_add(1, Ordering::Relaxed)) { + if stopped() { return; } - }; - let got = parse_answer(&answer); - let mut done = Vec::new(); - let mut again = Vec::new(); - for &(id, text) in &batch { - match got.get(&id) { - Some(t) if same_moves(text, t) => done.push((text.to_owned(), t.clone())), - _ => again.push((id, text)), - } - } - // One more try for each comment that came back wrong, alone. - let mut bad = Vec::new(); - for (id, text) in again { - if cancel.load(Ordering::Relaxed) { - break; - } - let retry = backend - .complete(&system, &request(context, &[(id, text)]), cancel) - .map(|a| parse_answer(&a)); - match retry { - Ok(got) => match got.get(&id) { + let answer = match backend.complete(&system, &request(context, batch), cancel) { + Ok(a) => a, + Err(e) => return fail(e), + }; + let got = parse_answer(&answer); + let mut done = Vec::new(); + let mut again = Vec::new(); + for &(id, text) in batch { + match got.get(&id) { Some(t) if same_moves(text, t) => done.push((text.to_owned(), t.clone())), - _ => bad.push(text.to_owned()), - }, - Err(e) => { - store(target, &done, via); - send(Event::Done(done)); - if !cancel.load(Ordering::Relaxed) { - send(Event::Failed(e)); - } - return; + _ => again.push((id, text)), } } + // One more try for each comment that came back wrong, alone. + let mut bad = Vec::new(); + for (id, text) in again { + if stopped() { + break; + } + let retry = backend + .complete(&system, &request(context, &[(id, text)]), cancel) + .map(|a| parse_answer(&a)); + match retry { + Ok(got) => match got.get(&id) { + Some(t) if same_moves(text, t) => done.push((text.to_owned(), t.clone())), + _ => bad.push(text.to_owned()), + }, + Err(e) => { + store(target, &done, via); + send(Event::Done(done)); + return fail(e); + } + } + } + store(target, &done, via); + send(Event::Done(done)); + if !bad.is_empty() { + send(Event::Bad(bad)); + } } - store(target, &done, via); - send(Event::Done(done)); - if !bad.is_empty() { - send(Event::Bad(bad)); + }; + std::thread::scope(|scope| { + for _ in 1..AT_ONCE.min(batches.len()) { + scope.spawn(worker); } - } + worker(); + }); } +/// Requests sent at once. +const AT_ONCE: usize = 3; + #[cfg(test)] mod tests { use super::*; @@ -1616,6 +1629,51 @@ mod tests { ); assert!(seen[0].get("response_format").is_none()); } + + #[test] + fn a_long_game_goes_in_requests_at_once_and_every_comment_comes_back() { + // 100 comments: three requests (40, 40, 20), sent together. + let (base, server) = fake_service(3); + let backend = Backend::Chat { + base, + key: None, + model: "m".into(), + json_mode: false, + local: true, + }; + let texts: Vec = (0..100) + .map(|i| format!("Comment {i} on 12.Nf3.")) + .collect(); + let job = Job::start( + &egui::Context::default(), + backend, + Lang::Es, + "test".into(), + GameContext::default(), + texts.clone(), + ); + let mut done = Vec::new(); + let start = std::time::Instant::now(); + 'wait: while start.elapsed() < Duration::from_secs(20) { + for e in job.poll() { + match e { + Event::Done(d) => done.extend(d), + Event::Bad(b) => panic!("bad {b:?}"), + Event::Failed(f) => panic!("{f}"), + Event::Finished => break 'wait, + } + } + std::thread::sleep(Duration::from_millis(20)); + } + done.sort(); + let mut want: Vec<_> = texts + .iter() + .map(|t| (t.clone(), format!("RU {t}"))) + .collect(); + want.sort(); + assert_eq!(done, want); + assert_eq!(server.join().unwrap().len(), 3); + } } #[cfg(test)]