Skip to content

Commit f1c29c8

Browse files
committed
fix: json format error
1 parent 23b4241 commit f1c29c8

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

packages/vmind/__tests__/performance/performanceTest.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import {
2222
mockUserInput12,
2323
mockUserInput13,
2424
mockUserInput14,
25-
mockUserInput16
25+
mockUserInput16,
26+
mockUserInput17
2627
} from '../browser/src/constants/mockData';
2728
const demoDataList: { [key: string]: any } = {
2829
pie: mockUserInput2,
@@ -44,7 +45,8 @@ const demoDataList: { [key: string]: any } = {
4445
'College entrance examination': acceptRatioData,
4546
'Shopping Mall Sales Performance': mallSalesData,
4647
'Global GDP': mockUserInput6Eng,
47-
'Sales of different drinkings': mockUserInput3Eng
48+
'Sales of different drinkings': mockUserInput3Eng,
49+
'Multi measure': mockUserInput17
4850
};
4951
const CHART_GENERATION_AVERAGE_TIME = 10000;
5052
const QPM_LIMIT = 10; //qpm limit of your llm service
@@ -92,7 +94,7 @@ if (gptKey && gptURL) {
9294
const vmind = new VMind({
9395
url: gptURL,
9496
model: Model.GPT3_5,
95-
cache: false,
97+
cache: true,
9698
headers: {
9799
'api-key': gptKey
98100
}

packages/vmind/src/common/utils.ts

+7
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ export const execPipeline = <PipelineContext>(
2121
const result = pipe(pre, context);
2222
return result;
2323
}, src);
24+
25+
export const matchJSONStr = (str: string) => {
26+
const first = str.indexOf('{');
27+
const last = str.lastIndexOf('}');
28+
const result = str.substring(first, last + 1);
29+
return result && result.length > 0 ? result : str;
30+
};

packages/vmind/src/common/vizDataToSpec/pipes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const funnelData = (spec: any, context: Context) => {
5959
// spec.data = [dataset]
6060
spec.data = {
6161
id: 'data',
62-
values: dataset.sort((a: any, b: any) => b[cell.y] - a[cell.y])
62+
values: dataset.sort((a: any, b: any) => b[cell.y as string] - a[cell.y as string])
6363
};
6464

6565
return spec;

packages/vmind/src/gpt/utils.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { GPTDataProcessResult, ILLMOptions, LLMResponse } from '../typings';
22
import axios from 'axios';
33
import JSON5 from 'json5';
44
import { omit } from 'lodash';
5+
import { matchJSONStr } from '../common/utils';
56

67
export const requestGPT = async (
78
prompt: string,
@@ -77,7 +78,9 @@ export const parseGPTResponse = (GPTRes: LLMResponse) => {
7778
}
7879
const choices = GPTRes.choices;
7980
const content = choices[0].message.content;
80-
const resJson: GPTDataProcessResult = parseGPTJson(content, '```');
81+
const jsonStr = matchJSONStr(content);
82+
83+
const resJson: GPTDataProcessResult = parseGPTJson(jsonStr, '```');
8184
return resJson;
8285
} catch (err: any) {
8386
return {

packages/vmind/src/skylark/dataProcess/query/utils.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { LLMResponse } from 'src/typings';
22
import JSON5 from 'json5';
33
import { replaceAll } from '../../../common/dataProcess/utils';
4+
import { matchJSONStr } from '../../../common/utils';
45

56
export const parseJson = (JsonStr: string, prefix?: string) => {
67
const parseNoPrefixStr = (str: string) => {
@@ -38,7 +39,8 @@ export const parseSkylarkResponseAsJSON = (skylarkRes: LLMResponse) => {
3839
}
3940
const choices = skylarkRes.choices;
4041
const content = replaceAll(choices[0].message.content, '\n', ' ');
41-
const resJson = parseJson(content, '```');
42+
const jsonStr = matchJSONStr(content);
43+
const resJson = parseJson(jsonStr, '```');
4244
return resJson;
4345
} catch (err: any) {
4446
return {

0 commit comments

Comments
 (0)