fix(translate): improve auto translate language detection (#9375)

fix(translate): 调整语言检测阈值并增加回退逻辑

当文本较短时使用LLM检测语言,较长时优先使用franc检测
当franc检测失败时回退到LLM检测
同时将LLM检测的文本长度限制从50提高到100
This commit is contained in:
Phantom 2025-08-21 14:55:11 +08:00 committed by GitHub
parent 4dabc214f2
commit 4615e97ad5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -29,7 +29,15 @@ export const detectLanguage = async (inputText: string): Promise<TranslateLangua
switch (method) {
case 'auto':
// hard encoded threshold
result = estimateTextTokens(text) < 50 ? await detectLanguageByLLM(text) : detectLanguageByFranc(text)
if (estimateTextTokens(text) < 100) {
result = await detectLanguageByLLM(text)
} else {
result = detectLanguageByFranc(text)
// fallback to llm when franc fails
if (result === UNKNOWN.langCode) {
result = await detectLanguageByLLM(text)
}
}
break
case 'franc':
result = detectLanguageByFranc(text)
@ -48,7 +56,7 @@ const detectLanguageByLLM = async (inputText: string): Promise<TranslateLanguage
logger.info('Detect langugage by llm')
let detectedLang = ''
await fetchLanguageDetection({
text: sliceByTokens(inputText, 0, 50),
text: sliceByTokens(inputText, 0, 100),
onResponse: (text) => {
detectedLang = text.replace(/^\s*\n+/g, '')
}