interstellar_ai/app/ProcessAPI.js

46 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-09-19 13:02:56 +02:00
import axios from 'axios'
2024-09-20 09:17:28 +02:00
import { type } from 'os';
2024-09-19 13:02:56 +02:00
onmessage = function (e) {
2024-09-20 09:17:28 +02:00
const { functionName = "getAccess", access_token = "", messages = [], ai_model = "phi3.5", system_prompt = {role:"system" ,content: "You are a helpful assistant that gives short answers"}} = e.data
let data = {
ai_model: ai_model,
messages: messages,
access_token: access_token
2024-09-19 15:56:26 +02:00
};
2024-09-20 09:58:13 +02:00
const getResponse = () => {
axios.get('https://localhost:5000/interstellar/api/ai_get?access_token=' + access_token)
.then(Response => {
postMessage(Response.data.response)
}).catch(error => {
console.error("Error with GET response request:", error)
})
}
2024-09-19 13:02:56 +02:00
switch (functionName) {
case "getAccess":
2024-09-20 09:17:28 +02:00
console.log("getting access...")
axios.get('https://localhost:5000/interstellar/api/ai_create')
.then(Response => {
postMessage(Response.data.access_token)
}).catch(error => {
console.error("Error with GET Token request:", error)
})
2024-09-19 13:02:56 +02:00
break
case "postRequest":
2024-09-20 09:17:28 +02:00
messages.unshift(system_prompt)
console.log("sending...")
console.log(messages)
axios.post('https://localhost:5000/interstellar/api/ai_send', data)
.then(Response => {
2024-09-20 09:58:13 +02:00
getResponse()
2024-09-20 09:17:28 +02:00
}).catch(error => {
console.error("Error:", error)
})
2024-09-19 13:02:56 +02:00
break
}
2024-09-19 16:24:16 +02:00
2024-09-19 13:02:56 +02:00
}