Started changes the file structure
This commit is contained in:
parent
464df4adac
commit
89b3b824c7
17 changed files with 12 additions and 12 deletions
39
app/backend/AudioRecorder(not yet).tsx
Normal file
39
app/backend/AudioRecorder(not yet).tsx
Normal file
|
@ -0,0 +1,39 @@
|
|||
// import React, { useState, useRef } from 'react'
|
||||
|
||||
// const AudioRecorder: React.FC = () => {
|
||||
// const [isRecording, setIsRecording] = useState(false)
|
||||
// const [audioURL, setAudioURL] = useState<string | null>(null)
|
||||
// const medaRecorderRef = useRef<MediaRecorder | null>(null)
|
||||
// const audioChunks = useRef<Blob[]>([])
|
||||
|
||||
// const startRecording = async () => {
|
||||
// const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
|
||||
// const mediaRecorder = new MediaRecorder(stream)
|
||||
// medaRecorderRef.current = mediaRecorder
|
||||
|
||||
// mediaRecorder.ondataavailable = (event) => {
|
||||
// audioChunks.current.push(event.data)
|
||||
// }
|
||||
|
||||
// mediaRecorder.onstop = () => {
|
||||
// const audioBlob = new Blob(audioChunks.current, { type: "audio/wav" })
|
||||
// const url = URL.createObjectURL(audioBlob)
|
||||
// setAudioURL(url)
|
||||
// audioChunks.current = []
|
||||
// }
|
||||
|
||||
// mediaRecorder.start()
|
||||
// setIsRecording(true)
|
||||
|
||||
// const stopRecording = () => {
|
||||
// medaRecorderRef.current?.stop()
|
||||
// setIsRecording(false)
|
||||
// }
|
||||
|
||||
// return (
|
||||
// <div></div>
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
|
||||
// export default AudioRecorder
|
86
app/backend/InputBackend.tsx
Normal file
86
app/backend/InputBackend.tsx
Normal file
|
@ -0,0 +1,86 @@
|
|||
import React, { useState } from 'react';
|
||||
import InputFrontend from '../components/InputFrontend';
|
||||
import ConversationFrontend from '../components/ConversationFrontend';
|
||||
import { Mistral } from '@mistralai/mistralai';
|
||||
|
||||
|
||||
|
||||
const handleMicClick = () => {
|
||||
console.log('Mic clicked!');
|
||||
// Do something when the mic button is clicked
|
||||
};
|
||||
|
||||
|
||||
const handleResendClick = () => {
|
||||
console.log('Resend button clicked');
|
||||
// Handle resend action
|
||||
};
|
||||
|
||||
const handleEditClick = () => {
|
||||
console.log('Edit button clicked');
|
||||
// Handle edit action
|
||||
};
|
||||
|
||||
const handleCopyClick = () => {
|
||||
console.log('Copy button clicked');
|
||||
// Handle copy action
|
||||
};
|
||||
|
||||
const InputBackend: React.FC = () => {
|
||||
async function prompt_mistral(model: string, prompt: string, system: string) {
|
||||
const apiKey = "m3kZRjN8DRSIo88r8Iti9hmKGWIklrLY";
|
||||
|
||||
const client = new Mistral({ apiKey: apiKey });
|
||||
|
||||
var chatResponse = await client.chat.complete({
|
||||
model: model,
|
||||
messages: [{ role: 'user', content: prompt }, { role: 'system', content: system, }],
|
||||
});
|
||||
|
||||
if (chatResponse && chatResponse.choices && chatResponse.choices.length > 0) {
|
||||
if (chatResponse.choices[0].message.content) {
|
||||
addMessage('AI: ' + chatResponse.choices[0].message.content);
|
||||
}
|
||||
} else {
|
||||
console.error('Error: Unexpected API response:', chatResponse);
|
||||
}
|
||||
}
|
||||
|
||||
const handleSendClick = (message: string) => {
|
||||
var system = "You are a helpful assistant. The following is the chat history."
|
||||
for (let index = 0; index < messages.length; index++) {
|
||||
system += messages[index] + " ";
|
||||
};
|
||||
|
||||
addMessage('User: ' + message);
|
||||
prompt_mistral("mistral-large-latest", message, system)
|
||||
};
|
||||
|
||||
const [messages, setMessages] = useState([
|
||||
'User: Hello!',
|
||||
'AI: Hi there!',
|
||||
'User: How are you?',
|
||||
'AI: I’m good, thank you!'
|
||||
]);
|
||||
|
||||
const addMessage = (message: string) => {
|
||||
setMessages((prevMessages) => [...prevMessages, message]);
|
||||
};
|
||||
return (
|
||||
<div>
|
||||
<ConversationFrontend
|
||||
messages={messages}
|
||||
onResendClick={handleResendClick}
|
||||
onEditClick={handleEditClick}
|
||||
onCopyClick={handleCopyClick}
|
||||
/>
|
||||
<InputFrontend
|
||||
message=""
|
||||
onSendClick={handleSendClick}
|
||||
onMicClick={handleMicClick}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default InputBackend;
|
109
app/backend/InputOutputHandler.tsx
Normal file
109
app/backend/InputOutputHandler.tsx
Normal file
|
@ -0,0 +1,109 @@
|
|||
"use client"
|
||||
import React, { useEffect, useRef, useState } from "react";
|
||||
import ConversationFrontend from "../components/ConversationFrontend";
|
||||
import InputFrontend from "../components/InputFrontend";
|
||||
|
||||
const handleMicClick = () => {
|
||||
console.log('Mic clicked!');
|
||||
// Do something when the mic button is clicked
|
||||
};
|
||||
|
||||
|
||||
const handleResendClick = () => {
|
||||
console.log('Resend button clicked');
|
||||
// Handle resend action
|
||||
};
|
||||
|
||||
const handleEditClick = () => {
|
||||
console.log('Edit button clicked');
|
||||
// Handle edit action
|
||||
};
|
||||
|
||||
const handleCopyClick = () => {
|
||||
console.log('Copy button clicked');
|
||||
// Handle copy action
|
||||
};
|
||||
|
||||
const InputOutputBackend: React.FC = () => {
|
||||
const [accessToken, setAccessToken] = useState("")
|
||||
const workerRef = useRef<Worker | null>(null)
|
||||
|
||||
const handleSendClick = (message: string) => {
|
||||
var system = "You give really short answers (maximum of 30 sentences). The following is the chat history."
|
||||
for (let index = 0; index < messages.length; index++) {
|
||||
system += messages[index] + " ";
|
||||
};
|
||||
|
||||
HandlePostRequest(message, "phi3.5", system)
|
||||
|
||||
addMessage('User: ' + message);
|
||||
};
|
||||
|
||||
const [messages, setMessages] = useState([
|
||||
'User: Hello!',
|
||||
'AI: Hi there!',
|
||||
'User: How are you?',
|
||||
'AI: I’m good, thank you!'
|
||||
]);
|
||||
|
||||
const addMessage = (message: string) => {
|
||||
setMessages((prevMessages) => [...prevMessages, message]);
|
||||
};
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
workerRef.current = new Worker(new URL("./ProcessAPI.js", import.meta.url))
|
||||
workerRef.current.postMessage({})
|
||||
workerRef.current.onmessage = (e) => {
|
||||
setAccessToken(e.data)
|
||||
}
|
||||
|
||||
return () => {
|
||||
if (workerRef.current) {
|
||||
workerRef.current.terminate()
|
||||
}
|
||||
}
|
||||
},[])
|
||||
|
||||
const HandleGetRequest = (message: string, ai_model: string, system_prompt: string) => {
|
||||
if (workerRef.current) {
|
||||
workerRef.current.postMessage({ functionName: "getResponse", access_token: accessToken, message: message, ai_model: ai_model, system_prompt: system_prompt })
|
||||
workerRef.current.onmessage = (e) => {
|
||||
addMessage("AI: " + e.data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const HandlePostRequest = (message: string, ai_model: string, system_prompt: string) => {
|
||||
if (workerRef.current) {
|
||||
workerRef.current.postMessage({ functionName: "postRequest", access_token: accessToken, message: message, ai_model: ai_model, system_prompt: system_prompt })
|
||||
workerRef.current.onmessage = (e) => {
|
||||
HandleGetRequest(message,ai_model,system_prompt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<ConversationFrontend
|
||||
messages={messages}
|
||||
onResendClick={handleResendClick}
|
||||
onEditClick={handleEditClick}
|
||||
onCopyClick={handleCopyClick}
|
||||
/>
|
||||
<InputFrontend
|
||||
message=""
|
||||
onSendClick={handleSendClick}
|
||||
onMicClick={handleMicClick}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default InputOutputBackend
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
39
app/backend/ProcessAPI.js
Normal file
39
app/backend/ProcessAPI.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
import axios from 'axios'
|
||||
|
||||
onmessage = function (e) {
|
||||
const { functionName = "getAccess", access_token = "", message = "", ai_model = "phi3.5", system_prompt = "You are a helpful assistant" } = e.data
|
||||
const data = {
|
||||
"ai_model": ai_model,
|
||||
"message": message,
|
||||
"system_prompt": system_prompt,
|
||||
"access_token": access_token
|
||||
};
|
||||
switch (functionName) {
|
||||
case "getAccess":
|
||||
axios.get('https://127.0.0.1:5000/interstellar/api/ai_create')
|
||||
.then(Response => {
|
||||
postMessage(Response.data.access_token)
|
||||
}).catch(error => {
|
||||
console.error("Error with GET Token request:", error)
|
||||
})
|
||||
break
|
||||
case "postRequest":
|
||||
axios.post('https://127.0.0.1:5000/interstellar/api/ai_send', data)
|
||||
.then(Response => {
|
||||
postMessage(Response.data)
|
||||
}).catch(error => {
|
||||
console.error("Error:", error)
|
||||
})
|
||||
break
|
||||
case "getResponse":
|
||||
axios.get('https://127.0.0.1:5000/interstellar/api/ai_get?access_token=' + access_token)
|
||||
.then(Response => {
|
||||
postMessage(Response.data.response)
|
||||
}).catch(error => {
|
||||
console.error("Error with GET response request:", error)
|
||||
})
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
}
|
13
app/backend/ProcessMemory.tsx
Normal file
13
app/backend/ProcessMemory.tsx
Normal file
|
@ -0,0 +1,13 @@
|
|||
import React from 'react'
|
||||
|
||||
type Chat = {
|
||||
name:string
|
||||
messages:string[]
|
||||
}
|
||||
|
||||
type History = {
|
||||
chats:Chat[]
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue