fix convo fr fr

This commit is contained in:
YasinOnm08 2024-09-18 15:52:51 +02:00
parent 981b96dc04
commit 24c1957fb9
2 changed files with 53 additions and 39 deletions

View file

@ -1,4 +1,4 @@
import React, { ForwardedRef } from 'react'; import React, { ForwardedRef, useEffect, useRef } from 'react';
interface ConversationProps { interface ConversationProps {
messages: string[]; messages: string[];
@ -9,22 +9,32 @@ interface ConversationProps {
const ConversationFrontend = React.forwardRef<HTMLDivElement, ConversationProps>( const ConversationFrontend = React.forwardRef<HTMLDivElement, ConversationProps>(
({ messages, onResendClick, onEditClick, onCopyClick }, ref: ForwardedRef<HTMLDivElement>) => { ({ messages, onResendClick, onEditClick, onCopyClick }, ref: ForwardedRef<HTMLDivElement>) => {
const endOfMessagesRef = useRef<HTMLDivElement>(null);
// Auto-scroll to the bottom of the conversation whenever a new message is added
useEffect(() => {
if (endOfMessagesRef.current) {
endOfMessagesRef.current.scrollIntoView({ behavior: 'smooth' });
}
}, [messages]); // Triggers the effect whenever the 'messages' array changes
return ( return (
<div className="output"> <div className="output">
<div className="conversation resize" id="conversation" ref={ref}> <div className="conversation resize" id="conversation" ref={ref}>
{messages.map((message, index) => { {messages.map((message, index) => {
const isUserMessage = message.startsWith('User:'); const isUserMessage = message.startsWith('User:');
console.log("output: "+messages)
return ( return (
<div <div
key={index} key={index}
className={isUserMessage ? 'user-message' : 'ai-message'} className={isUserMessage ? 'user-message' : 'ai-message'}
> >
<p> <p> {message}</p>
{message}
</p>
</div> </div>
); );
})} })}
{/* Dummy div to mark the end of the conversation for auto-scrolling */}
<div ref={endOfMessagesRef} />
<div className="button-container"> <div className="button-container">
<button type="button" onClick={onResendClick}> <button type="button" onClick={onResendClick}>
<img src="/img/resend.svg" alt="resend" /> <img src="/img/resend.svg" alt="resend" />

View file

@ -1,40 +1,9 @@
import React from 'react'; import React, { useState } from 'react';
import InputFrontend from './InputFrontend'; import InputFrontend from './InputFrontend';
import ConversationFrontend from './ConversationFrontend'; import ConversationFrontend from './ConversationFrontend';
import { Mistral } from '@mistralai/mistralai'; import { Mistral } from '@mistralai/mistralai';
var messages = [
'User: Hello!',
'AI: Hi there!',
'User: How are you?',
'AI: Im good, thank you!'
];
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) {
messages.push('AI: ' + chatResponse.choices[0].message.content);
console.error('Error: Brain Not Found');
console.log(messages)
}
} else {
console.error('Error: Unexpected API response:', chatResponse);
}
}
const handleSendClick = (message: string) => {
messages.push('User: ' + message);
prompt_mistral("mistral-large-latest", message, "You are a helpful assistant.")
};
const handleMicClick = () => { const handleMicClick = () => {
console.log('Mic clicked!'); console.log('Mic clicked!');
@ -57,7 +26,42 @@ const handleCopyClick = () => {
// Handle copy action // Handle copy action
}; };
const InputBackend = () => { 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);
console.error('Error: Brain Not Found');
console.log(messages)
}
} else {
console.error('Error: Unexpected API response:', chatResponse);
}
}
const handleSendClick = (message: string) => {
addMessage('User: ' + message);
prompt_mistral("mistral-large-latest", message, "You are a helpful assistant.")
};
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 ( return (
<div> <div>
<ConversationFrontend <ConversationFrontend