CLI Chatbot Utility
Overview
A utility function that creates an interactive REPL-style chatbot from any type implementing the Chat
trait. Manages chat history, I/O, and basic command handling.
Usage
use rig::{cli_chatbot, providers::openai};
let agent = openai.agent("gpt-4")
.preamble("You are a helpful assistant.")
.build();
cli_chatbot(agent).await?;
Features
- Interactive REPL interface with
>
prompt - Maintains chat history for context
- Simple “exit” command
- Error handling for I/O and chat operations
- Tracing support for debugging
Implementation
Reference:
pub async fn cli_chatbot(chatbot: impl Chat) -> Result<(), PromptError> {
let stdin = io::stdin();
let mut stdout = io::stdout();
let mut chat_log = vec![];
println!("Welcome to the chatbot! Type 'exit' to quit.");
loop {
print!("> ");
// Flush stdout to ensure the prompt appears before input
stdout.flush().unwrap();
let mut input = String::new();
match stdin.read_line(&mut input) {
Ok(_) => {
// Remove the newline character from the input
let input = input.trim();
// Check for a command to exit
if input == "exit" {
break;
}
tracing::info!("Prompt:\n{}\n", input);
let response = chatbot.chat(input, chat_log.clone()).await?;
chat_log.push(Message {
role: "user".into(),
content: input.into(),
});
chat_log.push(Message {
role: "assistant".into(),
content: response.clone(),
});
println!("========================== Response ============================");
println!("{response}");
println!("================================================================\n\n");
tracing::info!("Response:\n{}\n", response);
}
Err(error) => println!("Error reading input: {}", error),
}
}
Ok(())
}
Chat History Management
Automatically tracks conversation with Message
objects:
Message {
role: "user"|"assistant",
content: String
}
Examples
Used in calculator chatbot:
cli_chatbot(calculator_rag).await?;
And multi-agent systems:
// Spin up a chatbot using the agent
cli_chatbot(translator).await?;
Error Handling
- Returns
Result<(), PromptError>
- Handles I/O errors gracefully
- Propagates chat implementation errors