rig_agent/integrations/
cli_chatbot.rs1use futures::StreamExt;
11use rig_core::message::Message;
12use std::io::{self, Write};
13
14use crate::{
15 agent::{Agent, MultiTurnStreamItem, PromptResponse, StreamToStdoutError},
16 streaming::{Item, StreamEvent},
17};
18
19pub struct ChatBot {
22 agent: Agent,
23 max_turns: usize,
24 show_usage: bool,
25}
26
27impl ChatBot {
28 pub fn new(agent: Agent) -> Self {
30 Self {
31 agent,
32 max_turns: 1,
33 show_usage: false,
34 }
35 }
36
37 pub fn max_turns(self, max_turns: usize) -> Self {
40 Self { max_turns, ..self }
41 }
42
43 pub fn show_usage(self) -> Self {
45 Self {
46 show_usage: true,
47 ..self
48 }
49 }
50
51 pub async fn run(self) -> Result<(), StreamToStdoutError> {
56 self.run_on(|line| io::stdin().read_line(line)).await
57 }
58
59 async fn run_on(
62 self,
63 mut read_line: impl FnMut(&mut String) -> io::Result<usize>,
64 ) -> Result<(), StreamToStdoutError> {
65 let mut stdout = io::stdout();
66 let mut history = vec![];
67
68 loop {
69 print!("> ");
70 stdout.flush()?;
71
72 let mut input = String::new();
73 match read_line(&mut input) {
74 Ok(0) => break,
75 Ok(_) => {
76 let input = input.trim();
77 if input == "exit" {
78 break;
79 }
80
81 tracing::info!("Prompt:\n{input}\n");
82
83 println!();
84 println!("========================== Response ============================");
85
86 let response = self.request(input, &history).await?;
87
88 println!("================================================================");
89 println!();
90
91 if self.show_usage {
92 let input_tokens = response.usage.input_tokens.unwrap_or(0);
93 let output_tokens = response.usage.output_tokens.unwrap_or(0);
94 println!("Input {input_tokens} tokens\nOutput {output_tokens} tokens");
95 }
96 history.extend(response.messages);
97 }
98 Err(e) => println!("Error reading request: {e}"),
99 }
100 }
101
102 Ok(())
103 }
104
105 async fn request(
107 &self,
108 prompt: &str,
109 history: &[Message],
110 ) -> Result<PromptResponse, StreamToStdoutError> {
111 let mut response_stream = self
112 .agent
113 .prompt(prompt)
114 .history(history.to_vec())
115 .max_turns(self.max_turns)
116 .stream();
117
118 while let Some(chunk) = response_stream.next().await {
119 match chunk? {
120 MultiTurnStreamItem::StreamAssistantItem(Item::Event(StreamEvent::Text {
121 text,
122 ..
123 })) => print!("{text}"),
124 MultiTurnStreamItem::FinalResponse(response) => {
125 println!();
126 return Ok(response);
127 }
128 _ => {}
129 }
130 }
131 Err(StreamToStdoutError::Incomplete)
132 }
133}
134
135#[cfg(test)]
136mod tests;