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