interstellar_ai/app/InputBackend.tsx

88 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-09-18 15:52:51 +02:00
import React, { useState } from 'react';
2024-09-18 14:52:04 +02:00
import InputFrontend from './InputFrontend';
import ConversationFrontend from './ConversationFrontend';
import { Mistral } from '@mistralai/mistralai';
2024-09-18 15:03:32 +02:00
2024-09-18 14:52:04 +02:00
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
};
2024-09-18 16:16:53 +02:00
const InputBackend: React.FC = () => {
2024-09-18 15:52:51 +02:00
async function prompt_mistral(model: string, prompt: string, system: string) {
const apiKey = "m3kZRjN8DRSIo88r8Iti9hmKGWIklrLY";
2024-09-18 16:16:53 +02:00
2024-09-18 15:52:51 +02:00
const client = new Mistral({ apiKey: apiKey });
2024-09-18 16:16:53 +02:00
2024-09-18 15:52:51 +02:00
var chatResponse = await client.chat.complete({
model: model,
messages: [{ role: 'user', content: prompt }, { role: 'system', content: system, }],
});
2024-09-18 16:16:53 +02:00
2024-09-18 15:52:51 +02:00
if (chatResponse && chatResponse.choices && chatResponse.choices.length > 0) {
if (chatResponse.choices[0].message.content) {
addMessage('AI: ' + chatResponse.choices[0].message.content);
console.log(messages)
}
} else {
console.error('Error: Unexpected API response:', chatResponse);
}
}
2024-09-18 16:16:53 +02:00
2024-09-18 15:52:51 +02:00
const handleSendClick = (message: string) => {
2024-09-18 16:16:53 +02:00
var system = "You are a helpful assistant. The following is the chat history."
for (let index = 0; index < messages.length; index++) {
system += messages[index] + " ";
};
2024-09-18 15:52:51 +02:00
addMessage('User: ' + message);
2024-09-18 16:16:53 +02:00
prompt_mistral("mistral-large-latest", message, system)
2024-09-18 15:52:51 +02:00
};
2024-09-18 16:16:53 +02:00
2024-09-18 15:52:51 +02:00
const [messages, setMessages] = useState([
'User: Hello!',
'AI: Hi there!',
'User: How are you?',
'AI: Im good, thank you!'
]);
2024-09-18 16:16:53 +02:00
2024-09-18 15:52:51 +02:00
const addMessage = (message: string) => {
setMessages((prevMessages) => [...prevMessages, message]);
};
2024-09-18 14:52:04 +02:00
return (
<div>
<ConversationFrontend
messages={messages}
onResendClick={handleResendClick}
onEditClick={handleEditClick}
onCopyClick={handleCopyClick}
2024-09-18 16:16:53 +02:00
/>
2024-09-18 14:52:04 +02:00
<InputFrontend
message=""
onSendClick={handleSendClick}
onMicClick={handleMicClick}
2024-09-18 16:16:53 +02:00
/>
2024-09-18 14:52:04 +02:00
</div>
);
};
export default InputBackend;