Skip to main content

split_brain_harness/backends/
openai.rs

1use super::InferenceEngine;
2use async_trait::async_trait;
3use reqwest::Client;
4use serde_json::json;
5
6pub struct OpenAiEngine {
7    pub endpoint: String,
8    pub model: String,
9    pub client: Client,
10}
11
12#[async_trait]
13impl InferenceEngine for OpenAiEngine {
14    async fn generate(&self, system_prompt: &str, prompt_payload: &str) -> Result<String, String> {
15        let body = json!({
16            "model": self.model,
17            "messages": [
18                { "role": "system", "content": system_prompt },
19                { "role": "user",   "content": prompt_payload }
20            ],
21            "temperature": 0.1,
22            "max_tokens": 2048
23        });
24
25        let resp = self
26            .client
27            .post(format!(
28                "{}/chat/completions",
29                self.endpoint.trim_end_matches('/')
30            ))
31            .json(&body)
32            .send()
33            .await
34            .map_err(|e| e.to_string())?;
35
36        let json: serde_json::Value = resp.json().await.map_err(|e| e.to_string())?;
37
38        json["choices"][0]["message"]["content"]
39            .as_str()
40            .map(|s| s.to_string())
41            .ok_or_else(|| "missing content field in OpenAI response".into())
42    }
43}