Skip to content

Commit

Permalink
Bug fix on azure open ai (#164)
Browse files Browse the repository at this point in the history
* bug fix on azure open ai

* run "npm run build"
  • Loading branch information
shkangomelet authored Nov 8, 2024
1 parent 12a11df commit 355a405
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 55 deletions.
49 changes: 24 additions & 25 deletions action/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -150526,32 +150526,34 @@ exports.robot = robot;
/***/ }),

/***/ 85365:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {

"use strict";

var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.Chat = void 0;
const openai_1 = __importDefault(__nccwpck_require__(60047));
const openai_1 = __nccwpck_require__(60047);
class Chat {
openai;
isAzure;
apiVersion;
deployment;
constructor(apikey) {
this.isAzure = Boolean(process.env.AZURE_API_VERSION && process.env.AZURE_DEPLOYMENT);
this.apiVersion = process.env.AZURE_API_VERSION || '';
this.deployment = process.env.AZURE_DEPLOYMENT || '';
const baseURL = this.isAzure
? `${process.env.OPENAI_API_ENDPOINT}/openai/deployments/${this.deployment}/chat/completions?api-version=${this.apiVersion}`
: process.env.OPENAI_API_ENDPOINT || 'https://api.openai.com/v1';
this.openai = new openai_1.default({
apiKey: apikey,
baseURL,
});
if (this.isAzure) {
// Azure OpenAI configuration
this.openai = new openai_1.AzureOpenAI({
apiKey: apikey,
endpoint: process.env.OPENAI_API_ENDPOINT || '',
apiVersion: process.env.AZURE_API_VERSION || '',
deployment: process.env.AZURE_DEPLOYMENT || '',
});
}
else {
// Standard OpenAI configuration
this.openai = new openai_1.OpenAI({
apiKey: apikey,
baseURL: process.env.OPENAI_API_ENDPOINT || 'https://api.openai.com/v1',
});
}
}
generatePrompt = (patch) => {
const answerLanguage = process.env.LANGUAGE
Expand All @@ -150560,7 +150562,7 @@ class Chat {
const prompt = process.env.PROMPT ||
'Below is a code patch, please help me do a brief code review on it. Any bug risks and/or improvement suggestions are welcome:';
return `${prompt}, ${answerLanguage}:
${patch}
${patch}
`;
};
codeReview = async (patch) => {
Expand All @@ -150572,23 +150574,20 @@ class Chat {
const res = await this.openai.chat.completions.create({
messages: [
{
role: "user",
role: 'user',
content: prompt,
}
},
],
// Use model or deployment name based on the environment
model: (this.isAzure ? this.deployment : process.env.MODEL || 'gpt-4o-mini'),
model: process.env.MODEL || 'gpt-4o-mini',
temperature: +(process.env.temperature || 0) || 1,
top_p: +(process.env.top_p || 0) || 1,
max_tokens: process.env.max_tokens
? +process.env.max_tokens
: undefined,
max_tokens: process.env.max_tokens ? +process.env.max_tokens : undefined,
});
console.timeEnd('code-review cost');
if (res.choices.length) {
return res.choices[0].message.content;
}
return "";
return '';
};
}
exports.Chat = Chat;
Expand Down
2 changes: 0 additions & 2 deletions action/src/chat.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export declare class Chat {
private openai;
private isAzure;
private apiVersion?;
private deployment?;
constructor(apikey: string);
private generatePrompt;
codeReview: (patch: string) => Promise<string | null>;
Expand Down
59 changes: 31 additions & 28 deletions src/chat.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
import OpenAI from 'openai';
import { OpenAI, AzureOpenAI } from 'openai';

export class Chat {
private openai: OpenAI;
private openai: OpenAI | AzureOpenAI;
private isAzure: boolean;
private apiVersion?: string;
private deployment?: string;

constructor(apikey: string) {
this.isAzure = Boolean(process.env.AZURE_API_VERSION && process.env.AZURE_DEPLOYMENT);
this.apiVersion = process.env.AZURE_API_VERSION || '';
this.deployment = process.env.AZURE_DEPLOYMENT || '';

const baseURL = this.isAzure
? `${process.env.OPENAI_API_ENDPOINT}/openai/deployments/${this.deployment}/chat/completions?api-version=${this.apiVersion}`
: process.env.OPENAI_API_ENDPOINT || 'https://api.openai.com/v1';
this.isAzure = Boolean(
process.env.AZURE_API_VERSION && process.env.AZURE_DEPLOYMENT,
);

this.openai = new OpenAI({
apiKey: apikey,
baseURL,
});
if (this.isAzure) {
// Azure OpenAI configuration
this.openai = new AzureOpenAI({
apiKey: apikey,
endpoint: process.env.OPENAI_API_ENDPOINT || '',
apiVersion: process.env.AZURE_API_VERSION || '',
deployment: process.env.AZURE_DEPLOYMENT || '',
});
} else {
// Standard OpenAI configuration
this.openai = new OpenAI({
apiKey: apikey,
baseURL: process.env.OPENAI_API_ENDPOINT || 'https://api.openai.com/v1',
});
}
}

private generatePrompt = (patch: string) => {
const answerLanguage = process.env.LANGUAGE
? `Answer me in ${process.env.LANGUAGE},`
: '';
? `Answer me in ${process.env.LANGUAGE},`
: '';

const prompt =
process.env.PROMPT ||
'Below is a code patch, please help me do a brief code review on it. Any bug risks and/or improvement suggestions are welcome:';
process.env.PROMPT ||
'Below is a code patch, please help me do a brief code review on it. Any bug risks and/or improvement suggestions are welcome:';

return `${prompt}, ${answerLanguage}:
${patch}
${patch}
`;
};

Expand All @@ -45,17 +51,14 @@ export class Chat {
const res = await this.openai.chat.completions.create({
messages: [
{
role: "user",
role: 'user',
content: prompt,
}
},
],
// Use model or deployment name based on the environment
model: (this.isAzure ? this.deployment : process.env.MODEL || 'gpt-4o-mini') as any,
model: process.env.MODEL || 'gpt-4o-mini',
temperature: +(process.env.temperature || 0) || 1,
top_p: +(process.env.top_p || 0) || 1,
max_tokens: process.env.max_tokens
? +process.env.max_tokens
: undefined,
max_tokens: process.env.max_tokens ? +process.env.max_tokens : undefined,
});

console.timeEnd('code-review cost');
Expand All @@ -64,6 +67,6 @@ export class Chat {
return res.choices[0].message.content;
}

return "";
return '';
};
}

0 comments on commit 355a405

Please sign in to comment.