fix chat and ui max_tokens errors with Groq models

- cap all max_tokens to model's max completion tokens limit
- fixes sendMessageWithCustomHistory in chat.js
- fixes intro generation in ui.js
- fixes TTS processing token calculation
- prevents "max_tokens must be less than or equal to 8192" errors
This commit is contained in:
Russell Ballestrini 2025-07-12 07:51:31 -04:00
parent 337f9970a8
commit 4c75f7cd6e
3 changed files with 26 additions and 10 deletions

View file

@ -2,7 +2,7 @@
import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js";
import hljs from "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/es/highlight.min.js";
import { API_KEY, getAPIConfig } from "./config.js";
import { getSelectedModel, getSelectedModelEndpoint, getSelectedModelMaxTokens } from "./models.js";
import { getSelectedModel, getSelectedModelEndpoint, getSelectedModelMaxTokens, getSelectedModelMaxCompletionTokens } from "./models.js";
// Rough token estimation (4 chars = 1 token average for English)
function estimateTokens(text) {
@ -93,7 +93,11 @@ export async function* sendMessage(message) {
const modelMaxTokens = getSelectedModelMaxTokens();
const calculatedAvailable = calculateAvailableTokens(chatHistory, modelMaxTokens);
console.log("Chat using calculated available tokens:", calculatedAvailable, "of model max:", modelMaxTokens);
// Cap to model's max completion tokens limit
const maxCompletionTokens = getSelectedModelMaxCompletionTokens();
const finalAvailable = Math.min(calculatedAvailable, maxCompletionTokens);
console.log("Chat using tokens:", finalAvailable, "calculated:", calculatedAvailable, "max completion:", maxCompletionTokens);
const response = await fetch(apiUrl, {
method: "POST",
@ -102,7 +106,7 @@ export async function* sendMessage(message) {
model: model,
messages: chatHistory,
temperature: 0.5,
max_tokens: calculatedAvailable,
max_tokens: finalAvailable,
stream: true,
}),
});
@ -256,8 +260,12 @@ export async function* sendMessageWithCustomHistory(customHistory) {
}
const modelMaxTokens = getSelectedModelMaxTokens();
const availableTokens = calculateAvailableTokens(customHistory, modelMaxTokens);
console.log("Chat (custom history) using max_tokens:", availableTokens);
const calculatedAvailable = calculateAvailableTokens(customHistory, 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",
@ -265,7 +273,7 @@ export async function* sendMessageWithCustomHistory(customHistory) {
body: JSON.stringify({
model: model,
messages: customHistory,
max_tokens: availableTokens,
max_tokens: finalAvailable,
stream: true,
}),
});

View file

@ -145,7 +145,11 @@ Your task:
Return only the article text ready for TTS, nothing else.`;
const inputTokens = Math.ceil((systemPrompt + preCleanedContent).length / 4);
const buffer = Math.max(2048, Math.floor(inputTokens * 1.5)); // Use 1.5x input tokens as buffer, minimum 2048
const availableTokens = Math.max(100, modelMaxTokens - inputTokens - buffer);
const calculatedAvailable = Math.max(100, modelMaxTokens - inputTokens - buffer);
// Cap to model's max completion tokens limit
const maxCompletionTokens = getSelectedModelMaxCompletionTokens();
const availableTokens = Math.min(calculatedAvailable, maxCompletionTokens);
const payload = {
model: getSelectedModel(),

View file

@ -138,7 +138,7 @@ async function* sendMessageWithCustomHistory(messageHistory) {
}
// Import getSelectedModelMaxTokens to get dynamic max tokens and calculate available
const { getSelectedModelMaxTokens } = await import("./models.js");
const { getSelectedModelMaxTokens, getSelectedModelMaxCompletionTokens } = await import("./models.js");
const modelMaxTokens = getSelectedModelMaxTokens();
// Estimate input tokens for intro generation
@ -146,7 +146,11 @@ async function* sendMessageWithCustomHistory(messageHistory) {
const inputTokens = Math.ceil(inputText.length / 4);
const buffer = Math.max(2048, Math.floor(inputTokens * 1.5)); // Use 1.5x input tokens as buffer, minimum 2048
const calculatedAvailable = Math.max(100, modelMaxTokens - inputTokens - buffer);
console.log("UI (intro generation) using calculated available tokens:", calculatedAvailable, "buffer:", buffer, "of model max:", modelMaxTokens);
// Cap to model's max completion tokens limit
const maxCompletionTokens = getSelectedModelMaxCompletionTokens();
const finalAvailable = Math.min(calculatedAvailable, maxCompletionTokens);
console.log("UI (intro generation) using tokens:", finalAvailable, "calculated:", calculatedAvailable, "max completion:", maxCompletionTokens, "of model max:", modelMaxTokens);
const response = await fetch(apiUrl, {
method: "POST",
@ -155,7 +159,7 @@ async function* sendMessageWithCustomHistory(messageHistory) {
model: model,
messages: messageHistory,
temperature: 0.3,
max_tokens: calculatedAvailable,
max_tokens: finalAvailable,
stream: true,
}),
});