use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize)]
pub struct NonEmptyString(String);
impl NonEmptyString {
pub fn new(s: String) -> Option<Self> {
if s.is_empty() {
None
} else {
Some(Self(s))
}
}
pub fn inner(&self) -> &str {
&self.0
}
pub fn into_inner(self) -> String {
self.0
}
}
#[derive(Serialize)]
pub(crate) struct LogProbabilitiesRequest {
pub(crate) context: String,
pub(crate) continuation: NonEmptyString,
}
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Deserialize)]
pub struct LogProbabilities {
logprob: f64,
is_greedy: bool,
total_tokens: usize,
}
impl LogProbabilities {
pub const fn log_probability(&self) -> f64 {
self.logprob
}
pub const fn is_greedy(&self) -> bool {
self.is_greedy
}
pub const fn total_tokens(&self) -> usize {
self.total_tokens
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils;
#[test]
fn test_non_empty_string_new() {
let empty = String::new();
let non_empty = String::from("textsynth");
assert!(NonEmptyString::new(empty).is_none());
assert!(NonEmptyString::new(non_empty).is_some());
}
#[test]
fn test_non_empty_string_inner() {
let s = String::from("textsynth");
let non_empty = NonEmptyString::new(s).unwrap();
assert_eq!(non_empty.inner(), "textsynth");
}
#[test]
fn test_non_empty_string_into_inner() {
let s = String::from("textsynth");
let non_empty = NonEmptyString::new(s).unwrap();
assert_eq!(non_empty.into_inner(), "textsynth");
}
#[test]
fn test_log_probabilities_log_probability() {
let _ = test_utils::cache::log_probabilities().log_probability();
}
#[test]
fn test_log_probabilities_is_greedy() {
let _ = test_utils::cache::log_probabilities().is_greedy();
}
#[test]
fn test_log_probabilities_total_tokens() {
let _ = test_utils::cache::log_probabilities().total_tokens();
}
}