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

Fix json parse bug #3

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
17 changes: 11 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ ${chunk.changes
`;
}
function getAIResponse(prompt) {
var _a, _b;
var _a;
return __awaiter(this, void 0, void 0, function* () {
const queryConfig = {
model: OPENAI_API_MODEL,
Expand All @@ -153,18 +153,23 @@ function getAIResponse(prompt) {
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
response_format: { type: "json_object" },
};
try {
const response = yield openai.chat.completions.create(Object.assign(Object.assign(Object.assign({}, queryConfig), (OPENAI_API_MODEL === "gpt-4-1106-preview"
? { response_format: { type: "json_object" } }
: {})), { messages: [
const response = yield openai.chat.completions.create(Object.assign(Object.assign({}, queryConfig), { messages: [
{
role: "system",
content: prompt,
},
] }));
const res = ((_b = (_a = response.choices[0].message) === null || _a === void 0 ? void 0 : _a.content) === null || _b === void 0 ? void 0 : _b.trim()) || "{}";
return JSON.parse(res).reviews;
const rawContent = ((_a = response.choices[0].message) === null || _a === void 0 ? void 0 : _a.content) || "";
const startIndex = rawContent.indexOf("{");
const endIndex = rawContent.lastIndexOf("}") + 1;
if (startIndex === -1 || endIndex === -1) {
throw new Error("No valid JSON found in response content.");
}
const jsonString = rawContent.substring(startIndex, endIndex).trim();
return JSON.parse(jsonString).reviews;
}
catch (error) {
console.error("Error:", error);
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

22 changes: 15 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import OpenAI from "openai";
import { Octokit } from "@octokit/rest";
import parseDiff, { Chunk, File } from "parse-diff";
import minimatch from "minimatch";
import { ChatCompletionCreateParamsNonStreaming } from "openai/resources";

const GITHUB_TOKEN: string = core.getInput("GITHUB_TOKEN");
const OPENAI_API_KEY: string = core.getInput("OPENAI_API_KEY");
Expand Down Expand Up @@ -131,15 +132,12 @@ async function getAIResponse(prompt: string): Promise<Array<{
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
response_format: { type: "json_object" },
};

try {
const response = await openai.chat.completions.create({
...queryConfig,
// return JSON if the model supports it:
...(OPENAI_API_MODEL === "gpt-4-1106-preview"
? { response_format: { type: "json_object" } }
: {}),
...(queryConfig as ChatCompletionCreateParamsNonStreaming),
messages: [
{
role: "system",
Expand All @@ -148,8 +146,18 @@ async function getAIResponse(prompt: string): Promise<Array<{
],
});

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

const startIndex = rawContent.indexOf("{");
const endIndex = rawContent.lastIndexOf("}") + 1;

if (startIndex === -1 || endIndex === -1) {
throw new Error("No valid JSON found in response content.");
}

const jsonString = rawContent.substring(startIndex, endIndex).trim();
Copy link
Author

Choose a reason for hiding this comment

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

When there was no json_object a long time ago, my solution to this problem was to find the first "{" and the last "}" in the string, and then we parsed it


return JSON.parse(jsonString).reviews;
} catch (error) {
console.error("Error:", error);
return null;
Expand Down