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

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
djx30103 committed Jul 11, 2024
1 parent 1c0705d commit de09547
Show file tree
Hide file tree
Showing 10 changed files with 726 additions and 2 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Release

on:
push:
tags:
- 'v*'
workflow_dispatch:

jobs:
Build:
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- run: bash release.sh ${{ github.ref }}

- name: Release
uses: softprops/action-gh-release@v1
with:
draft: false
files: |
dist/*.bobplugin
env:
GITHUB_TOKEN: ${{ secrets.GT_TOKEN }}

- run: rm -rf dist

- uses: stefanzweifel/git-auto-commit-action@v4
with:
branch: main
commit_message: new release!
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,37 @@
# bob-plugin-doubao-translate
基于 豆包Doubao API 的文本翻译、文本润色、语法纠错 Bob 插件。
<div>
<h1 align="center">Doubao Translator Bob Plugin</h1>
</div>

## 简介

基于 [豆包Doubao API](https://www.volcengine.com/product/doubao) 的文本翻译、文本润色、语法纠错 Bob 插件。

### 语言模型

* `Doubao-pro-128k`(默认使用)
* `Doubao-pro-32k`
* `Doubao-pro-4k`
* `Doubao-lite-128k`
* `Doubao-lite-32k`
* `Doubao-lite-4k`
* `Doubao-embedding`
* `Moonshot-v1-128k`
* `Moonshot-v1-128k`
* `Moonshot-v1-128k`

## 使用方法

1. 安装 [Bob](https://bobtranslate.com/guide/#%E5%AE%89%E8%A3%85) (版本 >= 1.8.0),一款 macOS 平台的翻译和 OCR 软件

2. 下载此插件: [bob-plugin-doubao-translate.bobplugin](https://github.com/djx30103/bob-plugin-doubao-translate/releases/latest)

3. 安装此插件

4.[火山方舟控制台](https://console.volcengine.com/ark) 开通管理(开通服务) -> 模型推理(为每个模型创建接入点) -> API Key管理(创建 API Key)

5. 把 API Key、推理点ID 填入 Bob 偏好设置 > 服务 > 此插件配置界面对应的输入框中,选择你要使用的模型,点击保存即可。

## 感谢

本仓库参考部分其他优秀源码,感谢[bob-plugin-cohere](https://github.com/missuo/bob-plugin-cohere)[bob-plugin-gemini-translate](https://github.com/BrianShenCC/bob-plugin-gemini-translate)

1 change: 1 addition & 0 deletions appcast.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
20 changes: 20 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

version=${1#refs/tags/v}
zip -r -j bob-plugin-doubao-translate-$version.bobplugin src/*

sha256_doubao=$(sha256sum bob-plugin-doubao-translate-$version.bobplugin | cut -d ' ' -f 1)
echo $sha256_doubao

download_link="https://github.com/djx30103/bob-plugin-doubao-translate/releases/download/v$version/bob-plugin-doubao-translate-$version.bobplugin"

new_version="{\"version\": \"$version\", \"desc\": \"None\", \"sha256\": \"$sha256_doubao\", \"url\": \"$download_link\", \"minBobVersion\": \"1.8.0\"}"

json_file='appcast.json'
json_data=$(cat $json_file)

updated_json=$(echo $json_data | jq --argjson new_version "$new_version" '.versions = [$new_version] + .versions')

echo $updated_json > $json_file
mkdir dist
mv *.bobplugin dist
70 changes: 70 additions & 0 deletions src/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
var utils = require("./utils.js");

const defaultUrl = "https://ark.cn-beijing.volces.com/api/v3/chat/completions";
const streamRequest = async ({ url, headers, body, query }) => {
let resultText = "";
return $http.streamRequest({
method: "POST",
url: url,
header: headers,
body: body,
streamHandler: (stream) => {

let streamText = stream.text;
const dataReg = /^data: /gm;
const doneReg = /\s*\[DONE\]\s*/;

// 使用正则表达式将 streamText 按 "data: " 分割为多个块
const dataBlocks = streamText.split(dataReg);

dataBlocks.forEach((block) => {
// 去除首尾空格和换行符
block = block.trim();

// 检测和移除 [DONE] 标记
if (doneReg.test(block)) {
block = block.replace(doneReg, '');
}


// 忽略空块
if (block === "") {
return;
}

const resultJson = JSON.parse(block);
resultText += resultJson.choices[0].delta.content;
query.onStream({ result: { toParagraphs: [resultText] } });
});
},
handler: (result) => {
if (result.response.statusCode >= 400) {
utils.handleError(query.onCompletion, result);
} else {
query.onCompletion({ result: { toParagraphs: [resultText]} });
}
},
});
};

const normalRequest = async ({ url, headers, body , query}) => {
return $http.request({
method: "POST",
url: url,
header: headers,
body: body,
handler: (result) => {
if (result.response.statusCode >= 400) {
utils.handleError(query.onCompletion, result);
} else {
const data = result.data;
$log.info(JSON.stringify(data));
query.onCompletion({ result: { toParagraphs: [data.choices[0].message.content] } });
}
},
});
};

exports.streamRequest = streamRequest;
exports.normalRequest = normalRequest;
exports.defaultUrl = defaultUrl;
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.
Loading

0 comments on commit de09547

Please sign in to comment.