rig_core/client/
openai.rs1use 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 OpenAI,
30 OpenAIConfig
31);
32
33impl OpenAI {
34 #[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 #[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 pub fn completion(&self, model: impl Into<String>) -> Model<OpenAiWire> {
52 self.model(self.config.completion(model))
53 }
54
55 pub fn responses(&self, model: impl Into<String>) -> Model<Responses> {
57 self.model(self.config.responses(model))
58 }
59
60 pub fn chat(&self, model: impl Into<String>) -> Model<Chat> {
62 self.model(self.config.chat(model))
63 }
64
65 pub fn embedding(&self, model: impl Into<String>, ndims: Option<usize>) -> Model<Embeddings> {
67 self.model(self.config.embedding(model, ndims))
68 }
69
70 pub fn rerank(&self, model: impl Into<String>) -> Model<Rerank> {
72 self.model(self.config.rerank(model))
73 }
74
75 pub fn transcription(&self, model: impl Into<String>) -> Model<Transcriptions> {
77 self.model(self.config.transcription(model))
78 }
79
80 pub fn image_generation(&self, model: impl Into<String>) -> Model<Images> {
82 self.model(self.config.image_generation(model))
83 }
84
85 pub fn audio_generation(&self, model: impl Into<String>) -> Model<Speech> {
87 self.model(self.config.audio_generation(model))
88 }
89
90 pub async fn list_models(&self) -> Result<ModelList, ProviderError> {
92 self.model(self.config.models()).list().await
93 }
94
95 pub async fn verify(&self) -> Result<(), ProviderError> {
98 self.model(self.config.verify()).verify().await
99 }
100}