open_ai.rs

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