From 669f2c602c4631db1c91fd7a27098b7685027f9a Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 17 Aug 2024 16:01:39 +0800 Subject: feat: enable custom `api_base` for most clients (#793) --- src/client/claude.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/client/claude.rs') diff --git a/src/client/claude.rs b/src/client/claude.rs index 8811476..7b472a7 100644 --- a/src/client/claude.rs +++ b/src/client/claude.rs @@ -5,12 +5,13 @@ use reqwest::RequestBuilder; use serde::Deserialize; use serde_json::{json, Value}; -const API_BASE: &str = "https://api.anthropic.com/v1/messages"; +const API_BASE: &str = "https://api.anthropic.com/v1"; #[derive(Debug, Clone, Deserialize)] pub struct ClaudeConfig { pub name: Option, pub api_key: Option, + pub api_base: Option, #[serde(default)] pub models: Vec, pub patch: Option, @@ -19,6 +20,7 @@ pub struct ClaudeConfig { impl ClaudeClient { config_get_fn!(api_key, get_api_key); + config_get_fn!(api_base, get_api_base); pub const PROMPTS: [PromptAction<'static>; 1] = [("api_key", "API Key:", true, PromptKind::String)]; @@ -40,10 +42,14 @@ fn prepare_chat_completions( data: ChatCompletionsData, ) -> Result { let api_key = self_.get_api_key().ok(); + let api_base = self_ + .get_api_base() + .unwrap_or_else(|_| API_BASE.to_string()); + let url = format!("{}/messages", api_base.trim_end_matches('/')); let body = claude_build_chat_completions_body(data, &self_.model)?; - let mut request_data = RequestData::new(API_BASE, body); + let mut request_data = RequestData::new(url, body); request_data.header("anthropic-version", "2023-06-01"); if let Some(api_key) = api_key { -- cgit v1.2.3