use futures::StreamExt;
use rig_core::message::Message;
use std::io::{self, Write};
use crate::{
agent::{Agent, MultiTurnStreamItem, PromptResponse, StreamToStdoutError},
streaming::{Item, StreamEvent},
};
pub struct ChatBot {
agent: Agent,
max_turns: usize,
show_usage: bool,
}
impl ChatBot {
pub fn new(agent: Agent) -> Self {
Self {
agent,
max_turns: 1,
show_usage: false,
}
}
pub fn max_turns(self, max_turns: usize) -> Self {
Self { max_turns, ..self }
}
pub fn show_usage(self) -> Self {
Self {
show_usage: true,
..self
}
}
pub async fn run(self) -> Result<(), StreamToStdoutError> {
self.run_on(|line| io::stdin().read_line(line)).await
}
async fn run_on(
self,
mut read_line: impl FnMut(&mut String) -> io::Result<usize>,
) -> Result<(), StreamToStdoutError> {
let mut stdout = io::stdout();
let mut history = vec![];
loop {
print!("> ");
stdout.flush()?;
let mut input = String::new();
match read_line(&mut input) {
Ok(0) => break,
Ok(_) => {
let input = input.trim();
if input == "exit" {
break;
}
tracing::info!("Prompt:\n{input}\n");
println!();
println!("========================== Response ============================");
let response = self.request(input, &history).await?;
println!("================================================================");
println!();
if self.show_usage {
let input_tokens = response.usage.input_tokens.unwrap_or(0);
let output_tokens = response.usage.output_tokens.unwrap_or(0);
println!("Input {input_tokens} tokens\nOutput {output_tokens} tokens");
}
history.extend(response.messages);
}
Err(e) => println!("Error reading request: {e}"),
}
}
Ok(())
}
async fn request(
&self,
prompt: &str,
history: &[Message],
) -> Result<PromptResponse, StreamToStdoutError> {
let mut response_stream = self
.agent
.prompt(prompt)
.history(history.to_vec())
.max_turns(self.max_turns)
.stream();
while let Some(chunk) = response_stream.next().await {
match chunk? {
MultiTurnStreamItem::StreamAssistantItem(Item::Event(StreamEvent::Text {
text,
..
})) => print!("{text}"),
MultiTurnStreamItem::FinalResponse(response) => {
println!();
return Ok(response);
}
_ => {}
}
}
Err(StreamToStdoutError::Incomplete)
}
}
#[cfg(test)]
mod tests;