Skip to content

Commit 8998b2d

Browse files
committed
refactor: optimize the code support for Azure OpenAI service
1 parent aac2b68 commit 8998b2d

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

src/info.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@
1111
"minBobVersion": "0.5.0",
1212
"options": [
1313
{
14-
"identifier": "api_url",
14+
"identifier": "apiUrl",
1515
"type": "text",
1616
"title": "API URL",
1717
"defaultValue": "https://api.openai.com",
1818
"desc": "如果您的网络环境需要代理才能访问 OpenAI API, 可在这里修改为反代 API 的地址,默认为 https://api.openai.com"
1919
},
2020
{
21-
"identifier": "api_url_path",
21+
"identifier": "deploymentName",
2222
"type": "text",
23-
"title": "URL Path",
24-
"desc": "如果您使用的是 Azure OpenAI Service,需要填写不同的 API URL Path,如 `/openai/deployments/{NAME}/completions?api-version={VERSION}`"
23+
"title": "Dep. Name",
24+
"desc": "如果您使用的是 Azure OpenAI Service,需要填写对应的 deployment ID"
2525
},
2626
{
27-
"identifier": "api_keys",
27+
"identifier": "apiKeys",
2828
"type": "text",
2929
"title": "API KEY",
3030
"desc": "可以用英文逗号分割多个 API KEY 以实现额度加倍及负载均衡"

src/main.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,32 @@ function translate(query, completion) {
1111
"gpt-4",
1212
"gpt-4-0314",
1313
"gpt-4-32k",
14-
"gpt-4-32k-0314"
14+
"gpt-4-32k-0314",
1515
];
16-
const api_keys = $option.api_keys.split(",").map((key) => key.trim());
17-
const api_key = api_keys[Math.floor(Math.random() * api_keys.length)];
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");
16+
17+
const { model, apiKeys, apiUrl, deploymentName } = $option;
18+
19+
const apiKeySelection = apiKeys.split(",").map(key => key.trim());
20+
const apiKey = apiKeySelection[Math.floor(Math.random() * apiKeySelection.length)];
21+
22+
const isChatGPTModel = ChatGPTModels.includes(model);
23+
const isAzureServiceProvider = apiUrl.includes("openai.azure.com");
24+
let apiUrlPath = isChatGPTModel ? "/v1/chat/completions" : "/v1/completions";
25+
26+
if (isAzureServiceProvider) {
27+
if (deploymentName) {
28+
apiUrlPath = `/openai/deployments/${deploymentName}`;
29+
apiUrlPath += isChatGPTModel ? '/chat/completions?api-version=2023-03-15-preview' : '/completions?api-version=2022-12-01';
30+
} else {
31+
completion({
32+
error: {
33+
type: "param",
34+
message: `配置错误 - 未填写 Deployment Name`,
35+
addition: "The name of your model deployment. You're required to first deploy a model before you can make calls",
36+
},
37+
});
38+
}
39+
}
2140

2241
let systemPrompt =
2342
"You are a translation engine that can only translate text and cannot interpret it.";
@@ -64,9 +83,9 @@ function translate(query, completion) {
6483
userPrompt = `${userPrompt}:\n\n"${query.text}" =>`;
6584

6685
if (isAzureServiceProvider) {
67-
header["api-key"] = `${api_key}`
86+
header["api-key"] = `${apiKey}`
6887
} else {
69-
header["Authorization"] = `Bearer ${api_key}`
88+
header["Authorization"] = `Bearer ${apiKey}`
7089
}
7190
if (isChatGPTModel) {
7291
body["messages"] = [
@@ -87,8 +106,7 @@ function translate(query, completion) {
87106
(async () => {
88107
const resp = await $http.request({
89108
method: "POST",
90-
url:
91-
$option.api_url + apiUrlPath,
109+
url: apiUrl + apiUrlPath,
92110
header,
93111
body,
94112
});

0 commit comments

Comments
 (0)