macro_rules! http_client {
($(#[$meta:meta])* $client:ident, $config:ident) => {
$(#[$meta])*
#[derive(Clone, Debug)]
pub struct $client {
config: $config,
http: $crate::http_client::DynHttpClient,
}
impl $client {
pub fn with_http(
self,
http: impl $crate::http_client::HttpClientExt + 'static,
) -> Self {
Self {
http: $crate::http_client::DynHttpClient::new(http),
..self
}
}
pub fn config(&self) -> &$config {
&self.config
}
pub(crate) fn model<W>(&self, wire: W) -> $crate::driver::Model<W> {
$crate::driver::Model::new(wire, self.http.clone())
}
}
impl $config {
pub fn connect(self, http: impl $crate::http_client::HttpClientExt + 'static) -> $client {
$client {
config: self,
http: $crate::http_client::DynHttpClient::new(http),
}
}
#[cfg(feature = "reqwest")]
#[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))]
pub fn client(self) -> $client {
self.connect(rig_reqwest::shared())
}
}
};
}
pub(crate) use http_client;
macro_rules! openai_vendor {
($dialect:path, $name:literal) => {
$crate::client::macros::openai_vendor!($dialect, $name, from_env, new);
};
($dialect:path, $name:literal, $from_env:ident, $new:ident) => {
#[doc = concat!(
$name,
" from the environment variables [`",
stringify!($dialect),
"`] names, on the shared reqwest client."
)]
#[cfg(feature = "reqwest")]
#[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))]
pub fn $from_env()
-> Result<$crate::providers::openai::OpenAI, $crate::client::env::EnvError> {
Ok($crate::providers::openai::OpenAIConfig::from_env_with(&$dialect)?.client())
}
#[doc = concat!($name, " with `api_key`, on the shared reqwest client.")]
#[cfg(feature = "reqwest")]
#[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))]
pub fn $new(api_key: impl Into<$crate::wire::Secret>) -> $crate::providers::openai::OpenAI {
$crate::providers::openai::OpenAIConfig::with_key(&$dialect, api_key).client()
}
};
}
pub(crate) use openai_vendor;
macro_rules! anthropic_vendor {
($dialect:path, $name:literal, $from_env:ident, $new:ident) => {
#[doc = concat!(
$name,
"'s Messages-format endpoint from the environment variables [`",
stringify!($dialect),
"`] names, on the shared reqwest client."
)]
#[cfg(feature = "reqwest")]
#[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))]
pub fn $from_env()
-> Result<$crate::providers::anthropic::Anthropic, $crate::client::env::EnvError> {
Ok($crate::providers::anthropic::AnthropicConfig::from_env_with(&$dialect)?.client())
}
#[doc = concat!(
$name,
"'s Messages-format endpoint with `api_key`, on the shared reqwest client."
)]
#[cfg(feature = "reqwest")]
#[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))]
pub fn $new(
api_key: impl Into<$crate::wire::Secret>,
) -> $crate::providers::anthropic::Anthropic {
$crate::providers::anthropic::AnthropicConfig::with_key(&$dialect, api_key).client()
}
};
}
pub(crate) use anthropic_vendor;