From 26e43ff76634c8b9f57f384963a044c0c94634f9 Mon Sep 17 00:00:00 2001 From: Seunghyun Kang Date: Fri, 8 Nov 2024 14:12:47 +0900 Subject: [PATCH 1/2] bug fix on azure open ai --- src/chat.ts | 59 ++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/chat.ts b/src/chat.ts index 440a502..b6a547b 100644 --- a/src/chat.ts +++ b/src/chat.ts @@ -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} `; }; @@ -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'); @@ -64,6 +67,6 @@ export class Chat { return res.choices[0].message.content; } - return ""; + return ''; }; } From bbb7f20cdf9a614c3a6e57db9163aab9bcdae16a Mon Sep 17 00:00:00 2001 From: Seunghyun Kang Date: Fri, 8 Nov 2024 17:06:12 +0900 Subject: [PATCH 2/2] run "npm run build" --- action/index.cjs | 49 ++++++++++++++++++++++---------------------- action/src/chat.d.ts | 2 -- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/action/index.cjs b/action/index.cjs index 83e5823..5015dbc 100644 --- a/action/index.cjs +++ b/action/index.cjs @@ -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 @@ -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) => { @@ -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; diff --git a/action/src/chat.d.ts b/action/src/chat.d.ts index 627bd8d..88fd6b1 100644 --- a/action/src/chat.d.ts +++ b/action/src/chat.d.ts @@ -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;