Skip to content

Commit

Permalink
feat: 新增免费版gpt
Browse files Browse the repository at this point in the history
  • Loading branch information
akl7777777 committed Dec 10, 2023
1 parent 18f1c95 commit 4f77627
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 2 deletions.
168 changes: 168 additions & 0 deletions src/freeShellGPT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
const {readFile,historyFileName} = require("./file");
const file = require("./file");

async function translate(query, source_lang, target_lang, translate_text, completion) {
try {
let mode = $option.mode;
let model = $option.model;
let url = "https://veygrlhubijv.cloud.sealos.io/v1/chat/completions";
let prompt = $option.prompt;
const configValue = readFile();
if (configValue.mode) {
mode = configValue.mode;
}
let A = [{"role": "user", "content": translate_text}]
// 如果是翻译模式,需要拼接
if (mode === 'translate') {
translate_text = `请将以下${source_lang}内容翻译成${target_lang}:\n${translate_text}`
A = [{"role": "user", "content": translate_text}]
} else if (mode === 'polishing') {
translate_text = `请润色以下内容:\n${translate_text}`
A = [{"role": "user", "content": translate_text}]
} else if (mode === 'custom_prompt') {
translate_text = `${prompt}\n${translate_text}`
A = [{"role": "user", "content": translate_text}]
} else {
A = readFile(historyFileName).concat(A);
}
const L = Date.now();
let targetText = ""; // 初始化拼接结果变量
let buffer = ""; // 新增 buffer 变量
(async () => {
await $http.streamRequest({
method: "POST",
url: url,
header:{
"Content-Type": "application/json",
"Authorization": `Bearer sk-GTBL94nGR4BIesKwA9C678F956234097BdA5005aFd936b7f`
},
body:{
model: model,
stream: true,
temperature: 0.2,
max_tokens: 1000,
top_p: 1,
frequency_penalty: 1,
presence_penalty: 1,
messages: [
{
role: "user",
content: translate_text,
}
]
},
cancelSignal: query.cancelSignal,
streamHandler: (streamData) => {
if (streamData.text.includes("Invalid token")) {
query.onCompletion({
error: {
type: "secretKey",
message: "配置错误 - 请确保您在插件配置中填入了正确的 API Keys",
addtion: "请在插件配置中填写正确的 API Keys",
},
});
} else {
// 将新的数据添加到缓冲变量中
buffer += streamData.text;
// 检查缓冲变量是否包含一个完整的消息
while (true) {
const match = buffer.match(/data: (.*?})\n/);
if (match) {
// 如果是一个完整的消息,处理它并从缓冲变量中移除
const textFromResponse = match[1].trim();
targetText = handleResponse(query, true, targetText, textFromResponse);
buffer = buffer.slice(match[0].length);
} else {
// 如果没有完整的消息,等待更多的数据
break;
}
}
}
},
handler: (result) => {
if (result.response.statusCode >= 400) {
handleError(query, result);
} else {
query.onCompletion({
result: {
from: query.detectFrom,
to: query.detectTo,
toParagraphs: [targetText],
},
});
}
}
});
})().catch((err) => {
query.onCompletion({
error: {
type: err._type || "unknown",
message: err._message || "未知错误",
addtion: err._addition,
},
});
});
return "";
} catch (e) {
Object.assign(e, {
_type: 'network',
_message: '接口请求错误 - ' + JSON.stringify(e),
});
throw e;
}
}


function handleResponse(query, isChatGPTModel, targetText, textFromResponse) {
if (textFromResponse !== '[DONE]') {
try {
const dataObj = JSON.parse(textFromResponse);
const { choices } = dataObj;
if (!choices || choices.length === 0) {
query.onCompletion({
error: {
type: "api",
message: "接口未返回结果",
addtion: textFromResponse,
},
});
return targetText;
}

const content = isChatGPTModel ? choices[0].delta.content : choices[0].text;
if (content !== undefined) {
targetText += content;
query.onStream({
result: {
from: query.detectFrom,
to: query.detectTo,
toParagraphs: [targetText],
},
});
}
} catch (err) {
query.onCompletion({
error: {
type: err._type || "param",
message: err._message || "Failed to parse JSON",
addtion: err._addition,
},
});
}
}
return targetText;
}

function handleError(query, result) {
const { statusCode } = result.response;
const reason = (statusCode >= 400 && statusCode < 500) ? "param" : "api";
query.onCompletion({
error: {
type: reason,
message: `接口响应错误 - ${HttpErrorCodes[statusCode]}`,
addtion: `${JSON.stringify(result)}`,
},
});
}

exports.translate = translate;
8 changes: 6 additions & 2 deletions src/info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"identifier": "com.akl.bob-plugin-akl-chatgpt-free-translate",
"version": "2.0.5",
"version": "2.0.6",
"category": "translate",
"name": "Free ChatGPT",
"summary": "免秘钥免启动任何服务调用ChatGPT",
Expand Down Expand Up @@ -126,8 +126,12 @@
"identifier": "service",
"type": "menu",
"title": "版本",
"defaultValue": "default",
"defaultValue": "freeShellGPT",
"menuValues": [
{
"title": "免费版",
"value": "freeShellGPT"
},
{
"title": "自己填key反代线路1",
"value": "api_key_1"
Expand Down
3 changes: 3 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var gamma = require('./gamma.js');
var delta = require('./delta.js');
var of = require('./of.js');
var customKeyStream = require('./customKeyStream.js')
var freeShellGPT = require('./freeShellGPT.js')
var file = require("./file");
const {readFile} = require("./file");
// var { historyFileName, readFile, writeFile } = require("./file");
Expand Down Expand Up @@ -90,6 +91,8 @@ function translate(query, completion) {
chatResult = await aifree.translate(query, source_lang, target_lang, translate_text, completion)
} else if (server === 'oversea-1') {
chatResult = await cha.translate(query, source_lang, target_lang, translate_text, completion)
} else if (server === 'freeShellGPT') {
chatResult = await freeShellGPT.translate(query, source_lang, target_lang, translate_text, completion)
} else {
// chatResult = await dw.translate(query, source_lang, target_lang, translate_text, completion)
chatResult = await customKeyStream.translate(query, source_lang, target_lang, translate_text, completion)
Expand Down

0 comments on commit 4f77627

Please sign in to comment.