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:
Arun Murugan 2024-05-21 14:18:53 -04:00 committed by GitHub
commit a5b21294c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 8 deletions

14
dist/index.js vendored
View file

@ -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

File diff suppressed because one or more lines are too long

View file

@ -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) {