diff --git a/.DS_Store b/.DS_Store index 49850f7..59d9719 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/README.md b/README.md index f872954..cb8b4fa 100644 --- a/README.md +++ b/README.md @@ -5,24 +5,26 @@ ## 简介 -直接调用chatGPT网页端 而非 openai api 的 Bob 翻译插件。 +调用 pandora-next 的 Bob 翻译插件。 随意维护,随意更新,主要供自己使用。 支持社区版 Bob, 商店版应该也支持。但我没试。 +支持 shareToken 和 poolToken。 + ## 打包方式 -![fiL6r2](https://picture.zhuiyue.vip:444/images/2023/06/08/fiL6r2.png) +![fiL6r2](https://cdn.jsdelivr.net/gh/zhuiyue132/oss/2023-12-21/M3pFd9.png) ## 使用方式 0. 安装 Bob, 商店版和社区版都可以。 - 1. 先部署[潘多拉](https://github.com/pengzhile/pandora/blob/master/doc/wiki.md), 得到服务的地址。 + 1. 先部署[pandora-next](https://github.com/pandora-next/deploy), 得到服务的地址。 2. 安装插件,按照上方打包方式自行打包,并安装之。 - 3. 把潘多拉服务器地址填到插件里,并启用插件。 + 3. 在 Bob 的设置中,设置翻译插件为 GPT Translator 并设置服务地址和请求的 key。 ## 感谢 diff --git a/global.d.ts b/global.d.ts index 85bbfe4..451fe3d 100644 --- a/global.d.ts +++ b/global.d.ts @@ -242,7 +242,7 @@ declare namespace Bob { | 'post' | 'POST' | 'put' - | 'PUT'; + | 'PUT'| 'PATCH' | 'patch'; interface HttpRequestConfig { url: string; diff --git a/src/.DS_Store b/src/.DS_Store index 7102237..8c199f2 100644 Binary files a/src/.DS_Store and b/src/.DS_Store differ diff --git a/src/info.json b/src/info.json index fc4ada7..ea77adf 100644 --- a/src/info.json +++ b/src/info.json @@ -12,7 +12,18 @@ "type": "text", "title": "API URL", "defaultValue": "", - "desc": "部署潘多拉后的 url,最好可以公网访问,不要轻易暴露该 url,否则可能会有隐私泄露的风险", + "desc": "潘多拉的请求路径,也可以是官方的接口", + "textConfig": { + "type": "visible", + "placeholderText": "" + } + }, + { + "identifier": "apiKey", + "type": "text", + "title": "API Key", + "defaultValue": "", + "desc": "潘多拉的请求 APIKey,支持 shareToken 和 poolToken与官方的 sk", "textConfig": { "type": "visible", "placeholderText": "" diff --git a/src/main.js b/src/main.js index 2fd1d64..b63749b 100644 --- a/src/main.js +++ b/src/main.js @@ -1,7 +1,7 @@ //@ts-check var lang = require("./lang.js"); -var ChatGPTModels = ["text-davinci-002-render-sha"]; +var ChatGPTModels = ["gpt-3.5-turbo"]; var SYSTEM_PROMPT = "You are a translation engine that can only translate text and cannot interpret it."; @@ -89,6 +89,7 @@ function ensureHttpsAndNoTrailingSlash(url) { function buildHeader() { return { "Content-Type": "application/json", + "Authorization": `Bearer ${$option.apiKey}`, }; } @@ -151,17 +152,9 @@ function replacePromptKeywords(prompt, query) { .replace("$targetLang", query.detectTo); } -/** - * @param {Bob.TranslateQuery} query - * @returns {{ - * model: typeof ChatGPTModels[number]; - * prompt?: string; - * stream?: boolean; - * }} - */ function buildRequestBody(query) { let { customUserPrompt } = $option; - const { generatedUserPrompt } = generatePrompts(query); + const { generatedUserPrompt, generatedSystemPrompt } = generatePrompts(query); customUserPrompt = replacePromptKeywords(customUserPrompt, query); const userPrompt = `${SYSTEM_PROMPT}${ @@ -171,7 +164,10 @@ function buildRequestBody(query) { return { stream: false, model: ChatGPTModels[0], - prompt: userPrompt, + messages: [ + { role: "system", content: generatedSystemPrompt }, + { role: "user", content: userPrompt }, + ], }; } @@ -201,7 +197,8 @@ function translate(query, completion) { }); } - const { apiUrl } = $option; +const { apiUrl } = $option; + if (!apiUrl) { completion({ @@ -215,29 +212,25 @@ function translate(query, completion) { const modifiedApiUrl = ensureHttpsAndNoTrailingSlash(apiUrl); - let apiUrlPath = "/api/conversation/talk"; - let deleteUrl = `/api/conversation/`; + let apiUrlPath = "/v1/chat/completions"; const header = buildHeader(); const body = buildRequestBody(query); let targetText = ""; // 初始化拼接结果变量 - let conversation_id = null; (async () => { await $http.request({ method: "POST", url: modifiedApiUrl + apiUrlPath, header, - body: { ...body, message_id: uuid(), parent_message_id: uuid() }, + body: { ...body }, handler: async (result) => { if (result.response.statusCode >= 400) { handleError(completion, result); } else { $log.info(`result: ${JSON.stringify(result)}`); - targetText = result.data.message.content.parts[0]; - conversation_id = result.data.conversation_id; - $log.info(`conversation_id: ${conversation_id}`); + targetText = result.data.choices[0].message.content; completion({ result: { from: query.detectFrom, @@ -245,17 +238,6 @@ function translate(query, completion) { toParagraphs: [targetText], }, }); - // 删除对话 - // 每次翻译都是新的对话,为了避免登录网页版一看列表全是翻译记录,影响正常使用。 - // 所以选择每次翻译后删除对话。 - await $http.request({ - method: "DELETE", - url: modifiedApiUrl + deleteUrl + conversation_id, - header, - handler: (result) => { - $log.info(`result: ${JSON.stringify(result)}`); - }, - }); } }, });