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