pub(crate) mod convert;
mod request;
mod streaming;
mod transport;
mod usage;
#[cfg(test)]
mod tests;
use std::sync::{Arc, Mutex};
use crate::clients::base::{LLMUsageDetails, TokenUsage};
use anyllm_client::retry::RetryPolicy;
pub struct AnthropicClient {
base_url: String,
model: String,
api_key: Option<String>,
http_client: reqwest::Client,
max_tokens: i64,
timeout_secs: f64,
retry_policy: RetryPolicy,
tool_choice: Option<String>,
last_usage: Arc<Mutex<Option<TokenUsage>>>,
last_usage_details: Arc<Mutex<Option<LLMUsageDetails>>>,
}
impl AnthropicClient {
pub fn new(model: impl Into<String>, api_key: Option<String>) -> Self {
Self {
base_url: "https://api.anthropic.com/v1".to_string(),
model: model.into(),
api_key,
http_client: reqwest::Client::new(),
max_tokens: 4096,
timeout_secs: 300.0,
retry_policy: crate::clients::retry::upstream_retry_policy(),
tool_choice: None,
last_usage: Arc::new(Mutex::new(None)),
last_usage_details: Arc::new(Mutex::new(None)),
}
}
pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
self.base_url = base_url.into();
self
}
pub fn with_http_client(mut self, client: reqwest::Client) -> Self {
self.http_client = client;
self
}
pub fn with_max_tokens(mut self, max_tokens: i64) -> Self {
self.max_tokens = max_tokens;
self
}
pub fn with_timeout(mut self, timeout_secs: f64) -> Self {
self.timeout_secs = timeout_secs;
self
}
pub fn with_max_retries(mut self, max_retries: i64) -> Self {
self.retry_policy.max_retries = max_retries.max(0) as u32;
self
}
pub fn with_tool_choice(mut self, tool_choice: impl Into<String>) -> Self {
self.tool_choice = Some(tool_choice.into());
self
}
pub fn get_last_usage(&self) -> Option<TokenUsage> {
self.last_usage.lock().ok().and_then(|guard| guard.clone())
}
pub fn get_last_usage_details(&self) -> Option<LLMUsageDetails> {
self.last_usage_details
.lock()
.ok()
.and_then(|guard| guard.clone())
}
}