Skip to content

Commit 355a405

Browse files
authored
Bug fix on azure open ai (#164)
* bug fix on azure open ai * run "npm run build"
1 parent 12a11df commit 355a405

File tree

3 files changed

+55
-55
lines changed

3 files changed

+55
-55
lines changed

action/index.cjs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -150526,32 +150526,34 @@ exports.robot = robot;
150526150526
/***/ }),
150527150527

150528150528
/***/ 85365:
150529-
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
150529+
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
150530150530

150531150531
"use strict";
150532150532

150533-
var __importDefault = (this && this.__importDefault) || function (mod) {
150534-
return (mod && mod.__esModule) ? mod : { "default": mod };
150535-
};
150536150533
Object.defineProperty(exports, "__esModule", ({ value: true }));
150537150534
exports.Chat = void 0;
150538-
const openai_1 = __importDefault(__nccwpck_require__(60047));
150535+
const openai_1 = __nccwpck_require__(60047);
150539150536
class Chat {
150540150537
openai;
150541150538
isAzure;
150542-
apiVersion;
150543-
deployment;
150544150539
constructor(apikey) {
150545150540
this.isAzure = Boolean(process.env.AZURE_API_VERSION && process.env.AZURE_DEPLOYMENT);
150546-
this.apiVersion = process.env.AZURE_API_VERSION || '';
150547-
this.deployment = process.env.AZURE_DEPLOYMENT || '';
150548-
const baseURL = this.isAzure
150549-
? `${process.env.OPENAI_API_ENDPOINT}/openai/deployments/${this.deployment}/chat/completions?api-version=${this.apiVersion}`
150550-
: process.env.OPENAI_API_ENDPOINT || 'https://api.openai.com/v1';
150551-
this.openai = new openai_1.default({
150552-
apiKey: apikey,
150553-
baseURL,
150554-
});
150541+
if (this.isAzure) {
150542+
// Azure OpenAI configuration
150543+
this.openai = new openai_1.AzureOpenAI({
150544+
apiKey: apikey,
150545+
endpoint: process.env.OPENAI_API_ENDPOINT || '',
150546+
apiVersion: process.env.AZURE_API_VERSION || '',
150547+
deployment: process.env.AZURE_DEPLOYMENT || '',
150548+
});
150549+
}
150550+
else {
150551+
// Standard OpenAI configuration
150552+
this.openai = new openai_1.OpenAI({
150553+
apiKey: apikey,
150554+
baseURL: process.env.OPENAI_API_ENDPOINT || 'https://api.openai.com/v1',
150555+
});
150556+
}
150555150557
}
150556150558
generatePrompt = (patch) => {
150557150559
const answerLanguage = process.env.LANGUAGE
@@ -150560,7 +150562,7 @@ class Chat {
150560150562
const prompt = process.env.PROMPT ||
150561150563
'Below is a code patch, please help me do a brief code review on it. Any bug risks and/or improvement suggestions are welcome:';
150562150564
return `${prompt}, ${answerLanguage}:
150563-
${patch}
150565+
${patch}
150564150566
`;
150565150567
};
150566150568
codeReview = async (patch) => {
@@ -150572,23 +150574,20 @@ class Chat {
150572150574
const res = await this.openai.chat.completions.create({
150573150575
messages: [
150574150576
{
150575-
role: "user",
150577+
role: 'user',
150576150578
content: prompt,
150577-
}
150579+
},
150578150580
],
150579-
// Use model or deployment name based on the environment
150580-
model: (this.isAzure ? this.deployment : process.env.MODEL || 'gpt-4o-mini'),
150581+
model: process.env.MODEL || 'gpt-4o-mini',
150581150582
temperature: +(process.env.temperature || 0) || 1,
150582150583
top_p: +(process.env.top_p || 0) || 1,
150583-
max_tokens: process.env.max_tokens
150584-
? +process.env.max_tokens
150585-
: undefined,
150584+
max_tokens: process.env.max_tokens ? +process.env.max_tokens : undefined,
150586150585
});
150587150586
console.timeEnd('code-review cost');
150588150587
if (res.choices.length) {
150589150588
return res.choices[0].message.content;
150590150589
}
150591-
return "";
150590+
return '';
150592150591
};
150593150592
}
150594150593
exports.Chat = Chat;

action/src/chat.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
export declare class Chat {
22
private openai;
33
private isAzure;
4-
private apiVersion?;
5-
private deployment?;
64
constructor(apikey: string);
75
private generatePrompt;
86
codeReview: (patch: string) => Promise<string | null>;

src/chat.ts

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,42 @@
1-
import OpenAI from 'openai';
1+
import { OpenAI, AzureOpenAI } from 'openai';
2+
23
export 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

Comments
 (0)