Skip to content

Commit

Permalink
Spawn query_chat
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Aug 26, 2024
1 parent ffda1f7 commit a6d0a85
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions crates/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use miniserve::{http::StatusCode, Content, Request, Response};
use serde::{Deserialize, Serialize};
use tokio::join;
Expand All @@ -16,19 +18,23 @@ async fn chat(req: Request) -> Response {
let Request::Post(body) = req else {
return Err(StatusCode::METHOD_NOT_ALLOWED);
};
let Ok(mut messages) = serde_json::from_str::<Messages>(&body) else {
let Ok(mut data) = serde_json::from_str::<Messages>(&body) else {
return Err(StatusCode::INTERNAL_SERVER_ERROR);
};

let (i, mut responses) = join!(
let messages = Arc::new(data.messages);
let messages_ref = Arc::clone(&messages);
let (i, responses) = join!(
chatbot::gen_random_number(),
chatbot::query_chat(&messages.messages)
tokio::spawn(async move { chatbot::query_chat(&messages_ref).await })
);
let mut responses = responses.unwrap();

let response = responses.remove(i % responses.len());
messages.messages.push(response);
data.messages = Arc::into_inner(messages).unwrap();
data.messages.push(response);

Ok(Content::Json(serde_json::to_string(&messages).unwrap()))
Ok(Content::Json(serde_json::to_string(&data).unwrap()))
}

#[tokio::main]
Expand Down

0 comments on commit a6d0a85

Please sign in to comment.