Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Commit

Permalink
feat: 使用 pandora-next 的 chat2api 接口,支持 shareToken 和 poolToken;
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuiyue132 committed Dec 21, 2023
1 parent a76c884 commit 575ee93
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 36 deletions.
Binary file modified .DS_Store
Binary file not shown.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


## 感谢
Expand Down
2 changes: 1 addition & 1 deletion global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ declare namespace Bob {
| 'post'
| 'POST'
| 'put'
| 'PUT';
| 'PUT'| 'PATCH' | 'patch';

interface HttpRequestConfig {
url: string;
Expand Down
Binary file modified src/.DS_Store
Binary file not shown.
13 changes: 12 additions & 1 deletion src/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": ""
Expand Down
42 changes: 12 additions & 30 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -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.";
Expand Down Expand Up @@ -89,6 +89,7 @@ function ensureHttpsAndNoTrailingSlash(url) {
function buildHeader() {
return {
"Content-Type": "application/json",
"Authorization": `Bearer ${$option.apiKey}`,
};
}

Expand Down Expand Up @@ -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}${
Expand All @@ -171,7 +164,10 @@ function buildRequestBody(query) {
return {
stream: false,
model: ChatGPTModels[0],
prompt: userPrompt,
messages: [
{ role: "system", content: generatedSystemPrompt },
{ role: "user", content: userPrompt },
],
};
}

Expand Down Expand Up @@ -201,7 +197,8 @@ function translate(query, completion) {
});
}

const { apiUrl } = $option;
const { apiUrl } = $option;


if (!apiUrl) {
completion({
Expand All @@ -215,47 +212,32 @@ 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,
to: query.detectTo,
toParagraphs: [targetText],
},
});
// 删除对话
// 每次翻译都是新的对话,为了避免登录网页版一看列表全是翻译记录,影响正常使用。
// 所以选择每次翻译后删除对话。
await $http.request({
method: "DELETE",
url: modifiedApiUrl + deleteUrl + conversation_id,
header,
handler: (result) => {
$log.info(`result: ${JSON.stringify(result)}`);
},
});
}
},
});
Expand Down

0 comments on commit 575ee93

Please sign in to comment.