From c98da537f56cfbaef73e89c5e11b7391dc3f83ae Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Mon, 12 Aug 2024 15:09:17 -0700 Subject: [PATCH] Add new Chatbot data structure --- crates/chatbot/src/lib.rs | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/crates/chatbot/src/lib.rs b/crates/chatbot/src/lib.rs index 8415b7f..7ec2afc 100644 --- a/crates/chatbot/src/lib.rs +++ b/crates/chatbot/src/lib.rs @@ -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 { - 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 { + 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), + ] + } }