multithread trial 2
This commit is contained in:
parent
1744148b1b
commit
7e69674978
4 changed files with 140 additions and 114 deletions
29
app/backend/threads/GetWorker.js
Normal file
29
app/backend/threads/GetWorker.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
import axios from "axios";
|
||||
|
||||
let shouldRun = false
|
||||
onmessage = (event) => {
|
||||
if (event.data === "start") {
|
||||
shouldRun = true
|
||||
fetchData()
|
||||
} else if (event.date === "terminate") {
|
||||
shouldRun = false
|
||||
}
|
||||
}
|
||||
|
||||
const fetchData = () => {
|
||||
if (!shouldRun) return
|
||||
|
||||
const apiURL = "http://localhost:5000/interstellar/api/ai_get"
|
||||
|
||||
axios.get(apiURL)
|
||||
.then(response => {
|
||||
const data = response.data.response
|
||||
postMessage(data)
|
||||
setTimeout(fetchData,500)
|
||||
})
|
||||
.catch(error => {
|
||||
console.log('Error fetching data:', error);
|
||||
postMessage({error:"failed fetching data"})
|
||||
|
||||
})
|
||||
}
|
15
app/backend/threads/PostWorker.js
Normal file
15
app/backend/threads/PostWorker.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
import axios from "axios";
|
||||
|
||||
onmessage = (e) => {
|
||||
const { messages = [{ role: "system", content: "You are a helpful assistant" }], ai_model = "phi3.5", access_token } = e.data
|
||||
|
||||
axios.post("http://localhost:5000/interstellar/api/ai_send")
|
||||
.then(response => {
|
||||
const status = response.data.status
|
||||
postMessage({status})
|
||||
})
|
||||
.catch(error => {
|
||||
console.log("Error calling API:", error)
|
||||
postMessage({status:500})
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue