generated from cognitive-engineering-lab/rqst-async
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ovstinga/02-join-a
Add chatbot crate
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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; | ||
let most_recent = messages.last().unwrap(); | ||
vec![ | ||
format!("\"{most_recent}\"? And how does that make you feel?"), | ||
format!("\"{most_recent}\"! Interesting! Go on..."), | ||
] | ||
} |