Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix on azure open ai #164

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 '';
};
}