I have an egui app with a button to ask questions to chatgpt. However while connecting to chatgpt the main thread is blocked so the application isn't drawn as long as it's connecting.
This is the ask_chatgpt
function:
pub(crate) fn ask_chatgpt(prompt: String) -> Result<String, String> {
let url = "https://www.pizzagpt.it/api/chat-completion";
let response = ureq::post(url)
.send_json(ureq::json!({
"question": prompt,
"secret": "salame"
})).unwrap();
if response.status() >= 200 && response.status() < 300 {
Ok(response.into_string().unwrap())
} else {
Err(String::from("Response wasn't succesfull"))
}
}
How can I make this multithreaded? I already tried with this:
pub(crate) fn ask_chatgpt(prompt: String) -> Result<String, String> {
let url = "https://www.pizzagpt.it/api/chat-completion";
let handle = thread::spawn(move || {
let response = ureq::post(url)
.send_json(ureq::json!({
"question": prompt,
"secret": "salame"
})).unwrap();
if response.status() >= 200 && response.status() < 300 {
Ok(response.into_string().unwrap())
} else {
Err(String::from("Response wasn't succesfull"))
}
});
handle.join().unwrap()
}
While there are no errors, the main thread is still clogged.