Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Aug 13, 2024
1 parent 1a5c007 commit 5fb3024
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions crates/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::sync::{Arc, LazyLock};
use miniserve::{http::StatusCode, Content, Request, Response};
use serde::{Deserialize, Serialize};
use tokio::{
join,
fs, join,
sync::{mpsc, oneshot},
task::JoinSet,
};

async fn index(_req: Request) -> Response {
Expand All @@ -17,19 +18,31 @@ struct Messages {
messages: Vec<String>,
}

async fn query_chat(messages: &Arc<Vec<String>>) -> Vec<String> {
type Payload = (Arc<Vec<String>>, oneshot::Sender<Vec<String>>);
static SENDER: LazyLock<mpsc::Sender<Payload>> = LazyLock::new(|| {
let (tx, mut rx) = mpsc::channel::<Payload>(1024);
tokio::spawn(async move {
let mut chatbot = chatbot::Chatbot::new(vec![":-)".into(), "^^".into()]);
while let Some((messages, responder)) = rx.recv().await {
let response = chatbot.query_chat(&messages).await;
responder.send(response).unwrap();
type Payload = (Arc<Vec<String>>, oneshot::Sender<Vec<String>>);

fn chatbot_thread() -> mpsc::Sender<Payload> {
let (tx, mut rx) = mpsc::channel::<Payload>(1024);
tokio::spawn(async move {
let mut chatbot = chatbot::Chatbot::new(vec![":-)".into(), "^^".into()]);
while let Some((messages, responder)) = rx.recv().await {
let doc_paths = chatbot.retrieval_documents(&messages);
let mut doc_futs = doc_paths
.into_iter()
.map(fs::read_to_string)
.collect::<JoinSet<_>>();
let mut docs = Vec::new();
while let Some(result) = doc_futs.join_next().await {
docs.push(result.unwrap().unwrap());
}
});
tx
let response = chatbot.query_chat(&messages, &docs).await;
responder.send(response).unwrap();
}
});
tx
}

async fn query_chat(messages: &Arc<Vec<String>>) -> Vec<String> {
static SENDER: LazyLock<mpsc::Sender<Payload>> = LazyLock::new(chatbot_thread);

let (tx, rx) = oneshot::channel();
SENDER.send((Arc::clone(messages), tx)).await.unwrap();
Expand Down

0 comments on commit 5fb3024

Please sign in to comment.