use std::borrow::Cow;
use serde::{Deserialize, Serialize};
use crate::{
generation::{
images::Image,
parameters::{FormatType, KeepAlive, ThinkType},
},
models::ModelOptions,
};
use super::GenerationContext;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenerationRequest<'a> {
#[serde(rename = "model")]
pub model_name: String,
pub prompt: Cow<'a, str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub suffix: Option<Cow<'a, str>>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub images: Vec<Image>,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<ModelOptions>,
#[serde(skip_serializing_if = "Option::is_none")]
pub system: Option<Cow<'a, str>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub template: Option<Cow<'a, str>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub raw: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub context: Option<GenerationContext>,
#[serde(skip_serializing_if = "Option::is_none")]
pub format: Option<FormatType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub keep_alive: Option<KeepAlive>,
#[serde(skip_serializing_if = "Option::is_none")]
pub think: Option<ThinkType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub logprobs: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub top_logprobs: Option<u32>,
}
impl<'a> GenerationRequest<'a> {
pub fn new(model_name: String, prompt: impl Into<Cow<'a, str>>) -> Self {
Self {
model_name,
prompt: prompt.into(),
suffix: None,
images: Vec::new(),
options: None,
system: None,
template: None,
raw: None,
context: None,
format: None,
keep_alive: None,
think: None,
logprobs: None,
top_logprobs: None,
}
}
pub fn new_with_suffix(model_name: String, prompt: String, suffix: String) -> Self {
let out = Self::new(model_name, prompt);
out.suffix(suffix)
}
pub fn suffix(mut self, suffix: impl Into<Cow<'a, str>>) -> Self {
self.suffix = Some(suffix.into());
self
}
pub fn images(mut self, images: Vec<Image>) -> Self {
self.images = images;
self
}
pub fn add_image(mut self, image: Image) -> Self {
self.images.push(image);
self
}
pub fn options(mut self, options: ModelOptions) -> Self {
self.options = Some(options);
self
}
pub fn system(mut self, system: impl Into<Cow<'a, str>>) -> Self {
self.system = Some(system.into());
self
}
pub fn template(mut self, template: impl Into<Cow<'a, str>>) -> Self {
self.template = Some(template.into());
self
}
pub fn raw(mut self, raw: bool) -> Self {
self.raw = Some(raw);
self
}
pub fn context(mut self, context: GenerationContext) -> Self {
self.context = Some(context);
self
}
pub fn format(mut self, format: FormatType) -> Self {
self.format = Some(format);
self
}
pub fn keep_alive(mut self, keep_alive: KeepAlive) -> Self {
self.keep_alive = Some(keep_alive);
self
}
pub fn think(mut self, think: impl Into<ThinkType>) -> Self {
self.think = Some(think.into());
self
}
pub fn logprobs(mut self, logprobs: bool) -> Self {
self.logprobs = Some(logprobs);
self
}
pub fn top_logprobs(mut self, top_logprobs: u32) -> Self {
self.top_logprobs = Some(top_logprobs);
self
}
}