Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/backends/anthropic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -296,6 +297,7 @@ impl Anthropic {
#[allow(clippy::too_many_arguments)]
pub fn new(
api_key: impl Into<String>,
base_url: Option<String>,
model: Option<String>,
max_tokens: Option<u32>,
temperature: Option<f32>,
Expand All @@ -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),
Expand Down Expand Up @@ -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")
Expand Down
22 changes: 21 additions & 1 deletion src/bin/llm-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,26 @@ fn get_provider_info(args: &CliArgs) -> Option<(String, Option<String>)> {
.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<String> {
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
Expand Down Expand Up @@ -303,7 +323,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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);
}

Expand Down
1 change: 1 addition & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down