mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-19 09:53:33 +00:00
refactor(llms): replace resolve_non_empty with per-provider key structs
Each provider declares its api_key and api_base sources with the Layered derive. Explicit values are still trimmed and blank values still count as unset, matching resolve_non_empty Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
771be58849
commit
612ab3f86b
13 changed files with 275 additions and 140 deletions
|
|
@ -17,22 +17,6 @@ impl Lookup for ProcessEnvironment {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn resolve_non_empty(
|
||||
value: Option<&str>,
|
||||
env_lookup: &dyn Fn(&str) -> Option<String>,
|
||||
names: &[&str],
|
||||
) -> Option<String> {
|
||||
value
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(str::to_string)
|
||||
.or_else(|| {
|
||||
names
|
||||
.iter()
|
||||
.find_map(|name| env_lookup(name).filter(|value| !value.trim().is_empty()))
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
|||
|
|
@ -1,33 +0,0 @@
|
|||
use litellm_core_utils::settings::resolve_non_empty;
|
||||
use rstest::rstest;
|
||||
|
||||
#[rstest]
|
||||
#[case::explicit_wins(Some(" explicit "), &["FIRST", "SECOND"], Some("explicit"))]
|
||||
#[case::absent_falls_back(None, &["FIRST", "SECOND"], Some(" first "))]
|
||||
#[case::blank_falls_back(Some(" \t "), &["BLANK", "SECOND"], Some("second"))]
|
||||
#[case::skips_missing_and_blank(None, &["MISSING", "BLANK", "SECOND"], Some("second"))]
|
||||
#[case::environment_order(None, &["SECOND", "FIRST"], Some("second"))]
|
||||
#[case::missing(None, &["MISSING", "BLANK"], None)]
|
||||
#[case::no_environment(None, &[], None)]
|
||||
fn resolves_explicit_value_then_first_nonblank_environment_value(
|
||||
#[case] value: Option<&str>,
|
||||
#[case] names: &[&str],
|
||||
#[case] expected: Option<&str>,
|
||||
) {
|
||||
let env = |name: &str| match name {
|
||||
"FIRST" => Some(" first ".to_string()),
|
||||
"SECOND" => Some("second".to_string()),
|
||||
"BLANK" => Some(" \t ".to_string()),
|
||||
_ => None,
|
||||
};
|
||||
assert_eq!(resolve_non_empty(value, &env, names).as_deref(), expected);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn explicit_value_does_not_read_the_environment() {
|
||||
let env = |_: &str| panic!("an explicit value must short-circuit environment lookup");
|
||||
assert_eq!(
|
||||
resolve_non_empty(Some("key"), &env, &["KEY"]).as_deref(),
|
||||
Some("key")
|
||||
);
|
||||
}
|
||||
|
|
@ -22,6 +22,13 @@ pub fn merge<P: Partial>(highest_precedence_first: impl IntoIterator<Item = P>)
|
|||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn non_blank(value: Option<&str>) -> Option<String> {
|
||||
value
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(str::to_owned)
|
||||
}
|
||||
|
||||
pub fn read_env<T>(
|
||||
env: &(impl Lookup + ?Sized),
|
||||
names: &'static [&'static str],
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
use crate::base_llm::messages::context::MessagesModelCapabilities;
|
||||
use litellm_auth::{CredentialPlacement, SecretValue};
|
||||
use litellm_core_utils::settings::resolve_non_empty;
|
||||
use litellm_http::request::{
|
||||
has_header, header_value, header_values, with_header, without_headers,
|
||||
};
|
||||
use litellm_layered::{Layered, non_blank, read};
|
||||
use litellm_llms_types::{
|
||||
formats::messages::{
|
||||
ContentBlock, ContentBlockType, EffortLevel, Message, MessageContent, MessagesTool,
|
||||
|
|
@ -85,16 +85,40 @@ impl<'a> OauthToken<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Python's `AnthropicModelInfo.get_api_key`: the param, else `ANTHROPIC_API_KEY`.
|
||||
#[derive(Layered)]
|
||||
#[expect(dead_code, reason = "only the derived partial is read")]
|
||||
struct AnthropicKeys {
|
||||
#[layer(litellm_param = "api_key", env = "ANTHROPIC_API_KEY", read = read::text, secret, setting = "API Key")]
|
||||
api_key: String,
|
||||
#[layer(env = "ANTHROPIC_AUTH_TOKEN", read = read::text, secret)]
|
||||
auth_token: String,
|
||||
#[layer(
|
||||
litellm_param = "api_base",
|
||||
env = ["ANTHROPIC_API_BASE", "ANTHROPIC_BASE_URL"],
|
||||
read = read::text,
|
||||
setting = "API Base"
|
||||
)]
|
||||
api_base: String,
|
||||
}
|
||||
|
||||
impl AnthropicKeysPartial {
|
||||
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
|
||||
Self::from_env(&env).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Python's `AnthropicModelInfo.get_api_key`: the param, else `ANTHROPIC_API_KEY`, stripped.
|
||||
pub fn get_api_key(
|
||||
api_key: Option<&str>,
|
||||
env_lookup: &dyn Fn(&str) -> Option<String>,
|
||||
) -> Option<String> {
|
||||
resolve_non_empty(api_key, env_lookup, &[ANTHROPIC_API_KEY_ENV])
|
||||
non_blank(api_key)
|
||||
.or(AnthropicKeysPartial::env(env_lookup).api_key)
|
||||
.map(|key| key.trim().to_owned())
|
||||
}
|
||||
|
||||
pub fn get_auth_token(env_lookup: &dyn Fn(&str) -> Option<String>) -> Option<String> {
|
||||
resolve_non_empty(None, env_lookup, &[ANTHROPIC_AUTH_TOKEN_ENV])
|
||||
AnthropicKeysPartial::env(env_lookup).auth_token
|
||||
}
|
||||
|
||||
/// Python's `AnthropicModelInfo.get_auth_header`, naming the credential instead of building
|
||||
|
|
@ -125,7 +149,7 @@ pub fn resolve_anthropic_api_key(
|
|||
) -> Result<String, litellm_auth::Error> {
|
||||
get_api_key(api_key, env_lookup).ok_or(litellm_auth::Error::MissingApiKey {
|
||||
provider: "Anthropic",
|
||||
environment_variable: ANTHROPIC_API_KEY_ENV,
|
||||
environment_variable: AnthropicKeys::API_KEY.env[0],
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -138,12 +162,9 @@ pub fn resolve_anthropic_api_base(
|
|||
api_base: Option<&str>,
|
||||
env_lookup: &dyn Fn(&str) -> Option<String>,
|
||||
) -> String {
|
||||
resolve_non_empty(
|
||||
api_base,
|
||||
env_lookup,
|
||||
&[ANTHROPIC_API_BASE_ENV, ANTHROPIC_BASE_URL_ENV],
|
||||
)
|
||||
.unwrap_or_else(|| API_BASE.to_string())
|
||||
non_blank(api_base)
|
||||
.or(AnthropicKeysPartial::env(env_lookup).api_base)
|
||||
.unwrap_or_else(|| API_BASE.to_string())
|
||||
}
|
||||
|
||||
pub fn complete_anthropic_url(
|
||||
|
|
|
|||
|
|
@ -1,25 +1,43 @@
|
|||
use crate::Error;
|
||||
use litellm_core_utils::settings::resolve_non_empty;
|
||||
use litellm_layered::{Layered, non_blank, read};
|
||||
|
||||
pub const AZURE_API_KEY_ENV: &str = "AZURE_API_KEY";
|
||||
pub const AZURE_API_BASE_ENV: &str = "AZURE_API_BASE";
|
||||
|
||||
#[derive(Layered)]
|
||||
#[expect(dead_code, reason = "only the derived partial is read")]
|
||||
struct AzureAiKeys {
|
||||
#[layer(litellm_param = "api_key", env = "AZURE_API_KEY", read = read::text, secret, setting = "API Key")]
|
||||
api_key: String,
|
||||
#[layer(litellm_param = "api_base", env = "AZURE_API_BASE", read = read::text, setting = "API Base")]
|
||||
api_base: String,
|
||||
}
|
||||
|
||||
impl AzureAiKeysPartial {
|
||||
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
|
||||
Self::from_env(&env).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolve_azure_api_key(
|
||||
api_key: Option<&str>,
|
||||
env_lookup: &dyn Fn(&str) -> Option<String>,
|
||||
) -> Result<String, Error> {
|
||||
resolve_non_empty(api_key, env_lookup, &[AZURE_API_KEY_ENV]).ok_or_else(|| {
|
||||
Error::from(litellm_auth::Error::MissingApiKey {
|
||||
provider: "Azure",
|
||||
environment_variable: AZURE_API_KEY_ENV,
|
||||
non_blank(api_key)
|
||||
.or(AzureAiKeysPartial::env(env_lookup).api_key)
|
||||
.ok_or_else(|| {
|
||||
Error::from(litellm_auth::Error::MissingApiKey {
|
||||
provider: "Azure",
|
||||
environment_variable: AzureAiKeys::API_KEY.env[0],
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub fn resolve_azure_api_base(
|
||||
api_base: Option<&str>,
|
||||
env_lookup: &dyn Fn(&str) -> Option<String>,
|
||||
) -> Result<String, Error> {
|
||||
resolve_non_empty(api_base, env_lookup, &[AZURE_API_BASE_ENV])
|
||||
non_blank(api_base)
|
||||
.or(AzureAiKeysPartial::env(env_lookup).api_base)
|
||||
.ok_or_else(|| Error::from(litellm_auth::Error::MissingApiBase { provider: "Azure", guidance: "Set `api_base` or the AZURE_API_BASE environment variable. Expected format: https://<resource-name>.services.ai.azure.com/anthropic" }))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,8 @@ use std::sync::LazyLock;
|
|||
|
||||
use litellm_auth::{CredentialPlacement, SecretValue};
|
||||
use litellm_auth_aws::{AwsCredentialSource, resolve_aws_region};
|
||||
use litellm_core_utils::settings::resolve_non_empty;
|
||||
use litellm_http::request::{header_value, with_header, without_headers};
|
||||
use litellm_layered::{Layered, read};
|
||||
use litellm_layered::{Layered, non_blank, read};
|
||||
use litellm_llms_types::{
|
||||
formats::messages::{MessagesOptionalParams, MessagesRequest},
|
||||
providers::anthropic::{BetaProvider, DEFAULT_HEADERS, MESSAGES_PATH},
|
||||
|
|
@ -26,14 +25,14 @@ use crate::{
|
|||
};
|
||||
|
||||
const SERVICE: &str = "aws-external-anthropic";
|
||||
const BASE_ENVS: &[&str] = &["ANTHROPIC_AWS_BASE_URL", "ANTHROPIC_AWS_API_BASE"];
|
||||
const WORKSPACE_ENVS: &[&str] = WorkspaceKeys::ENV_NAMES;
|
||||
const WORKSPACE_FIELDS: &[&str] = WorkspaceKeys::LITELLM_PARAMS;
|
||||
const BASE_ENVS: &[&str] = ClaudePlatformKeys::API_BASE.env;
|
||||
const WORKSPACE_ENVS: &[&str] = ClaudePlatformKeys::WORKSPACE_ID.env;
|
||||
const WORKSPACE_FIELDS: &[&str] = ClaudePlatformKeys::WORKSPACE_ID.litellm_params;
|
||||
const WORKSPACE_HEADER: &str = "anthropic-workspace-id";
|
||||
|
||||
#[derive(Layered)]
|
||||
#[expect(dead_code, reason = "only WorkspaceKeysPartial is built")]
|
||||
struct WorkspaceKeys {
|
||||
#[expect(dead_code, reason = "only the derived partial is read")]
|
||||
struct ClaudePlatformKeys {
|
||||
#[layer(
|
||||
litellm_param = [
|
||||
"workspace_id",
|
||||
|
|
@ -46,6 +45,22 @@ struct WorkspaceKeys {
|
|||
setting = "workspace_id"
|
||||
)]
|
||||
workspace_id: String,
|
||||
#[layer(litellm_param = "api_key", env = "ANTHROPIC_AWS_API_KEY", read = read::text, secret)]
|
||||
api_key: String,
|
||||
#[layer(
|
||||
litellm_param = "api_base",
|
||||
env = ["ANTHROPIC_AWS_BASE_URL", "ANTHROPIC_AWS_API_BASE"],
|
||||
read = read::text
|
||||
)]
|
||||
api_base: String,
|
||||
#[layer(env = "AWS_DEFAULT_REGION", read = read::text)]
|
||||
default_region: String,
|
||||
}
|
||||
|
||||
impl ClaudePlatformKeysPartial {
|
||||
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
|
||||
Self::from_env(&env).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
const UNSUPPORTED_OVERRIDE: &str = "claude_platform_unsupported_params";
|
||||
|
||||
|
|
@ -79,14 +94,15 @@ impl BaseMessagesConfig for BedrockClaudePlatformMessagesConfig {
|
|||
.or_else(|| workspace_value(params.extra.get(*field)))
|
||||
})
|
||||
});
|
||||
let workspace = resolve_non_empty(workspace, env, WORKSPACE_ENVS).ok_or_else(|| {
|
||||
let keys = ClaudePlatformKeysPartial::env(env);
|
||||
let workspace = non_blank(workspace).or(keys.workspace_id).ok_or_else(|| {
|
||||
Error::Auth(litellm_auth::Error::missing(
|
||||
"Claude Platform on AWS",
|
||||
WorkspaceKeys::WORKSPACE_ID,
|
||||
ClaudePlatformKeys::WORKSPACE_ID,
|
||||
))
|
||||
})?;
|
||||
let headers = with_header(headers, WORKSPACE_HEADER, workspace);
|
||||
let key = resolve_non_empty(api_key, env, &["ANTHROPIC_AWS_API_KEY"]);
|
||||
let key = non_blank(api_key).or(keys.api_key);
|
||||
let headers = without_headers(headers, &["authorization"]);
|
||||
if header_value(&headers, "x-api-key").is_some_and(|value| !value.trim().is_empty()) {
|
||||
return Ok(ValidatedEnvironment {
|
||||
|
|
@ -103,7 +119,7 @@ impl BaseMessagesConfig for BedrockClaudePlatformMessagesConfig {
|
|||
},
|
||||
});
|
||||
}
|
||||
let base = resolve_non_empty(params.api_base.as_deref(), env, BASE_ENVS);
|
||||
let base = non_blank(params.api_base.as_deref()).or(keys.api_base);
|
||||
Ok(ValidatedEnvironment {
|
||||
headers: without_headers(headers, &["x-api-key"]),
|
||||
auth: AuthScheme::AwsSigV4 {
|
||||
|
|
@ -122,13 +138,12 @@ impl BaseMessagesConfig for BedrockClaudePlatformMessagesConfig {
|
|||
_stream: bool,
|
||||
env: &dyn Fn(&str) -> Option<String>,
|
||||
) -> Result<String, Error> {
|
||||
let base = resolve_non_empty(
|
||||
let base = non_blank(
|
||||
api_base
|
||||
.filter(|base| !base.trim().is_empty())
|
||||
.or(params.api_base.as_deref()),
|
||||
env,
|
||||
BASE_ENVS,
|
||||
);
|
||||
)
|
||||
.or(ClaudePlatformKeysPartial::env(env).api_base);
|
||||
let base = match base {
|
||||
Some(base) => base,
|
||||
None => format!(
|
||||
|
|
@ -209,7 +224,7 @@ fn platform_region(
|
|||
});
|
||||
let region = host_region
|
||||
.or_else(|| resolve_aws_region(None, ¶ms.aws, env))
|
||||
.or_else(|| resolve_non_empty(None, env, &["AWS_DEFAULT_REGION"]))
|
||||
.or_else(|| ClaudePlatformKeysPartial::env(env).default_region)
|
||||
.ok_or_else(|| {
|
||||
Error::Auth(litellm_auth::Error::missing(
|
||||
"Claude Platform on AWS",
|
||||
|
|
@ -286,7 +301,7 @@ mod tests {
|
|||
.unwrap_err(),
|
||||
Error::Auth(litellm_auth::Error::missing(
|
||||
"Claude Platform on AWS",
|
||||
WorkspaceKeys::WORKSPACE_ID,
|
||||
ClaudePlatformKeys::WORKSPACE_ID,
|
||||
))
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ use std::sync::LazyLock;
|
|||
use litellm_auth::{CredentialPlacement, SecretValue};
|
||||
use litellm_auth_aws::AwsCredentialSource;
|
||||
use litellm_auth_aws::bedrock_model_id_and_region;
|
||||
use litellm_core_utils::settings::resolve_non_empty;
|
||||
use litellm_http::request::{with_header, without_headers};
|
||||
use litellm_layered::{Layered, non_blank, read};
|
||||
use litellm_llms_types::{
|
||||
formats::messages::{MessagesOptionalParams, MessagesRequest},
|
||||
providers::anthropic::{AnthropicBeta, BetaProvider, BetaSet, DEFAULT_HEADERS},
|
||||
|
|
@ -31,14 +31,34 @@ use crate::{
|
|||
bedrock::common_utils::validated_region,
|
||||
};
|
||||
|
||||
const API_BASE_ENV: &str = "BEDROCK_MANTLE_API_BASE";
|
||||
const REGION_ENVS: &[&str] = &[
|
||||
"BEDROCK_MANTLE_REGION",
|
||||
"AWS_REGION_NAME",
|
||||
"AWS_REGION",
|
||||
"AWS_DEFAULT_REGION",
|
||||
];
|
||||
const KEY_ENVS: &[&str] = &["BEDROCK_MANTLE_API_KEY", "AWS_BEARER_TOKEN_BEDROCK"];
|
||||
const API_BASE_ENV: &str = MantleKeys::API_BASE.env[0];
|
||||
const REGION_ENVS: &[&str] = MantleKeys::REGION.env;
|
||||
|
||||
#[derive(Layered)]
|
||||
#[expect(dead_code, reason = "only the derived partial is read")]
|
||||
struct MantleKeys {
|
||||
#[layer(
|
||||
litellm_param = "api_key",
|
||||
env = ["BEDROCK_MANTLE_API_KEY", "AWS_BEARER_TOKEN_BEDROCK"],
|
||||
read = read::text,
|
||||
secret,
|
||||
setting = "API Key"
|
||||
)]
|
||||
api_key: String,
|
||||
#[layer(litellm_param = "api_base", env = "BEDROCK_MANTLE_API_BASE", read = read::text, setting = "API Base")]
|
||||
api_base: String,
|
||||
#[layer(
|
||||
env = ["BEDROCK_MANTLE_REGION", "AWS_REGION_NAME", "AWS_REGION", "AWS_DEFAULT_REGION"],
|
||||
read = read::text
|
||||
)]
|
||||
region: String,
|
||||
}
|
||||
|
||||
impl MantleKeysPartial {
|
||||
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
|
||||
Self::from_env(&env).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
const MESSAGES_PATH: &str = "/anthropic/v1/messages";
|
||||
const BASE_SUFFIXES: &[&str] = &[
|
||||
MESSAGES_PATH,
|
||||
|
|
@ -78,7 +98,7 @@ impl BaseMessagesConfig for BedrockMantleAnthropicMessagesConfig {
|
|||
Some(project) => with_header(headers, "anthropic-workspace-id", project.to_string()),
|
||||
None => headers,
|
||||
};
|
||||
let auth = match resolve_non_empty(api_key, env, KEY_ENVS) {
|
||||
let auth = match non_blank(api_key).or(MantleKeysPartial::env(env).api_key) {
|
||||
Some(token) => AuthScheme::Credential {
|
||||
placement: CredentialPlacement::Bearer,
|
||||
secret: SecretValue::new(token),
|
||||
|
|
@ -242,7 +262,7 @@ fn mantle_base(
|
|||
.into_iter()
|
||||
.flatten()
|
||||
.find(|base| !base.trim().is_empty());
|
||||
resolve_non_empty(explicit, env, &[API_BASE_ENV])
|
||||
non_blank(explicit).or(MantleKeysPartial::env(env).api_base)
|
||||
}
|
||||
|
||||
fn mantle_region(
|
||||
|
|
@ -253,9 +273,9 @@ fn mantle_region(
|
|||
) -> Result<String, Error> {
|
||||
let region = mantle_base(base, params, env)
|
||||
.and_then(|base| mantle_host_region(&base))
|
||||
.or_else(|| resolve_non_empty(params.aws.aws_region_name.as_deref(), &|_| None, &[]))
|
||||
.or_else(|| non_blank(params.aws.aws_region_name.as_deref()))
|
||||
.or_else(|| mantle_model(model).1)
|
||||
.or_else(|| resolve_non_empty(None, env, REGION_ENVS))
|
||||
.or_else(|| MantleKeysPartial::env(env).region)
|
||||
.unwrap_or_else(|| "us-east-1".into());
|
||||
validated_region(region)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use litellm_auth::{CredentialPlacement, SecretValue};
|
||||
use litellm_core_utils::settings::resolve_non_empty;
|
||||
use litellm_layered::{Layered, non_blank, read};
|
||||
use litellm_llms_types::{
|
||||
formats::messages::{MessagesOptionalParams, MessagesRequest, MessagesTool},
|
||||
providers::anthropic::{DEFAULT_HEADERS, MESSAGES_PATH},
|
||||
|
|
@ -138,20 +138,37 @@ impl BaseMessagesConfig for DeepSeekAnthropicMessagesConfig {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Layered)]
|
||||
#[expect(dead_code, reason = "only the derived partial is read")]
|
||||
struct DeepSeekKeys {
|
||||
#[layer(litellm_param = "api_key", env = "DEEPSEEK_API_KEY", read = read::text, secret, setting = "API Key")]
|
||||
api_key: String,
|
||||
#[layer(
|
||||
litellm_param = "api_base",
|
||||
env = ["DEEPSEEK_ANTHROPIC_API_BASE", "DEEPSEEK_API_BASE"],
|
||||
read = read::text,
|
||||
setting = "API Base"
|
||||
)]
|
||||
api_base: String,
|
||||
}
|
||||
|
||||
impl DeepSeekKeysPartial {
|
||||
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
|
||||
Self::from_env(&env).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_api_key(
|
||||
api_key: Option<&str>,
|
||||
env_lookup: &dyn Fn(&str) -> Option<String>,
|
||||
) -> Option<String> {
|
||||
resolve_non_empty(api_key, env_lookup, &[DEEPSEEK_API_KEY_ENV])
|
||||
non_blank(api_key).or(DeepSeekKeysPartial::env(env_lookup).api_key)
|
||||
}
|
||||
|
||||
pub fn get_api_base(api_base: Option<&str>, env_lookup: &dyn Fn(&str) -> Option<String>) -> String {
|
||||
resolve_non_empty(
|
||||
api_base,
|
||||
env_lookup,
|
||||
&[DEEPSEEK_ANTHROPIC_API_BASE_ENV, DEEPSEEK_API_BASE_ENV],
|
||||
)
|
||||
.unwrap_or_else(|| DEFAULT_DEEPSEEK_ANTHROPIC_API_BASE.to_string())
|
||||
non_blank(api_base)
|
||||
.or(DeepSeekKeysPartial::env(env_lookup).api_base)
|
||||
.unwrap_or_else(|| DEFAULT_DEEPSEEK_ANTHROPIC_API_BASE.to_string())
|
||||
}
|
||||
|
||||
/// Python's `get_complete_url`: a base already ending in the Anthropic messages path is kept,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use litellm_auth::{CredentialPlacement, SecretValue};
|
||||
use litellm_core_utils::settings::resolve_non_empty;
|
||||
use litellm_layered::{Layered, non_blank, read};
|
||||
use litellm_llms_types::formats::messages::{
|
||||
MessagesOptionalParams, MessagesRequest, MessagesResponse,
|
||||
};
|
||||
|
|
@ -27,6 +27,21 @@ const API_KEY_ENV: &str = "EDENAI_API_KEY";
|
|||
const API_BASE_ENV: &str = "EDENAI_API_BASE";
|
||||
const DEFAULT_BASE: &str = "https://api.edenai.run/v3";
|
||||
|
||||
#[derive(Layered)]
|
||||
#[expect(dead_code, reason = "only the derived partial is read")]
|
||||
struct EdenAiKeys {
|
||||
#[layer(litellm_param = "api_key", env = "EDENAI_API_KEY", read = read::text, secret, setting = "API Key")]
|
||||
api_key: String,
|
||||
#[layer(litellm_param = "api_base", env = "EDENAI_API_BASE", read = read::text, setting = "API Base")]
|
||||
api_base: String,
|
||||
}
|
||||
|
||||
impl EdenAiKeysPartial {
|
||||
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
|
||||
Self::from_env(&env).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EdenAIAnthropicMessagesConfig;
|
||||
pub const EDENAI_MESSAGES_CONFIG: EdenAIAnthropicMessagesConfig = EdenAIAnthropicMessagesConfig;
|
||||
|
||||
|
|
@ -40,12 +55,12 @@ impl BaseMessagesConfig for EdenAIAnthropicMessagesConfig {
|
|||
_params: &LitellmParams,
|
||||
env: &dyn Fn(&str) -> Option<String>,
|
||||
) -> Result<ValidatedEnvironment, Error> {
|
||||
let key = resolve_non_empty(api_key, env, &[API_KEY_ENV]).ok_or(Error::Auth(
|
||||
litellm_auth::Error::MissingApiKey {
|
||||
let key = non_blank(api_key)
|
||||
.or(EdenAiKeysPartial::env(env).api_key)
|
||||
.ok_or(Error::Auth(litellm_auth::Error::MissingApiKey {
|
||||
provider: "Eden AI",
|
||||
environment_variable: API_KEY_ENV,
|
||||
},
|
||||
))?;
|
||||
environment_variable: EdenAiKeys::API_KEY.env[0],
|
||||
}))?;
|
||||
if has_anthropic_credential(&headers) {
|
||||
return Ok(ValidatedEnvironment {
|
||||
headers,
|
||||
|
|
@ -69,7 +84,8 @@ impl BaseMessagesConfig for EdenAIAnthropicMessagesConfig {
|
|||
_stream: bool,
|
||||
env: &dyn Fn(&str) -> Option<String>,
|
||||
) -> Result<String, Error> {
|
||||
let base = resolve_non_empty(api_base, env, &[API_BASE_ENV])
|
||||
let base = non_blank(api_base)
|
||||
.or(EdenAiKeysPartial::env(env).api_base)
|
||||
.unwrap_or_else(|| DEFAULT_BASE.into());
|
||||
Ok(complete_messages_url(&base))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use litellm_auth::{CredentialPlacement, SecretValue};
|
||||
use litellm_core_utils::settings::resolve_non_empty;
|
||||
use litellm_layered::{Layered, non_blank, read};
|
||||
use litellm_llms_types::{
|
||||
formats::messages::{MessagesOptionalParams, MessagesRequest},
|
||||
providers::anthropic::{DEFAULT_HEADERS, MESSAGES_PATH},
|
||||
|
|
@ -25,6 +25,21 @@ const API_KEY_ENV: &str = "MINIMAX_API_KEY";
|
|||
const API_BASE_ENV: &str = "MINIMAX_API_BASE";
|
||||
const DEFAULT_BASE: &str = "https://api.minimax.io/anthropic";
|
||||
|
||||
#[derive(Layered)]
|
||||
#[expect(dead_code, reason = "only the derived partial is read")]
|
||||
struct MinimaxKeys {
|
||||
#[layer(litellm_param = "api_key", env = "MINIMAX_API_KEY", read = read::text, secret, setting = "API Key")]
|
||||
api_key: String,
|
||||
#[layer(litellm_param = "api_base", env = "MINIMAX_API_BASE", read = read::text, setting = "API Base")]
|
||||
api_base: String,
|
||||
}
|
||||
|
||||
impl MinimaxKeysPartial {
|
||||
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
|
||||
Self::from_env(&env).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MinimaxMessagesConfig;
|
||||
pub const MINIMAX_MESSAGES_CONFIG: MinimaxMessagesConfig = MinimaxMessagesConfig;
|
||||
|
||||
|
|
@ -52,12 +67,12 @@ impl BaseMessagesConfig for MinimaxMessagesConfig {
|
|||
auth: AuthScheme::Forwarded,
|
||||
});
|
||||
}
|
||||
let key = resolve_non_empty(api_key, env, &[API_KEY_ENV]).ok_or(Error::Auth(
|
||||
litellm_auth::Error::MissingApiKey {
|
||||
let key = non_blank(api_key)
|
||||
.or(MinimaxKeysPartial::env(env).api_key)
|
||||
.ok_or(Error::Auth(litellm_auth::Error::MissingApiKey {
|
||||
provider: "MiniMax",
|
||||
environment_variable: API_KEY_ENV,
|
||||
},
|
||||
))?;
|
||||
environment_variable: MinimaxKeys::API_KEY.env[0],
|
||||
}))?;
|
||||
Ok(ValidatedEnvironment {
|
||||
headers,
|
||||
auth: AuthScheme::Credential {
|
||||
|
|
@ -75,7 +90,8 @@ impl BaseMessagesConfig for MinimaxMessagesConfig {
|
|||
_stream: bool,
|
||||
env: &dyn Fn(&str) -> Option<String>,
|
||||
) -> Result<String, Error> {
|
||||
let base = resolve_non_empty(api_base, env, &[API_BASE_ENV])
|
||||
let base = non_blank(api_base)
|
||||
.or(MinimaxKeysPartial::env(env).api_base)
|
||||
.unwrap_or_else(|| DEFAULT_BASE.into());
|
||||
let base = base.trim_end_matches('/');
|
||||
if base.ends_with(MESSAGES_PATH) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use litellm_auth::{CredentialPlacement, SecretValue};
|
||||
use litellm_core_utils::settings::resolve_non_empty;
|
||||
use litellm_http::request::{has_bearer_auth, without_headers};
|
||||
use litellm_layered::{Layered, non_blank, read};
|
||||
use litellm_llms_types::{
|
||||
formats::messages::{MessagesOptionalParams, MessagesRequest},
|
||||
providers::anthropic::DEFAULT_HEADERS,
|
||||
|
|
@ -22,6 +22,21 @@ use crate::{
|
|||
const API_KEY_ENV: &str = "OPENROUTER_API_KEY";
|
||||
const API_BASE_ENV: &str = "OPENROUTER_API_BASE";
|
||||
const DEFAULT_BASE: &str = "https://openrouter.ai/api/v1";
|
||||
|
||||
#[derive(Layered)]
|
||||
#[expect(dead_code, reason = "only the derived partial is read")]
|
||||
struct OpenRouterKeys {
|
||||
#[layer(litellm_param = "api_key", env = "OPENROUTER_API_KEY", read = read::text, secret, setting = "API Key")]
|
||||
api_key: String,
|
||||
#[layer(litellm_param = "api_base", env = "OPENROUTER_API_BASE", read = read::text, setting = "API Base")]
|
||||
api_base: String,
|
||||
}
|
||||
|
||||
impl OpenRouterKeysPartial {
|
||||
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
|
||||
Self::from_env(&env).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
const MESSAGES_PATH: &str = "/v1/messages";
|
||||
|
||||
pub struct OpenRouterAnthropicMessagesConfig;
|
||||
|
|
@ -45,12 +60,12 @@ impl BaseMessagesConfig for OpenRouterAnthropicMessagesConfig {
|
|||
auth: AuthScheme::Forwarded,
|
||||
});
|
||||
}
|
||||
let key = resolve_non_empty(api_key, env, &[API_KEY_ENV]).ok_or(Error::Auth(
|
||||
litellm_auth::Error::MissingApiKey {
|
||||
let key = non_blank(api_key)
|
||||
.or(OpenRouterKeysPartial::env(env).api_key)
|
||||
.ok_or(Error::Auth(litellm_auth::Error::MissingApiKey {
|
||||
provider: "OpenRouter",
|
||||
environment_variable: API_KEY_ENV,
|
||||
},
|
||||
))?;
|
||||
environment_variable: OpenRouterKeys::API_KEY.env[0],
|
||||
}))?;
|
||||
Ok(ValidatedEnvironment {
|
||||
headers: without_headers(headers, &["authorization"]),
|
||||
auth: AuthScheme::Credential {
|
||||
|
|
@ -71,7 +86,8 @@ impl BaseMessagesConfig for OpenRouterAnthropicMessagesConfig {
|
|||
let explicit = api_base
|
||||
.filter(|base| !base.trim().is_empty())
|
||||
.or(params.api_base.as_deref());
|
||||
let base = resolve_non_empty(explicit, env, &[API_BASE_ENV])
|
||||
let base = non_blank(explicit)
|
||||
.or(OpenRouterKeysPartial::env(env).api_base)
|
||||
.unwrap_or_else(|| DEFAULT_BASE.into());
|
||||
let trimmed = base.trim_end_matches('/');
|
||||
if trimmed.ends_with(MESSAGES_PATH) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use litellm_auth::{CredentialPlacement, SecretValue};
|
||||
use litellm_core_utils::settings::resolve_non_empty;
|
||||
use litellm_layered::{Layered, non_blank, read};
|
||||
use litellm_llms_types::formats::responses::ResponsesApiResponse;
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
|
|
@ -15,6 +15,27 @@ use crate::{
|
|||
const API_KEY_ENVS: [&str; 2] = ["OPENROUTER_API_KEY", "OR_API_KEY"];
|
||||
const API_BASE_ENV: &str = "OPENROUTER_API_BASE";
|
||||
const DEFAULT_BASE: &str = "https://openrouter.ai/api/v1";
|
||||
|
||||
#[derive(Layered)]
|
||||
#[expect(dead_code, reason = "only the derived partial is read")]
|
||||
struct OpenRouterKeys {
|
||||
#[layer(
|
||||
litellm_param = "api_key",
|
||||
env = ["OPENROUTER_API_KEY", "OR_API_KEY"],
|
||||
read = read::text,
|
||||
secret,
|
||||
setting = "API Key"
|
||||
)]
|
||||
api_key: String,
|
||||
#[layer(litellm_param = "api_base", env = "OPENROUTER_API_BASE", read = read::text, setting = "API Base")]
|
||||
api_base: String,
|
||||
}
|
||||
|
||||
impl OpenRouterKeysPartial {
|
||||
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
|
||||
Self::from_env(&env).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
const RESPONSES_PATH: &str = "/responses";
|
||||
|
||||
pub struct OpenRouterResponsesApiConfig;
|
||||
|
|
@ -43,12 +64,12 @@ impl BaseResponsesApiConfig for OpenRouterResponsesApiConfig {
|
|||
api_key: Option<&str>,
|
||||
lookup: &dyn Fn(&str) -> Option<String>,
|
||||
) -> Result<ValidatedEnvironment, Error> {
|
||||
let key = resolve_non_empty(api_key, lookup, &API_KEY_ENVS).ok_or(
|
||||
litellm_auth::Error::MissingApiKey {
|
||||
let key = non_blank(api_key)
|
||||
.or(OpenRouterKeysPartial::env(lookup).api_key)
|
||||
.ok_or(litellm_auth::Error::MissingApiKey {
|
||||
provider: "OpenRouter",
|
||||
environment_variable: API_KEY_ENVS[0],
|
||||
},
|
||||
)?;
|
||||
environment_variable: OpenRouterKeys::API_KEY.env[0],
|
||||
})?;
|
||||
Ok(ValidatedEnvironment {
|
||||
headers: litellm_http::request::with_default_headers(
|
||||
headers,
|
||||
|
|
@ -66,7 +87,8 @@ impl BaseResponsesApiConfig for OpenRouterResponsesApiConfig {
|
|||
api_base: Option<&str>,
|
||||
lookup: &dyn Fn(&str) -> Option<String>,
|
||||
) -> String {
|
||||
let base = resolve_non_empty(api_base, lookup, &[API_BASE_ENV])
|
||||
let base = non_blank(api_base)
|
||||
.or(OpenRouterKeysPartial::env(lookup).api_base)
|
||||
.unwrap_or_else(|| DEFAULT_BASE.into());
|
||||
format!("{}{RESPONSES_PATH}", base.trim_end_matches('/'))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use litellm_auth::{CredentialPlacement, SecretValue};
|
||||
use litellm_core_utils::settings::resolve_non_empty;
|
||||
use litellm_layered::{Layered, non_blank, read};
|
||||
use litellm_llms_types::{
|
||||
formats::messages::{MessagesOptionalParams, MessagesRequest},
|
||||
providers::anthropic::{DEFAULT_HEADERS, MESSAGES_PATH},
|
||||
|
|
@ -26,6 +26,21 @@ const API_BASE_ENV: &str = "TENCENT_API_BASE";
|
|||
const ANTHROPIC_API_BASE_ENV: &str = "TENCENT_ANTHROPIC_API_BASE";
|
||||
const DEFAULT_BASE: &str = "https://tokenhub-intl.tencentcloudmaas.com";
|
||||
|
||||
#[derive(Layered)]
|
||||
#[expect(dead_code, reason = "only the derived partial is read")]
|
||||
struct TencentKeys {
|
||||
#[layer(litellm_param = "api_key", env = "TENCENT_API_KEY", read = read::text, secret, setting = "API Key")]
|
||||
api_key: String,
|
||||
#[layer(litellm_param = "api_base", env = ["TENCENT_ANTHROPIC_API_BASE", "TENCENT_API_BASE"], read = read::text, setting = "API Base")]
|
||||
api_base: String,
|
||||
}
|
||||
|
||||
impl TencentKeysPartial {
|
||||
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
|
||||
Self::from_env(&env).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TencentAnthropicMessagesConfig;
|
||||
pub const TENCENT_MESSAGES_CONFIG: TencentAnthropicMessagesConfig = TencentAnthropicMessagesConfig;
|
||||
|
||||
|
|
@ -53,12 +68,12 @@ impl BaseMessagesConfig for TencentAnthropicMessagesConfig {
|
|||
auth: AuthScheme::Forwarded,
|
||||
});
|
||||
}
|
||||
let key = resolve_non_empty(api_key, env, &[API_KEY_ENV]).ok_or(Error::Auth(
|
||||
litellm_auth::Error::MissingApiKey {
|
||||
let key = non_blank(api_key)
|
||||
.or(TencentKeysPartial::env(env).api_key)
|
||||
.ok_or(Error::Auth(litellm_auth::Error::MissingApiKey {
|
||||
provider: "Tencent",
|
||||
environment_variable: API_KEY_ENV,
|
||||
},
|
||||
))?;
|
||||
environment_variable: TencentKeys::API_KEY.env[0],
|
||||
}))?;
|
||||
Ok(ValidatedEnvironment {
|
||||
headers,
|
||||
auth: AuthScheme::Credential {
|
||||
|
|
@ -76,7 +91,8 @@ impl BaseMessagesConfig for TencentAnthropicMessagesConfig {
|
|||
_stream: bool,
|
||||
env: &dyn Fn(&str) -> Option<String>,
|
||||
) -> Result<String, Error> {
|
||||
let base = resolve_non_empty(api_base, env, &[ANTHROPIC_API_BASE_ENV, API_BASE_ENV])
|
||||
let base = non_blank(api_base)
|
||||
.or(TencentKeysPartial::env(env).api_base)
|
||||
.unwrap_or_else(|| DEFAULT_BASE.into());
|
||||
let base = base.trim_end_matches('/');
|
||||
if base.ends_with(MESSAGES_PATH) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue