general fixes

This commit is contained in:
Patrick 2024-10-09 20:20:04 +02:00
parent 281d786c19
commit 885b838704
7 changed files with 66 additions and 23 deletions

View file

@ -6,6 +6,8 @@ import { sendToVoiceRecognition } from "./voice_backend"
import axios from "axios";
import { useChatHistory } from '../hooks/useChatHistory';
import { getWeather } from "./weather";
import { changeHistory, getHistory } from "./database";
import { type } from "os";
const InputOutputBackend: React.FC = () => {
// # variables
@ -25,7 +27,9 @@ const InputOutputBackend: React.FC = () => {
const [messages, setMessages] = useState<Message[]>(chatHistory.chats[chatHistory.selectedIndex].messages || []);
const [myBoolean, setMyBoolean] = useState<boolean>(false);
const [systemMessage, setSystemMessage] = useState<string>("You are a helpful assistant")
const [weatherData, setWeatherData] = useState<string>("false")
const [weatherData, setWeatherData] = useState<string>("")
const [weatherTriggered, setWeatherTriggered] = useState<boolean>(false)
const [chatHistoryTriggered, setChatHistoryTriggered] = useState<boolean>(false)
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_create")
if (typeof window !== 'undefined') {
apiURL.hostname = window.location.hostname;
@ -65,15 +69,42 @@ const InputOutputBackend: React.FC = () => {
setDateFormat(localStorage.getItem("dateFormat") || "DD-MM-YYYY");
setMyBoolean(localStorage.getItem('myBoolean') === 'true');
getWeatherHere()
getChatHistory()
}
}, [])
useEffect(() => {
const username = localStorage.getItem("accountName")
const password = localStorage.getItem("accountPassword")
if (username && password && chatHistoryTriggered) {
changeHistory(username, password, chatHistory)
console.log("changed history in backend")
}
}, [chatHistory])
const getWeatherHere = async () => {
setWeatherData(await getWeather({ "unit_type": preferredMeasurement, "city": localStorage.getItem("weatherInfo") || "New York" }))
console.log(weatherData)
console.log("Got the Data!")
setWeatherTriggered(true)
}
const getChatHistory = async () => {
const username = localStorage.getItem("accountName")
const password = localStorage.getItem("accountPassword")
if (username && password) {
const tempChatHistory = await getHistory(username, password)
if (tempChatHistory && typeof tempChatHistory == "object") {
setChatHistory(tempChatHistory)
console.log("got history from backend")
}
}
setChatHistoryTriggered(true)
}
useEffect(() => {
console.log("creating system prompt")
console.log(weatherData)
const measurementString = (preferredMeasurement == "Metric")
? "All measurements follow the metric system. Refuse to use any other measurement system."
: "All measurements follow the imperial system. Refuse to use any other measurement system.";
@ -85,10 +116,11 @@ const InputOutputBackend: React.FC = () => {
Communicate in the language specified by the user (country code: ${preferredLanguage}), and only in this language.
You are only able to change language if the user specifically states you must.
Do not answer in multiple languages or multiple measurement systems under any circumstances other than the user requesting it.
When asked about the weather use those infos: ${weatherData}. If there is nothing there say there is no data`
These are the currently newest Weather infos for the region. In case the user asks about anything weather related, you can use the following data to help the user: ${weatherData}. If there is nothing there say there is no data`
: `You are a helpful assistant.`;
console.log(newSystemMessage)
setSystemMessage(newSystemMessage)
}, [preferredCurrency, preferredLanguage, timeFormat, preferredMeasurement, timeZone, dateFormat, myBoolean]);
}, [preferredCurrency, preferredLanguage, timeFormat, preferredMeasurement, timeZone, dateFormat, myBoolean, weatherTriggered]);
useEffect(() => {
const messageIndex = 0 // system prompt is the first so index 0

View file

@ -13,7 +13,8 @@ export const getWeather = async (data: object): Promise<string> => {
const status = response.data.status;
const success = response.data.response;
postMessage({ status, success });
return success;
console.log(JSON.stringify(success))
return JSON.stringify(success);
} catch (error) {
postMessage({ status: 500, success: false });
console.log(error)