diff --git a/src/backends/anthropic.rs b/src/backends/anthropic.rs index d565a6c..4624c28 100644 --- a/src/backends/anthropic.rs +++ b/src/backends/anthropic.rs @@ -31,6 +31,7 @@ use serde_json::Value; #[derive(Debug)] pub struct Anthropic { pub api_key: String, + pub base_url: String, pub model: String, pub max_tokens: u32, pub temperature: f32, @@ -296,6 +297,7 @@ impl Anthropic { #[allow(clippy::too_many_arguments)] pub fn new( api_key: impl Into, + base_url: Option, model: Option, max_tokens: Option, temperature: Option, @@ -314,6 +316,7 @@ impl Anthropic { } Self { api_key: api_key.into(), + base_url: base_url.map(|u| u.trim_end_matches('/').to_string()).unwrap_or_else(|| "https://api.anthropic.com".to_string()), model: model.unwrap_or_else(|| "claude-3-sonnet-20240229".to_string()), max_tokens: max_tokens.unwrap_or(300), temperature: temperature.unwrap_or(0.7), @@ -508,7 +511,7 @@ impl ChatProvider for Anthropic { let mut request = self .client - .post("https://api.anthropic.com/v1/messages") + .post(&format!("{}/v1/messages", &self.base_url)) .header("x-api-key", &self.api_key) .header("Content-Type", "application/json") .header("anthropic-version", "2023-06-01") diff --git a/src/bin/llm-cli.rs b/src/bin/llm-cli.rs index aecff22..87afabe 100644 --- a/src/bin/llm-cli.rs +++ b/src/bin/llm-cli.rs @@ -113,6 +113,26 @@ fn get_provider_info(args: &CliArgs) -> Option<(String, Option)> { .map(|provider| (provider, args.model.clone())) } +/// Retrieves the base URL for the API from command line arguments or secret store +/// +/// # Arguments +/// +/// * `args` - Command line arguments that may contain a base URL +/// +/// # Returns +/// +/// * `Some(String)` - The base URL if found +/// * `None` - If no base URL could be found +fn get_base_url(args: &CliArgs) -> Option { + args.base_url.clone().or_else(|| { + let store = SecretStore::new().ok()?; + store + .get("BASE_URL") + .cloned() + .or_else(|| std::env::var("LLM_BASE_URL").ok()) + }) +} + /// Retrieves the appropriate API key for the specified backend /// /// # Arguments @@ -303,7 +323,7 @@ async fn main() -> Result<(), Box> { builder = builder.api_key(key); } - if let Some(url) = args.base_url.clone() { + if let Some(url) = get_base_url(&args) { builder = builder.base_url(url); } diff --git a/src/builder.rs b/src/builder.rs index 92033b1..ae67076 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -718,6 +718,7 @@ impl LLMBuilder { })?; let anthro = crate::backends::anthropic::Anthropic::new( api_key, + self.base_url, self.model, self.max_tokens, self.temperature,