use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Prompt {
pub instruction: String,
pub input: String,
pub max_output_tokens: u32,
pub temperature: f32,
}
impl Prompt {
pub fn new(instruction: impl Into<String>, input: impl Into<String>) -> Self {
Self {
instruction: instruction.into(),
input: input.into(),
max_output_tokens: 512,
temperature: 0.0,
}
}
pub fn with_max_output_tokens(mut self, max_output_tokens: u32) -> Self {
self.max_output_tokens = max_output_tokens;
self
}
pub fn with_temperature(mut self, temperature: f32) -> Self {
self.temperature = temperature.clamp(0.0, 2.0);
self
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Usage {
pub input_tokens: u32,
pub output_tokens: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Completion {
pub text: String,
pub usage: Usage,
pub model: String,
pub truncated: bool,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_default_prompt_is_deterministic() {
let prompt = Prompt::new("Summarise", "text");
assert_eq!(prompt.temperature, 0.0);
assert_eq!(prompt.max_output_tokens, 512);
}
#[test]
fn temperature_is_clamped_rather_than_trusted() {
assert_eq!(Prompt::new("x", "y").with_temperature(9.0).temperature, 2.0);
assert_eq!(
Prompt::new("x", "y").with_temperature(-1.0).temperature,
0.0
);
}
}