1- import OpenAI from 'openai' ;
1+ import { OpenAI , AzureOpenAI } from 'openai' ;
2+
23export class Chat {
3- private openai : OpenAI ;
4+ private openai : OpenAI | AzureOpenAI ;
45 private isAzure : boolean ;
5- private apiVersion ?: string ;
6- private deployment ?: string ;
76
87 constructor ( apikey : string ) {
9- this . isAzure = Boolean ( process . env . AZURE_API_VERSION && process . env . AZURE_DEPLOYMENT ) ;
10- this . apiVersion = process . env . AZURE_API_VERSION || '' ;
11- this . deployment = process . env . AZURE_DEPLOYMENT || '' ;
12-
13- const baseURL = this . isAzure
14- ? `${ process . env . OPENAI_API_ENDPOINT } /openai/deployments/${ this . deployment } /chat/completions?api-version=${ this . apiVersion } `
15- : process . env . OPENAI_API_ENDPOINT || 'https://api.openai.com/v1' ;
8+ this . isAzure = Boolean (
9+ process . env . AZURE_API_VERSION && process . env . AZURE_DEPLOYMENT ,
10+ ) ;
1611
17- this . openai = new OpenAI ( {
18- apiKey : apikey ,
19- baseURL,
20- } ) ;
12+ if ( this . isAzure ) {
13+ // Azure OpenAI configuration
14+ this . openai = new AzureOpenAI ( {
15+ apiKey : apikey ,
16+ endpoint : process . env . OPENAI_API_ENDPOINT || '' ,
17+ apiVersion : process . env . AZURE_API_VERSION || '' ,
18+ deployment : process . env . AZURE_DEPLOYMENT || '' ,
19+ } ) ;
20+ } else {
21+ // Standard OpenAI configuration
22+ this . openai = new OpenAI ( {
23+ apiKey : apikey ,
24+ baseURL : process . env . OPENAI_API_ENDPOINT || 'https://api.openai.com/v1' ,
25+ } ) ;
26+ }
2127 }
2228
2329 private generatePrompt = ( patch : string ) => {
2430 const answerLanguage = process . env . LANGUAGE
25- ? `Answer me in ${ process . env . LANGUAGE } ,`
26- : '' ;
31+ ? `Answer me in ${ process . env . LANGUAGE } ,`
32+ : '' ;
2733
2834 const prompt =
29- process . env . PROMPT ||
30- 'Below is a code patch, please help me do a brief code review on it. Any bug risks and/or improvement suggestions are welcome:' ;
35+ process . env . PROMPT ||
36+ 'Below is a code patch, please help me do a brief code review on it. Any bug risks and/or improvement suggestions are welcome:' ;
3137
3238 return `${ prompt } , ${ answerLanguage } :
33- ${ patch }
39+ ${ patch }
3440 ` ;
3541 } ;
3642
@@ -45,17 +51,14 @@ export class Chat {
4551 const res = await this . openai . chat . completions . create ( {
4652 messages : [
4753 {
48- role : " user" ,
54+ role : ' user' ,
4955 content : prompt ,
50- }
56+ } ,
5157 ] ,
52- // Use model or deployment name based on the environment
53- model : ( this . isAzure ? this . deployment : process . env . MODEL || 'gpt-4o-mini' ) as any ,
58+ model : process . env . MODEL || 'gpt-4o-mini' ,
5459 temperature : + ( process . env . temperature || 0 ) || 1 ,
5560 top_p : + ( process . env . top_p || 0 ) || 1 ,
56- max_tokens : process . env . max_tokens
57- ? + process . env . max_tokens
58- : undefined ,
61+ max_tokens : process . env . max_tokens ? + process . env . max_tokens : undefined ,
5962 } ) ;
6063
6164 console . timeEnd ( 'code-review cost' ) ;
@@ -64,6 +67,6 @@ export class Chat {
6467 return res . choices [ 0 ] . message . content ;
6568 }
6669
67- return "" ;
70+ return '' ;
6871 } ;
6972}
0 commit comments