open_ai.rs

  1use anyhow::{Context as _, Result, anyhow};
  2use futures::{AsyncBufReadExt, AsyncReadExt, StreamExt, io::BufReader, stream::BoxStream};
  3use http_client::{
  4    AsyncBody, HttpClient, Method, Request as HttpRequest, StatusCode,
  5    http::{HeaderMap, HeaderValue},
  6};
  7use serde::{Deserialize, Serialize};
  8use serde_json::Value;
  9pub use settings::OpenAiReasoningEffort as ReasoningEffort;
 10use std::{convert::TryFrom, future::Future};
 11use strum::EnumIter;
 12use thiserror::Error;
 13
 14pub const OPEN_AI_API_URL: &str = "https://api.openai.com/v1";
 15
 16fn is_none_or_empty<T: AsRef<[U]>, U>(opt: &Option<T>) -> bool {
 17    opt.as_ref().is_none_or(|v| v.as_ref().is_empty())
 18}
 19
 20#[derive(Clone, Copy, Serialize, Deserialize, Debug, Eq, PartialEq)]
 21#[serde(rename_all = "lowercase")]
 22pub enum Role {
 23    User,
 24    Assistant,
 25    System,
 26    Tool,
 27}
 28
 29impl TryFrom<String> for Role {
 30    type Error = anyhow::Error;
 31
 32    fn try_from(value: String) -> Result<Self> {
 33        match value.as_str() {
 34            "user" => Ok(Self::User),
 35            "assistant" => Ok(Self::Assistant),
 36            "system" => Ok(Self::System),
 37            "tool" => Ok(Self::Tool),
 38            _ => anyhow::bail!("invalid role '{value}'"),
 39        }
 40    }
 41}
 42
 43impl From<Role> for String {
 44    fn from(val: Role) -> Self {
 45        match val {
 46            Role::User => "user".to_owned(),
 47            Role::Assistant => "assistant".to_owned(),
 48            Role::System => "system".to_owned(),
 49            Role::Tool => "tool".to_owned(),
 50        }
 51    }
 52}
 53
 54#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
 55#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, EnumIter)]
 56pub enum Model {
 57    #[serde(rename = "gpt-3.5-turbo")]
 58    ThreePointFiveTurbo,
 59    #[serde(rename = "gpt-4")]
 60    Four,
 61    #[serde(rename = "gpt-4-turbo")]
 62    FourTurbo,
 63    #[serde(rename = "gpt-4o")]
 64    #[default]
 65    FourOmni,
 66    #[serde(rename = "gpt-4o-mini")]
 67    FourOmniMini,
 68    #[serde(rename = "gpt-4.1")]
 69    FourPointOne,
 70    #[serde(rename = "gpt-4.1-mini")]
 71    FourPointOneMini,
 72    #[serde(rename = "gpt-4.1-nano")]
 73    FourPointOneNano,
 74    #[serde(rename = "o1")]
 75    O1,
 76    #[serde(rename = "o3-mini")]
 77    O3Mini,
 78    #[serde(rename = "o3")]
 79    O3,
 80    #[serde(rename = "o4-mini")]
 81    O4Mini,
 82    #[serde(rename = "gpt-5")]
 83    Five,
 84    #[serde(rename = "gpt-5-mini")]
 85    FiveMini,
 86    #[serde(rename = "gpt-5-nano")]
 87    FiveNano,
 88    #[serde(rename = "gpt-5.1")]
 89    FivePointOne,
 90    #[serde(rename = "gpt-5.2")]
 91    FivePointTwo,
 92    #[serde(rename = "custom")]
 93    Custom {
 94        name: String,
 95        /// The name displayed in the UI, such as in the assistant panel model dropdown menu.
 96        display_name: Option<String>,
 97        max_tokens: u64,
 98        max_output_tokens: Option<u64>,
 99        max_completion_tokens: Option<u64>,
100        reasoning_effort: Option<ReasoningEffort>,
101    },
102}
103
104impl Model {
105    pub fn default_fast() -> Self {
106        // TODO: Replace with FiveMini since all other models are deprecated
107        Self::FourPointOneMini
108    }
109
110    pub fn from_id(id: &str) -> Result<Self> {
111        match id {
112            "gpt-3.5-turbo" => Ok(Self::ThreePointFiveTurbo),
113            "gpt-4" => Ok(Self::Four),
114            "gpt-4-turbo-preview" => Ok(Self::FourTurbo),
115            "gpt-4o" => Ok(Self::FourOmni),
116            "gpt-4o-mini" => Ok(Self::FourOmniMini),
117            "gpt-4.1" => Ok(Self::FourPointOne),
118            "gpt-4.1-mini" => Ok(Self::FourPointOneMini),
119            "gpt-4.1-nano" => Ok(Self::FourPointOneNano),
120            "o1" => Ok(Self::O1),
121            "o3-mini" => Ok(Self::O3Mini),
122            "o3" => Ok(Self::O3),
123            "o4-mini" => Ok(Self::O4Mini),
124            "gpt-5" => Ok(Self::Five),
125            "gpt-5-mini" => Ok(Self::FiveMini),
126            "gpt-5-nano" => Ok(Self::FiveNano),
127            "gpt-5.1" => Ok(Self::FivePointOne),
128            "gpt-5.2" => Ok(Self::FivePointTwo),
129            invalid_id => anyhow::bail!("invalid model id '{invalid_id}'"),
130        }
131    }
132
133    pub fn id(&self) -> &str {
134        match self {
135            Self::ThreePointFiveTurbo => "gpt-3.5-turbo",
136            Self::Four => "gpt-4",
137            Self::FourTurbo => "gpt-4-turbo",
138            Self::FourOmni => "gpt-4o",
139            Self::FourOmniMini => "gpt-4o-mini",
140            Self::FourPointOne => "gpt-4.1",
141            Self::FourPointOneMini => "gpt-4.1-mini",
142            Self::FourPointOneNano => "gpt-4.1-nano",
143            Self::O1 => "o1",
144            Self::O3Mini => "o3-mini",
145            Self::O3 => "o3",
146            Self::O4Mini => "o4-mini",
147            Self::Five => "gpt-5",
148            Self::FiveMini => "gpt-5-mini",
149            Self::FiveNano => "gpt-5-nano",
150            Self::FivePointOne => "gpt-5.1",
151            Self::FivePointTwo => "gpt-5.2",
152            Self::Custom { name, .. } => name,
153        }
154    }
155
156    pub fn display_name(&self) -> &str {
157        match self {
158            Self::ThreePointFiveTurbo => "gpt-3.5-turbo",
159            Self::Four => "gpt-4",
160            Self::FourTurbo => "gpt-4-turbo",
161            Self::FourOmni => "gpt-4o",
162            Self::FourOmniMini => "gpt-4o-mini",
163            Self::FourPointOne => "gpt-4.1",
164            Self::FourPointOneMini => "gpt-4.1-mini",
165            Self::FourPointOneNano => "gpt-4.1-nano",
166            Self::O1 => "o1",
167            Self::O3Mini => "o3-mini",
168            Self::O3 => "o3",
169            Self::O4Mini => "o4-mini",
170            Self::Five => "gpt-5",
171            Self::FiveMini => "gpt-5-mini",
172            Self::FiveNano => "gpt-5-nano",
173            Self::FivePointOne => "gpt-5.1",
174            Self::FivePointTwo => "gpt-5.2",
175            Self::Custom {
176                name, display_name, ..
177            } => display_name.as_ref().unwrap_or(name),
178        }
179    }
180
181    pub fn max_token_count(&self) -> u64 {
182        match self {
183            Self::ThreePointFiveTurbo => 16_385,
184            Self::Four => 8_192,
185            Self::FourTurbo => 128_000,
186            Self::FourOmni => 128_000,
187            Self::FourOmniMini => 128_000,
188            Self::FourPointOne => 1_047_576,
189            Self::FourPointOneMini => 1_047_576,
190            Self::FourPointOneNano => 1_047_576,
191            Self::O1 => 200_000,
192            Self::O3Mini => 200_000,
193            Self::O3 => 200_000,
194            Self::O4Mini => 200_000,
195            Self::Five => 272_000,
196            Self::FiveMini => 272_000,
197            Self::FiveNano => 272_000,
198            Self::FivePointOne => 400_000,
199            Self::FivePointTwo => 400_000,
200            Self::Custom { max_tokens, .. } => *max_tokens,
201        }
202    }
203
204    pub fn max_output_tokens(&self) -> Option<u64> {
205        match self {
206            Self::Custom {
207                max_output_tokens, ..
208            } => *max_output_tokens,
209            Self::ThreePointFiveTurbo => Some(4_096),
210            Self::Four => Some(8_192),
211            Self::FourTurbo => Some(4_096),
212            Self::FourOmni => Some(16_384),
213            Self::FourOmniMini => Some(16_384),
214            Self::FourPointOne => Some(32_768),
215            Self::FourPointOneMini => Some(32_768),
216            Self::FourPointOneNano => Some(32_768),
217            Self::O1 => Some(100_000),
218            Self::O3Mini => Some(100_000),
219            Self::O3 => Some(100_000),
220            Self::O4Mini => Some(100_000),
221            Self::Five => Some(128_000),
222            Self::FiveMini => Some(128_000),
223            Self::FiveNano => Some(128_000),
224            Self::FivePointOne => Some(128_000),
225            Self::FivePointTwo => Some(128_000),
226        }
227    }
228
229    pub fn reasoning_effort(&self) -> Option<ReasoningEffort> {
230        match self {
231            Self::Custom {
232                reasoning_effort, ..
233            } => reasoning_effort.to_owned(),
234            _ => None,
235        }
236    }
237
238    /// Returns whether the given model supports the `parallel_tool_calls` parameter.
239    ///
240    /// If the model does not support the parameter, do not pass it up, or the API will return an error.
241    pub fn supports_parallel_tool_calls(&self) -> bool {
242        match self {
243            Self::ThreePointFiveTurbo
244            | Self::Four
245            | Self::FourTurbo
246            | Self::FourOmni
247            | Self::FourOmniMini
248            | Self::FourPointOne
249            | Self::FourPointOneMini
250            | Self::FourPointOneNano
251            | Self::Five
252            | Self::FiveMini
253            | Self::FivePointOne
254            | Self::FivePointTwo
255            | Self::FiveNano => true,
256            Self::O1 | Self::O3 | Self::O3Mini | Self::O4Mini | Model::Custom { .. } => false,
257        }
258    }
259
260    /// Returns whether the given model supports the `prompt_cache_key` parameter.
261    ///
262    /// If the model does not support the parameter, do not pass it up.
263    pub fn supports_prompt_cache_key(&self) -> bool {
264        true
265    }
266}
267
268#[derive(Debug, Serialize, Deserialize)]
269pub struct Request {
270    pub model: String,
271    pub messages: Vec<RequestMessage>,
272    pub stream: bool,
273    #[serde(default, skip_serializing_if = "Option::is_none")]
274    pub max_completion_tokens: Option<u64>,
275    #[serde(default, skip_serializing_if = "Vec::is_empty")]
276    pub stop: Vec<String>,
277    #[serde(default, skip_serializing_if = "Option::is_none")]
278    pub temperature: Option<f32>,
279    #[serde(default, skip_serializing_if = "Option::is_none")]
280    pub tool_choice: Option<ToolChoice>,
281    /// Whether to enable parallel function calling during tool use.
282    #[serde(default, skip_serializing_if = "Option::is_none")]
283    pub parallel_tool_calls: Option<bool>,
284    #[serde(default, skip_serializing_if = "Vec::is_empty")]
285    pub tools: Vec<ToolDefinition>,
286    #[serde(default, skip_serializing_if = "Option::is_none")]
287    pub prompt_cache_key: Option<String>,
288    #[serde(default, skip_serializing_if = "Option::is_none")]
289    pub reasoning_effort: Option<ReasoningEffort>,
290}
291
292#[derive(Debug, Serialize, Deserialize)]
293#[serde(rename_all = "lowercase")]
294pub enum ToolChoice {
295    Auto,
296    Required,
297    None,
298    #[serde(untagged)]
299    Other(ToolDefinition),
300}
301
302#[derive(Clone, Deserialize, Serialize, Debug)]
303#[serde(tag = "type", rename_all = "snake_case")]
304pub enum ToolDefinition {
305    #[allow(dead_code)]
306    Function { function: FunctionDefinition },
307}
308
309#[derive(Clone, Debug, Serialize, Deserialize)]
310pub struct FunctionDefinition {
311    pub name: String,
312    pub description: Option<String>,
313    pub parameters: Option<Value>,
314}
315
316#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
317#[serde(tag = "role", rename_all = "lowercase")]
318pub enum RequestMessage {
319    Assistant {
320        content: Option<MessageContent>,
321        #[serde(default, skip_serializing_if = "Vec::is_empty")]
322        tool_calls: Vec<ToolCall>,
323    },
324    User {
325        content: MessageContent,
326    },
327    System {
328        content: MessageContent,
329    },
330    Tool {
331        content: MessageContent,
332        tool_call_id: String,
333    },
334}
335
336#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
337#[serde(untagged)]
338pub enum MessageContent {
339    Plain(String),
340    Multipart(Vec<MessagePart>),
341}
342
343impl MessageContent {
344    pub fn empty() -> Self {
345        MessageContent::Multipart(vec![])
346    }
347
348    pub fn push_part(&mut self, part: MessagePart) {
349        match self {
350            MessageContent::Plain(text) => {
351                *self =
352                    MessageContent::Multipart(vec![MessagePart::Text { text: text.clone() }, part]);
353            }
354            MessageContent::Multipart(parts) if parts.is_empty() => match part {
355                MessagePart::Text { text } => *self = MessageContent::Plain(text),
356                MessagePart::Image { .. } => *self = MessageContent::Multipart(vec![part]),
357            },
358            MessageContent::Multipart(parts) => parts.push(part),
359        }
360    }
361}
362
363impl From<Vec<MessagePart>> for MessageContent {
364    fn from(mut parts: Vec<MessagePart>) -> Self {
365        if let [MessagePart::Text { text }] = parts.as_mut_slice() {
366            MessageContent::Plain(std::mem::take(text))
367        } else {
368            MessageContent::Multipart(parts)
369        }
370    }
371}
372
373#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
374#[serde(tag = "type")]
375pub enum MessagePart {
376    #[serde(rename = "text")]
377    Text { text: String },
378    #[serde(rename = "image_url")]
379    Image { image_url: ImageUrl },
380}
381
382#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
383pub struct ImageUrl {
384    pub url: String,
385    #[serde(skip_serializing_if = "Option::is_none")]
386    pub detail: Option<String>,
387}
388
389#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
390pub struct ToolCall {
391    pub id: String,
392    #[serde(flatten)]
393    pub content: ToolCallContent,
394}
395
396#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
397#[serde(tag = "type", rename_all = "lowercase")]
398pub enum ToolCallContent {
399    Function { function: FunctionContent },
400}
401
402#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
403pub struct FunctionContent {
404    pub name: String,
405    pub arguments: String,
406}
407
408#[derive(Clone, Serialize, Deserialize, Debug)]
409pub struct Response {
410    pub id: String,
411    pub object: String,
412    pub created: u64,
413    pub model: String,
414    pub choices: Vec<Choice>,
415    pub usage: Usage,
416}
417
418#[derive(Clone, Serialize, Deserialize, Debug)]
419pub struct Choice {
420    pub index: u32,
421    pub message: RequestMessage,
422    pub finish_reason: Option<String>,
423}
424
425#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
426pub struct ResponseMessageDelta {
427    pub role: Option<Role>,
428    pub content: Option<String>,
429    #[serde(default, skip_serializing_if = "is_none_or_empty")]
430    pub tool_calls: Option<Vec<ToolCallChunk>>,
431}
432
433#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
434pub struct ToolCallChunk {
435    pub index: usize,
436    pub id: Option<String>,
437
438    // There is also an optional `type` field that would determine if a
439    // function is there. Sometimes this streams in with the `function` before
440    // it streams in the `type`
441    pub function: Option<FunctionChunk>,
442}
443
444#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
445pub struct FunctionChunk {
446    pub name: Option<String>,
447    pub arguments: Option<String>,
448}
449
450#[derive(Clone, Serialize, Deserialize, Debug)]
451pub struct Usage {
452    pub prompt_tokens: u64,
453    pub completion_tokens: u64,
454    pub total_tokens: u64,
455}
456
457#[derive(Serialize, Deserialize, Debug)]
458pub struct ChoiceDelta {
459    pub index: u32,
460    pub delta: Option<ResponseMessageDelta>,
461    pub finish_reason: Option<String>,
462}
463
464#[derive(Error, Debug)]
465pub enum RequestError {
466    #[error("HTTP response error from {provider}'s API: status {status_code} - {body:?}")]
467    HttpResponseError {
468        provider: String,
469        status_code: StatusCode,
470        body: String,
471        headers: HeaderMap<HeaderValue>,
472    },
473    #[error(transparent)]
474    Other(#[from] anyhow::Error),
475}
476
477#[derive(Serialize, Deserialize, Debug)]
478pub struct ResponseStreamError {
479    message: String,
480}
481
482#[derive(Serialize, Deserialize, Debug)]
483#[serde(untagged)]
484pub enum ResponseStreamResult {
485    Ok(ResponseStreamEvent),
486    Err { error: ResponseStreamError },
487}
488
489#[derive(Serialize, Deserialize, Debug)]
490pub struct ResponseStreamEvent {
491    pub choices: Vec<ChoiceDelta>,
492    pub usage: Option<Usage>,
493}
494
495pub async fn stream_completion(
496    client: &dyn HttpClient,
497    provider_name: &str,
498    api_url: &str,
499    api_key: &str,
500    request: Request,
501) -> Result<BoxStream<'static, Result<ResponseStreamEvent>>, RequestError> {
502    let uri = format!("{api_url}/chat/completions");
503    let request_builder = HttpRequest::builder()
504        .method(Method::POST)
505        .uri(uri)
506        .header("Content-Type", "application/json")
507        .header("Authorization", format!("Bearer {}", api_key.trim()));
508
509    let request = request_builder
510        .body(AsyncBody::from(
511            serde_json::to_string(&request).map_err(|e| RequestError::Other(e.into()))?,
512        ))
513        .map_err(|e| RequestError::Other(e.into()))?;
514
515    let mut response = client.send(request).await?;
516    if response.status().is_success() {
517        let reader = BufReader::new(response.into_body());
518        Ok(reader
519            .lines()
520            .filter_map(|line| async move {
521                match line {
522                    Ok(line) => {
523                        let line = line.strip_prefix("data: ").or_else(|| line.strip_prefix("data:"))?;
524                        if line == "[DONE]" {
525                            None
526                        } else {
527                            match serde_json::from_str(line) {
528                                Ok(ResponseStreamResult::Ok(response)) => Some(Ok(response)),
529                                Ok(ResponseStreamResult::Err { error }) => {
530                                    Some(Err(anyhow!(error.message)))
531                                }
532                                Err(error) => {
533                                    log::error!(
534                                        "Failed to parse OpenAI response into ResponseStreamResult: `{}`\n\
535                                        Response: `{}`",
536                                        error,
537                                        line,
538                                    );
539                                    Some(Err(anyhow!(error)))
540                                }
541                            }
542                        }
543                    }
544                    Err(error) => Some(Err(anyhow!(error))),
545                }
546            })
547            .boxed())
548    } else {
549        let mut body = String::new();
550        response
551            .body_mut()
552            .read_to_string(&mut body)
553            .await
554            .map_err(|e| RequestError::Other(e.into()))?;
555
556        Err(RequestError::HttpResponseError {
557            provider: provider_name.to_owned(),
558            status_code: response.status(),
559            body,
560            headers: response.headers().clone(),
561        })
562    }
563}
564
565#[derive(Copy, Clone, Serialize, Deserialize)]
566pub enum OpenAiEmbeddingModel {
567    #[serde(rename = "text-embedding-3-small")]
568    TextEmbedding3Small,
569    #[serde(rename = "text-embedding-3-large")]
570    TextEmbedding3Large,
571}
572
573#[derive(Serialize)]
574struct OpenAiEmbeddingRequest<'a> {
575    model: OpenAiEmbeddingModel,
576    input: Vec<&'a str>,
577}
578
579#[derive(Deserialize)]
580pub struct OpenAiEmbeddingResponse {
581    pub data: Vec<OpenAiEmbedding>,
582}
583
584#[derive(Deserialize)]
585pub struct OpenAiEmbedding {
586    pub embedding: Vec<f32>,
587}
588
589pub fn embed<'a>(
590    client: &dyn HttpClient,
591    api_url: &str,
592    api_key: &str,
593    model: OpenAiEmbeddingModel,
594    texts: impl IntoIterator<Item = &'a str>,
595) -> impl 'static + Future<Output = Result<OpenAiEmbeddingResponse>> {
596    let uri = format!("{api_url}/embeddings");
597
598    let request = OpenAiEmbeddingRequest {
599        model,
600        input: texts.into_iter().collect(),
601    };
602    let body = AsyncBody::from(serde_json::to_string(&request).unwrap());
603    let request = HttpRequest::builder()
604        .method(Method::POST)
605        .uri(uri)
606        .header("Content-Type", "application/json")
607        .header("Authorization", format!("Bearer {}", api_key.trim()))
608        .body(body)
609        .map(|request| client.send(request));
610
611    async move {
612        let mut response = request?.await?;
613        let mut body = String::new();
614        response.body_mut().read_to_string(&mut body).await?;
615
616        anyhow::ensure!(
617            response.status().is_success(),
618            "error during embedding, status: {:?}, body: {:?}",
619            response.status(),
620            body
621        );
622        let response: OpenAiEmbeddingResponse =
623            serde_json::from_str(&body).context("failed to parse OpenAI embedding response")?;
624        Ok(response)
625    }
626}