@@ -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