Skip to content

Commit aac2b68

Browse files
authored
feat: add support for gpt-4 and Azure OpenAI service (#62)
* feat: add support for gpt-4 * feat: add Azure OpenAI service support
1 parent 7d1f9ab commit aac2b68

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

src/info.json

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"identifier": "yetone.openai.translator",
3-
"version": "0.1.0",
3+
"version": "0.2.6",
44
"category": "translate",
55
"name": "OpenAI Translator",
66
"summary": "GPT powered translator",
@@ -15,7 +15,13 @@
1515
"type": "text",
1616
"title": "API URL",
1717
"defaultValue": "https://api.openai.com",
18-
"desc": "如果您的网络环境需要代理才能访问 OpenAI API, 可在这里修改为反代 API 的地址 默认为 https://api.openai.com"
18+
"desc": "如果您的网络环境需要代理才能访问 OpenAI API, 可在这里修改为反代 API 的地址,默认为 https://api.openai.com"
19+
},
20+
{
21+
"identifier": "api_url_path",
22+
"type": "text",
23+
"title": "URL Path",
24+
"desc": "如果您使用的是 Azure OpenAI Service,需要填写不同的 API URL Path,如 `/openai/deployments/{NAME}/completions?api-version={VERSION}`"
1925
},
2026
{
2127
"identifier": "api_keys",
@@ -37,6 +43,22 @@
3743
"title": "gpt-3.5-turbo (recommended)",
3844
"value": "gpt-3.5-turbo"
3945
},
46+
{
47+
"title": "gpt-4",
48+
"value": "gpt-4"
49+
},
50+
{
51+
"title": "gpt-4-0314",
52+
"value": "gpt-4-0314"
53+
},
54+
{
55+
"title": "gpt-4-32k",
56+
"value": "gpt-4-32k"
57+
},
58+
{
59+
"title": "gpt-4-32k-0314",
60+
"value": "gpt-4-32k-0314"
61+
},
4062
{
4163
"title": "text-davinci-003",
4264
"value": "text-davinci-003"

src/main.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,28 @@ function supportLanguages() {
55
}
66

77
function translate(query, completion) {
8-
const ChatGPTModels = ["gpt-3.5-turbo", "gpt-3.5-turbo-0301"];
8+
const ChatGPTModels = [
9+
"gpt-3.5-turbo",
10+
"gpt-3.5-turbo-0301",
11+
"gpt-4",
12+
"gpt-4-0314",
13+
"gpt-4-32k",
14+
"gpt-4-32k-0314"
15+
];
916
const api_keys = $option.api_keys.split(",").map((key) => key.trim());
1017
const api_key = api_keys[Math.floor(Math.random() * api_keys.length)];
11-
const header = {
12-
"Content-Type": "application/json",
13-
Authorization: `Bearer ${api_key}`,
14-
};
18+
const isChatGPTModel = ChatGPTModels.indexOf($option.model) > -1;
19+
const isAzureServiceProvider = $option.api_url.includes("openai.azure.com");
20+
const apiUrlPath = $option.api_url_path ? $option.api_url_path : (isChatGPTModel ? "/v1/chat/completions" : "/v1/completions");
21+
1522
let systemPrompt =
1623
"You are a translation engine that can only translate text and cannot interpret it.";
1724
let userPrompt = `translate from ${lang.langMap.get(query.detectFrom) || query.detectFrom
1825
} to ${lang.langMap.get(query.detectTo) || query.detectTo}`;
1926
if (query.detectTo === "wyw" || query.detectTo === "yue") {
2027
userPrompt = `翻译成${lang.langMap.get(query.detectTo) || query.detectTo}`;
2128
}
29+
2230
if (
2331
query.detectFrom === "wyw" ||
2432
query.detectFrom === "zh-Hans" ||
@@ -41,6 +49,10 @@ function translate(query, completion) {
4149
userPrompt = "polish this sentence";
4250
}
4351
}
52+
53+
const header = {
54+
"Content-Type": "application/json",
55+
};
4456
const body = {
4557
model: $option.model,
4658
temperature: 0,
@@ -50,24 +62,33 @@ function translate(query, completion) {
5062
presence_penalty: 1,
5163
};
5264
userPrompt = `${userPrompt}:\n\n"${query.text}" =>`;
53-
const isChatGPTModel = ChatGPTModels.indexOf($option.model) > -1;
65+
66+
if (isAzureServiceProvider) {
67+
header["api-key"] = `${api_key}`
68+
} else {
69+
header["Authorization"] = `Bearer ${api_key}`
70+
}
5471
if (isChatGPTModel) {
55-
body.messages = [
72+
body["messages"] = [
5673
{
5774
role: "system",
5875
content: systemPrompt,
5976
},
60-
{ role: "user", content: userPrompt },
77+
{
78+
role: "user",
79+
content: userPrompt,
80+
},
81+
{ role: "user", content: `"${query.text}"` },
6182
];
6283
} else {
63-
body.prompt = userPrompt;
84+
body["prompt"] = userPrompt;
6485
}
86+
6587
(async () => {
6688
const resp = await $http.request({
6789
method: "POST",
6890
url:
69-
$option.api_url +
70-
(isChatGPTModel ? "/v1/chat/completions" : "/v1/completions"),
91+
$option.api_url + apiUrlPath,
7192
header,
7293
body,
7394
});

0 commit comments

Comments
 (0)