Skip to content

Commit

Permalink
Solution for blocking chatbot
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Jul 19, 2024
1 parent bab0ac3 commit dc539ae
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions crates/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::sync::Arc;

use anyhow::Result;
use axum::{
response::Html,
routing::{get, post},
Json, Router,
};
use serde::{Deserialize, Serialize};
use tokio::join;

async fn root() -> Html<&'static str> {
Html(include_str!("../index.html"))
Expand All @@ -15,11 +18,15 @@ struct Chat {
messages: Vec<String>,
}

async fn chat(Json(mut chat): Json<Chat>) -> Json<Chat> {
let mut responses = chatbot::query_chat(&chat.messages);
async fn chat(Json(chat): Json<Chat>) -> Json<Chat> {
let chat_arc = Arc::new(chat);
let chat_arc_ref = Arc::clone(&chat_arc);
let responses_fut = tokio::spawn(async move { chatbot::query_chat(&chat_arc_ref.messages) });
let random_fut = chatbot::gen_random_number();
let random = random_fut.await;
let (responses, random) = join!(responses_fut, random_fut);
let mut responses = responses.unwrap();
let response = responses.remove(random % responses.len());
let mut chat = Arc::into_inner(chat_arc).unwrap();
chat.messages.push(response);
Json(chat)
}
Expand Down

0 comments on commit dc539ae

Please sign in to comment.