Merge pull request 'weather part 1' (#48) from React-Group/interstellar_ai:main into main
Reviewed-on: https://interstellardevelopment.org/code/code/sageTheDm/interstellar_ai/pulls/48
This commit is contained in:
commit
c3bff8bebb
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;
|
||||||
|
@ -34,7 +36,7 @@ const InputOutputBackend: React.FC = () => {
|
||||||
console.log(setSelectedIndex)
|
console.log(setSelectedIndex)
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log("History", chatHistory);
|
console.log("History", chatHistory);
|
||||||
console.log("Messages", messages);
|
console.log("Messages", messages);
|
||||||
|
|
||||||
|
@ -43,14 +45,14 @@ useEffect(() => {
|
||||||
|
|
||||||
// If the selected chat has messages, set them
|
// If the selected chat has messages, set them
|
||||||
if (currentMessages.length > 0) {
|
if (currentMessages.length > 0) {
|
||||||
setMessages(currentMessages);
|
setMessages(currentMessages);
|
||||||
} else if (currentMessages.length === 0) {
|
} else if (currentMessages.length === 0) {
|
||||||
// When creating a new chat and no messages exist yet, set default messages
|
// When creating a new chat and no messages exist yet, set default messages
|
||||||
addMessage("system", systemMessage)
|
addMessage("system", systemMessage)
|
||||||
addMessage("assistant", "Hello! How can I help you?")
|
addMessage("assistant", "Hello! How can I help you?")
|
||||||
console.log(systemMessage)
|
console.log(systemMessage)
|
||||||
}
|
}
|
||||||
}, [chatHistory, chatHistory.selectedIndex, systemMessage]);
|
}, [chatHistory, chatHistory.selectedIndex, systemMessage]);
|
||||||
|
|
||||||
// Update messages when any of the settings change
|
// Update messages when any of the settings change
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -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]);
|
||||||
|
@ -86,7 +100,7 @@ useEffect(() => {
|
||||||
const messageIndex = 0 // system prompt is the first so index 0
|
const messageIndex = 0 // system prompt is the first so index 0
|
||||||
updateMessage(messageIndex, systemMessage)
|
updateMessage(messageIndex, systemMessage)
|
||||||
console.log(messages)
|
console.log(messages)
|
||||||
},[systemMessage])
|
}, [systemMessage])
|
||||||
|
|
||||||
|
|
||||||
const conversationRef = useRef<HTMLDivElement>(null)
|
const conversationRef = useRef<HTMLDivElement>(null)
|
||||||
|
@ -185,8 +199,8 @@ useEffect(() => {
|
||||||
if (newContent == "") {
|
if (newContent == "") {
|
||||||
newContent = "Generating answer..."
|
newContent = "Generating answer..."
|
||||||
}
|
}
|
||||||
const messageIndex = chatHistory.chats[chatHistory.selectedIndex].messages.length-1
|
const messageIndex = chatHistory.chats[chatHistory.selectedIndex].messages.length - 1
|
||||||
updateMessage(messageIndex,newContent)
|
updateMessage(messageIndex, newContent)
|
||||||
};
|
};
|
||||||
|
|
||||||
const addMessage = (role: string, content: string) => {
|
const addMessage = (role: string, content: string) => {
|
||||||
|
@ -194,7 +208,7 @@ useEffect(() => {
|
||||||
setMessages((prevMessages) => [...prevMessages, newMessage])
|
setMessages((prevMessages) => [...prevMessages, newMessage])
|
||||||
const updatedChats = [...chatHistory.chats]
|
const updatedChats = [...chatHistory.chats]
|
||||||
updatedChats[chatHistory.selectedIndex].messages.push(newMessage)
|
updatedChats[chatHistory.selectedIndex].messages.push(newMessage)
|
||||||
setChatHistory({...chatHistory, chats:updatedChats})
|
setChatHistory({ ...chatHistory, chats: updatedChats })
|
||||||
}
|
}
|
||||||
const handleSendClick = (inputValue: string, override: boolean) => {
|
const handleSendClick = (inputValue: string, override: boolean) => {
|
||||||
if (inputValue != "") {
|
if (inputValue != "") {
|
||||||
|
@ -202,7 +216,7 @@ useEffect(() => {
|
||||||
setInputDisabled(true)
|
setInputDisabled(true)
|
||||||
if (postWorkerRef.current) {
|
if (postWorkerRef.current) {
|
||||||
addMessage("user", inputValue)
|
addMessage("user", inputValue)
|
||||||
let type:string = "local"
|
let type: string = "local"
|
||||||
let api_key: string = ""
|
let api_key: string = ""
|
||||||
if (typeof localStorage !== 'undefined') {
|
if (typeof localStorage !== 'undefined') {
|
||||||
type = localStorage.getItem('type') || "local"
|
type = localStorage.getItem('type') || "local"
|
||||||
|
|
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