Skip to content

Commit d64f630

Browse files
committed
feat: error handler
1 parent 4de147b commit d64f630

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

packages/vmind/__tests__/browser/src/pages/DataInput.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import VMind from '../../../../src/index';
3939
import { Model } from '../../../../src/index';
4040
import { queryDataset } from '../../../../src/gpt/dataProcess';
4141
import { isArray } from 'lodash';
42+
//import { mockDataset } from './mockData'
4243

4344
const TextArea = Input.TextArea;
4445
const Option = Select.Option;
@@ -122,7 +123,9 @@ export function DataInput(props: IPropsType) {
122123
//setLoading(true);
123124
const { fieldInfo, dataset } = vmind.parseCSVData(csv);
124125
//const { fieldInfo: fieldInfoQuery, dataset: datasetQuery } = await vmind?.dataQuery(describe, fieldInfo, dataset);
125-
//const { fieldInfo, dataset } = await vmind.parseCSVDataWithLLM(csv, describe);
126+
//const { fieldInfo, dataset, usage } = await vmind.parseCSVDataWithLLM(csv, describe);
127+
//const dataset = mockDataset
128+
//const fieldInfo = vmind?.getFieldInfo(dataset)
126129
const startTime = new Date().getTime();
127130
const chartGenerationRes = await vmind.generateChart(describe, fieldInfo, dataset);
128131
const endTime = new Date().getTime();

packages/vmind/src/gpt/chart-generation/NLToChart.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export const generateChartWithGPT = async (
4040
queryDatasetUsage = usage;
4141
}
4242
} catch (err) {
43-
console.warn('data query error!');
44-
console.warn(err);
43+
console.error('data query error!');
44+
console.error(err);
4545
}
4646

4747
const schema = getSchemaFromFieldInfo(fieldInfo);
@@ -111,7 +111,7 @@ export const chartAdvisorGPT = async (
111111
const advisorResJson: GPTChartAdvisorResult = parseGPTResponse(advisorRes) as unknown as GPTChartAdvisorResult;
112112

113113
if (advisorResJson.error) {
114-
throw Error('Network Error!');
114+
throw Error((advisorResJson as any).message);
115115
}
116116
if (!SUPPORTED_CHART_LIST.includes(advisorResJson['CHART_TYPE'])) {
117117
throw Error('Unsupported Chart Type. Please Change User Input');

packages/vmind/src/gpt/utils.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const requestGPT = async (
3434
stream: false
3535
//response_format: { type: 'json_object' } //Only models after gpt-3.5-turbo-1106 support this parameter.
3636
}
37-
}).then(response => response.data);
37+
}).then((response: any) => response.data);
3838

3939
return res;
4040
} catch (err: any) {
@@ -68,16 +68,23 @@ export const parseGPTJson = (JsonStr: string, prefix?: string) => {
6868
};
6969

7070
export const parseGPTResponse = (GPTRes: LLMResponse) => {
71-
if (GPTRes.error) {
71+
try {
72+
if (GPTRes.error) {
73+
return {
74+
error: true,
75+
...GPTRes.error
76+
};
77+
}
78+
const choices = GPTRes.choices;
79+
const content = choices[0].message.content;
80+
const resJson: GPTDataProcessResult = parseGPTJson(content, '```');
81+
return resJson;
82+
} catch (err: any) {
7283
return {
7384
error: true,
74-
...GPTRes.error
85+
message: err.message
7586
};
7687
}
77-
const choices = GPTRes.choices;
78-
const content = choices[0].message.content;
79-
const resJson: GPTDataProcessResult = parseGPTJson(content, '```');
80-
return resJson;
8188
};
8289

8390
export const replaceAll = (originStr: string, replaceStr: string, newStr: string) => {

packages/vmind/src/skylark/chart-generation/NLToChart.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const chartAdvisorSkylark = async (
8585
const chartRecommendRes = await requestSkyLark(chartRecommendPrompt, userMessage, options);
8686
const chartRecommendResJSON = parseSkylarkResponse(chartRecommendRes);
8787
if (chartRecommendResJSON.error) {
88-
throw Error('Network Error!');
88+
throw Error(chartRecommendResJSON.message);
8989
}
9090
if (!SUPPORTED_CHART_LIST.includes(chartRecommendResJSON['charttype'])) {
9191
throw Error('Unsupported Chart Type. Please Change User Input');

packages/vmind/src/skylark/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export const parseSkylarkResponse = (larkResponse: LLMResponse): Record<string,
4747
resJson.usage = usage;
4848
//replace all the keys to lower case.
4949
return Object.keys(resJson).reduce((prev, cur) => ({ ...prev, [cur.toLocaleLowerCase()]: resJson[cur] }), {});
50-
} catch (err) {
50+
} catch (err: any) {
5151
console.error(err);
52-
return { error: true };
52+
return { error: true, message: err.message };
5353
}
5454
};

0 commit comments

Comments
 (0)