[feat] add llm translation
This commit is contained in:
@@ -520,7 +520,7 @@ export interface Database{
|
||||
mancerHeader:string
|
||||
emotionProcesser:'submodel'|'embedding',
|
||||
showMenuChatList?:boolean,
|
||||
translatorType:'google'|'deepl'|'none',
|
||||
translatorType:'google'|'deepl'|'none'|'llm',
|
||||
NAIadventure?:boolean,
|
||||
NAIappendName?:boolean,
|
||||
deeplOptions:{
|
||||
@@ -541,6 +541,7 @@ export interface Database{
|
||||
automark?:boolean
|
||||
huggingfaceKey:string
|
||||
allowAllExtentionFiles?:boolean
|
||||
translatorPrompt:string
|
||||
}
|
||||
|
||||
export interface customscript{
|
||||
|
||||
@@ -3,6 +3,8 @@ import { translatorPlugin } from "../plugins/plugins"
|
||||
import { DataBase } from "../storage/database"
|
||||
import { globalFetch } from "../storage/globalApi"
|
||||
import { alertError } from "../alert"
|
||||
import { requestChatData } from "../process/request"
|
||||
import { doingChat } from "../process"
|
||||
|
||||
let cache={
|
||||
origin: [''],
|
||||
@@ -98,12 +100,16 @@ export async function runTranslator(text:string, reverse:boolean, from:string,ta
|
||||
|
||||
async function translateMain(text:string, arg:{from:string, to:string, host:string}){
|
||||
let db = get(DataBase)
|
||||
|
||||
if(db.translatorType === 'llm'){
|
||||
const tr = db.translator || 'en'
|
||||
return translateLLM(text, {to: tr})
|
||||
}
|
||||
if(db.translatorType === 'deepl'){
|
||||
let url = db.deeplOptions.freeApi ? "https://api-free.deepl.com/v2/translate" : "https://api.deepl.com/v2/translate"
|
||||
const f = await globalFetch(url, {
|
||||
headers: {
|
||||
"Authorization": "DeepL-Auth-Key " + db.deeplOptions.key,
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: {
|
||||
text: text,
|
||||
@@ -162,9 +168,23 @@ async function jaTrans(text:string) {
|
||||
return await runTranslator(text, true, 'en','ja')
|
||||
}
|
||||
|
||||
|
||||
export function isExpTranslator(){
|
||||
const db = get(DataBase)
|
||||
return db.translatorType === 'llm' || db.translatorType === 'deepl'
|
||||
}
|
||||
|
||||
export async function translateHTML(html: string, reverse:boolean): Promise<string> {
|
||||
let db = get(DataBase)
|
||||
let DoingChat = get(doingChat)
|
||||
if(DoingChat){
|
||||
if(isExpTranslator()){
|
||||
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)
|
||||
|
||||
@@ -219,4 +239,35 @@ export async function translateHTML(html: string, reverse:boolean): Promise<stri
|
||||
// console.log(translatedHTML)
|
||||
// Return the translated HTML, excluding the outer <body> tags if needed
|
||||
return translatedHTML
|
||||
}
|
||||
|
||||
let llmCache = new Map<string, string>()
|
||||
async function translateLLM(text:string, arg:{to:string}){
|
||||
if(llmCache.has(text)){
|
||||
return llmCache.get(text)
|
||||
}
|
||||
const db = get(DataBase)
|
||||
let prompt = db.translatorPrompt || `You are a translator. translate the following html or text into {{slot}}. do not output anything other than the translation.`
|
||||
prompt = prompt.replace('{{slot}}', arg.to)
|
||||
const rq = await requestChatData({
|
||||
formated: [
|
||||
{
|
||||
'role': 'system',
|
||||
'content': prompt
|
||||
},
|
||||
{
|
||||
'role': 'user',
|
||||
'content': text
|
||||
}
|
||||
],
|
||||
bias: {},
|
||||
useStreaming: false,
|
||||
}, 'submodel')
|
||||
|
||||
if(rq.type === 'fail' || rq.type === 'streaming' || rq.type === 'multiline'){
|
||||
alertError(`${rq.result}`)
|
||||
return text
|
||||
}
|
||||
llmCache.set(text, rq.result)
|
||||
return rq.result
|
||||
}
|
||||
Reference in New Issue
Block a user