Fix JSON?

This commit is contained in:
Aymeric Gaurat-Apelli 2024-10-02 15:52:08 +10:00
commit 7d720b5447

View file

@ -140,7 +140,15 @@ async function getAIResponse(prompt: string): Promise<Array<{
}); });
const res = response.choices[0].message?.content?.trim() || "{}"; const res = response.choices[0].message?.content?.trim() || "{}";
return JSON.parse(res).reviews; // Remove lines ```json and ``` from the response
const jsonStart = res.indexOf("{");
const jsonEnd = res.lastIndexOf("}");
if (jsonStart === -1 || jsonEnd === -1) {
console.error("Error: JSON not found in response: " + res);
return null;
}
const jsonRes = res.substring(jsonStart, jsonEnd + 1);
return JSON.parse(jsonRes).reviews;
} catch (error) { } catch (error) {
console.error("Error:", error); console.error("Error:", error);
return null; return null;