weather part 1
This commit is contained in:
parent
12d7ac37fe
commit
ef23ef032f
2 changed files with 46 additions and 10 deletions
|
@ -5,6 +5,7 @@ import InputFrontend from "../components/InputFrontend";
|
||||||
import { sendToVoiceRecognition } from "./voice_backend"
|
import { sendToVoiceRecognition } from "./voice_backend"
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { useChatHistory } from '../hooks/useChatHistory';
|
import { useChatHistory } from '../hooks/useChatHistory';
|
||||||
|
import { getWeather } from "./weather";
|
||||||
|
|
||||||
const InputOutputBackend: React.FC = () => {
|
const InputOutputBackend: React.FC = () => {
|
||||||
// # variables
|
// # variables
|
||||||
|
@ -24,6 +25,7 @@ const InputOutputBackend: React.FC = () => {
|
||||||
const [messages, setMessages] = useState<Message[]>(chatHistory.chats[chatHistory.selectedIndex].messages || []);
|
const [messages, setMessages] = useState<Message[]>(chatHistory.chats[chatHistory.selectedIndex].messages || []);
|
||||||
const [myBoolean, setMyBoolean] = useState<boolean>(false);
|
const [myBoolean, setMyBoolean] = useState<boolean>(false);
|
||||||
const [systemMessage, setSystemMessage] = useState<string>("You are a helpful assistant")
|
const [systemMessage, setSystemMessage] = useState<string>("You are a helpful assistant")
|
||||||
|
const [weatherData, setWeatherData] = useState<string>("false")
|
||||||
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_create")
|
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_create")
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
apiURL.hostname = window.location.hostname;
|
apiURL.hostname = window.location.hostname;
|
||||||
|
@ -62,14 +64,25 @@ useEffect(() => {
|
||||||
setTimeZone(localStorage.getItem("timeZone") || "GMT");
|
setTimeZone(localStorage.getItem("timeZone") || "GMT");
|
||||||
setDateFormat(localStorage.getItem("dateFormat") || "DD-MM-YYYY");
|
setDateFormat(localStorage.getItem("dateFormat") || "DD-MM-YYYY");
|
||||||
setMyBoolean(localStorage.getItem('myBoolean') === 'true');
|
setMyBoolean(localStorage.getItem('myBoolean') === 'true');
|
||||||
|
getWeatherHere()
|
||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
const getWeatherHere = async () => {
|
||||||
|
setWeatherData(await getWeather({ "unit_type": preferredMeasurement, "city": localStorage.getItem("weatherInfo") || "New York" }))
|
||||||
|
console.log(weatherData)
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const measurementString = (preferredMeasurement == "Metric")
|
const measurementString = (preferredMeasurement == "Metric")
|
||||||
? "All measurements follow the metric system. Refuse to use any other measurement system."
|
? "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.";
|
: "All measurements follow the imperial system. Refuse to use any other measurement system.";
|
||||||
|
|
||||||
|
if (!weatherData && localStorage.getItem("activeSelectedAIFunction") == "Weather") {
|
||||||
|
setWeatherData("")
|
||||||
|
console.log("Weather is overrated.")
|
||||||
|
}
|
||||||
|
|
||||||
const newSystemMessage = myBoolean
|
const newSystemMessage = myBoolean
|
||||||
? `You are operating in the timezone: ${timeZone}. Use the ${timeFormat} time format and ${dateFormat} for dates.
|
? `You are operating in the timezone: ${timeZone}. Use the ${timeFormat} time format and ${dateFormat} for dates.
|
||||||
${measurementString}
|
${measurementString}
|
||||||
|
@ -77,7 +90,8 @@ useEffect(() => {
|
||||||
Communicate in the language specified by the user (country code: ${preferredLanguage}), and only in this language.
|
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.
|
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.`
|
Do not answer in multiple languages or multiple measurement systems under any circumstances other than the user requesting it.`
|
||||||
: `You are a helpful assistant`;
|
: `You are a helpful assistant.
|
||||||
|
${weatherData}`;
|
||||||
|
|
||||||
setSystemMessage(newSystemMessage)
|
setSystemMessage(newSystemMessage)
|
||||||
}, [preferredCurrency, preferredLanguage, timeFormat, preferredMeasurement, timeZone, dateFormat, myBoolean]);
|
}, [preferredCurrency, preferredLanguage, timeFormat, preferredMeasurement, timeZone, dateFormat, myBoolean]);
|
||||||
|
|
22
app/backend/weather.ts
Normal file
22
app/backend/weather.ts
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
|
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/weather")
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
apiURL.hostname = window.location.hostname;
|
||||||
|
} else {
|
||||||
|
apiURL.hostname = "localhost"
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getWeather = async (data: object): Promise<string> => {
|
||||||
|
try {
|
||||||
|
const response = await axios.post(apiURL.href, data);
|
||||||
|
const status = response.data.status;
|
||||||
|
const success = response.data.response;
|
||||||
|
postMessage({ status, success });
|
||||||
|
return success;
|
||||||
|
} catch (error) {
|
||||||
|
postMessage({ status: 500, success: false });
|
||||||
|
console.log(error)
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in a new issue