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

Support latest gpt 4 #32

Merged
merged 2 commits into from
Dec 2, 2023
Merged

Support latest gpt 4 #32

merged 2 commits into from
Dec 2, 2023

Conversation

villesau
Copy link
Contributor

@villesau villesau commented Dec 2, 2023

Support gpt-4-1106-preview and json mode

@@ -1,6 +1,6 @@
import { readFileSync } from "fs";
import * as core from "@actions/core";
import { Configuration, OpenAIApi } from "openai";
import OpenAI from "openai";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directly importing OpenAI without destructuring may lead to potential issues if the library expects specific imports. Ensure that the OpenAI import aligns with the library's updated export structure for gpt-4-1106-preview and json mode.

@@ -98,7 +96,7 @@ async function getBaseAndHeadShas(

function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string {
return `Your task is to review pull requests. Instructions:
- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
- Provide the response in following JSON format: {"reviews": [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSON format provided in the updated line does not match the standard JSON array format. It should be an array of objects within square brackets [].

...queryConfig,
// return JSON if the model supports it:
...(OPENAI_API_MODEL === "gpt-4-1106-preview"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response_format object structure seems incorrect. The response_format should be a string, not an object. It should be 'response_format': 'json' if you want to specify JSON format.

@@ -152,8 +154,8 @@ async function getAIResponse(prompt: string): Promise<Array<{
],
});

const res = response.data.choices[0].message?.content?.trim() || "[]";
return JSON.parse(res);
const res = response.choices[0].message?.content?.trim() || "{}";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback value for res should be an empty array [] instead of an empty object {} to match the expected return type of the function.

const res = response.data.choices[0].message?.content?.trim() || "[]";
return JSON.parse(res);
const res = response.choices[0].message?.content?.trim() || "{}";
return JSON.parse(res).reviews;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the property reviews exists on the parsed object to avoid potential runtime errors. Consider adding a check before accessing reviews property.

@villesau villesau force-pushed the support-latest-gpt-4 branch 2 times, most recently from 9b70faa to 82c725a Compare December 2, 2023 21:05
@villesau villesau merged commit 8dff117 into main Dec 2, 2023
1 check passed
@sarons
Copy link

sarons commented May 6, 2024

Hey @villesau, thanks for adding this but it doesn't seem to work. I think it worked first time, but later on I get the same error as #56

Run freeedcom/[email protected]
  
Error: SyntaxError: Unexpected token ` in JSON at position 0
    at JSON.parse (<anonymous>)
    at /home/runner/work/_actions/freeedcom/ai-codereviewer/v2.7.0/webpack:/open-ai-reviewer/lib/main.js:151:1
    at Generator.next (<anonymous>)
    at fulfilled (/home/runner/work/_actions/freeedcom/ai-codereviewer/v2.7.0/webpack:/open-ai-reviewer/lib/main.js:28:1)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
Error: SyntaxError: Unexpected token ` in JSON at position 0
    at JSON.parse (<anonymous>)
    at /home/runner/work/_actions/freeedcom/ai-codereviewer/v2.7.0/webpack:/open-ai-reviewer/lib/main.js:151:1
    at Generator.next (<anonymous>)
    at fulfilled (/home/runner/work/_actions/freeedcom/ai-codereviewer/v2.7.0/webpack:/open-ai-reviewer/lib/main.js:28:1)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
Error: SyntaxError: Unexpected token ` in JSON at position 0
    at JSON.parse (<anonymous>)
    at /home/runner/work/_actions/freeedcom/ai-codereviewer/v2.7.0/webpack:/open-ai-reviewer/lib/main.js:151:1
    at Generator.next (<anonymous>)

@sarons
Copy link

sarons commented May 6, 2024

@lukehollenback I also tried your branch #57, still the same error

Run lukehollenback/ai-codereviewer@issue-56_json-mode-on-more-models
  
Error: SyntaxError: Unexpected token ` in JSON at position 0
    at JSON.parse (<anonymous>)
    at /home/runner/work/_actions/lukehollenback/ai-codereviewer/issue-56_json-mode-on-more-models/webpack:/open-ai-reviewer/lib/main.js:151:1
    at Generator.next (<anonymous>)
    at fulfilled (/home/runner/work/_actions/lukehollenback/ai-codereviewer/issue-56_json-mode-on-more-models/webpack:/open-ai-reviewer/lib/main.js:28:1)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

@yagiz2000
Copy link

yagiz2000 commented May 19, 2024

@lukehollenback I also tried your branch #57, still the same error

Run lukehollenback/ai-codereviewer@issue-56_json-mode-on-more-models
  
Error: SyntaxError: Unexpected token ` in JSON at position 0
    at JSON.parse (<anonymous>)
    at /home/runner/work/_actions/lukehollenback/ai-codereviewer/issue-56_json-mode-on-more-models/webpack:/open-ai-reviewer/lib/main.js:151:1
    at Generator.next (<anonymous>)
    at fulfilled (/home/runner/work/_actions/lukehollenback/ai-codereviewer/issue-56_json-mode-on-more-models/webpack:/open-ai-reviewer/lib/main.js:28:1)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
  • Provide json response with reviews array and do not add any prefix or suffix just send reviews array.

If you add this prompt after - Provide the response in following JSON format: {"reviews": [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]} it will send json string without any prefix and json parse wont fail. (You can test it locally)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants