Skip to content

Commit

Permalink
feat: add support to o1-mini
Browse files Browse the repository at this point in the history
  • Loading branch information
lfsevergnini committed Dec 27, 2024
1 parent 14d0d31 commit 9904b99
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 18 deletions.
46 changes: 37 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ You are an expert code reviewer. Analyze the provided code changes and provide d
Follow this JSON format:
${exports.outputFormat}

------
Understanding the diff:
- Lines starting with "-" (del) show code that was REMOVED
- Lines starting with "+" (add) show code that was ADDED
- Lines without prefix (normal) show unchanged context

------
For the "summary" field, use Markdown formatting and follow these guidelines:
1. 🎯 Core Changes
Expand Down Expand Up @@ -186,13 +192,15 @@ Note:
------

For the "comments" field:

- ONLY add comments for actual issues that need to be addressed
- DO NOT add comments for:
* Compliments or positive feedback
* Style preferences
* Minor suggestions
* Obvious changes
* General observations
* Ensuring/Confirming intended behavior
- Each comment must be:
* Actionable (something specific that needs to change)
* Important enough to discuss
Expand All @@ -203,12 +211,17 @@ For the "comments" field:
* comment: The comment text
- Other rules for "comments" field:
* ONLY use line numbers that appear in the "diff" property of each file
* Each diff line starts with a prefix:
* "normal" for unchanged lines
* "del" for removed lines
* "add" for added lines
* Extract the line number that appears after the prefix
* DO NOT use line number 0 or line numbers not present in the diff
* DO NOT comment on removed lines unless their removal creates a problem:
** Focus your review on:
1. New code (lines with "+")
2. The impact of changes on existing code
3. Potential issues in the new implementation
** For example:
- BAD: "This line was removed" (unless removal causes issues)
- GOOD: "The new implementation might cause X issue"
- GOOD: "Consider adding Y to the new code"

------
For the "suggestedAction" field, provide a single word that indicates the action to be taken. Options are:
Expand Down Expand Up @@ -562,22 +575,21 @@ class OpenAIProvider {
this.client = new openai_1.default({ apiKey: config.apiKey });
}
async review(request) {
var _a;
core.info(`Sending request to OpenAI with prompt structure: ${JSON.stringify(request, null, 2)}`);
const response = await this.client.chat.completions.create({
model: this.config.model,
messages: [
{
role: 'system',
role: this.getSystemPromptRole(),
content: this.buildSystemPrompt(request),
},
{
role: 'user',
content: this.buildPullRequestPrompt(request),
},
],
temperature: (_a = this.config.temperature) !== null && _a !== void 0 ? _a : 0.3,
response_format: { type: 'json_object' },
temperature: this.getTemperature(),
response_format: this.isO1Mini() ? { type: 'text' } : { type: 'json_object' },
});
core.debug(`Raw OpenAI response: ${JSON.stringify(response.choices[0].message.content, null, 2)}`);
const parsedResponse = this.parseResponse(response);
Expand Down Expand Up @@ -610,15 +622,31 @@ class OpenAIProvider {
}
parseResponse(response) {
var _a;
let rawContent = (_a = response.choices[0].message.content) !== null && _a !== void 0 ? _a : '{}';
if (rawContent.startsWith('```json')) {
rawContent = rawContent.slice(7, -3);
}
// Implement response parsing
const content = JSON.parse((_a = response.choices[0].message.content) !== null && _a !== void 0 ? _a : '{}');
const content = JSON.parse(rawContent);
return {
summary: content.summary,
lineComments: content.comments,
suggestedAction: content.suggestedAction,
confidence: content.confidence,
};
}
isO1Mini() {
return this.config.model.includes('o1-mini');
}
getSystemPromptRole() {
// o1 doesn't support 'system' role
return this.isO1Mini() ? 'user' : 'system';
}
getTemperature() {
var _a;
// o1 only supports 1.0
return this.isO1Mini() ? 1 : (_a = this.config.temperature) !== null && _a !== void 0 ? _a : 0.3;
}
}
exports.OpenAIProvider = OpenAIProvider;

Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions src/prompts/code-reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ You are an expert code reviewer. Analyze the provided code changes and provide d
Follow this JSON format:
${outputFormat}
------
Understanding the diff:
- Lines starting with "-" (del) show code that was REMOVED
- Lines starting with "+" (add) show code that was ADDED
- Lines without prefix (normal) show unchanged context
------
For the "summary" field, use Markdown formatting and follow these guidelines:
1. 🎯 Core Changes
Expand Down Expand Up @@ -54,13 +60,15 @@ Note:
------
For the "comments" field:
- ONLY add comments for actual issues that need to be addressed
- DO NOT add comments for:
* Compliments or positive feedback
* Style preferences
* Minor suggestions
* Obvious changes
* General observations
* Ensuring/Confirming intended behavior
- Each comment must be:
* Actionable (something specific that needs to change)
* Important enough to discuss
Expand All @@ -71,12 +79,17 @@ For the "comments" field:
* comment: The comment text
- Other rules for "comments" field:
* ONLY use line numbers that appear in the "diff" property of each file
* Each diff line starts with a prefix:
* "normal" for unchanged lines
* "del" for removed lines
* "add" for added lines
* Extract the line number that appears after the prefix
* DO NOT use line number 0 or line numbers not present in the diff
* DO NOT comment on removed lines unless their removal creates a problem:
** Focus your review on:
1. New code (lines with "+")
2. The impact of changes on existing code
3. Potential issues in the new implementation
** For example:
- BAD: "This line was removed" (unless removal causes issues)
- GOOD: "The new implementation might cause X issue"
- GOOD: "Consider adding Y to the new code"
------
For the "suggestedAction" field, provide a single word that indicates the action to be taken. Options are:
Expand Down
28 changes: 24 additions & 4 deletions src/providers/OpenAIProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ export class OpenAIProvider implements AIProvider {
model: this.config.model,
messages: [
{
role: 'system',
role: this.getSystemPromptRole(),
content: this.buildSystemPrompt(request),
},
{
role: 'user',
content: this.buildPullRequestPrompt(request),
},
],
temperature: this.config.temperature ?? 0.3,
response_format: { type: 'json_object' },
temperature: this.getTemperature(),
response_format: this.isO1Mini() ? { type: 'text' } : { type: 'json_object' },
});

core.debug(`Raw OpenAI response: ${JSON.stringify(response.choices[0].message.content, null, 2)}`);
Expand Down Expand Up @@ -65,13 +65,33 @@ export class OpenAIProvider implements AIProvider {
}

private parseResponse(response: OpenAI.Chat.Completions.ChatCompletion): ReviewResponse {
let rawContent = response.choices[0].message.content ?? '{}';

if (rawContent.startsWith('```json')) {
rawContent = rawContent.slice(7, -3);
}

// Implement response parsing
const content = JSON.parse(response.choices[0].message.content ?? '{}');
const content = JSON.parse(rawContent);
return {
summary: content.summary,
lineComments: content.comments,
suggestedAction: content.suggestedAction,
confidence: content.confidence,
};
}

private isO1Mini(): boolean {
return this.config.model.includes('o1-mini');
}

private getSystemPromptRole(): 'system' | 'user' {
// o1 doesn't support 'system' role
return this.isO1Mini() ? 'user' : 'system';
}

private getTemperature(): number {
// o1 only supports 1.0
return this.isO1Mini() ? 1 : this.config.temperature ?? 0.3;
}
}

0 comments on commit 9904b99

Please sign in to comment.