mirror of
https://github.com/freeedcom/ai-codereviewer.git
synced 2025-04-21 10:06:47 +00:00
Add fix to parse json object from GPT-3.5 response
This commit is contained in:
parent
62c5563c93
commit
fcd39ade07
3 changed files with 22 additions and 8 deletions
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