From 7cc04794ca23c0faf01716fecf71a0948869cbfa Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Tue, 30 Jul 2024 16:39:01 -0700 Subject: [PATCH] Add chatbot crate --- crates/chatbot/Cargo.toml | 8 ++++++++ crates/chatbot/src/lib.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 crates/chatbot/Cargo.toml create mode 100644 crates/chatbot/src/lib.rs diff --git a/crates/chatbot/Cargo.toml b/crates/chatbot/Cargo.toml new file mode 100644 index 0000000..5b0f90d --- /dev/null +++ b/crates/chatbot/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "chatbot" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = { version = "0.8.5", features = ["small_rng"] } +tokio = { workspace = true, features = ["time"] } diff --git a/crates/chatbot/src/lib.rs b/crates/chatbot/src/lib.rs new file mode 100644 index 0000000..b3ab021 --- /dev/null +++ b/crates/chatbot/src/lib.rs @@ -0,0 +1,30 @@ +use rand::{rngs::SmallRng, Rng, SeedableRng}; +use std::{cell::RefCell, time::Duration}; + +thread_local! { + static RNG: RefCell = RefCell::new(SmallRng::from_entropy()); +} + +/// Seeds the thread-local RNG used by [`gen_random_number`]. +pub fn seed_rng(seed: u64) { + RNG.with(|rng| *rng.borrow_mut() = SmallRng::seed_from_u64(seed)); +} + +/// Generates a random `usize`. +/// +/// Warning: may take a few seconds! +pub async fn gen_random_number() -> usize { + tokio::time::sleep(Duration::from_secs(2)).await; + 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 { + tokio::time::sleep(Duration::from_secs(2)).await; + vec![ + "And how does that make you feel?".to_string(), + "Interesting! Go on...".to_string(), + ] +}