apply 2048 token buffer to all upstream API requests: translation and TTS now calculate available tokens properly
This commit is contained in:
parent
3a4952087d
commit
8b0fe6d2ac
2 changed files with 23 additions and 12 deletions
|
|
@ -70,10 +70,15 @@ async function* sendMessageWithHistory(messageHistory) {
|
|||
model = getSelectedModel();
|
||||
}
|
||||
|
||||
const maxTokens = getSelectedModelMaxTokens();
|
||||
// Calculate available tokens based on input length
|
||||
const modelMaxTokens = getSelectedModelMaxTokens();
|
||||
const inputText = messageHistory.map(msg => msg.content).join('');
|
||||
const inputTokens = Math.ceil(inputText.length / 4); // Simple token estimation
|
||||
const availableTokens = Math.max(100, modelMaxTokens - inputTokens - 2048); // 2048 token buffer
|
||||
|
||||
console.log("Translation using endpoint:", apiUrl);
|
||||
console.log("Translation using model:", model);
|
||||
console.log("Translation using max_tokens:", maxTokens);
|
||||
console.log("Translation token calculation: max=" + modelMaxTokens + ", input≈" + inputTokens + ", available≈" + availableTokens);
|
||||
|
||||
const response = await fetch(apiUrl, {
|
||||
method: "POST",
|
||||
|
|
@ -82,7 +87,7 @@ async function* sendMessageWithHistory(messageHistory) {
|
|||
model: model,
|
||||
messages: messageHistory,
|
||||
temperature: 0.3, // Lower temperature for more consistent translation
|
||||
max_tokens: maxTokens,
|
||||
max_tokens: availableTokens,
|
||||
stream: true,
|
||||
}),
|
||||
});
|
||||
|
|
|
|||
24
src/tts.js
24
src/tts.js
|
|
@ -1,6 +1,6 @@
|
|||
// Text-to-speech functionality
|
||||
import { API_KEY, MODEL, setLastTTS, TTS_API_URL } from "./config.js";
|
||||
import { getSelectedModel, getSelectedModelEndpoint } from "./models.js";
|
||||
import { getSelectedModel, getSelectedModelEndpoint, getSelectedModelMaxTokens } from "./models.js";
|
||||
|
||||
// Simple JavaScript text cleaning for TTS
|
||||
function cleanTextForTTS(text) {
|
||||
|
|
@ -129,12 +129,9 @@ export async function extractSpokenTokens(content) {
|
|||
.replace(/\s+/g, ' ')
|
||||
.trim();
|
||||
|
||||
const payload = {
|
||||
model: getSelectedModel(),
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: `You are extracting the main article content for text-to-speech. The input may be markdown or pre-cleaned HTML.
|
||||
// Calculate available tokens for TTS processing
|
||||
const modelMaxTokens = getSelectedModelMaxTokens();
|
||||
const systemPrompt = `You are extracting the main article content for text-to-speech. The input may be markdown or pre-cleaned HTML.
|
||||
|
||||
Your task:
|
||||
1. Find and extract the main article content (title, subtitle, body text)
|
||||
|
|
@ -145,7 +142,16 @@ Your task:
|
|||
6. Remove any remaining HTML tags but keep the text inside them
|
||||
7. Start with the article title, then the main content
|
||||
|
||||
Return only the article text ready for TTS, nothing else.`,
|
||||
Return only the article text ready for TTS, nothing else.`;
|
||||
const inputTokens = Math.ceil((systemPrompt + preCleanedContent).length / 4);
|
||||
const availableTokens = Math.max(100, modelMaxTokens - inputTokens - 2048);
|
||||
|
||||
const payload = {
|
||||
model: getSelectedModel(),
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: systemPrompt,
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
|
|
@ -153,7 +159,7 @@ Return only the article text ready for TTS, nothing else.`,
|
|||
},
|
||||
],
|
||||
temperature: 0,
|
||||
max_tokens: 76000,
|
||||
max_tokens: availableTokens,
|
||||
};
|
||||
|
||||
console.log("Processing page content for TTS...");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue