Merge branch 'sageTheDm-main'

This commit is contained in:
Patrick_Pluto 2024-09-20 11:17:28 +02:00
commit e08b8e0783
18 changed files with 16 additions and 14 deletions

View 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

View 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: Im 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;

View file

@ -0,0 +1,100 @@
"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)
type Message = {
role: string
content: string
}
const handleSendClick = (message:string) => {
var system:Message = {role:"system" ,content:"You are a helpful assistant."}
addMessage("user", message);
console.log("added User Message")
HandlePostRequest([...messages, { role: "user", content: message }], "phi3.5", system);
};
const [messages, setMessages] = useState([
{ role:"assistant", content:'Hello. I\'m Your AI Virtual Assistant' }
]);
const addMessage = (role:string ,content: string) => {
setMessages((prevMessages) => [...prevMessages, {role,content}]);
};
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 HandlePostRequest = (messages: Message[], ai_model: string, system_prompt: Message) => {
if (workerRef.current) {
workerRef.current.postMessage({ functionName: "postRequest", access_token: accessToken, messages: messages, ai_model: ai_model, system_prompt: system_prompt })
workerRef.current.onmessage = (e) => {
addMessage("assistant",e.data)
}
}
}
return (
<div>
<ConversationFrontend
messages={messages}
onResendClick={handleResendClick}
onEditClick={handleEditClick}
onCopyClick={handleCopyClick}
/>
<InputFrontend
message=""
onSendClick={handleSendClick}
onMicClick={handleMicClick}
/>
</div>
)
}
export default InputOutputBackend

51
app/backend/ProcessAPI.js Normal file
View file

@ -0,0 +1,51 @@
import axios from 'axios'
import { type } from 'os';
onmessage = function (e) {
const { functionName = "getAccess", access_token = "", messages = [], ai_model = "phi3.5", system_prompt = {role:"system" ,content: "You are a helpful assistant that gives short answers"}} = e.data
let data = {
ai_model: ai_model,
messages: messages,
access_token: access_token
};
const getResponse = () => {
messageComplete:boolean = false
while(!messageComplete)
axios.get('https://localhost:5000/interstellar/api/ai_get?access_token=' + access_token)
.then(Response => {
postMessage(Response.data.response)
if (Response.data.status == 200) {
messageComplete = true
}
}).catch(error => {
console.error("Error with GET response request:", error)
})
}
switch (functionName) {
case "getAccess":
console.log("getting access...")
axios.get('https://localhost: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":
messages.unshift(system_prompt)
console.log("sending...")
console.log(messages)
axios.post('https://localhost:5000/interstellar/api/ai_send', data)
.then(Response => {
getResponse()
}).catch(error => {
console.error("Error:", error)
})
break
}
}

View file

@ -0,0 +1,13 @@
import React from 'react'
type Chat = {
name:string
messages:string[]
}
type History = {
chats:Chat[]
}