Skip to main content

rig/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 = reqwest::Client> =
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/// ```
131/// use rig::providers::anthropic::{ClientBuilder, self};
132///
133/// // Initialize the Anthropic client
134/// let anthropic_client = ClientBuilder::new("your-claude-api-key")
135///    .anthropic_version(ANTHROPIC_VERSION_LATEST)
136///    .anthropic_beta("prompt-caching-2024-07-31")
137///    .build()
138/// ```
139impl<H> ClientBuilder<H> {
140    pub fn anthropic_version(self, anthropic_version: &str) -> Self {
141        self.over_ext(|ext| AnthropicBuilder {
142            anthropic_version: anthropic_version.into(),
143            ..ext
144        })
145    }
146
147    pub fn anthropic_betas(self, anthropic_betas: &[&str]) -> Self {
148        self.over_ext(|mut ext| {
149            ext.anthropic_betas
150                .extend(anthropic_betas.iter().copied().map(String::from));
151
152            ext
153        })
154    }
155
156    pub fn anthropic_beta(self, anthropic_beta: &str) -> Self {
157        self.over_ext(|mut ext| {
158            ext.anthropic_betas.push(anthropic_beta.into());
159
160            ext
161        })
162    }
163}
164
165pub fn normalize_anthropic_base_url(base_url: &str) -> String {
166    let trimmed = base_url.trim_end_matches('/');
167
168    if let Some(stripped) = trimmed.strip_suffix("/v1/messages") {
169        stripped.to_string()
170    } else if let Some(stripped) = trimmed.strip_suffix("/messages") {
171        stripped.to_string()
172    } else if let Some(stripped) = trimmed.strip_suffix("/v1") {
173        stripped.to_string()
174    } else {
175        trimmed.to_string()
176    }
177}
178
179pub fn finish_anthropic_builder<ExtBuilder, H>(
180    ext: &AnthropicBuilder,
181    mut builder: client::ClientBuilder<ExtBuilder, AnthropicKey, H>,
182) -> http_client::Result<client::ClientBuilder<ExtBuilder, AnthropicKey, H>>
183where
184    ExtBuilder: Clone,
185{
186    let normalized_base_url = normalize_anthropic_base_url(builder.get_base_url());
187    builder = builder.base_url(normalized_base_url);
188
189    builder.headers_mut().insert(
190        "anthropic-version",
191        HeaderValue::from_str(&ext.anthropic_version)?,
192    );
193
194    if !ext.anthropic_betas.is_empty() {
195        builder.headers_mut().insert(
196            "anthropic-beta",
197            HeaderValue::from_str(&ext.anthropic_betas.join(","))?,
198        );
199    }
200
201    Ok(builder)
202}
203#[cfg(test)]
204mod tests {
205    #[test]
206    fn test_client_initialization() {
207        let _client =
208            crate::providers::anthropic::Client::new("dummy-key").expect("Client::new() failed");
209        let _client_from_builder = crate::providers::anthropic::Client::builder()
210            .api_key("dummy-key")
211            .build()
212            .expect("Client::builder() failed");
213    }
214}