Skip to content

Commit

Permalink
Add chatbot crate
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Aug 26, 2024
1 parent 8e00b43 commit 9e7813d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions crates/chatbot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
30 changes: 30 additions & 0 deletions crates/chatbot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use rand::{rngs::SmallRng, Rng, SeedableRng};
use std::{cell::RefCell, time::Duration};

thread_local! {
static RNG: RefCell<SmallRng> = 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<String> {
tokio::time::sleep(Duration::from_secs(2)).await;
vec![
"And how does that make you feel?".to_string(),
"Interesting! Go on...".to_string(),
]
}
1 change: 1 addition & 0 deletions crates/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
miniserve = { path = "../miniserve" }
chatbot = { path = "../chatbot" }
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.121"
tokio = { workspace = true, features = ["full"] }

0 comments on commit 9e7813d

Please sign in to comment.