Skip to main content

rig_core/providers/anthropic/
client.rs

1//! Anthropic client api implementation
2use http::{HeaderName, HeaderValue};
3
4use super::completion::{ANTHROPIC_VERSION_LATEST, CompletionModel};
5use crate::{
6    client::{
7        self, ApiKey, Capabilities, Capable, DebugExt, Nothing, Provider, ProviderBuilder,
8        ProviderClient,
9    },
10    http_client::{self, HttpClientExt},
11    providers::anthropic::model_listing::AnthropicModelLister,
12};
13
14// ================================================================
15// Main Anthropic Client
16// ================================================================
17#[derive(Debug, Default, Clone)]
18pub struct AnthropicExt;
19
20impl Provider for AnthropicExt {
21    type Builder = AnthropicBuilder;
22    const VERIFY_PATH: &'static str = "/v1/models";
23}
24
25impl<H> Capabilities<H> for AnthropicExt {
26    type Completion = Capable<CompletionModel<H>>;
27
28    type Embeddings = Nothing;
29    type Transcription = Nothing;
30    type ModelListing = Capable<AnthropicModelLister<H>>;
31    #[cfg(feature = "image")]
32    type ImageGeneration = Nothing;
33    #[cfg(feature = "audio")]
34    type AudioGeneration = Nothing;
35}
36
37#[derive(Debug, Clone)]
38pub struct AnthropicBuilder {
39    pub(crate) anthropic_version: String,
40    pub(crate) anthropic_betas: Vec<String>,
41}
42
43#[derive(Debug, Clone)]
44pub struct AnthropicKey(String);
45
46impl<S> From<S> for AnthropicKey
47where
48    S: Into<String>,
49{
50    fn from(value: S) -> Self {
51        Self(value.into())
52    }
53}
54
55impl ApiKey for AnthropicKey {
56    fn into_header(self) -> Option<http_client::Result<(http::HeaderName, HeaderValue)>> {
57        Some(
58            HeaderValue::from_str(&self.0)
59                .map(|val| (HeaderName::from_static("x-api-key"), val))
60                .map_err(Into::into),
61        )
62    }
63}
64
65pub type Client<H = reqwest::Client> = client::Client<AnthropicExt, H>;
66pub type ClientBuilder<H = crate::markers::Missing> =
67    client::ClientBuilder<AnthropicBuilder, AnthropicKey, H>;
68
69impl Default for AnthropicBuilder {
70    fn default() -> Self {
71        Self {
72            anthropic_version: ANTHROPIC_VERSION_LATEST.into(),
73            anthropic_betas: Vec::new(),
74        }
75    }
76}
77
78impl ProviderBuilder for AnthropicBuilder {
79    type Extension<H>
80        = AnthropicExt
81    where
82        H: HttpClientExt;
83    type ApiKey = AnthropicKey;
84
85    const BASE_URL: &'static str = "https://api.anthropic.com";
86
87    fn build<H>(
88        _builder: &client::ClientBuilder<Self, Self::ApiKey, H>,
89    ) -> http_client::Result<Self::Extension<H>>
90    where
91        H: HttpClientExt,
92    {
93        Ok(AnthropicExt)
94    }
95
96    fn finish<H>(
97        &self,
98        builder: client::ClientBuilder<Self, AnthropicKey, H>,
99    ) -> http_client::Result<client::ClientBuilder<Self, AnthropicKey, H>> {
100        finish_anthropic_builder(self, builder)
101    }
102}
103
104impl DebugExt for AnthropicExt {}
105
106impl ProviderClient for Client {
107    type Input = String;
108    type Error = crate::client::ProviderClientError;
109
110    fn from_env() -> Result<Self, Self::Error>
111    where
112        Self: Sized,
113    {
114        let key = crate::client::required_env_var("ANTHROPIC_API_KEY")?;
115
116        Self::builder().api_key(key).build().map_err(Into::into)
117    }
118
119    fn from_val(input: Self::Input) -> Result<Self, Self::Error>
120    where
121        Self: Sized,
122    {
123        Self::builder().api_key(input).build().map_err(Into::into)
124    }
125}
126
127/// Create a new anthropic client using the builder
128///
129/// # Example
130/// ```no_run
131/// use rig_core::providers::anthropic::{Client, self};
132/// use rig_core::providers::anthropic::completion::ANTHROPIC_VERSION_LATEST;
133///
134/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
135/// // Initialize the Anthropic client
136/// let anthropic_client = Client::builder()
137///    .api_key("your-claude-api-key")
138///    .anthropic_version(ANTHROPIC_VERSION_LATEST)
139///    .anthropic_beta("prompt-caching-2024-07-31")
140///    .build()?;
141/// # Ok(())
142/// # }
143/// ```
144impl<H> ClientBuilder<H> {
145    pub fn anthropic_version(self, anthropic_version: &str) -> Self {
146        self.over_ext(|ext| AnthropicBuilder {
147            anthropic_version: anthropic_version.into(),
148            ..ext
149        })
150    }
151
152    pub fn anthropic_betas(self, anthropic_betas: &[&str]) -> Self {
153        self.over_ext(|mut ext| {
154            ext.anthropic_betas
155                .extend(anthropic_betas.iter().copied().map(String::from));
156
157            ext
158        })
159    }
160
161    pub fn anthropic_beta(self, anthropic_beta: &str) -> Self {
162        self.over_ext(|mut ext| {
163            ext.anthropic_betas.push(anthropic_beta.into());
164
165            ext
166        })
167    }
168}
169
170pub fn normalize_anthropic_base_url(base_url: &str) -> String {
171    let trimmed = base_url.trim_end_matches('/');
172
173    if let Some(stripped) = trimmed.strip_suffix("/v1/messages") {
174        stripped.to_string()
175    } else if let Some(stripped) = trimmed.strip_suffix("/messages") {
176        stripped.to_string()
177    } else if let Some(stripped) = trimmed.strip_suffix("/v1") {
178        stripped.to_string()
179    } else {
180        trimmed.to_string()
181    }
182}
183
184pub fn finish_anthropic_builder<ExtBuilder, H>(
185    ext: &AnthropicBuilder,
186    mut builder: client::ClientBuilder<ExtBuilder, AnthropicKey, H>,
187) -> http_client::Result<client::ClientBuilder<ExtBuilder, AnthropicKey, H>>
188where
189    ExtBuilder: Clone,
190{
191    let normalized_base_url = normalize_anthropic_base_url(builder.get_base_url());
192    builder = builder.base_url(normalized_base_url);
193
194    builder.headers_mut().insert(
195        "anthropic-version",
196        HeaderValue::from_str(&ext.anthropic_version)?,
197    );
198
199    if !ext.anthropic_betas.is_empty() {
200        builder.headers_mut().insert(
201            "anthropic-beta",
202            HeaderValue::from_str(&ext.anthropic_betas.join(","))?,
203        );
204    }
205
206    Ok(builder)
207}
208#[cfg(test)]
209mod tests {
210    #[test]
211    fn test_client_initialization() {
212        let _client =
213            crate::providers::anthropic::Client::new("dummy-key").expect("Client::new() failed");
214        let _client_from_builder = crate::providers::anthropic::Client::builder()
215            .api_key("dummy-key")
216            .build()
217            .expect("Client::builder() failed");
218    }
219}