Merge pull request 'main' (#44) from React-Group/interstellar_ai:main into main

Reviewed-on: https://interstellardevelopment.org/code/code/YasinOnm08/interstellar_ai/pulls/44
This commit is contained in:
YasinOnm08 2024-10-03 14:11:04 +02:00
commit c206104859
6 changed files with 62 additions and 34 deletions

View file

@ -15,7 +15,7 @@ const InputOutputBackend: React.FC = () => {
content: string content: string
} }
// Define state variables for user preferences and messages // Define state variables for user preferences and messages
const [preferredCurrency, setPreferredCurrency] = useState<string>("USD"); const [preferredCurrency, setPreferredCurrency] = useState<string>("USD");
const [preferredLanguage, setPreferredLanguage] = useState<string>("english"); const [preferredLanguage, setPreferredLanguage] = useState<string>("english");
const [timeFormat, setTimeFormat] = useState<string>("24-hour"); const [timeFormat, setTimeFormat] = useState<string>("24-hour");
@ -24,6 +24,8 @@ const InputOutputBackend: React.FC = () => {
const [dateFormat, setDateFormat] = useState<string>("DD-MM-YYYY"); const [dateFormat, setDateFormat] = useState<string>("DD-MM-YYYY");
const [messages, setMessages] = useState<Message[]>([]); const [messages, setMessages] = useState<Message[]>([]);
const [myBoolean, setMyBoolean] = useState<boolean>(() => localStorage.getItem('myBoolean') === 'true') || false; const [myBoolean, setMyBoolean] = useState<boolean>(() => localStorage.getItem('myBoolean') === 'true') || false;
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_create")
apiURL.hostname = window.location.hostname;
// Fetch local storage values and update state on component mount // Fetch local storage values and update state on component mount
useEffect(() => { useEffect(() => {
@ -55,7 +57,7 @@ const InputOutputBackend: React.FC = () => {
{ role: "assistant", content: "Hello! How may I help you?" }, { role: "assistant", content: "Hello! How may I help you?" },
]); ]);
}, [preferredCurrency, preferredLanguage, timeFormat, preferredMeasurement, timeZone, dateFormat, myBoolean]); }, [preferredCurrency, preferredLanguage, timeFormat, preferredMeasurement, timeZone, dateFormat, myBoolean]);
const conversationRef = useRef<HTMLDivElement>(null) const conversationRef = useRef<HTMLDivElement>(null)
const [copyClicked, setCopyClicked] = useState(false) const [copyClicked, setCopyClicked] = useState(false)
@ -72,7 +74,7 @@ const InputOutputBackend: React.FC = () => {
useEffect(() => { useEffect(() => {
getNewToken() getNewToken()
postWorkerRef.current = new Worker(new URL("./threads/PostWorker.js", import.meta.url)) postWorkerRef.current = new Worker(new URL("./threads/PostWorker.ts", import.meta.url))
postWorkerRef.current.onmessage = (event) => { postWorkerRef.current.onmessage = (event) => {
const status = event.data.status const status = event.data.status
@ -101,7 +103,7 @@ const InputOutputBackend: React.FC = () => {
}, []) }, [])
const getNewToken = () => { const getNewToken = () => {
axios.get("http://localhost:5000/interstellar_ai/api/ai_create") axios.get(apiURL.href)
.then(response => { .then(response => {
setAccessToken(response.data.access_token) setAccessToken(response.data.access_token)
}) })
@ -113,9 +115,11 @@ const InputOutputBackend: React.FC = () => {
const startGetWorker = () => { const startGetWorker = () => {
if (!getWorkerRef.current) { if (!getWorkerRef.current) {
getWorkerRef.current = new Worker(new URL("./threads/GetWorker.js", import.meta.url)) getWorkerRef.current = new Worker(new URL("./threads/GetWorker.ts", import.meta.url))
getWorkerRef.current.postMessage({ action: "start", access_token: accessToken }) const windowname = window.location.hostname
getWorkerRef.current.postMessage({ action: "start", access_token: accessToken, windowname })
addMessage("assistant", "") addMessage("assistant", "")
getWorkerRef.current.onmessage = (event) => { getWorkerRef.current.onmessage = (event) => {
@ -178,7 +182,8 @@ const InputOutputBackend: React.FC = () => {
} }
} }
setInputMessage("") setInputMessage("")
postWorkerRef.current.postMessage({ messages: [...messages, { role: "user", content: inputValue }], ai_model: "llama3.2", model_type: type, access_token: accessToken, api_key: api_key }) const windowname = window.location.hostname
postWorkerRef.current.postMessage({ messages: [...messages, { role: "user", content: inputValue }], ai_model: "llama3.2", model_type: type, access_token: accessToken, api_key: api_key, windowname })
startGetWorker() startGetWorker()
} }
} }
@ -228,7 +233,7 @@ const InputOutputBackend: React.FC = () => {
} }
}; };
const handleStopClick = () => { const handleStopClick = () => {
endGetWorker() endGetWorker()
getNewToken() getNewToken()
} }
@ -246,7 +251,7 @@ const InputOutputBackend: React.FC = () => {
const handleEditClick = () => { const handleEditClick = () => {
let newestMessage = messages[messages.length - 2].content let newestMessage = messages[messages.length - 2].content
setInputMessage(newestMessage) setInputMessage(newestMessage)
const updatedMessages = messages.slice(0, messages.length-2) const updatedMessages = messages.slice(0, messages.length - 2)
setMessages(updatedMessages) setMessages(updatedMessages)
endGetWorker() endGetWorker()
getNewToken() getNewToken()

View file

@ -18,9 +18,12 @@ if all went well, you will get the status 200 in response.data.status
to check if the request was accepted or declined, check response.data.response, it will be either true or false depending on if it worked, or not. to check if the request was accepted or declined, check response.data.response, it will be either true or false depending on if it worked, or not.
*/ */
const apiURL = new URL("http://localhost:5000/interstellar_ai/db")
apiURL.hostname = window.location.hostname;
export const sendToDatabase = async (data: any): Promise<boolean> => { export const sendToDatabase = async (data: any): Promise<boolean> => {
try { try {
const response = await axios.post("http://localhost:5000/interstellar_ai/db", data); const response = await axios.post(apiURL.href, data);
const status = response.data.status; const status = response.data.status;
const success = response.data.response; const success = response.data.response;
postMessage({ status, success }); postMessage({ status, success });
@ -33,7 +36,7 @@ export const sendToDatabase = async (data: any): Promise<boolean> => {
export const sendToDatabaseAndGetString = async (data: any): Promise<string> => { export const sendToDatabaseAndGetString = async (data: any): Promise<string> => {
try { try {
const response = await axios.post("http://localhost:5000/interstellar_ai/db", data); const response = await axios.post(apiURL.href, data);
const status = response.data.status; const status = response.data.status;
const success = response.data.response; const success = response.data.response;
postMessage({ status, success }); postMessage({ status, success });

View file

@ -1,10 +1,14 @@
import axios from "axios"; import axios from "axios";
let accesstoken let windownameGlobal = ""
let accesstoken = ""
onmessage = (event) => { onmessage = (event) => {
const { action, access_token } = event.data const { action, access_token, windowname } = event.data
accesstoken = access_token accesstoken = access_token
windownameGlobal = windowname
if (action === "start") { if (action === "start") {
fetchData() fetchData()
} else if (action === "terminate") { } else if (action === "terminate") {
@ -14,9 +18,12 @@ onmessage = (event) => {
const fetchData = () => { const fetchData = () => {
const apiURL = "http://localhost:5000/interstellar_ai/api/ai_get?access_token=" + accesstoken const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_get?access_token=" + accesstoken)
apiURL.hostname = windownameGlobal;
axios.get(apiURL) console.log(apiURL.href)
axios.get(apiURL.href)
.then(response => { .then(response => {
const data = response.data const data = response.data
postMessage(data) postMessage(data)

View file

@ -1,7 +1,7 @@
import axios from "axios"; import axios from "axios";
onmessage = (e) => { onmessage = (e) => {
const { messages, ai_model, model_type, access_token, api_key } = e.data const { messages, ai_model, model_type, access_token, api_key, windowname } = e.data
const Message = { const Message = {
@ -12,7 +12,13 @@ onmessage = (e) => {
api_key: api_key api_key: api_key
} }
axios.post("http://localhost:5000/interstellar_ai/api/ai_send", Message) const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_send")
console.log(windowname)
apiURL.hostname = windowname;
console.log(apiURL.href)
axios.post(apiURL.href, Message)
.then(response => { .then(response => {
const status = response.data.status const status = response.data.status
postMessage({ status }) postMessage({ status })

View file

@ -5,7 +5,10 @@ export const sendToVoiceRecognition = (audio_data: Blob): Promise<string> => {
const formdata = new FormData() const formdata = new FormData()
formdata.append("audio", audio_data) formdata.append("audio", audio_data)
return axios.post("http://localhost:5000/interstellar_ai/api/voice_recognition", formdata) const apiURL = new URL("http://localhost:5000/interstellar_ai/api/voice_recognition")
apiURL.hostname = window.location.hostname;
return axios.post(apiURL.href, formdata)
.then((response) => { .then((response) => {
return response.data.response return response.data.response
}) })

View file

@ -154,15 +154,15 @@ const modelDropdown = {
}; };
const selectedAIFunction = [ const selectedAIFunction = [
'Code', 'Code',
'Math', 'Math',
'Language', 'Language',
'Character', 'Character',
'Finance', 'Finance',
'Weather', 'Weather',
'Time', 'Time',
'Image', 'Image',
'Custom1', 'Custom1',
'Custom2' 'Custom2'
] ]
@ -172,25 +172,29 @@ const ModelSection: React.FC = () => {
const [radioSelection, setRadioSelection] = useState<string | null>("") const [radioSelection, setRadioSelection] = useState<string | null>("")
const [activeSelectedAIFunction, setActiveSelectedAIFunction] = useState(''); const [activeSelectedAIFunction, setActiveSelectedAIFunction] = useState('');
const [currentSelectedAIFunction, setCurrentSelectedAIFunction] = useState<string | null>(""); const [currentSelectedAIFunction, setCurrentSelectedAIFunction] = useState<string | null>("");
useEffect(() => { useEffect(() => {
var temp = localStorage.getItem("activeSelectedAIFunction") || "" var temp = localStorage.getItem("activeSelectedAIFunction") || ""
setActiveSelectedAIFunction(temp) setActiveSelectedAIFunction(temp)
if (!localStorage.getItem('selectedModelDropdown')) { if (!localStorage.getItem('selectedModelDropdown')) {
localStorage.setItem("selectedModelDropdown", "Offline Fast" ) localStorage.setItem("selectedModelDropdown", "Offline Fast")
} }
if (!localStorage.getItem("activeSelectedAIFunction")) { if (!localStorage.getItem("activeSelectedAIFunction")) {
setActiveSelectedAIFunction('Code') setActiveSelectedAIFunction('Code')
localStorage.setItem('activeSelectedAIFunction' ,'Code') localStorage.setItem('activeSelectedAIFunction', 'Code')
} }
if (!localStorage.getItem("model")) { if (!localStorage.getItem("model")) {
localStorage.setItem("model" ,'starcoder2') localStorage.setItem("model", 'starcoder2')
} }
if (!localStorage.getItem("radioSelection")) { if (!localStorage.getItem("radioSelection")) {
localStorage.setItem("radioSelection" ,'None') localStorage.setItem("radioSelection", 'None')
}
if (!localStorage.getItem("type")) {
localStorage.setItem("type", 'local')
} }
const handleStorageChange = () => { const handleStorageChange = () => {
@ -277,7 +281,7 @@ const ModelSection: React.FC = () => {
modelDropdown.offlineNonFoss.includes(model) || modelDropdown.offlineFoss.includes(model); modelDropdown.offlineNonFoss.includes(model) || modelDropdown.offlineFoss.includes(model);
const modelClicked = (model: string) => { const modelClicked = (model: string) => {
localStorage.setItem('activeSelectedAIFunction' , model) localStorage.setItem('activeSelectedAIFunction', model)
setActiveSelectedAIFunction(model) setActiveSelectedAIFunction(model)
const selectedAIFunction = selectedModelDropdown as keyof typeof modelList; const selectedAIFunction = selectedModelDropdown as keyof typeof modelList;
localStorage.setItem("model", modelList[selectedAIFunction][model as keyof typeof modelList[typeof selectedAIFunction]]) localStorage.setItem("model", modelList[selectedAIFunction][model as keyof typeof modelList[typeof selectedAIFunction]])
@ -309,7 +313,7 @@ const ModelSection: React.FC = () => {
(displayedCategory) => ( (displayedCategory) => (
<button <button
key={displayedCategory} key={displayedCategory}
className={`${displayedCategory.toLowerCase()}-model model-box ${currentSelectedAIFunction === displayedCategory ? 'selected' : ''}`} className={`${displayedCategory.toLowerCase()}-model model-box ${currentSelectedAIFunction === displayedCategory ? 'selected' : ''}`}
onClick={() => modelClicked(displayedCategory)} onClick={() => modelClicked(displayedCategory)}
> >
<div className="overlay"> <div className="overlay">