Skip to content

Commit

Permalink
explorer: repair the wrong json generated by AI (pingcap#1288)
Browse files Browse the repository at this point in the history
* explorer: repair the wrong json generated by AI

* fix
  • Loading branch information
Mini256 committed Feb 9, 2023
1 parent ac880fe commit 1303a45
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"fastify-socket.io": "^4.0.0",
"generic-pool": "^3.9.0",
"ioredis": "^5.2.4",
"jsonrepair": "^3.0.2",
"luxon": "^3.1.0",
"mustache": "^4.2.0",
"mysql2": "^2.3.3",
Expand Down
13 changes: 7 additions & 6 deletions packages/api-server/src/plugins/services/bot-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import pino from "pino";
import stream from "node:stream";
// @ts-ignore
import JSONStream from 'JSONStream';
import {jsonrepair} from "jsonrepair";

declare module 'fastify' {
interface FastifyInstance {
Expand Down Expand Up @@ -231,7 +232,6 @@ export class BotService {
}

let choice = undefined;
let answer = null;
try {
this.log.info("Requesting answer for question: %s", question);
const start = DateTime.now();
Expand All @@ -251,12 +251,13 @@ export class BotService {

if (Array.isArray(choices) && choices[0].text) {
choice = choices[0].text;
answer = JSON.parse(choice);
const result: any = {};
for (const [key, value] of Object.entries(answer)) {
this.setAnswerValue(question, result, key, value);
const repaired = jsonrepair(choice);
const obj = JSON.parse(repaired);
const answer: any = {};
for (const [key, value] of Object.entries(obj)) {
this.setAnswerValue(question, answer, key, value);
}
return result;
return answer;
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export class ExplorerService {
// Reset the question and answer, and try again.
const errorShouldRetry = ['Request failed with status code 429', 'aborted'].includes(e?.message);
if (errorShouldRetry && i < 3) {
logger.warn(e, `Failed to generate answer for question ${questionId}, retrying (${i}/3)...`);
logger.warn(e, `Failed to generate answer for question ${questionId}, retrying (${i + 1}/3)...`);
question.revisedTitle = undefined;
question.notClear = undefined;
question.assumption = undefined;
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1303a45

Please sign in to comment.