-
Notifications
You must be signed in to change notification settings - Fork 581
feat: add interactive chat example for cactus crate #4878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| use std::io::{self, BufRead, Write}; | ||
|
|
||
| use cactus::{CompleteOptions, Message, Model}; | ||
|
|
||
| fn main() { | ||
| let path = std::env::args().nth(1).expect("Usage: chat <model-path>"); | ||
| let mut model = Model::new(&path).expect("Failed to load model"); | ||
|
|
||
| let options = CompleteOptions { | ||
| max_tokens: Some(1024), | ||
| temperature: Some(0.7), | ||
| confidence_threshold: Some(0.0), | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| let mut messages: Vec<Message> = vec![Message::system("You are a helpful assistant.")]; | ||
|
|
||
| println!("Chat with your model. Type 'exit' to quit.\n"); | ||
|
|
||
| loop { | ||
| print!("> "); | ||
| let _ = io::stdout().flush(); | ||
|
|
||
| let mut input = String::new(); | ||
| io::stdin().lock().read_line(&mut input).unwrap(); | ||
| let input = input.trim(); | ||
|
|
||
| if input.is_empty() || input == "exit" || input == "quit" { | ||
| break; | ||
| } | ||
|
|
||
| messages.push(Message::user(input)); | ||
| model.reset(); | ||
|
|
||
| let mut response_text = String::new(); | ||
| let result = model.complete_streaming(&messages, &options, |token| { | ||
| print!("{token}"); | ||
| let _ = io::stdout().flush(); | ||
| response_text.push_str(token); | ||
| true | ||
| }); | ||
|
|
||
| println!(); | ||
|
|
||
| match result { | ||
| Ok(_) => messages.push(Message::assistant(&response_text)), | ||
| Err(e) => eprintln!("Error: {e}"), | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Failed completion leaves orphaned user message in historyMedium Severity The user message is pushed into |
||
| } | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty input exits chat instead of reprompting
Medium Severity
The
input.is_empty()check in the break condition causes the chat loop to exit when the user simply presses Enter without typing anything. Most interactive CLI chat applications skip blank lines and re-display the prompt. This makes accidental exits too easy and contradicts the PR description which states only "exit" or "quit" trigger clean shutdown. Theis_empty()condition conflates EOF handling with blank-line handling — EOF fromread_line(returningOk(0)) could be detected via the return value instead.