use crate::error::LlmError;
use crate::params::anthropic::AnthropicParams;
use crate::request_factory::{RequestBuilder, RequestBuilderConfig};
use crate::types::{ChatMessage, ChatRequest, CommonParams, ProviderParams, Tool};
pub trait AnthropicParameterMapper {
fn map_common_to_anthropic(&self, params: &CommonParams) -> serde_json::Value;
fn merge_anthropic_params(
&self,
base: serde_json::Value,
anthropic_params: &AnthropicParams,
) -> serde_json::Value;
fn validate_anthropic_params(&self, params: &serde_json::Value) -> Result<(), LlmError>;
}
#[derive(Clone)]
pub struct AnthropicRequestBuilder {
common_params: CommonParams,
anthropic_params: AnthropicParams,
}
impl AnthropicParameterMapper for AnthropicRequestBuilder {
fn map_common_to_anthropic(&self, params: &CommonParams) -> serde_json::Value {
let mut json = serde_json::json!({
"model": params.model
});
if let Some(temp) = params.temperature {
json["temperature"] = temp.into();
}
if let Some(max_tokens) = params.max_tokens {
json["max_tokens"] = max_tokens.into();
} else {
json["max_tokens"] = 4096.into();
}
if let Some(top_p) = params.top_p {
json["top_p"] = top_p.into();
}
if let Some(stop) = ¶ms.stop_sequences {
json["stop_sequences"] = stop.clone().into();
}
json
}
fn merge_anthropic_params(
&self,
mut base: serde_json::Value,
anthropic_params: &AnthropicParams,
) -> serde_json::Value {
if let Ok(anthropic_json) = serde_json::to_value(anthropic_params)
&& let Some(anthropic_obj) = anthropic_json.as_object()
&& let Some(base_obj) = base.as_object_mut()
{
for (key, value) in anthropic_obj {
if !value.is_null() {
base_obj.insert(key.clone(), value.clone());
}
}
}
base
}
fn validate_anthropic_params(&self, params: &serde_json::Value) -> Result<(), LlmError> {
self.validate_anthropic_params_with_config(params, &RequestBuilderConfig::default())
}
}
impl AnthropicRequestBuilder {
pub fn new(common_params: CommonParams, anthropic_params: AnthropicParams) -> Self {
Self {
common_params,
anthropic_params,
}
}
fn create_provider_params(&self) -> ProviderParams {
ProviderParams::from_anthropic(self.anthropic_params.clone())
}
fn validate_anthropic_params_with_config(
&self,
params: &serde_json::Value,
config: &RequestBuilderConfig,
) -> Result<(), LlmError> {
if !config.provider_validation {
return Ok(()); }
if let Some(temp) = params.get("temperature").and_then(|v| v.as_f64())
&& !(0.0..=1.0).contains(&temp)
{
return Err(LlmError::InvalidParameter(
"Anthropic temperature must be between 0.0 and 1.0 per official API spec (validation can be disabled)".to_string(),
));
}
if let Some(top_p) = params.get("top_p").and_then(|v| v.as_f64())
&& !(0.0..=1.0).contains(&top_p)
{
return Err(LlmError::InvalidParameter(
"Anthropic top_p must be between 0.0 and 1.0 per official API spec (validation can be disabled)".to_string(),
));
}
if config.strict_validation
&& let Some(max_tokens) = params.get("max_tokens").and_then(|v| v.as_i64())
&& max_tokens <= 0
{
return Err(LlmError::InvalidParameter(
"Anthropic max_tokens must be positive per official API spec (strict validation enabled)".to_string(),
));
}
if let Some(thinking_budget) = params.get("thinking_budget").and_then(|v| v.as_i64())
&& thinking_budget < 1024
{
return Err(LlmError::InvalidParameter(
"Anthropic thinking budget must be at least 1024 tokens per official API spec (validation can be disabled)".to_string(),
));
}
if let Some(stop_sequences) = params.get("stop_sequences")
&& let Some(stop_array) = stop_sequences.as_array()
{
if config.strict_validation && stop_array.len() > 10 {
return Err(LlmError::InvalidParameter(
"Anthropic stop_sequences should be reasonable in number (strict validation enabled)".to_string(),
));
}
}
Ok(())
}
fn validate_anthropic_request(&self, request: &ChatRequest) -> Result<(), LlmError> {
let model = &request.common_params.model;
if model.is_empty() {
return Err(LlmError::InvalidParameter(
"Model name is required for Anthropic".to_string(),
));
}
if !model.starts_with("claude-") {
return Err(LlmError::InvalidParameter(
"Anthropic model names should start with 'claude-'".to_string(),
));
}
if request.common_params.max_tokens.is_none() {
}
if let Some(thinking_budget) = self.anthropic_params.thinking_budget {
if thinking_budget < 1024 {
return Err(LlmError::InvalidParameter(
"Anthropic thinking budget must be at least 1024 tokens".to_string(),
));
}
if thinking_budget > 60000 {
return Err(LlmError::InvalidParameter(
"Anthropic thinking budget cannot exceed 60000 tokens".to_string(),
));
}
}
if let Some(temp) = request.common_params.temperature
&& !(0.0..=1.0).contains(&temp)
{
return Err(LlmError::InvalidParameter(
"Anthropic temperature must be between 0.0 and 1.0".to_string(),
));
}
Ok(())
}
}
impl RequestBuilder for AnthropicRequestBuilder {
fn build_chat_request(
&self,
messages: Vec<ChatMessage>,
tools: Option<Vec<Tool>>,
stream: bool,
) -> Result<ChatRequest, LlmError> {
self.build_chat_request_with_config(
messages,
tools,
stream,
&RequestBuilderConfig::default(),
)
}
fn build_chat_request_with_config(
&self,
messages: Vec<ChatMessage>,
tools: Option<Vec<Tool>>,
stream: bool,
config: &RequestBuilderConfig,
) -> Result<ChatRequest, LlmError> {
let mut params_json = self.map_common_to_anthropic(&self.common_params);
params_json = self.merge_anthropic_params(params_json, &self.anthropic_params);
self.validate_anthropic_params_with_config(¶ms_json, config)?;
let request = ChatRequest {
messages,
tools,
common_params: self.common_params.clone(),
provider_params: Some(self.create_provider_params()),
http_config: None,
web_search: None,
stream,
};
if config.strict_validation {
self.validate_request(&request)?;
self.validate_anthropic_request(&request)?;
}
Ok(request)
}
fn get_common_params(&self) -> &CommonParams {
&self.common_params
}
fn get_provider_params(&self) -> Option<ProviderParams> {
Some(self.create_provider_params())
}
fn validate_request(&self, request: &ChatRequest) -> Result<(), LlmError> {
if request.messages.is_empty() {
return Err(LlmError::InvalidParameter(
"Messages cannot be empty".to_string(),
));
}
if request.common_params.model.is_empty() {
return Err(LlmError::InvalidParameter(
"Model must be specified".to_string(),
));
}
self.validate_anthropic_request(request)?;
Ok(())
}
}
pub fn create_anthropic_request_builder(
common_params: CommonParams,
anthropic_params: AnthropicParams,
) -> AnthropicRequestBuilder {
AnthropicRequestBuilder::new(common_params, anthropic_params)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{MessageContent, MessageRole};
#[test]
fn test_anthropic_request_builder() {
let common_params = CommonParams {
model: "claude-3-5-sonnet-20241022".to_string(),
temperature: Some(0.7),
max_tokens: Some(1000),
..Default::default()
};
let anthropic_params = AnthropicParams::default();
let builder = AnthropicRequestBuilder::new(common_params, anthropic_params);
let messages = vec![crate::types::ChatMessage {
role: MessageRole::User,
content: MessageContent::Text("Hello".to_string()),
metadata: Default::default(),
tool_calls: None,
tool_call_id: None,
}];
let request = builder
.build_chat_request(messages, None, false)
.expect("Should build request successfully");
assert_eq!(request.common_params.model, "claude-3-5-sonnet-20241022");
assert!(!request.stream);
assert!(request.provider_params.is_some());
}
#[test]
fn test_invalid_model_name() {
let common_params = CommonParams {
model: "gpt-4".to_string(), ..Default::default()
};
let anthropic_params = AnthropicParams::default();
let builder = AnthropicRequestBuilder::new(common_params, anthropic_params);
let messages = vec![crate::types::ChatMessage {
role: MessageRole::User,
content: MessageContent::Text("Hello".to_string()),
metadata: Default::default(),
tool_calls: None,
tool_call_id: None,
}];
let config = RequestBuilderConfig {
strict_validation: true,
provider_validation: true,
};
let result = builder.build_chat_request_with_config(messages, None, false, &config);
assert!(result.is_err());
assert!(
result
.unwrap_err()
.to_string()
.contains("should start with 'claude-'")
);
}
#[test]
fn test_thinking_budget_validation() {
let common_params = CommonParams {
model: "claude-3-5-sonnet-20241022".to_string(),
..Default::default()
};
let anthropic_params = AnthropicParams {
thinking_budget: Some(500), ..Default::default()
};
let builder = AnthropicRequestBuilder::new(common_params, anthropic_params);
let messages = vec![crate::types::ChatMessage {
role: MessageRole::User,
content: MessageContent::Text("Hello".to_string()),
metadata: Default::default(),
tool_calls: None,
tool_call_id: None,
}];
let config = RequestBuilderConfig {
strict_validation: true,
provider_validation: true,
};
let result = builder.build_chat_request_with_config(messages, None, false, &config);
assert!(result.is_err());
assert!(
result
.unwrap_err()
.to_string()
.contains("at least 1024 tokens")
);
}
#[test]
fn test_temperature_validation() {
let common_params = CommonParams {
model: "claude-3-5-sonnet-20241022".to_string(),
temperature: Some(1.5), ..Default::default()
};
let anthropic_params = AnthropicParams::default();
let builder = AnthropicRequestBuilder::new(common_params, anthropic_params);
let messages = vec![crate::types::ChatMessage {
role: MessageRole::User,
content: MessageContent::Text("Hello".to_string()),
metadata: Default::default(),
tool_calls: None,
tool_call_id: None,
}];
let config = RequestBuilderConfig {
strict_validation: true,
provider_validation: true,
};
let result = builder.build_chat_request_with_config(messages, None, false, &config);
assert!(result.is_err());
assert!(
result
.unwrap_err()
.to_string()
.contains("between 0.0 and 1.0")
);
}
}