#![allow(unused_imports, dead_code, non_camel_case_types, unused_variables, clippy::all)]
use super::super::context::{LoadContext, SaveContext};
use super::anthropic_tool_definition::AnthropicToolDefinition;
use super::anthropic_wire_message::AnthropicWireMessage;
#[derive(Debug, Clone, Default)]
pub struct AnthropicMessagesRequest {
pub model: String,
pub messages: Vec<AnthropicWireMessage>,
pub max_tokens: i32,
pub system: Option<String>,
pub temperature: Option<f32>,
pub top_p: Option<f32>,
pub top_k: Option<i32>,
pub stop_sequences: Option<Vec<String>>,
pub tools: Vec<AnthropicToolDefinition>,
}
impl AnthropicMessagesRequest {
pub fn new() -> Self {
Self::default()
}
pub fn from_json(json: &str, ctx: &LoadContext) -> Result<Self, serde_json::Error> {
let value: serde_json::Value = serde_json::from_str(json)?;
Ok(Self::load_from_value(&value, ctx))
}
pub fn from_yaml(yaml: &str, ctx: &LoadContext) -> Result<Self, serde_yaml::Error> {
let value: serde_json::Value = serde_yaml::from_str(yaml)?;
Ok(Self::load_from_value(&value, ctx))
}
pub fn load_from_value(value: &serde_json::Value, ctx: &LoadContext) -> Self {
let value = ctx.process_input(value.clone());
Self {
model: value.get("model").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
messages: value.get("messages").map(|v| Self::load_messages(v, ctx)).unwrap_or_default(),
max_tokens: value.get("max_tokens").and_then(|v| v.as_i64()).unwrap_or(0) as i32,
system: value.get("system").and_then(|v| v.as_str()).map(|s| s.to_string()),
temperature: value.get("temperature").and_then(|v| v.as_f64()).map(|v| v as f32),
top_p: value.get("top_p").and_then(|v| v.as_f64()).map(|v| v as f32),
top_k: value.get("top_k").and_then(|v| v.as_i64()).map(|v| v as i32),
stop_sequences: value.get("stop_sequences").and_then(|v| v.as_array()).map(|arr| arr.iter().filter_map(|v| v.as_str().map(|s| s.to_string())).collect()),
tools: value.get("tools").map(|v| Self::load_tools(v, ctx)).unwrap_or_default(),
}
}
pub fn to_value(&self, ctx: &SaveContext) -> serde_json::Value {
let mut result = serde_json::Map::new();
if !self.model.is_empty() {
result.insert("model".to_string(), serde_json::Value::String(self.model.clone()));
}
if !self.messages.is_empty() {
result.insert("messages".to_string(), Self::save_messages(&self.messages, ctx));
}
if self.max_tokens != 0 {
result.insert("max_tokens".to_string(), serde_json::Value::Number(serde_json::Number::from(self.max_tokens)));
}
if let Some(ref val) = self.system {
result.insert("system".to_string(), serde_json::Value::String(val.clone()));
}
if let Some(val) = self.temperature {
result.insert("temperature".to_string(), serde_json::Number::from_f64(val as f64).map(serde_json::Value::Number).unwrap_or(serde_json::Value::Null));
}
if let Some(val) = self.top_p {
result.insert("top_p".to_string(), serde_json::Number::from_f64(val as f64).map(serde_json::Value::Number).unwrap_or(serde_json::Value::Null));
}
if let Some(val) = self.top_k {
result.insert("top_k".to_string(), serde_json::Value::Number(serde_json::Number::from(val)));
}
if let Some(ref items) = self.stop_sequences {
result.insert("stop_sequences".to_string(), serde_json::to_value(items).unwrap_or(serde_json::Value::Null));
}
if !self.tools.is_empty() {
result.insert("tools".to_string(), Self::save_tools(&self.tools, ctx));
}
ctx.process_dict(serde_json::Value::Object(result))
}
pub fn to_json(&self, ctx: &SaveContext) -> Result<String, serde_json::Error> {
serde_json::to_string_pretty(&self.to_value(ctx))
}
pub fn to_yaml(&self, ctx: &SaveContext) -> Result<String, serde_yaml::Error> {
serde_yaml::to_string(&self.to_value(ctx))
}
fn load_messages(data: &serde_json::Value, ctx: &LoadContext) -> Vec<AnthropicWireMessage> {
match data {
serde_json::Value::Array(arr) => {
arr.iter().map(|v| AnthropicWireMessage::load_from_value(v, ctx)).collect()
}
_ => Vec::new(),
}
}
fn save_messages(items: &[AnthropicWireMessage], ctx: &SaveContext) -> serde_json::Value {
serde_json::Value::Array(items.iter().map(|item| item.to_value(ctx)).collect::<Vec<_>>())
}
fn load_tools(data: &serde_json::Value, ctx: &LoadContext) -> Vec<AnthropicToolDefinition> {
match data {
serde_json::Value::Array(arr) => {
arr.iter().map(|v| AnthropicToolDefinition::load_from_value(v, ctx)).collect()
}
serde_json::Value::Object(obj) => {
obj.iter()
.filter_map(|(name, value)| {
if value.is_array() {
return None;
}
let mut v = if value.is_object() {
value.clone()
} else {
serde_json::json!({ "description": value })
};
if let serde_json::Value::Object(ref mut m) = v {
m.entry("name".to_string()).or_insert_with(|| serde_json::Value::String(name.clone()));
}
Some(AnthropicToolDefinition::load_from_value(&v, ctx))
})
.collect()
}
_ => Vec::new(),
}
}
fn save_tools(items: &[AnthropicToolDefinition], ctx: &SaveContext) -> serde_json::Value {
if ctx.collection_format == "array" {
return serde_json::Value::Array(items.iter().map(|item| item.to_value(ctx)).collect::<Vec<_>>());
}
let mut result = serde_json::Map::new();
for item in items {
let mut item_data = match item.to_value(ctx) {
serde_json::Value::Object(m) => m,
other => { let mut m = serde_json::Map::new(); m.insert("value".to_string(), other); m },
};
if let Some(serde_json::Value::String(name)) = item_data.remove("name") {
result.insert(name, serde_json::Value::Object(item_data));
}
}
serde_json::Value::Object(result)
}
}