use crate::error::RsGuardError;
use crate::llm::{
deepseek::DeepSeekClient, kimi::KimiClient, openai::OpenAiClient, openrouter::OpenRouterClient,
providers, qwen::QwenClient, Provider, ProviderConfig,
};
pub fn create_provider(
provider_name: &str,
api_key: &str,
config: &ProviderConfig,
) -> Result<Provider, RsGuardError> {
match provider_name {
"deepseek" => {
let mut client = DeepSeekClient::new(api_key)?;
if let Some(ref url) = config.base_url {
client = client.with_base_url(url.clone());
}
client = client
.with_model(config.model.clone())
.with_max_tokens(config.max_tokens);
Ok(Box::new(client))
}
"kimi" => {
let mut client = KimiClient::new(api_key)?;
if let Some(ref url) = config.base_url {
client = client.with_base_url(url.clone());
}
client = client
.with_model(config.model.clone())
.with_max_tokens(config.max_tokens);
Ok(Box::new(client))
}
"qwen" => {
let mut client = QwenClient::new(api_key)?;
if let Some(ref url) = config.base_url {
client = client.with_base_url(url.clone());
}
client = client
.with_model(config.model.clone())
.with_max_tokens(config.max_tokens);
Ok(Box::new(client))
}
"openrouter" => {
let mut client = OpenRouterClient::new(api_key)?;
if let Some(ref url) = config.base_url {
client = client.with_base_url(url.clone());
}
if let Some(ref referer) = config.http_referer {
client = client.with_http_referer(referer, api_key)?;
}
client = client
.with_model(config.model.clone())
.with_max_tokens(config.max_tokens);
Ok(Box::new(client))
}
"openai" => {
let mut client = OpenAiClient::new(api_key)?;
if let Some(ref url) = config.base_url {
client = client.with_base_url(url.clone());
}
client = client
.with_model(config.model.clone())
.with_max_tokens(config.max_tokens);
Ok(Box::new(client))
}
other => {
let names = providers::known_provider_names().join(", ");
Err(RsGuardError::Config(format!(
"Unknown provider: '{}'. Supported: {}",
other, names
)))
}
}
}