Fix token estimation: use 3 chars/token instead of 4
This commit is contained in:
parent
bd312f0979
commit
1ec196275f
4 changed files with 6 additions and 6 deletions
|
|
@ -5,9 +5,9 @@ import { API_KEY, getAPIConfig } from "./config.js";
|
|||
import { getSelectedModel, getSelectedModelEndpoint, getSelectedModelMaxTokens, getSelectedModelMaxCompletionTokens } from "./models.js";
|
||||
import { groqAwareStreamingFetch } from "./groq-fetch.js";
|
||||
|
||||
// Rough token estimation (4 chars = 1 token average for English)
|
||||
// Rough token estimation (3 chars = 1 token to be conservative)
|
||||
function estimateTokens(text) {
|
||||
return Math.ceil(text.length / 4);
|
||||
return Math.ceil(text.length / 3);
|
||||
}
|
||||
|
||||
// Calculate available output tokens
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ async function* sendMessageWithHistory(messageHistory) {
|
|||
// Calculate available tokens for output based on current prompt
|
||||
const modelMaxTokens = getSelectedModelMaxTokens();
|
||||
const inputText = messageHistory.map(msg => msg.content).join('');
|
||||
const inputTokens = Math.ceil(inputText.length / 4); // Simple token estimation
|
||||
const inputTokens = Math.ceil(inputText.length / 3); // Conservative token estimation
|
||||
const outputReserve = Math.floor(modelMaxTokens * 0.4); // Reserve 40% for output
|
||||
let availableTokens = Math.max(512, modelMaxTokens - inputTokens - outputReserve);
|
||||
|
||||
|
|
|
|||
|
|
@ -143,7 +143,7 @@ Your task:
|
|||
7. Start with the article title, then the main content
|
||||
|
||||
Return only the article text ready for TTS, nothing else.`;
|
||||
const inputTokens = Math.ceil((systemPrompt + preCleanedContent).length / 4);
|
||||
const inputTokens = Math.ceil((systemPrompt + preCleanedContent).length / 3);
|
||||
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);
|
||||
|
||||
|
|
|
|||
|
|
@ -141,9 +141,9 @@ async function* sendMessageWithCustomHistory(messageHistory) {
|
|||
const { getSelectedModelMaxTokens, getSelectedModelMaxCompletionTokens } = await import("./models.js");
|
||||
const modelMaxTokens = getSelectedModelMaxTokens();
|
||||
|
||||
// Estimate input tokens for intro generation
|
||||
// Estimate input tokens for intro generation (3 chars = 1 token to be conservative)
|
||||
const inputText = messageHistory.map(msg => msg.content).join('');
|
||||
const inputTokens = Math.ceil(inputText.length / 4);
|
||||
const inputTokens = Math.ceil(inputText.length / 3);
|
||||
|
||||
// For intro generation, be more generous - we want complete intros
|
||||
// With large context models, we have plenty of room
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue