forked from React-Group/interstellar_ai
main #58
2 changed files with 46 additions and 10 deletions
|
@ -5,6 +5,7 @@ import InputFrontend from "../components/InputFrontend";
|
|||
import { sendToVoiceRecognition } from "./voice_backend"
|
||||
import axios from "axios";
|
||||
import { useChatHistory } from '../hooks/useChatHistory';
|
||||
import { getWeather } from "./weather";
|
||||
|
||||
const InputOutputBackend: React.FC = () => {
|
||||
// # variables
|
||||
|
@ -24,6 +25,7 @@ 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 apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_create")
|
||||
if (typeof window !== 'undefined') {
|
||||
apiURL.hostname = window.location.hostname;
|
||||
|
@ -34,7 +36,7 @@ const InputOutputBackend: React.FC = () => {
|
|||
console.log(setSelectedIndex)
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
useEffect(() => {
|
||||
console.log("History", chatHistory);
|
||||
console.log("Messages", messages);
|
||||
|
||||
|
@ -43,14 +45,14 @@ useEffect(() => {
|
|||
|
||||
// If the selected chat has messages, set them
|
||||
if (currentMessages.length > 0) {
|
||||
setMessages(currentMessages);
|
||||
setMessages(currentMessages);
|
||||
} else if (currentMessages.length === 0) {
|
||||
// When creating a new chat and no messages exist yet, set default messages
|
||||
addMessage("system", systemMessage)
|
||||
addMessage("assistant", "Hello! How can I help you?")
|
||||
console.log(systemMessage)
|
||||
}
|
||||
}, [chatHistory, chatHistory.selectedIndex, systemMessage]);
|
||||
}, [chatHistory, chatHistory.selectedIndex, systemMessage]);
|
||||
|
||||
// Update messages when any of the settings change
|
||||
useEffect(() => {
|
||||
|
@ -62,14 +64,25 @@ useEffect(() => {
|
|||
setTimeZone(localStorage.getItem("timeZone") || "GMT");
|
||||
setDateFormat(localStorage.getItem("dateFormat") || "DD-MM-YYYY");
|
||||
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(() => {
|
||||
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.";
|
||||
|
||||
if (!weatherData && localStorage.getItem("activeSelectedAIFunction") == "Weather") {
|
||||
setWeatherData("")
|
||||
console.log("Weather is overrated.")
|
||||
}
|
||||
|
||||
const newSystemMessage = myBoolean
|
||||
? `You are operating in the timezone: ${timeZone}. Use the ${timeFormat} time format and ${dateFormat} for dates.
|
||||
${measurementString}
|
||||
|
@ -77,7 +90,8 @@ useEffect(() => {
|
|||
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.`
|
||||
: `You are a helpful assistant`;
|
||||
: `You are a helpful assistant.
|
||||
${weatherData}`;
|
||||
|
||||
setSystemMessage(newSystemMessage)
|
||||
}, [preferredCurrency, preferredLanguage, timeFormat, preferredMeasurement, timeZone, dateFormat, myBoolean]);
|
||||
|
@ -86,7 +100,7 @@ useEffect(() => {
|
|||
const messageIndex = 0 // system prompt is the first so index 0
|
||||
updateMessage(messageIndex, systemMessage)
|
||||
console.log(messages)
|
||||
},[systemMessage])
|
||||
}, [systemMessage])
|
||||
|
||||
|
||||
const conversationRef = useRef<HTMLDivElement>(null)
|
||||
|
@ -185,8 +199,8 @@ useEffect(() => {
|
|||
if (newContent == "") {
|
||||
newContent = "Generating answer..."
|
||||
}
|
||||
const messageIndex = chatHistory.chats[chatHistory.selectedIndex].messages.length-1
|
||||
updateMessage(messageIndex,newContent)
|
||||
const messageIndex = chatHistory.chats[chatHistory.selectedIndex].messages.length - 1
|
||||
updateMessage(messageIndex, newContent)
|
||||
};
|
||||
|
||||
const addMessage = (role: string, content: string) => {
|
||||
|
@ -194,7 +208,7 @@ useEffect(() => {
|
|||
setMessages((prevMessages) => [...prevMessages, newMessage])
|
||||
const updatedChats = [...chatHistory.chats]
|
||||
updatedChats[chatHistory.selectedIndex].messages.push(newMessage)
|
||||
setChatHistory({...chatHistory, chats:updatedChats})
|
||||
setChatHistory({ ...chatHistory, chats: updatedChats })
|
||||
}
|
||||
const handleSendClick = (inputValue: string, override: boolean) => {
|
||||
if (inputValue != "") {
|
||||
|
@ -202,7 +216,7 @@ useEffect(() => {
|
|||
setInputDisabled(true)
|
||||
if (postWorkerRef.current) {
|
||||
addMessage("user", inputValue)
|
||||
let type:string = "local"
|
||||
let type: string = "local"
|
||||
let api_key: string = ""
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
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