Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spawn query_chat #26

Open
wants to merge 1 commit into
base: 03-spawn-a
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading