Add echo only to LLM request, not stored chat history
This commit is contained in:
parent
cdada47eab
commit
6e2ad4e196
3 changed files with 32 additions and 26 deletions
|
|
@ -33,7 +33,7 @@ function calculateAvailableTokens(chatHistory, maxTokens) {
|
|||
console.log(`Token calculation: max=${maxTokens}, input≈${inputTokens}, buffer=${buffer}, available≈${availableTokens}`);
|
||||
return availableTokens;
|
||||
}
|
||||
import { initializeChatHistory, saveConversationHistory } from "./storage.js";
|
||||
import { initializeChatHistory, saveConversationHistory, getCustomPromptEcho } from "./storage.js";
|
||||
import { generateTitleForTTS, speakText } from "./tts.js";
|
||||
|
||||
// Helper function to add copy buttons to code blocks
|
||||
|
|
@ -106,19 +106,25 @@ export async function* sendMessage(message) {
|
|||
model = getSelectedModel();
|
||||
}
|
||||
|
||||
// Build messages for LLM: chatHistory + echo (echo is only for LLM attention, not stored)
|
||||
const echo = getCustomPromptEcho();
|
||||
const messagesForLLM = echo
|
||||
? [...chatHistory, { role: "assistant", content: echo }]
|
||||
: chatHistory;
|
||||
|
||||
const modelMaxTokens = getSelectedModelMaxTokens();
|
||||
const calculatedAvailable = calculateAvailableTokens(chatHistory, modelMaxTokens);
|
||||
|
||||
const calculatedAvailable = calculateAvailableTokens(messagesForLLM, modelMaxTokens);
|
||||
|
||||
// Cap to model's max completion tokens limit
|
||||
const maxCompletionTokens = getSelectedModelMaxCompletionTokens();
|
||||
// Use the full calculated available space or the model's limit, whichever is smaller
|
||||
const finalAvailable = Math.min(calculatedAvailable, maxCompletionTokens);
|
||||
|
||||
|
||||
console.log("Chat using tokens:", finalAvailable, "calculated:", calculatedAvailable, "max completion:", maxCompletionTokens);
|
||||
|
||||
|
||||
const requestBody = JSON.stringify({
|
||||
model: model,
|
||||
messages: chatHistory,
|
||||
messages: messagesForLLM,
|
||||
temperature: 0.5,
|
||||
max_tokens: finalAvailable,
|
||||
stream: true,
|
||||
|
|
@ -273,20 +279,26 @@ export async function* sendMessageWithCustomHistory(customHistory) {
|
|||
model = getSelectedModel();
|
||||
}
|
||||
|
||||
// Build messages for LLM: customHistory + echo (echo is only for LLM attention, not stored)
|
||||
const echo = getCustomPromptEcho();
|
||||
const messagesForLLM = echo
|
||||
? [...customHistory, { role: "assistant", content: echo }]
|
||||
: customHistory;
|
||||
|
||||
const modelMaxTokens = getSelectedModelMaxTokens();
|
||||
const calculatedAvailable = calculateAvailableTokens(customHistory, modelMaxTokens);
|
||||
|
||||
const calculatedAvailable = calculateAvailableTokens(messagesForLLM, modelMaxTokens);
|
||||
|
||||
// Cap to model's max completion tokens limit
|
||||
const maxCompletionTokens = getSelectedModelMaxCompletionTokens();
|
||||
const finalAvailable = Math.min(calculatedAvailable, maxCompletionTokens);
|
||||
console.log("Chat (custom history) using tokens:", finalAvailable, "calculated:", calculatedAvailable, "max completion:", maxCompletionTokens);
|
||||
|
||||
|
||||
const response = await fetch(apiUrl, {
|
||||
method: "POST",
|
||||
headers: headers,
|
||||
body: JSON.stringify({
|
||||
model: model,
|
||||
messages: customHistory,
|
||||
messages: messagesForLLM,
|
||||
max_tokens: finalAvailable,
|
||||
stream: true,
|
||||
}),
|
||||
|
|
|
|||
|
|
@ -35,7 +35,8 @@ export function loadConversationHistory() {
|
|||
}
|
||||
|
||||
// Get just the custom prompt for attention echo (not full system message with page content)
|
||||
function getCustomPromptEcho() {
|
||||
// This is exported so chat.js can add it only to LLM requests, not stored history
|
||||
export function getCustomPromptEcho() {
|
||||
if (typeof window !== "undefined" && window.UNCLOSEAI_SYSTEM_PROMPT) {
|
||||
return window.UNCLOSEAI_SYSTEM_PROMPT;
|
||||
}
|
||||
|
|
@ -45,20 +46,16 @@ function getCustomPromptEcho() {
|
|||
export async function clearConversationHistory() {
|
||||
localStorage.removeItem(getPageSpecificKey("hermes-conversation-history"));
|
||||
const systemContent = await getSystemMessage();
|
||||
const echo = getCustomPromptEcho();
|
||||
const history = [{ role: "system", content: systemContent }];
|
||||
if (echo) history.push({ role: "assistant", content: echo });
|
||||
return history;
|
||||
// Echo is NOT added here - it's only added at LLM request time in chat.js
|
||||
return [{ role: "system", content: systemContent }];
|
||||
}
|
||||
|
||||
// Initialize chat history
|
||||
export async function initializeChatHistory() {
|
||||
const systemContent = await getSystemMessage();
|
||||
const echo = getCustomPromptEcho();
|
||||
const history = [
|
||||
// Echo is NOT added here - it's only added at LLM request time in chat.js
|
||||
return [
|
||||
{ role: "system", content: systemContent },
|
||||
...loadConversationHistory(),
|
||||
];
|
||||
if (echo) history.push({ role: "assistant", content: echo });
|
||||
return history;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1496,12 +1496,11 @@ You have complete knowledge of this page content and can reference any details,
|
|||
const { getSystemMessage } = await import("./config.js");
|
||||
|
||||
// Create new history with updated system message and loaded conversation
|
||||
// Echo is NOT added here - it's only added at LLM request time in chat.js
|
||||
const systemContent = await getSystemMessage();
|
||||
const echo = (typeof window !== "undefined" && window.UNCLOSEAI_SYSTEM_PROMPT) ? window.UNCLOSEAI_SYSTEM_PROMPT : null;
|
||||
const newHistory = [
|
||||
{ role: "system", content: systemContent },
|
||||
...history,
|
||||
...(echo ? [{ role: "assistant", content: echo }] : []),
|
||||
];
|
||||
|
||||
// Update the chat.js module's history
|
||||
|
|
@ -1644,14 +1643,13 @@ You have complete knowledge of this page content and can reference any details,
|
|||
await saveConversationHistory(currentHistory);
|
||||
|
||||
// Update the chat.js module history to match localStorage
|
||||
// Echo is NOT added here - it's only added at LLM request time in chat.js
|
||||
const { updateChatHistory } = await import("./chat.js");
|
||||
const { getSystemMessage } = await import("./config.js");
|
||||
const systemContent = await getSystemMessage();
|
||||
const echo = (typeof window !== "undefined" && window.UNCLOSEAI_SYSTEM_PROMPT) ? window.UNCLOSEAI_SYSTEM_PROMPT : null;
|
||||
const newHistory = [
|
||||
{ role: "system", content: systemContent },
|
||||
...currentHistory,
|
||||
...(echo ? [{ role: "assistant", content: echo }] : []),
|
||||
];
|
||||
await updateChatHistory(newHistory);
|
||||
}
|
||||
|
|
@ -1965,13 +1963,12 @@ You have complete knowledge of this page content and can reference any details,
|
|||
const { updateChatHistory, getChatHistory } = await import("./chat.js");
|
||||
const { getSystemMessage } = await import("./config.js");
|
||||
|
||||
// Create new history with system message, intro, and echo for attention
|
||||
// Create new history with system message and intro
|
||||
// Echo is NOT added here - it's only added at LLM request time in chat.js
|
||||
const systemContent = await getSystemMessage();
|
||||
const echo = (typeof window !== "undefined" && window.UNCLOSEAI_SYSTEM_PROMPT) ? window.UNCLOSEAI_SYSTEM_PROMPT : null;
|
||||
const newHistory = [
|
||||
{ role: "system", content: systemContent },
|
||||
{ role: "assistant", content: response },
|
||||
...(echo ? [{ role: "assistant", content: echo }] : []),
|
||||
];
|
||||
|
||||
// Update the chat.js module's history
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue