Skip to main content

rig_agent/integrations/
cli_chatbot.rs

1//! A terminal chat loop over an agent.
2//!
3//! ```no_run
4//! use rig_agent::{Agent, agent::StreamToStdoutError, integrations::cli_chatbot::ChatBot};
5//! async fn chat(agent: Agent) -> Result<(), StreamToStdoutError> {
6//!     ChatBot::new(agent).max_turns(3).run().await
7//! }
8//! ```
9
10use 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
19/// A terminal chat loop over an [`Agent`] that streams each answer as it is
20/// produced; see [`ChatBot::run`].
21pub struct ChatBot {
22    agent: Agent,
23    max_turns: usize,
24    show_usage: bool,
25}
26
27impl ChatBot {
28    /// A chatbot over `agent` with a budget of one model call per prompt.
29    pub fn new(agent: Agent) -> Self {
30        Self {
31            agent,
32            max_turns: 1,
33            show_usage: false,
34        }
35    }
36
37    /// Set the total model-call budget for each prompt, including the initial
38    /// call and every retry or continuation. Zero emits no model calls.
39    pub fn max_turns(self, max_turns: usize) -> Self {
40        Self { max_turns, ..self }
41    }
42
43    /// Print the token usage after every answer.
44    pub fn show_usage(self) -> Self {
45        Self {
46            show_usage: true,
47            ..self
48        }
49    }
50
51    /// Read prompts from stdin and print answers until `exit` or end of input.
52    /// Returns prompt failures, stdout flush errors, and
53    /// [`StreamToStdoutError::Incomplete`] when a stream ends without a final
54    /// response; input read errors are printed.
55    pub async fn run(self) -> Result<(), StreamToStdoutError> {
56        self.run_on(|line| io::stdin().read_line(line)).await
57    }
58
59    /// [`Self::run`] reading each prompt with `read_line`, which appends one
60    /// line to its buffer. Stdin is locked only while a line is read.
61    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    /// Stream one answer's text to stdout and return the run's response.
106    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;