llm_chain/
summarization.rsuse crate::{
chains::map_reduce::{self, MapReduceChainError},
frame::FormatAndExecuteError,
parameters, prompt,
step::Step,
traits,
};
pub struct TextSummarizer {
chain: map_reduce::Chain,
}
impl Default for TextSummarizer {
fn default() -> Self {
let map_prompt = Step::for_prompt_template(prompt!(
"You are a text summarizer. You will be given a text and you will have to summarize it",
"Text:\n\n{{text}}\n\nPlease write a summary of the text above. Respond only with the summary."
));
let reduce_prompt = Step::for_prompt_template(prompt!(
"You are a text summarizer. You will be given a text and you will have to summarize it",
"Text:\n\n{{text}}\n\nPlease write a combined summary of the segment summaries above. Respond only with the summary."
));
TextSummarizer {
chain: map_reduce::Chain::new(map_prompt, reduce_prompt),
}
}
}
#[derive(thiserror::Error, Debug)]
pub enum TextSummarizerError {
#[error("MapReduceChainError: {0}")]
MapReduceChainError(#[from] MapReduceChainError),
#[error("No output was produced")]
NoOutput,
}
impl TextSummarizer {
pub async fn summarize_text<E: traits::Executor>(
&self,
exec: &E,
text: &str,
) -> Result<String, TextSummarizerError> {
let params = parameters! {
"text" => text,
};
let chain_output = self.chain.run(vec![params], parameters!(), exec).await?;
chain_output
.to_immediate()
.await
.map_err(|err| {
TextSummarizerError::MapReduceChainError(
MapReduceChainError::FormatAndExecuteError(FormatAndExecuteError::Execute(err)),
)
})?
.primary_textual_output()
.ok_or(TextSummarizerError::NoOutput)
}
}
pub async fn summarize_text<E: traits::Executor>(
exec: &E,
text: &str,
) -> Result<String, TextSummarizerError> {
TextSummarizer::default().summarize_text(exec, text).await
}