add complete chat history
This commit is contained in:
parent
08b5b62442
commit
cd00e28fd7
@ -310,8 +310,7 @@ main {
|
|||||||
|
|
||||||
#chatBox {
|
#chatBox {
|
||||||
display: flex;
|
display: flex;
|
||||||
/*flex-direction: column-reverse; /* flex-direction: column-reverse; */
|
flex-direction: column-reverse;
|
||||||
flex-direction: column; /* normale Richtung */
|
|
||||||
overflow-y: auto;
|
overflow-y: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ function createNewChat() {
|
|||||||
|
|
||||||
async function saveMessage(chatId, message) {
|
async function saveMessage(chatId, message) {
|
||||||
const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || [];
|
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));
|
localStorage.setItem(`chatHistory-${chatId}`, JSON.stringify(chatHistory));
|
||||||
|
|
||||||
// Aktualisiere den Button-Text in der Sidebar
|
// Aktualisiere den Button-Text in der Sidebar
|
||||||
@ -51,7 +51,7 @@ async function saveMessage(chatId, message) {
|
|||||||
let a = await stream_api_open_ai(userMessage);
|
let a = await stream_api_open_ai(userMessage);
|
||||||
console.log("Komplette Nachricht: " + a)
|
console.log("Komplette Nachricht: " + a)
|
||||||
const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || [];
|
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));
|
localStorage.setItem(`chatHistory-${chatId}`, JSON.stringify(chatHistory));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,7 +61,7 @@ function updateChatSessionButton(chatId, message) {
|
|||||||
if (button) {
|
if (button) {
|
||||||
const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || [];
|
const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || [];
|
||||||
// Hole die erste Nachricht oder lasse es leer, wenn keine vorhanden ist
|
// 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"
|
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}`)) || [];
|
const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || [];
|
||||||
chatHistory.forEach(entry => {
|
chatHistory.forEach(entry => {
|
||||||
const messageElement = document.createElement('div');
|
const messageElement = document.createElement('div');
|
||||||
messageElement.classList.add('chat-message', entry.user);
|
messageElement.classList.add('chat-message', entry.role);
|
||||||
messageElement.innerText = entry.text;
|
messageElement.innerText = entry.content;
|
||||||
chatBox.prepend(messageElement);
|
chatBox.prepend(messageElement);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -208,16 +208,23 @@ async function getModels() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getAllCurrentChatMessages() {
|
||||||
|
const cookieData = JSON.parse(localStorage.getItem(`chatHistory-${currentChatId}`)) || [];
|
||||||
|
return cookieData
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function getLastUserMessage() {
|
function getLastUserMessage() {
|
||||||
// Lade die Cookie-Daten (ersetze dies durch den tatsächlichen Weg zur Cookie-Datenstruktur)
|
// Lade die Cookie-Daten (ersetze dies durch den tatsächlichen Weg zur Cookie-Datenstruktur)
|
||||||
const cookieData = JSON.parse(localStorage.getItem(`chatHistory-${currentChatId}`)) || [];
|
const cookieData = JSON.parse(localStorage.getItem(`chatHistory-${currentChatId}`)) || [];
|
||||||
|
|
||||||
|
|
||||||
// Filter nur die User-Nachrichten heraus
|
// 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
|
// Falls es User-Nachrichten gibt, die letzte davon zurückgeben
|
||||||
if (userMessages.length > 0) {
|
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
|
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(),
|
model: await getModels(),
|
||||||
messages: [
|
messages: [
|
||||||
{ role: 'system', content: 'You are a knowledgeable assistant.' },
|
{ role: 'system', content: 'You are a knowledgeable assistant.' },
|
||||||
{ role: 'user', content: userMessage }
|
...getAllCurrentChatMessages()
|
||||||
],
|
],
|
||||||
stream: true,
|
stream: true,
|
||||||
temperature: 0.3
|
temperature: 0.3
|
||||||
|
Loading…
Reference in New Issue
Block a user