@@ -5,20 +5,28 @@ function supportLanguages() {
55}
66
77function 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