Skip to content

Commit

Permalink
feat: support stream output
Browse files Browse the repository at this point in the history
  • Loading branch information
liby committed May 20, 2023
1 parent 2462f4b commit ed31459
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 56 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -35,9 +35,9 @@ ChatGPT 向我们展示了 GPT 模型的伟大之处,所以我使用 ChatGPT

## 使用方法

1. 安装 [Bob](https://bobtranslate.com/guide/#%E5%AE%89%E8%A3%85) (版本 >= 0.50),一款 macOS 平台的翻译和 OCR 软件
1. 安装 [Bob](https://bobtranslate.com/guide/#%E5%AE%89%E8%A3%85),一款 macOS 平台的翻译和 OCR 软件;[openai-translator.bobplugin](https://github.com/yetone/bob-plugin-openai-translator/releases/latest) >= **1.0.0** 以后默认开启流式输出,需要 Bob 版本 >= **1.8.0**

2. 下载此插件: [openai-polisher.bobplugin](https://github.com/openai-translator/bob-plugin-openai-polisher/releases/latest)
2. 下载此插件: [openai-polisher.bobplugin](https://github.com/yetone/bob-plugin-openai-polisher/releases)

3. 安装此插件:
![安装步骤](https://user-images.githubusercontent.com/1206493/222712959-4a4b27e2-b129-408a-a8af-24a3a89df2dd.gif)
Expand Down
2 changes: 1 addition & 1 deletion docs/README_EN.md
Expand Up @@ -35,7 +35,7 @@ ChatGPT has shown us the greatness of the GPT model, so I used ChatGPT's API to

## Usage

1. Install [Bob](https://bobtranslate.com/guide/#%E5%AE%89%E8%A3%85) (version >= 0.50), a macOS translation and OCR software
1. Install [Bob](https://bobtranslate.com/guide/#%E5%AE%89%E8%A3%85), a translation and OCR software for macOS platform; [openai-translator.bobplugin](https://github.com/yetone/bob-plugin-openai-translator/releases/latest) >= **1.0.0** enables streaming output by default after installation, requiring Bob version >= **1.8.0**.

2. Download this plugin: [openai-polisher.bobplugin](https://github.com/openai-translator/bob-plugin-openai-polisher/releases/latest)

Expand Down
48 changes: 44 additions & 4 deletions global.d.ts
Expand Up @@ -126,21 +126,41 @@ declare namespace Bob {
'yua' = '尤卡坦玛雅语',
'zu' = '祖鲁语',
}

type Languages = Array<keyof typeof LanguagesEnum>;
type supportLanguages = Languages;
type Language = keyof typeof LanguagesEnum;


interface DataPayload {
message: string;
}

interface Disposable {
dispose: () => void;
}

interface Signal {
send: (data?: DataPayload) => void;
subscribe: (callback: (data?: DataPayload) => void) => Disposable;
removeAllSubscriber: () => void;
}

// https://ripperhe.gitee.io/bob/#/plugin/quickstart/translate
type Translate = (query: TranslateQuery, completion: Completion) => void;
type completionResult = { result: Result };
type CompletionResult = { error: ServiceError };
type Completion = (args: completionResult | CompletionResult) => void;
type CompletionError = { error: ServiceError };
type Completion = (args: completionResult | CompletionError) => void;
type HandleStream = (args: completionResult) => void;
interface TranslateQuery {
text: string; // 需要翻译的文本
from: Language; // 用户选中的源语种标准码
to: Language; // 用户选中的目标语种标准码
detectFrom: Exclude<Language, 'auto'>; // 检测过后的源语种
detectTo: Exclude<Language, 'auto'>; // 检测过后的目标语种
cancelSignal: Signal,
onStream: HandleStream,
onCompletion: Completion; // 用于回调翻译结果的函数
}
interface OcrQuery {
from: Language; // 目前用户选中的源语言
Expand All @@ -164,7 +184,7 @@ declare namespace Bob {
author?: string; // 插件作者。
homepage?: string; // 插件主页网址。
appcast?: string; // 插件发布信息 URL。
minBobVersion?: string; // 最低支持本插件的 Bob 版本,建议填写您开发插件时候的调试插件的 Bob 版本,目前应该是 0.5.0。
minBobVersion?: string; // 最低支持本插件的 Bob 版本,建议填写您开发插件时候的调试插件的 Bob 版本,目前应该是 1.8.0。
options?: OptionObject[];
}
interface MenuObject {
Expand Down Expand Up @@ -203,6 +223,7 @@ declare namespace Bob {
polishingMode: "simplicity" | "detailed";
};


// https://ripperhe.gitee.io/bob/#/plugin/api/log
interface Log {
info: (msg: string) => void; // 用于打印一些常规的信息
Expand All @@ -214,6 +235,7 @@ declare namespace Bob {
request<T = any, R = HttpResponsePromise<T>>(config: HttpRequestConfig): Promise<R>;
get<T = any, R = HttpResponsePromise<T>>(config: HttpRequestConfig): Promise<R>;
post<T = any, R = HttpResponsePromise<T>>(config: HttpRequestConfig): Promise<R>;
streamRequest<T = any, R = HttpResponsePromise<T>>(config: HttpStreamRequestConfig): Promise<R>;
}
type HttpMethod =
| 'get'
Expand All @@ -239,6 +261,20 @@ declare namespace Bob {
handler?: (resp: HttpResponse) => void;
timeout?: number;
}

interface HttpStreamRequestConfig {
url: string;
method?: HttpMethod;
header?: any;
params?: any;
body?: any;
files?: HttpRequestFiles;
handler?: (resp: HttpResponse) => void;
cancelSignal?: Signal;
streamHandler?: (stream: { text: string, rawData: Data }) => void
timeout?: number;
}

interface HttpRequestFiles {
data: DataObject; // 二进制数据
name: string; // 上传表单中的名称
Expand Down Expand Up @@ -390,8 +426,12 @@ declare var $option: Bob.Option;
declare var $log: Bob.Log;
declare var $data: Bob.Data;
declare var $file: Bob.File;
declare var $signal: {
new: () => Bob.Signal;
};


declare function supportLanguages(): Bob.supportLanguages;
declare function translate(query: Bob.TranslateQuery, completion: Bob.Completion): void;
declare function translate(query: Bob.TranslateQuery): void;
declare function ocr(query: Bob.OcrQuery, completion: Bob.Completion): void;
declare function tts(query: Bob.TTSQuery, completion: Bob.Completion): void;
2 changes: 1 addition & 1 deletion scripts/update_release.py
Expand Up @@ -15,7 +15,7 @@ def update_appcast(version, desc):
'desc': desc,
'sha256': file_hash,
'url': f'https://github.com/yetone/bob-plugin-openai-polisher/releases/download/v{version}/{release_file.name}',
'minBobVersion': '0.5.0'
'minBobVersion': '1.8.0'
}
appcast_file = Path('appcast.json')
if appcast_file.is_file():
Expand Down
49 changes: 41 additions & 8 deletions src/info.json
Expand Up @@ -8,26 +8,38 @@
"author": "yetone <yetoneful@gmail.com>",
"homepage": "https://github.com/yetone/bob-plugin-openai-polisher",
"appcast": "https://raw.githubusercontent.com/yetone/bob-plugin-openai-polisher/main/appcast.json",
"minBobVersion": "0.5.0",
"minBobVersion": "1.8.0",
"options": [
{
"identifier": "apiUrl",
"type": "text",
"title": "API URL",
"defaultValue": "https://api.openai.com",
"desc": "可选项。如果您的网络环境需要代理才能访问 OpenAI API, 可在这里修改为反代 API 的地址,默认为 https://api.openai.com"
"desc": "可选项。如果您的网络环境需要代理才能访问 OpenAI API, 可在这里修改为反代 API 的地址",
"textConfig": {
"type": "visible",
"placeholderText": "https://api.openai.com"
}
},
{
"identifier": "deploymentName",
"type": "text",
"title": "Dep. Name",
"desc": "可选项。如果您使用的是 Azure OpenAI Service,需要填写对应的 deployment ID"
"desc": "可选项。此值为在部署模型时为部署选择的自定义名称,可在 Azure 门户中的 “资源管理“>“部署“下查看",
"textConfig": {
"type": "visible"
}
},
{
"identifier": "apiKeys",
"type": "text",
"title": "API KEY",
"desc": "必填项。可以用英文逗号分割多个 API KEY 以实现额度加倍及负载均衡"
"desc": "必填项。可以用英文逗号分割多个 API KEY 以实现额度加倍及负载均衡",
"textConfig": {
"type": "secure",
"height": "40",
"placeholderText": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
},
{
"identifier": "model",
Expand Down Expand Up @@ -72,15 +84,36 @@
{
"identifier": "customSystemPrompt",
"type": "text",
"title": "Sys PPT",
"title": "系统指令",
"defaultValue": "Revise the following sentences to make them more clear, concise, and coherent.",
"desc": "可选项。自定义 System Prompt;必须开启明文显示才可输入中文"
"desc": "可选项。自定义 System Prompt,填写则会覆盖默认的 System Prompt。自定义 Prompt可使用以下变量:\n\n`$text` - 需要润色的文本,即翻译窗口输入框内的文本 `$sourceLang` - 原文语言,即翻译窗口输入框内文本的语言,比如「简体中文」\n\n`$targetLang` - 目标语言,可以在翻译窗口中手动选择或自动检测,比如「English」",
"textConfig": {
"type": "visible",
"height": "100",
"placeholderText": "Revise the following sentences to make them more clear, concise, and coherent.",
"keyWords": [
"$text",
"$sourceLang",
"$targetLang"
]
}
},
{
"identifier": "customUserPrompt",
"type": "text",
"title": "User PPT",
"desc": "可选项。自定义 User Prompt,输入内容会自动拼接在句尾"
"title": "用户指令",
"defaultValue": "$text",
"desc": "可选项。自定义 User Prompt,填写则会覆盖默认的 User Prompt,默认值为`$text`(即翻译窗口输入框内的文本)\n\n自定义 Prompt 中可以使用与系统指令中相同的变量",
"textConfig": {
"type": "visible",
"height": "100",
"placeholderText": "$text",
"keyWords": [
"$text",
"$sourceLang",
"$targetLang"
]
}
},
{
"identifier": "polishingMode",
Expand Down

0 comments on commit ed31459

Please sign in to comment.