live message! (limit:1)

This commit is contained in:
YasinOnm08 2024-09-23 16:34:55 +02:00
parent 2fc501d2bf
commit 8cc191dad1
3 changed files with 39 additions and 15 deletions

View file

@ -6,12 +6,20 @@ import axios from "axios";
import { log } from 'console'; import { log } from 'console';
const InputOutputBackend: React.FC = () => { const InputOutputBackend: React.FC = () => {
type Message = {
role: string
content:string
}
const [accessToken, setAccessToken] = useState("") const [accessToken, setAccessToken] = useState("")
const postWorkerRef = useRef<Worker| null>(null) const postWorkerRef = useRef<Worker| null>(null)
const getWorkerRef = useRef<Worker | null>(null) const getWorkerRef = useRef<Worker | null>(null)
const [messages, setMessages] = useState([{role:"assistant", content:"Hello! How can I help you?"}]) const [messages, setMessages] = useState<Message[]>([{role:"assistant", content:"Hello! How can I help you?"}])
const [liveMessage, setLiveMessage] = useState("") const [liveMessage, setLiveMessage] = useState("")
console.log(messages);
useEffect(() => { useEffect(() => {
console.log("getting access"); console.log("getting access");
axios.get("http://localhost:5000/interstellar/api/ai_create") axios.get("http://localhost:5000/interstellar/api/ai_create")
@ -56,6 +64,7 @@ const InputOutputBackend: React.FC = () => {
getWorkerRef.current.postMessage({ action: "start", access_token:accessToken}) getWorkerRef.current.postMessage({ action: "start", access_token:accessToken})
addMessage("assistant","")
getWorkerRef.current.onmessage = (event) => { getWorkerRef.current.onmessage = (event) => {
const data = event.data const data = event.data
@ -63,7 +72,7 @@ const InputOutputBackend: React.FC = () => {
setLiveMessage("error getting AI response: "+ data.error) setLiveMessage("error getting AI response: "+ data.error)
} else { } else {
console.log("Received data:", data); console.log("Received data:", data);
setLiveMessage(data) editLastMessage(data.response)
} }
} }
@ -75,13 +84,27 @@ const InputOutputBackend: React.FC = () => {
const endGetWorker = () => { const endGetWorker = () => {
if (getWorkerRef.current) { if (getWorkerRef.current) {
addMessage("assistant", liveMessage)
getWorkerRef.current.postMessage({action:"terminate"}) getWorkerRef.current.postMessage({action:"terminate"})
getWorkerRef.current.terminate() getWorkerRef.current.terminate()
console.log(messages); console.log(messages);
} }
} }
const editLastMessage = (newContent: string) => {
setMessages((prevMessages) => {
const updatedMessages = prevMessages.slice(); // Create a shallow copy of the current messages
if (updatedMessages.length > 0) {
const lastMessage = updatedMessages[updatedMessages.length - 1];
updatedMessages[updatedMessages.length - 1] = {
...lastMessage, // Keep the existing role and other properties
content: newContent, // Update only the content
};
}
return updatedMessages; // Return the updated array
});
};
const addMessage = (role: string, content: string) => { const addMessage = (role: string, content: string) => {
setMessages(previous => [...previous,{role,content}]) setMessages(previous => [...previous,{role,content}])
} }
@ -89,7 +112,7 @@ const InputOutputBackend: React.FC = () => {
if (postWorkerRef.current) { if (postWorkerRef.current) {
addMessage("user", inputValue) addMessage("user", inputValue)
console.log("input:",inputValue); console.log("input:",inputValue);
postWorkerRef.current.postMessage({messages:messages, ai_model:"phi3.5", access_token:accessToken}) postWorkerRef.current.postMessage({messages:[...messages, { role: "user", content: inputValue }], ai_model:"phi3.5", access_token:accessToken})
startGetWorker() startGetWorker()
} }
} }

View file

@ -1,23 +1,23 @@
import axios from "axios"; import axios from "axios";
let shouldRun = false let accesstoken
onmessage = (event) => { onmessage = (event) => {
const { action, access_token } = event.data const { action, access_token } = event.data
accesstoken=access_token
if (action === "start") { if (action === "start") {
shouldRun = true fetchData()
fetchData(access_token)
} else if (action === "terminate") { } else if (action === "terminate") {
shouldRun = false
} }
} }
console.log('starting get loop'); console.log('starting get loop');
const fetchData = (access_token) => { const fetchData = () => {
if (!shouldRun) return console.log(accesstoken);
const apiURL = "http://localhost:5000/interstellar/api/ai_get?access_token="+access_token
const apiURL = "http://localhost:5000/interstellar/api/ai_get?access_token="+accesstoken
axios.get(apiURL) axios.get(apiURL)
.then(response => { .then(response => {

View file

@ -2,17 +2,18 @@ import axios from "axios";
onmessage = (e) => { onmessage = (e) => {
const { messages = [{ role: "assistant", content: "Hello! How can I help you?" }], 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
messages.unshift({ role: "system", content: "You are a Helpful assistant" })
const promptedMessage = messages.unshift({role:"system", content:"You are a Helpful assistant"})
const Message = { const Message = {
messages: promptedMessage, messages: messages,
ai_model: "phi3.5", ai_model: "phi3.5",
model_type:"local", model_type:"local",
access_token:access_token access_token:access_token
} }
console.log(Message);
axios.post("http://localhost:5000/interstellar/api/ai_send",Message) axios.post("http://localhost:5000/interstellar/api/ai_send",Message)
.then(response => { .then(response => {
const status = response.data.status const status = response.data.status