use crate::PoolError;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VisionApiConfig {
pub url: String,
pub model: String,
pub api_key: Option<String>,
}
fn post_chat_completions(
body: serde_json::Value,
config: &VisionApiConfig,
) -> Result<String, PoolError> {
let cfg = config.clone();
std::thread::spawn(move || -> Result<String, PoolError> {
let client = reqwest::blocking::Client::builder()
.timeout(std::time::Duration::from_secs(300))
.build()
.map_err(|e| PoolError::VisionApi(format!("build http client: {e}")))?;
let url = format!("{}/v1/chat/completions", cfg.url.trim_end_matches('/'));
let mut request = client.post(&url).json(&body);
if let Some(ref api_key) = cfg.api_key {
request = request.header("Authorization", format!("Bearer {api_key}"));
}
let mut response = None;
for attempt in 0..3 {
match request
.try_clone()
.unwrap_or_else(|| client.post(&url).json(&body))
.send()
{
Ok(resp) => {
if resp.status() == 503 {
if attempt < 2 {
let delay_secs = 5 + attempt * 10;
std::thread::sleep(std::time::Duration::from_secs(delay_secs));
continue;
}
let text = resp.text().unwrap_or_default();
return Err(PoolError::VisionApi(format!(
"API 503 after retries: {text}"
)));
}
response = Some(resp);
break;
}
Err(e) => {
if attempt < 2 {
std::thread::sleep(std::time::Duration::from_secs(5));
continue;
}
return Err(PoolError::VisionApi(format!("API request: {e}")));
}
}
}
let response = response
.ok_or_else(|| PoolError::VisionApi("API request failed after retries".to_string()))?;
let status = response.status();
if !status.is_success() {
let text = response.text().unwrap_or_default();
return Err(PoolError::VisionApi(format!("API {status}: {text}")));
}
let result: serde_json::Value = response
.json()
.map_err(|e| PoolError::VisionApi(format!("API response json: {e}")))?;
let content = result["choices"][0]["message"]["content"]
.as_str()
.ok_or_else(|| {
PoolError::VisionApi("response missing choices[0].message.content".to_string())
})?
.to_string();
Ok(content)
})
.join()
.map_err(|_| PoolError::VisionApi("thread panicked".to_string()))?
}
#[cfg(feature = "vision-api")]
pub fn call_vision_api(
b64_png: &str,
question: &str,
config: &VisionApiConfig,
) -> Result<String, PoolError> {
let body = serde_json::json!({
"model": config.model,
"messages": [{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": format!("data:image/png;base64,{b64_png}")
}
},
{
"type": "text",
"text": question
}
]
}],
"max_tokens": 4096,
});
post_chat_completions(body, config)
}
#[cfg(feature = "vision-api")]
pub fn call_vision_api_sequence(
frames: &[(String, String)],
question: &str,
config: &VisionApiConfig,
) -> Result<String, PoolError> {
if frames.is_empty() {
return Err(PoolError::VisionApi(
"sequence vision evaluation requires at least one frame".to_owned(),
));
}
let mut content = Vec::with_capacity(frames.len() * 2 + 1);
for (label, b64_png) in frames {
content.push(serde_json::json!({
"type": "text",
"text": format!("Checkpoint: {label}"),
}));
content.push(serde_json::json!({
"type": "image_url",
"image_url": {
"url": format!("data:image/png;base64,{b64_png}")
}
}));
}
content.push(serde_json::json!({"type": "text", "text": question}));
let body = serde_json::json!({
"model": config.model,
"messages": [{"role": "user", "content": content}],
"max_tokens": 4096,
});
post_chat_completions(body, config)
}
#[cfg(feature = "http-rubric")]
pub fn call_text_api(prompt: &str, config: &VisionApiConfig) -> Result<String, PoolError> {
let body = serde_json::json!({
"model": config.model,
"messages": [{"role": "user", "content": prompt}],
"max_tokens": 4096,
});
post_chat_completions(body, config)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn vision_api_rejects_bad_url() {
let config = VisionApiConfig {
url: "http://127.0.0.1:1".to_string(),
model: "test-model".to_string(),
api_key: None,
};
let result = call_vision_api("fake-base64", "test question", &config);
assert!(result.is_err(), "expected error for unreachable port");
match result {
Err(PoolError::VisionApi(msg)) => {
assert!(!msg.is_empty(), "error message should not be empty");
}
other => panic!("expected VisionApi error, got {other:?}"),
}
}
}