multithread trial 3

This commit is contained in:
YasinOnm08 2024-09-23 14:54:13 +02:00
parent 5ff6913426
commit f587864d3c
3 changed files with 37 additions and 16 deletions

View file

@ -2,20 +2,22 @@
import React, { useEffect, useRef, useState } from "react";
import ConversationFrontend from "../components/ConversationFrontend";
import InputFrontend from "../components/InputFrontend";
import { error, log } from "console";
import axios from "axios";
import { log } from 'console';
const InputOutputBackend: React.FC = () => {
const [accessToken, setAccessToken] = useState("")
const postWorkerRef = useRef<Worker| null>(null)
const getWorkerRef = useRef<Worker | null>(null)
const [messages, setMessages] = useState([{role:"system", content:"You are a helpful assistant"}])
const [messages, setMessages] = useState([{role:"assistant", content:"Hello! How can I help you?"}])
const [liveMessage, setLiveMessage] = useState("")
useEffect(() => {
console.log("getting access");
axios.get("http://localhost:5000/interstellar/api/ai_create")
.then(response => {
setAccessToken(response.data.access_token)
setAccessToken(response.data.access_token)
console.log(response.data.access_token);
})
.catch(error => {
console.log("error:", error.message);
@ -52,12 +54,12 @@ const InputOutputBackend: React.FC = () => {
if (!getWorkerRef.current) {
getWorkerRef.current = new Worker(new URL("./threads/GetWorker.js", import.meta.url))
getWorkerRef.current.postMessage("start")
getWorkerRef.current.postMessage({ action: "start", access_token:accessToken})
getWorkerRef.current.onmessage = (event) => {
const data = event.data
if (data.error) {
if (event.data == "error") {
setLiveMessage("error getting AI response: "+ data.error)
} else {
console.log("Received data:", data);
@ -74,8 +76,9 @@ const InputOutputBackend: React.FC = () => {
const endGetWorker = () => {
if (getWorkerRef.current) {
addMessage("assistant", liveMessage)
getWorkerRef.current.postMessage("terminate")
getWorkerRef.current.postMessage({action:"terminate"})
getWorkerRef.current.terminate()
console.log(messages);
}
}

View file

@ -2,24 +2,29 @@ import axios from "axios";
let shouldRun = false
onmessage = (event) => {
if (event.data === "start") {
const { action, access_token } = event.data
if (action === "start") {
shouldRun = true
fetchData()
} else if (event.date === "terminate") {
fetchData(access_token)
} else if (action === "terminate") {
shouldRun = false
}
}
const fetchData = () => {
console.log('starting get loop');
const fetchData = (access_token) => {
if (!shouldRun) return
const apiURL = "http://localhost:5000/interstellar/api/ai_get"
const apiURL = "http://localhost:5000/interstellar/api/ai_get?access_token="+access_token
axios.get(apiURL)
.then(response => {
const data = response.data.response
const data = response.data
console.log(data);
postMessage(data)
setTimeout(fetchData,500)
setTimeout(fetchData,100)
})
.catch(error => {
console.log('Error fetching data:', error);

View file

@ -1,12 +1,25 @@
import axios from "axios";
onmessage = (e) => {
const { messages = [{ role: "system", content: "You are a helpful assistant" }], ai_model = "phi3.5", access_token } = e.data
const { messages = [{ role: "assistant", content: "Hello! How can I help you?" }], ai_model = "phi3.5", access_token } = e.data
axios.post("http://localhost:5000/interstellar/api/ai_send")
const promptedMessage = messages.unshift({role:"system", content:"You are a Helpful assistant"})
const Message = {
messages: promptedMessage,
ai_model: "phi3.5",
model_type:"local",
access_token:access_token
}
axios.post("http://localhost:5000/interstellar/api/ai_send",Message)
.then(response => {
const status = response.data.status
postMessage({status})
console.log(status);
postMessage({ status })
console.log('message posted');
})
.catch(error => {
console.log("Error calling API:", error)