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