use crate::raw::{ChatCompletionRequest, Message, ResponseFormat, ResponseFormatType, Tool};
#[derive(Debug)]
pub struct ApiRequest {
raw: ChatCompletionRequest,
}
impl ApiRequest {
pub fn builder() -> Self {
Self {
raw: ChatCompletionRequest::default(),
}
}
pub fn with_model(mut self, name: impl Into<String>) -> Self {
self.raw.model = crate::raw::Model::Custom(name.into());
self
}
pub fn deepseek_chat(messages: Vec<Message>) -> Self {
let mut r = Self::builder();
r.raw.messages = messages;
r.raw.model = crate::raw::Model::DeepseekChat;
r
}
pub fn deepseek_reasoner(messages: Vec<Message>) -> Self {
let mut r = Self::builder();
r.raw.messages = messages;
r.raw.model = crate::raw::Model::DeepseekReasoner;
r
}
pub fn add_message(mut self, msg: Message) -> Self {
self.raw.messages.push(msg);
self
}
pub fn messages(mut self, msgs: Vec<Message>) -> Self {
self.raw.messages = msgs;
self
}
pub fn json(mut self) -> Self {
self.raw.response_format = Some(ResponseFormat {
r#type: ResponseFormatType::JsonObject,
});
self
}
pub fn text(mut self) -> Self {
self.raw.response_format = Some(ResponseFormat {
r#type: ResponseFormatType::Text,
});
self
}
pub fn temperature(mut self, t: f32) -> Self {
self.raw.temperature = Some(t);
self
}
pub fn max_tokens(mut self, n: u32) -> Self {
self.raw.max_tokens = Some(n);
self
}
pub fn add_tool(mut self, tool: Tool) -> Self {
if let Some(ref mut v) = self.raw.tools {
v.push(tool);
} else {
self.raw.tools = Some(vec![tool]);
}
self
}
pub fn tool_choice_auto(mut self) -> Self {
use crate::raw::request::tool_choice::{ToolChoice, ToolChoiceType};
self.raw.tool_choice = Some(ToolChoice::String(ToolChoiceType::Auto));
self
}
pub fn stream(mut self, enabled: bool) -> Self {
self.raw.stream = Some(enabled);
self
}
pub fn extra_body(mut self, map: serde_json::Map<String, serde_json::Value>) -> Self {
self.raw.extra_body = Some(map);
self
}
pub fn add_extra_field(&mut self, key: impl Into<String>, value: serde_json::Value) {
if let Some(ref mut m) = self.raw.extra_body {
m.insert(key.into(), value);
} else {
let mut m = serde_json::Map::new();
m.insert(key.into(), value);
self.raw.extra_body = Some(m);
}
}
pub fn with_extra_field(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
self.add_extra_field(key, value);
self
}
pub fn extra_field(self, key: impl Into<String>, value: serde_json::Value) -> Self {
self.with_extra_field(key, value)
}
pub(crate) fn into_raw(self) -> ChatCompletionRequest {
self.raw
}
}