mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-17 05:47:11 +00:00
fix: 添加基于LLM的翻译
This commit is contained in:
parent
e5e85621d9
commit
e1d40ba009
6 changed files with 341 additions and 215 deletions
|
|
@ -177,6 +177,7 @@ const defaultConfig = {
|
|||
chatglmRefreshToken: '',
|
||||
sunoSessToken: '',
|
||||
sunoClientToken: '',
|
||||
translateSource: 'openai',
|
||||
version: 'v2.7.10'
|
||||
}
|
||||
const _path = process.cwd()
|
||||
|
|
|
|||
|
|
@ -1,5 +1,13 @@
|
|||
import md5 from 'md5'
|
||||
import _ from 'lodash'
|
||||
import { Config } from './config.js'
|
||||
import { ChatGPTAPI } from './openai/chatgpt-api.js'
|
||||
import { newFetch } from './proxy.js'
|
||||
import { CustomGoogleGeminiClient } from '../client/CustomGoogleGeminiClient.js'
|
||||
import XinghuoClient from './xinghuo/xinghuo.js'
|
||||
import {getImg, getMessageById, upsertMessage} from './common.js'
|
||||
import {QwenApi} from "./alibaba/qwen-api.js";
|
||||
import {v4 as uuid} from "uuid";
|
||||
|
||||
// 代码参考:https://github.com/yeyang52/yenai-plugin/blob/b50b11338adfa5a4ef93912eefd2f1f704e8b990/model/api/funApi.js#L25
|
||||
export const translateLangSupports = [
|
||||
|
|
@ -20,7 +28,7 @@ export const translateLangSupports = [
|
|||
{ code: 'zh-CHS', label: '中文', abbr: '中', alphabet: 'Z' }
|
||||
]
|
||||
const API_ERROR = '出了点小问题,待会再试试吧'
|
||||
export async function translate (msg, to = 'auto') {
|
||||
export async function translateOld (msg, to = 'auto') {
|
||||
let from = 'auto'
|
||||
if (to !== 'auto') to = translateLangSupports.find(item => item.abbr == to)?.code
|
||||
if (!to) return `未找到翻译的语种,支持的语言为:\n${translateLangSupports.map(item => item.abbr).join(',')}\n`
|
||||
|
|
@ -95,3 +103,101 @@ export async function translate (msg, to = 'auto') {
|
|||
return API_ERROR
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param msg 要翻译的
|
||||
* @param from 语种
|
||||
* @param to 语种
|
||||
* @param ai ai来源,支持openai, gemini, xh, qwen
|
||||
* @returns {Promise<*|string>}
|
||||
*/
|
||||
export async function translate (msg, to = '中', from = 'auto', ai = Config.translateSource) {
|
||||
try {
|
||||
const lang = translateLangSupports.find(item => item.abbr == to)?.code
|
||||
if (!lang) return `未找到翻译的语种,支持的语言为:\n${translateLangSupports.map(item => item.abbr).join(',')}\n`
|
||||
// if ai is not in the list, throw error
|
||||
if (!['openai', 'gemini', 'xh', 'qwen'].includes(ai)) throw new Error('ai来源错误')
|
||||
let system = `You will be provided with a sentence in the language with language code [${from}], and your task is to translate it into [${lang}]. Just print the result without any other words.`
|
||||
switch (ai) {
|
||||
case 'openai': {
|
||||
let api = new ChatGPTAPI({
|
||||
apiBaseUrl: Config.openAiBaseUrl,
|
||||
apiKey: Config.apiKey,
|
||||
fetch: newFetch
|
||||
})
|
||||
const res = await api.sendMessage(msg, {
|
||||
systemMessage: system,
|
||||
completionParams: {
|
||||
model: 'gpt-3.5-turbo'
|
||||
}
|
||||
})
|
||||
return res.text
|
||||
}
|
||||
case 'gemini': {
|
||||
let client = new CustomGoogleGeminiClient({
|
||||
key: Config.geminiKey,
|
||||
model: Config.geminiModel,
|
||||
baseUrl: Config.geminiBaseUrl,
|
||||
debug: Config.debug
|
||||
})
|
||||
let option = {
|
||||
stream: false,
|
||||
onProgress: (data) => {
|
||||
if (Config.debug) {
|
||||
logger.info(data)
|
||||
}
|
||||
},
|
||||
system
|
||||
}
|
||||
let res = await client.sendMessage(msg, option)
|
||||
return res.text
|
||||
}
|
||||
case 'xh': {
|
||||
let client = new XinghuoClient({
|
||||
ssoSessionId: Config.xinghuoToken
|
||||
})
|
||||
let response = await client.sendMessage(msg, { system })
|
||||
return response.text
|
||||
}
|
||||
case 'qwen': {
|
||||
let completionParams = {
|
||||
parameters: {
|
||||
top_p: Config.qwenTopP || 0.5,
|
||||
top_k: Config.qwenTopK || 50,
|
||||
seed: Config.qwenSeed > 0 ? Config.qwenSeed : Math.floor(Math.random() * 114514),
|
||||
temperature: Config.qwenTemperature || 1,
|
||||
enable_search: !!Config.qwenEnableSearch
|
||||
}
|
||||
}
|
||||
if (Config.qwenModel) {
|
||||
completionParams.model = Config.qwenModel
|
||||
}
|
||||
let opts = {
|
||||
apiKey: Config.qwenApiKey,
|
||||
debug: false,
|
||||
systemMessage: system,
|
||||
completionParams,
|
||||
fetch: newFetch
|
||||
}
|
||||
let client = new QwenApi(opts)
|
||||
let option = {
|
||||
timeoutMs: 600000,
|
||||
completionParams
|
||||
}
|
||||
let result
|
||||
try {
|
||||
result = await client.sendMessage(msg, option)
|
||||
} catch (err) {
|
||||
logger.error(err)
|
||||
throw new Error(err)
|
||||
}
|
||||
return result.text
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error(e)
|
||||
logger.info('基于LLM的翻译失败,转用老版翻译')
|
||||
return await translateOld(msg, to)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -395,7 +395,7 @@ export default class XinghuoClient {
|
|||
logger.warn('星火设定序列化失败,本次对话不附带设定')
|
||||
}
|
||||
} else {
|
||||
Prompt = Config.xhPrompt ? [{ role: 'system', content: Config.xhPrompt }] : []
|
||||
Prompt = option.system ? [{ role: 'system', content: option.system }] : []
|
||||
}
|
||||
if (Config.xhPromptEval) {
|
||||
Prompt.forEach(obj => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue