use smart_default::SmartDefault as Default;
use crate::Message;
use crate::prelude::*;
#[derive(Debug, Default)]
pub struct ChatPrompt
{
pub messages: Vec<Message>,
#[default(Format::Text)]
pub format: Format,
}
impl ChatPrompt
{
pub fn new() -> Self
{
Self {
messages: Default::default(),
format: Format::Text,
}
}
pub fn message(mut self, role: Role, content: impl Into<String>) -> Self
{
self.messages.push(Message {
role,
content: content.into(),
});
self
}
pub fn message_opt(mut self, role: Role, content: Option<String>) -> Self
{
if let Some(content) = content
{
self.messages.push(Message { role, content });
}
self
}
pub fn user(self, content: impl Into<String>) -> Self
{
self.message(Role::User, content)
}
pub fn system(self, content: impl Into<String>) -> Self
{
self.message(Role::System, content)
}
pub fn system_opt(self, content: Option<String>) -> Self
{
self.message_opt(Role::System, content)
}
pub fn assistant(self, content: impl Into<String>) -> Self
{
self.message(Role::Assistant, content)
}
pub fn assistant_opt(self, content: Option<String>) -> Self
{
self.message_opt(Role::Assistant, content)
}
pub fn format(mut self, format: impl Into<Format>) -> Self
{
self.format = format.into();
self
}
}
#[derive(Debug)]
pub struct GenerationPrompt
{
pub user: String,
pub system: Option<String>,
pub assistant: Option<String>,
pub format: Format,
#[cfg(feature = "image")]
pub image: Option<kproc_values::Image>,
}
impl GenerationPrompt
{
pub fn prompt(user: impl Into<String>) -> Self
{
Self {
user: user.into(),
system: Default::default(),
assistant: Default::default(),
format: Format::Text,
#[cfg(feature = "image")]
image: None,
}
}
pub fn system(mut self, content: impl Into<String>) -> Self
{
self.system = Some(content.into());
self
}
pub fn assistant(mut self, content: impl Into<String>) -> Self
{
self.assistant = Some(content.into());
self
}
pub fn format(mut self, format: impl Into<Format>) -> Self
{
self.format = format.into();
self
}
#[cfg(feature = "image")]
pub fn image(mut self, image: impl Into<kproc_values::Image>) -> Self
{
self.image = Some(image.into());
self
}
}
impl From<GenerationPrompt> for Vec<Message>
{
fn from(value: GenerationPrompt) -> Self
{
let mut vec = Self::default();
if let Some(system) = value.system
{
vec.push(Message {
role: Role::System,
content: system,
});
}
if let Some(assistant) = value.assistant
{
vec.push(Message {
role: Role::Assistant,
content: assistant,
});
}
vec.push(Message {
role: Role::User,
content: value.user,
});
vec
}
}