Improve LLM Translation (#477)

# PR Checklist
- [x] Did you check if it works normally in all models? *ignore this
when it dosen't uses models*
- [x] Did you check if it works normally in all of web, local and node
hosted versions? if it dosen't, did you blocked it in those versions?
- [x] Did you added a type def?

# Description
1. LLM 번역 응답 크기 소프트코딩화
2. LLM도 DOM파서를 통한 문장 단위 및 병렬 번역을 하도록 수정
3. 바깥쪽 태그가 제대로 제거가 안되는 문제 해결
4. 추가적으로, 기본 번역 프롬프트를 ```You are a translator. Translate the following
html or text into {{slot}}. Do not output anything other than the
translation, punctuations and quotation marks.```으로 변경하고 싶습니다.
------
1. Softcoded LLM translation response size
2. Modified LLM to also do sentence-by-sentence and parallel translation
via DOM parser
3. Fixed an issue with outer tags not being removed properly
4. Additionally, I want to change the default translation prompt to
```You are a translator. Translate the following html or text into
{{slot}}. Do not output anything other than the translation,
punctuations and quotation marks.```
This commit is contained in:
kwaroran
2024-06-03 17:33:15 +09:00
committed by GitHub
7 changed files with 21 additions and 11 deletions

View File

@@ -93,6 +93,9 @@ export function setDatabase(data:Database){
if(checkNullish(data.translator)){
data.translator = ''
}
if(checkNullish(data.translatorMaxResponse)){
data.translatorMaxResponse = 1000
}
if(checkNullish(data.currentPluginProvider)){
data.currentPluginProvider = ''
}
@@ -610,6 +613,7 @@ export interface Database{
huggingfaceKey:string
allowAllExtentionFiles?:boolean
translatorPrompt:string
translatorMaxResponse:number
top_p: number,
google: {
accessToken: string
@@ -897,6 +901,7 @@ export interface botPreset{
useInstructPrompt?:boolean
customPromptTemplateToggle?:string
templateDefaultVariables?:string
translatorMaxResponse: number
}
@@ -1101,7 +1106,7 @@ export const presetTemplate:botPreset = {
},
top_p: 1,
useInstructPrompt: false,
translatorMaxResponse: 1000,
}
const defaultSdData:[string,string][] = [
@@ -1165,7 +1170,8 @@ export function saveCurrentPreset(){
openrouterProvider: db.openrouterProvider,
useInstructPrompt: db.useInstructPrompt,
customPromptTemplateToggle: db.customPromptTemplateToggle ?? "",
templateDefaultVariables: db.templateDefaultVariables ?? ""
templateDefaultVariables: db.templateDefaultVariables ?? "",
translatorMaxResponse: db.translatorMaxResponse
}
db.botPresets = pres
setDatabase(db)
@@ -1250,6 +1256,7 @@ export function setPreset(db:Database, newPres: botPreset){
db.useInstructPrompt = newPres.useInstructPrompt ?? false
db.customPromptTemplateToggle = newPres.customPromptTemplateToggle ?? ''
db.templateDefaultVariables = newPres.templateDefaultVariables ?? ''
db.translatorMaxResponse = newPres.translatorMaxResponse ?? db.translatorMaxResponse
return db
}

View File

@@ -238,10 +238,7 @@ export async function translateHTML(html: string, reverse:boolean, charArg:simpl
return html
}
}
if(db.translatorType === 'llm'){
const tr = db.translator || 'en'
return translateLLM(html, {to: tr})
}
const dom = new DOMParser().parseFromString(html, 'text/html');
console.log(html)
@@ -416,8 +413,8 @@ export async function translateHTML(html: string, reverse:boolean, charArg:simpl
// Serialize the DOM back to HTML
const serializer = new XMLSerializer();
let translatedHTML = serializer.serializeToString(dom);
// Remove the outer <body> tags
translatedHTML = translatedHTML.replace(/^<body[^>]*>|<\/body>$/g, '');
// Remove the outer <html|body|head> tags
translatedHTML = translatedHTML.replace(/<\/?(html|body|head)[^>]*>/g, '');
if(charArg !== ''){
let scripts:customscript[] = []
@@ -464,7 +461,7 @@ async function translateLLM(text:string, arg:{to:string}){
bias: {},
useStreaming: false,
noMultiGen: true,
maxTokens: 1000,
maxTokens: db.translatorMaxResponse,
}, 'submodel')
if(rq.type === 'fail' || rq.type === 'streaming' || rq.type === 'multiline'){