use super::{
deformalize, formalize, summarize, to_topic, Statement, SummarizationConfig, SummarizationMode,
};
#[derive(Debug, Clone)]
pub struct DialogTurn {
pub role: String,
pub text: String,
}
impl DialogTurn {
#[must_use]
pub fn new(role: impl Into<String>, text: impl Into<String>) -> Self {
Self {
role: role.into(),
text: text.into(),
}
}
#[must_use]
pub fn user(text: impl Into<String>) -> Self {
Self::new("user", text)
}
#[must_use]
pub fn assistant(text: impl Into<String>) -> Self {
Self::new("assistant", text)
}
}
#[must_use]
pub fn formalize_dialog(turns: &[DialogTurn]) -> Vec<Statement> {
let mut out = Vec::new();
for turn in turns {
let bias: i16 = match turn.role.as_str() {
"user" => 20,
"assistant" => -10,
_ => 0,
};
for mut stmt in formalize(&turn.text) {
let bumped = i16::from(stmt.weight).saturating_add(bias).clamp(0, 100);
stmt.weight = u8::try_from(bumped).unwrap_or(0);
out.push(stmt);
}
}
out
}
#[must_use]
pub fn summarize_dialog(turns: &[DialogTurn], config: &SummarizationConfig) -> String {
let statements = formalize_dialog(turns);
if config.mode == SummarizationMode::Topic {
let highest = statements.iter().max_by_key(|s| s.weight);
return highest
.map(|s| to_topic("", std::slice::from_ref(s)))
.unwrap_or_default();
}
if statements.is_empty() {
return String::new();
}
let summarized = summarize(&statements, config);
deformalize(&summarized)
}
#[must_use]
pub fn generate_chat_title(turns: &[DialogTurn], language: &str) -> String {
let config = SummarizationConfig::default()
.with_mode(SummarizationMode::Topic)
.with_language(language);
summarize_dialog(turns, &config)
}