add complete chat history

This commit is contained in:
Christian Rute 2024-10-31 15:02:07 +01:00
parent 08b5b62442
commit cd00e28fd7
2 changed files with 16 additions and 10 deletions

View File

@ -310,8 +310,7 @@ main {
#chatBox {
display: flex;
/*flex-direction: column-reverse; /* flex-direction: column-reverse; */
flex-direction: column; /* normale Richtung */
flex-direction: column-reverse;
overflow-y: auto;
}

View File

@ -39,7 +39,7 @@ function createNewChat() {
async function saveMessage(chatId, message) {
const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || [];
chatHistory.push({ user: "user", text: message });
chatHistory.push({ role: "user", content: message });
localStorage.setItem(`chatHistory-${chatId}`, JSON.stringify(chatHistory));
// Aktualisiere den Button-Text in der Sidebar
@ -51,7 +51,7 @@ async function saveMessage(chatId, message) {
let a = await stream_api_open_ai(userMessage);
console.log("Komplette Nachricht: " + a)
const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || [];
chatHistory.push({ user: "bot", text: a });
chatHistory.push({ role: "assistant", content: a });
localStorage.setItem(`chatHistory-${chatId}`, JSON.stringify(chatHistory));
}
}
@ -61,7 +61,7 @@ function updateChatSessionButton(chatId, message) {
if (button) {
const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || [];
// Hole die erste Nachricht oder lasse es leer, wenn keine vorhanden ist
const chatName = chatHistory.length > 0 ? chatHistory[0].text.slice(0, 10) : ''; // Nimm die ersten 10 Zeichen der ersten Nachricht
const chatName = chatHistory.length > 0 ? chatHistory[0].content.slice(0, 10) : ''; // Nimm die ersten 10 Zeichen der ersten Nachricht
button.innerText = chatName + "..." || `Chat ${chatId + 1}`; // Falls leer, setze auf "Chat x"
}
}
@ -115,8 +115,8 @@ function loadChatHistory(chatId) {
const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || [];
chatHistory.forEach(entry => {
const messageElement = document.createElement('div');
messageElement.classList.add('chat-message', entry.user);
messageElement.innerText = entry.text;
messageElement.classList.add('chat-message', entry.role);
messageElement.innerText = entry.content;
chatBox.prepend(messageElement);
});
}
@ -208,16 +208,23 @@ async function getModels() {
}
}
function getAllCurrentChatMessages() {
const cookieData = JSON.parse(localStorage.getItem(`chatHistory-${currentChatId}`)) || [];
return cookieData
}
function getLastUserMessage() {
// Lade die Cookie-Daten (ersetze dies durch den tatsächlichen Weg zur Cookie-Datenstruktur)
const cookieData = JSON.parse(localStorage.getItem(`chatHistory-${currentChatId}`)) || [];
// Filter nur die User-Nachrichten heraus
const userMessages = cookieData.filter(message => message.user === "user");
const userMessages = cookieData.filter(message => message.role === "user");
// Falls es User-Nachrichten gibt, die letzte davon zurückgeben
if (userMessages.length > 0) {
return userMessages[userMessages.length - 1].text;
return userMessages[userMessages.length - 1].content;
}
return ''; // Rückgabe eines leeren Strings, falls keine Nachricht gefunden wird
@ -240,7 +247,7 @@ async function stream_api_open_ai(userMessage) {
model: await getModels(),
messages: [
{ role: 'system', content: 'You are a knowledgeable assistant.' },
{ role: 'user', content: userMessage }
...getAllCurrentChatMessages()
],
stream: true,
temperature: 0.3