Skip to content

Commit

Permalink
Add support for retrieval-augmented generation
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Sep 4, 2024
1 parent 4f0ce5b commit 819ffe2
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions crates/chatbot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rand::{rngs::SmallRng, Rng, SeedableRng};
use std::{cell::RefCell, time::Duration};
use std::{cell::RefCell, path::PathBuf, time::Duration};

thread_local! {
static RNG: RefCell<SmallRng> = RefCell::new(SmallRng::from_entropy());
Expand Down Expand Up @@ -33,17 +33,26 @@ impl Chatbot {
}
}

pub fn retrieval_documents(&self, _messages: &[String]) -> Vec<PathBuf> {
vec![
PathBuf::from("data/doc1.txt"),
PathBuf::from("data/doc2.txt"),
]
}

/// Generates a list of possible responses given the current chat.
///
/// Warning: may take a few seconds!
pub async fn query_chat(&mut self, messages: &[String]) -> Vec<String> {
pub async fn query_chat(&mut self, messages: &[String], docs: &[String]) -> Vec<String> {
std::thread::sleep(Duration::from_secs(2));
let most_recent = messages.last().unwrap();
let emoji = &self.emojis[self.emoji_counter];
self.emoji_counter = (self.emoji_counter + 1) % self.emojis.len();
vec![
format!("\"{most_recent}\"? And how does that make you feel? {emoji}",),
format!("\"{most_recent}\"! Interesting! Go on... {emoji}"),
format!("Have you considered: {}", docs.first().unwrap()),
format!("I might recommend: {}", docs.last().unwrap()),
]
}
}

0 comments on commit 819ffe2

Please sign in to comment.