Skip to content

Commit

Permalink
Add new Chatbot data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Aug 21, 2024
1 parent 10cbedc commit c98da53
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions crates/chatbot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,29 @@ pub async fn gen_random_number() -> usize {
RNG.with(|rng| rng.borrow_mut().gen())
}

/// Generates a list of possible responses given the current chat.
///
/// Warning: may take a few seconds!
pub async fn query_chat(messages: &[String]) -> Vec<String> {
std::thread::sleep(Duration::from_secs(2));
let most_recent = messages.last().unwrap();
vec![
format!("\"{most_recent}\"? And how does that make you feel?"),
format!("\"{most_recent}\"! Interesting! Go on..."),
]
/// A chatbot that responds to inputs.
pub struct Chatbot {
emoji: String,
}

impl Chatbot {
/// Creates a new chatbot that uses the provided emoji in its responses.
pub fn new(emoji: String) -> Self {
Chatbot { emoji }
}

/// Generates a list of possible responses given the current chat.
///
/// Warning: may take a few seconds!
pub async fn query_chat(&self, messages: &[String]) -> Vec<String> {
std::thread::sleep(Duration::from_secs(2));
let most_recent = messages.last().unwrap();
vec![
format!(
"\"{most_recent}\"? And how does that make you feel? {}",
self.emoji
),
format!("\"{most_recent}\"! Interesting! Go on... {}", self.emoji),
]
}
}

0 comments on commit c98da53

Please sign in to comment.