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 model: String,
416    pub choices: Vec<ChoiceDelta>,
417    pub usage: Option<Usage>,
418}
419
420#[derive(Serialize, Deserialize, Debug)]
421pub struct CompletionResponse {
422    pub id: String,
423    pub object: String,
424    pub created: u64,
425    pub model: String,
426    pub choices: Vec<CompletionChoice>,
427    pub usage: Usage,
428}
429
430#[derive(Serialize, Deserialize, Debug)]
431pub struct CompletionChoice {
432    pub text: String,
433}
434
435#[derive(Serialize, Deserialize, Debug)]
436pub struct Response {
437    pub id: String,
438    pub object: String,
439    pub created: u64,
440    pub model: String,
441    pub choices: Vec<Choice>,
442    pub usage: Usage,
443}
444
445#[derive(Serialize, Deserialize, Debug)]
446pub struct Choice {
447    pub index: u32,
448    pub message: RequestMessage,
449    pub finish_reason: Option<String>,
450}
451
452pub async fn complete(
453    client: &dyn HttpClient,
454    api_url: &str,
455    api_key: &str,
456    request: Request,
457) -> Result<Response> {
458    let uri = format!("{api_url}/chat/completions");
459    let request_builder = HttpRequest::builder()
460        .method(Method::POST)
461        .uri(uri)
462        .header("Content-Type", "application/json")
463        .header("Authorization", format!("Bearer {}", api_key));
464
465    let mut request_body = request;
466    request_body.stream = false;
467
468    let request = request_builder.body(AsyncBody::from(serde_json::to_string(&request_body)?))?;
469    let mut response = client.send(request).await?;
470
471    if response.status().is_success() {
472        let mut body = String::new();
473        response.body_mut().read_to_string(&mut body).await?;
474        let response: Response = serde_json::from_str(&body)?;
475        Ok(response)
476    } else {
477        let mut body = String::new();
478        response.body_mut().read_to_string(&mut body).await?;
479
480        #[derive(Deserialize)]
481        struct OpenAiResponse {
482            error: OpenAiError,
483        }
484
485        #[derive(Deserialize)]
486        struct OpenAiError {
487            message: String,
488        }
489
490        match serde_json::from_str::<OpenAiResponse>(&body) {
491            Ok(response) if !response.error.message.is_empty() => anyhow::bail!(
492                "Failed to connect to OpenAI API: {}",
493                response.error.message,
494            ),
495            _ => anyhow::bail!(
496                "Failed to connect to OpenAI API: {} {}",
497                response.status(),
498                body,
499            ),
500        }
501    }
502}
503
504pub async fn complete_text(
505    client: &dyn HttpClient,
506    api_url: &str,
507    api_key: &str,
508    request: CompletionRequest,
509) -> Result<CompletionResponse> {
510    let uri = format!("{api_url}/completions");
511    let request_builder = HttpRequest::builder()
512        .method(Method::POST)
513        .uri(uri)
514        .header("Content-Type", "application/json")
515        .header("Authorization", format!("Bearer {}", api_key));
516
517    let request = request_builder.body(AsyncBody::from(serde_json::to_string(&request)?))?;
518    let mut response = client.send(request).await?;
519
520    if response.status().is_success() {
521        let mut body = String::new();
522        response.body_mut().read_to_string(&mut body).await?;
523        let response = serde_json::from_str(&body)?;
524        Ok(response)
525    } else {
526        let mut body = String::new();
527        response.body_mut().read_to_string(&mut body).await?;
528
529        #[derive(Deserialize)]
530        struct OpenAiResponse {
531            error: OpenAiError,
532        }
533
534        #[derive(Deserialize)]
535        struct OpenAiError {
536            message: String,
537        }
538
539        match serde_json::from_str::<OpenAiResponse>(&body) {
540            Ok(response) if !response.error.message.is_empty() => anyhow::bail!(
541                "Failed to connect to OpenAI API: {}",
542                response.error.message,
543            ),
544            _ => anyhow::bail!(
545                "Failed to connect to OpenAI API: {} {}",
546                response.status(),
547                body,
548            ),
549        }
550    }
551}
552
553fn adapt_response_to_stream(response: Response) -> ResponseStreamEvent {
554    ResponseStreamEvent {
555        created: response.created as u32,
556        model: response.model,
557        choices: response
558            .choices
559            .into_iter()
560            .map(|choice| {
561                let content = match &choice.message {
562                    RequestMessage::Assistant { content, .. } => content.as_ref(),
563                    RequestMessage::User { content } => Some(content),
564                    RequestMessage::System { content } => Some(content),
565                    RequestMessage::Tool { content, .. } => Some(content),
566                };
567
568                let mut text_content = String::new();
569                match content {
570                    Some(MessageContent::Plain(text)) => text_content.push_str(&text),
571                    Some(MessageContent::Multipart(parts)) => {
572                        for part in parts {
573                            match part {
574                                MessagePart::Text { text } => text_content.push_str(&text),
575                                MessagePart::Image { .. } => {}
576                            }
577                        }
578                    }
579                    None => {}
580                };
581
582                ChoiceDelta {
583                    index: choice.index,
584                    delta: ResponseMessageDelta {
585                        role: Some(match choice.message {
586                            RequestMessage::Assistant { .. } => Role::Assistant,
587                            RequestMessage::User { .. } => Role::User,
588                            RequestMessage::System { .. } => Role::System,
589                            RequestMessage::Tool { .. } => Role::Tool,
590                        }),
591                        content: if text_content.is_empty() {
592                            None
593                        } else {
594                            Some(text_content)
595                        },
596                        tool_calls: None,
597                    },
598                    finish_reason: choice.finish_reason,
599                }
600            })
601            .collect(),
602        usage: Some(response.usage),
603    }
604}
605
606pub async fn stream_completion(
607    client: &dyn HttpClient,
608    api_url: &str,
609    api_key: &str,
610    request: Request,
611) -> Result<BoxStream<'static, Result<ResponseStreamEvent>>> {
612    if request.model.starts_with("o1") {
613        let response = complete(client, api_url, api_key, request).await;
614        let response_stream_event = response.map(adapt_response_to_stream);
615        return Ok(stream::once(future::ready(response_stream_event)).boxed());
616    }
617
618    let uri = format!("{api_url}/chat/completions");
619    let request_builder = HttpRequest::builder()
620        .method(Method::POST)
621        .uri(uri)
622        .header("Content-Type", "application/json")
623        .header("Authorization", format!("Bearer {}", api_key));
624
625    let request = request_builder.body(AsyncBody::from(serde_json::to_string(&request)?))?;
626    let mut response = client.send(request).await?;
627    if response.status().is_success() {
628        let reader = BufReader::new(response.into_body());
629        Ok(reader
630            .lines()
631            .filter_map(|line| async move {
632                match line {
633                    Ok(line) => {
634                        let line = line.strip_prefix("data: ")?;
635                        if line == "[DONE]" {
636                            None
637                        } else {
638                            match serde_json::from_str(line) {
639                                Ok(ResponseStreamResult::Ok(response)) => Some(Ok(response)),
640                                Ok(ResponseStreamResult::Err { error }) => {
641                                    Some(Err(anyhow!(error)))
642                                }
643                                Err(error) => Some(Err(anyhow!(error))),
644                            }
645                        }
646                    }
647                    Err(error) => Some(Err(anyhow!(error))),
648                }
649            })
650            .boxed())
651    } else {
652        let mut body = String::new();
653        response.body_mut().read_to_string(&mut body).await?;
654
655        #[derive(Deserialize)]
656        struct OpenAiResponse {
657            error: OpenAiError,
658        }
659
660        #[derive(Deserialize)]
661        struct OpenAiError {
662            message: String,
663        }
664
665        match serde_json::from_str::<OpenAiResponse>(&body) {
666            Ok(response) if !response.error.message.is_empty() => Err(anyhow!(
667                "Failed to connect to OpenAI API: {}",
668                response.error.message,
669            )),
670
671            _ => anyhow::bail!(
672                "Failed to connect to OpenAI API: {} {}",
673                response.status(),
674                body,
675            ),
676        }
677    }
678}
679
680#[derive(Copy, Clone, Serialize, Deserialize)]
681pub enum OpenAiEmbeddingModel {
682    #[serde(rename = "text-embedding-3-small")]
683    TextEmbedding3Small,
684    #[serde(rename = "text-embedding-3-large")]
685    TextEmbedding3Large,
686}
687
688#[derive(Serialize)]
689struct OpenAiEmbeddingRequest<'a> {
690    model: OpenAiEmbeddingModel,
691    input: Vec<&'a str>,
692}
693
694#[derive(Deserialize)]
695pub struct OpenAiEmbeddingResponse {
696    pub data: Vec<OpenAiEmbedding>,
697}
698
699#[derive(Deserialize)]
700pub struct OpenAiEmbedding {
701    pub embedding: Vec<f32>,
702}
703
704pub fn embed<'a>(
705    client: &dyn HttpClient,
706    api_url: &str,
707    api_key: &str,
708    model: OpenAiEmbeddingModel,
709    texts: impl IntoIterator<Item = &'a str>,
710) -> impl 'static + Future<Output = Result<OpenAiEmbeddingResponse>> {
711    let uri = format!("{api_url}/embeddings");
712
713    let request = OpenAiEmbeddingRequest {
714        model,
715        input: texts.into_iter().collect(),
716    };
717    let body = AsyncBody::from(serde_json::to_string(&request).unwrap());
718    let request = HttpRequest::builder()
719        .method(Method::POST)
720        .uri(uri)
721        .header("Content-Type", "application/json")
722        .header("Authorization", format!("Bearer {}", api_key))
723        .body(body)
724        .map(|request| client.send(request));
725
726    async move {
727        let mut response = request?.await?;
728        let mut body = String::new();
729        response.body_mut().read_to_string(&mut body).await?;
730
731        anyhow::ensure!(
732            response.status().is_success(),
733            "error during embedding, status: {:?}, body: {:?}",
734            response.status(),
735            body
736        );
737        let response: OpenAiEmbeddingResponse =
738            serde_json::from_str(&body).context("failed to parse OpenAI embedding response")?;
739        Ok(response)
740    }
741}