mirror of
https://github.com/freeedcom/ai-codereviewer.git
synced 2025-04-21 01:56:47 +00:00
chore: replace parseJsonForChatGPTResponse with jsonic for cleaner response parsing
This commit is contained in:
parent
977935fd5e
commit
b0ef6c864e
2 changed files with 7 additions and 22 deletions
|
@ -4,7 +4,7 @@ import OpenAI from "openai";
|
|||
import {Octokit} from "@octokit/rest";
|
||||
import parseDiff, {Chunk, File} from "parse-diff";
|
||||
import minimatch from "minimatch";
|
||||
import {parseJsonForChatGPTResponse} from "./parseJsonForChatGPTResponse";
|
||||
import jsonic from "jsonic";
|
||||
|
||||
const GITHUB_TOKEN: string = core.getInput("GITHUB_TOKEN");
|
||||
const OPENAI_API_KEY: string = core.getInput("OPENAI_API_KEY");
|
||||
|
@ -147,7 +147,12 @@ async function getAIResponse(prompt: string): Promise<Array<{
|
|||
const res = response.choices[0].message?.content?.trim() || "{}";
|
||||
console.log("GPT response:")
|
||||
console.log(res);
|
||||
return parseJsonForChatGPTResponse(res).reviews;
|
||||
|
||||
const cleanupResponse = res
|
||||
.replace(/```json/g, "")
|
||||
.replace(/```/g, "");
|
||||
|
||||
return jsonic(cleanupResponse).reviews;
|
||||
}
|
||||
|
||||
function createComment(
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
import jsonic from "jsonic";
|
||||
|
||||
export function parseJsonForChatGPTResponse(response: string) {
|
||||
let jsonData: any = null;
|
||||
|
||||
const responseString = response
|
||||
.replace(/```json/g, "")
|
||||
.replace(/```/g, "");
|
||||
|
||||
try {
|
||||
jsonData = jsonic(responseString);
|
||||
} catch (e) {
|
||||
try {
|
||||
jsonData = JSON.parse(responseString);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
return jsonData;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue