Skip to main content

rig_core/client/
openai.rs

1//! The OpenAI client: an [`OpenAIConfig`] on a transport, and the models it
2//! builds.
3
4use crate::client::macros::http_client;
5use crate::driver::Model;
6use crate::error::ProviderError;
7use crate::model::ModelList;
8use crate::providers::openai::responses_api::wire::Responses;
9use crate::providers::openai::wire::{
10    Chat, Embeddings, Images, OpenAIConfig, OpenAiWire, Rerank, Speech, Transcriptions,
11};
12
13http_client!(
14    /// An OpenAI-shaped provider: its [`OpenAIConfig`] on a transport. Every
15    /// model it builds sends through that transport.
16    ///
17    /// ```no_run
18    /// use rig_core::completion::CompletionRequest;
19    /// use rig_core::providers::openai::{self, OpenAIConfig};
20    ///
21    /// # async fn run(http: rig_core::http_client::DynHttpClient) -> Result<(), Box<dyn std::error::Error>> {
22    /// let openai = OpenAIConfig::from_env()?.connect(http);
23    /// let model = openai.completion(openai::GPT_5_2);
24    /// let response = model.call(CompletionRequest::new("Capital of France?")).await?;
25    /// # let _ = response;
26    /// # Ok(())
27    /// # }
28    /// ```
29    OpenAI,
30    OpenAIConfig
31);
32
33impl OpenAI {
34    /// Official OpenAI with `api_key`, on the shared reqwest client.
35    #[cfg(feature = "reqwest")]
36    #[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))]
37    pub fn new(api_key: impl Into<crate::wire::Secret>) -> Self {
38        OpenAIConfig::new(api_key).client()
39    }
40
41    /// Official OpenAI from `OPENAI_API_KEY` and the optional
42    /// `OPENAI_BASE_URL`, on the shared reqwest client.
43    #[cfg(feature = "reqwest")]
44    #[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))]
45    pub fn from_env() -> Result<Self, crate::client::env::EnvError> {
46        Ok(OpenAIConfig::from_env()?.client())
47    }
48
49    /// The completion model for `model` on the configuration's
50    /// [`completion_route`](OpenAIConfig::completion_route).
51    pub fn completion(&self, model: impl Into<String>) -> Model<OpenAiWire> {
52        self.model(self.config.completion(model))
53    }
54
55    /// The Responses model for `model`: `POST /responses`.
56    pub fn responses(&self, model: impl Into<String>) -> Model<Responses> {
57        self.model(self.config.responses(model))
58    }
59
60    /// The Chat Completions model for `model`.
61    pub fn chat(&self, model: impl Into<String>) -> Model<Chat> {
62        self.model(self.config.chat(model))
63    }
64
65    /// The embedding model for `model`, `ndims` wide when set.
66    pub fn embedding(&self, model: impl Into<String>, ndims: Option<usize>) -> Model<Embeddings> {
67        self.model(self.config.embedding(model, ndims))
68    }
69
70    /// The rerank model for `model`.
71    pub fn rerank(&self, model: impl Into<String>) -> Model<Rerank> {
72        self.model(self.config.rerank(model))
73    }
74
75    /// The transcription model for `model`.
76    pub fn transcription(&self, model: impl Into<String>) -> Model<Transcriptions> {
77        self.model(self.config.transcription(model))
78    }
79
80    /// The image-generation model for `model`.
81    pub fn image_generation(&self, model: impl Into<String>) -> Model<Images> {
82        self.model(self.config.image_generation(model))
83    }
84
85    /// The speech model for `model`.
86    pub fn audio_generation(&self, model: impl Into<String>) -> Model<Speech> {
87        self.model(self.config.audio_generation(model))
88    }
89
90    /// The models this provider serves, every page followed.
91    pub async fn list_models(&self) -> Result<ModelList, ProviderError> {
92        self.model(self.config.models()).list().await
93    }
94
95    /// Check that the provider accepts the configured credential. A 401 or
96    /// 403 reply is [`ProviderError::InvalidAuthentication`].
97    pub async fn verify(&self) -> Result<(), ProviderError> {
98        self.model(self.config.verify()).verify().await
99    }
100}