Skip to content

Commit

Permalink
feat: add support for gpt-4 and Azure OpenAI service (openai-translat…
Browse files Browse the repository at this point in the history
…or#62)

* feat: add support for gpt-4

* feat: add Azure OpenAI service support
  • Loading branch information
liby authored Mar 23, 2023
1 parent 7d1f9ab commit aac2b68
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
26 changes: 24 additions & 2 deletions src/info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"identifier": "yetone.openai.translator",
"version": "0.1.0",
"version": "0.2.6",
"category": "translate",
"name": "OpenAI Translator",
"summary": "GPT powered translator",
Expand All @@ -15,7 +15,13 @@
"type": "text",
"title": "API URL",
"defaultValue": "https://api.openai.com",
"desc": "如果您的网络环境需要代理才能访问 OpenAI API, 可在这里修改为反代 API 的地址 默认为 https://api.openai.com"
"desc": "如果您的网络环境需要代理才能访问 OpenAI API, 可在这里修改为反代 API 的地址,默认为 https://api.openai.com"
},
{
"identifier": "api_url_path",
"type": "text",
"title": "URL Path",
"desc": "如果您使用的是 Azure OpenAI Service,需要填写不同的 API URL Path,如 `/openai/deployments/{NAME}/completions?api-version={VERSION}`"
},
{
"identifier": "api_keys",
Expand All @@ -37,6 +43,22 @@
"title": "gpt-3.5-turbo (recommended)",
"value": "gpt-3.5-turbo"
},
{
"title": "gpt-4",
"value": "gpt-4"
},
{
"title": "gpt-4-0314",
"value": "gpt-4-0314"
},
{
"title": "gpt-4-32k",
"value": "gpt-4-32k"
},
{
"title": "gpt-4-32k-0314",
"value": "gpt-4-32k-0314"
},
{
"title": "text-davinci-003",
"value": "text-davinci-003"
Expand Down
43 changes: 32 additions & 11 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@ function supportLanguages() {
}

function translate(query, completion) {
const ChatGPTModels = ["gpt-3.5-turbo", "gpt-3.5-turbo-0301"];
const ChatGPTModels = [
"gpt-3.5-turbo",
"gpt-3.5-turbo-0301",
"gpt-4",
"gpt-4-0314",
"gpt-4-32k",
"gpt-4-32k-0314"
];
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 isChatGPTModel = ChatGPTModels.indexOf($option.model) > -1;
const isAzureServiceProvider = $option.api_url.includes("openai.azure.com");
const apiUrlPath = $option.api_url_path ? $option.api_url_path : (isChatGPTModel ? "/v1/chat/completions" : "/v1/completions");

let systemPrompt =
"You are a translation engine that can only translate text and cannot interpret it.";
let userPrompt = `translate from ${lang.langMap.get(query.detectFrom) || query.detectFrom
} to ${lang.langMap.get(query.detectTo) || query.detectTo}`;
if (query.detectTo === "wyw" || query.detectTo === "yue") {
userPrompt = `翻译成${lang.langMap.get(query.detectTo) || query.detectTo}`;
}

if (
query.detectFrom === "wyw" ||
query.detectFrom === "zh-Hans" ||
Expand All @@ -41,6 +49,10 @@ function translate(query, completion) {
userPrompt = "polish this sentence";
}
}

const header = {
"Content-Type": "application/json",
};
const body = {
model: $option.model,
temperature: 0,
Expand All @@ -50,24 +62,33 @@ function translate(query, completion) {
presence_penalty: 1,
};
userPrompt = `${userPrompt}:\n\n"${query.text}" =>`;
const isChatGPTModel = ChatGPTModels.indexOf($option.model) > -1;

if (isAzureServiceProvider) {
header["api-key"] = `${api_key}`
} else {
header["Authorization"] = `Bearer ${api_key}`
}
if (isChatGPTModel) {
body.messages = [
body["messages"] = [
{
role: "system",
content: systemPrompt,
},
{ role: "user", content: userPrompt },
{
role: "user",
content: userPrompt,
},
{ role: "user", content: `"${query.text}"` },
];
} else {
body.prompt = userPrompt;
body["prompt"] = userPrompt;
}

(async () => {
const resp = await $http.request({
method: "POST",
url:
$option.api_url +
(isChatGPTModel ? "/v1/chat/completions" : "/v1/completions"),
$option.api_url + apiUrlPath,
header,
body,
});
Expand Down

0 comments on commit aac2b68

Please sign in to comment.