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