mirror of
https://github.com/freeedcom/ai-codereviewer.git
synced 2025-04-20 09:36:47 +00:00
Merge pull request #6 from arunsnt/use-gpt-3-5
Add fix to parse json object from GPT-3.5 response
This commit is contained in:
commit
a5b21294c1
3 changed files with 22 additions and 8 deletions
14
dist/index.js
vendored
14
dist/index.js
vendored
|
@ -135,7 +135,7 @@ ${chunk.changes
|
|||
`;
|
||||
}
|
||||
function getAIResponse(prompt) {
|
||||
var _a, _b;
|
||||
var _a, _b, _c;
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const queryConfig = {
|
||||
model: OPENAI_API_MODEL,
|
||||
|
@ -154,14 +154,20 @@ function getAIResponse(prompt) {
|
|||
] }));
|
||||
// Log the raw response for debugging
|
||||
console.log('Raw response:', JSON.stringify(response, null, 2));
|
||||
const res = ((_b = (_a = response.choices[0].message) === null || _a === void 0 ? void 0 : _a.content) === null || _b === void 0 ? void 0 : _b.trim()) || "{}";
|
||||
const res = ((_b = (_a = response.choices[0].message) === null || _a === void 0 ? void 0 : _a.content) === null || _b === void 0 ? void 0 : _b.trim()) || "";
|
||||
// Extract JSON content from Markdown code block
|
||||
const jsonContent = (_c = res.match(/```json([\s\S]*)```/)) === null || _c === void 0 ? void 0 : _c[1];
|
||||
if (!jsonContent) {
|
||||
console.error("Failed to extract JSON content from response.");
|
||||
return null;
|
||||
}
|
||||
// Attempt to parse JSON
|
||||
try {
|
||||
return JSON.parse(res).reviews;
|
||||
return JSON.parse(jsonContent).reviews;
|
||||
}
|
||||
catch (e) {
|
||||
console.error("Failed to parse JSON:", e);
|
||||
console.error("Response content:", res);
|
||||
console.error("Response content:", jsonContent);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
14
src/main.ts
14
src/main.ts
|
@ -137,14 +137,22 @@ async function getAIResponse(prompt: string): Promise<Array<{
|
|||
// Log the raw response for debugging
|
||||
console.log('Raw response:', JSON.stringify(response, null, 2));
|
||||
|
||||
const res = response.choices[0].message?.content?.trim() || "{}";
|
||||
const res = response.choices[0].message?.content?.trim() || "";
|
||||
|
||||
// Extract JSON content from Markdown code block
|
||||
const jsonContent = res.match(/```json([\s\S]*)```/)?.[1];
|
||||
|
||||
if (!jsonContent) {
|
||||
console.error("Failed to extract JSON content from response.");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Attempt to parse JSON
|
||||
try {
|
||||
return JSON.parse(res).reviews;
|
||||
return JSON.parse(jsonContent).reviews;
|
||||
} catch (e) {
|
||||
console.error("Failed to parse JSON:", e);
|
||||
console.error("Response content:", res);
|
||||
console.error("Response content:", jsonContent);
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue