Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone committed Feb 19, 2023
0 parents commit 91bfedf
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Release

on:
push:
tags: [ v\d+\.\d+\.\d+ ]


jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Get tag
id: tag
uses: dawidd6/action-get-tag@v1
with:
strip_v: true

- name: Change version
run: sed -i -e "s/0\.1\.0/${{ steps.tag.outputs.tag }}/" src/info.json

- name: Package plugin
run: mkdir release && zip -j -r release/openai-translator-${{ steps.tag.outputs.tag }}.bobplugin ./src/*

- name: Upload binaries to release
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: release/openai-translator-${{ steps.tag.outputs.tag }}.bobplugin
asset_name: openai-translator-${{ steps.tag.outputs.tag }}.bobplugin
tag: ${{ github.ref }}
overwrite: true
body: "Release OpenAI Translator Bob plugin v${{ steps.tag.outpus.tag}}!"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.bobplugin
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
OpenAI Translator Bob Plugin
----------------------------

# 简介

ChatGPT 向我们展示了 GPT 模型的伟大之处,我使用 OpenAI 的 API 实现了这个翻译插件,虽然 OpenAI API 用的还是 GPT-3 模型(不是 ChatGPT 用的 GPT-3.5),但是其翻译效果已经很棒了。

基于 ChatGPT 的 Bob 翻译插件正在开发中!

# 使用截图


# 使用方法

1. 安装 [Bob](https://bobtranslate.com/guide/#%E5%AE%89%E8%A3%85) (版本 >= 0.50)
2. 下载此插件: [openai-translator.bobplugin](https://github.com/yetone/bob-plugin-openai-translator/releases)
3. 安装此插件:

4.[OpenAI](https://platform.openai.com/account/api-keys) 获取你的 API KEY
5. 把 API KEY 填入 Bob 此插件配置界面的 API KEY
输入框中

6. 安装 [PopClip](https://bobtranslate.com/guide/integration/popclip.html) 实现划词后鼠标附近出现小图标
Binary file added src/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions src/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"identifier": "yetone.openai.translator",
"version": "0.1.0",
"category": "translate",
"name": "OpenAI Translator",
"summary": "",
"icon": "",
"author": "yetone",
"homepage": "",
"appcast": "",
"minBobVersion": "0.5.0",
"options": [
{
"identifier": "api_keys",
"type": "text",
"title": "API KEY",
"desc": "可以用英文逗号分割多个 api key 以实现额度加倍及负载均衡"
},
{
"identifier": "model",
"type": "menu",
"title": "模型",
"defaultValue": "text-davinci-003",
"menuValues": [
{
"title": "text-davinci-003",
"value": "text-davinci-003"
},
{
"title": "text-davinci-002",
"value": "text-davinci-002"
}
]
}
]
}
84 changes: 84 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
var items = [
["auto", "auto"],
["zh-Hans", "zh"],
["zh-Hant", "zh"],
["en", "en"],
];

var langMap = new Map(items);
var langMapReverse = new Map(
items.map(([standardLang, lang]) => [lang, standardLang])
);

function supportLanguages() {
return items.map(([standardLang, lang]) => standardLang);
}

function translate(query, completion) {
const api_keys = $option.api_keys.split(",").map((key) => key.trim());
const api_key = api_keys[Math.floor(Math.random() * api_keys.length)];
const header = {
"Content-Type": "application/json",
Authorization: "Bearer " + api_key,
};
const body = {
model: $option.model,
prompt: `translate to ${langMap.get(query.detectTo)}:${query.text}`,
temperature: 0,
max_tokens: 4000,
top_p: 1,
frequency_penalty: 0.5,
presence_penalty: 0,
};
(async () => {
const resp = await $http.request({
method: "POST",
url: "https://api.openai.com/v1/completions",
header,
body,
});

if (resp.error) {
const { statusCode } = resp.response;
let reason;
if (statusCode >= 400 && statusCode < 500) {
reason = "param";
} else {
reason = "api";
}
completion({
error: {
type: reason,
message: `接口响应错误 - ${response.data.msg}`,
addtion: JSON.stringify(response),
},
});
} else {
const { choices } = resp.data;
if (!choices || choices.length === 0) {
completion({
error: {
type: "api",
message: "接口未返回结果",
},
});
return;
}
completion({
result: {
from: query.detectFrom,
to: query.detectTo,
toParagraphs: choices[0].text.trim().split("\n"),
},
});
}
})().catch((err) => {
completion({
error: {
type: err._type || "unknown",
message: err._message || "未知错误",
addtion: err._addtion,
},
});
});
}

0 comments on commit 91bfedf

Please sign in to comment.