diff --git a/src/info.json b/src/info.json index c747332..440734d 100644 --- a/src/info.json +++ b/src/info.json @@ -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", @@ -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", @@ -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" diff --git a/src/main.js b/src/main.js index 2223f71..e4bf636 100644 --- a/src/main.js +++ b/src/main.js @@ -5,13 +5,20 @@ 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 @@ -19,6 +26,7 @@ function translate(query, completion) { if (query.detectTo === "wyw" || query.detectTo === "yue") { userPrompt = `翻译成${lang.langMap.get(query.detectTo) || query.detectTo}`; } + if ( query.detectFrom === "wyw" || query.detectFrom === "zh-Hans" || @@ -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, @@ -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, });