Skip to content

Commit e009a1a

Browse files
committed
Add chatbot crate
1 parent d86685d commit e009a1a

4 files changed

Lines changed: 126 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 87 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/chatbot/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "chatbot"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
rand = { version = "0.8.5", features = ["small_rng"] }
8+
tokio = { workspace = true, features = ["time"] }

crates/chatbot/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use rand::{rngs::SmallRng, Rng, SeedableRng};
2+
use std::{cell::RefCell, time::Duration};
3+
4+
thread_local! {
5+
static RNG: RefCell<SmallRng> = RefCell::new(SmallRng::from_entropy());
6+
}
7+
8+
/// Seeds the thread-local RNG used by [`gen_random_number`].
9+
pub fn seed_rng(seed: u64) {
10+
RNG.with(|rng| *rng.borrow_mut() = SmallRng::seed_from_u64(seed));
11+
}
12+
13+
/// Generates a random `usize`.
14+
///
15+
/// Warning: may take a few seconds!
16+
pub async fn gen_random_number() -> usize {
17+
tokio::time::sleep(Duration::from_secs(2)).await;
18+
RNG.with(|rng| rng.borrow_mut().gen())
19+
}
20+
21+
/// Generates a list of possible responses given the current chat.
22+
///
23+
/// Warning: may take a few seconds!
24+
pub async fn query_chat(_messages: &[String]) -> Vec<String> {
25+
tokio::time::sleep(Duration::from_secs(2)).await;
26+
vec![
27+
"And how does that make you feel?".to_string(),
28+
"Interesting! Go on...".to_string(),
29+
]
30+
}

crates/server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
miniserve = { path = "../miniserve" }
8+
chatbot = { path = "../chatbot" }
89
serde = { version = "1.0.204", features = ["derive"] }
910
serde_json = "1.0.121"
1011
tokio = { workspace = true, features = ["full"] }

0 commit comments

Comments
 (0)