Skip to content

Commit dbb4871

Browse files
Yzingyanzhi.yan
andauthored
chore(ava): add llm validate for extract method in test case, support invoke tbox api with ci (#841)
* chore(ava): fix most test cases bug * chore(ava): add llm validate for extract method in test case, support invoke tbox api with ci --------- Co-authored-by: yanzhi.yan <yanzhi.yan@antgroup.com>
1 parent f1ada6f commit dbb4871

6 files changed

Lines changed: 54 additions & 31 deletions

File tree

__tests__/unit/extract/index.test.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
import _ from 'lodash';
22
import { extractData } from '../../../src/extract';
3-
4-
const EXTRACT_MOCK_RESULT = [
5-
{
6-
shape: 'plain',
7-
data: [
8-
{ id: 1001, name: '张三', age: 25 },
9-
{ id: 1002, name: '李四', age: 30 },
10-
{ id: 1003, name: '王五', age: 28 },
11-
{ id: 1004, name: '赵六', age: 35 },
12-
],
13-
meta: [
14-
{ id: 'id', name: '用户ID', dataType: 'number' },
15-
{ id: 'name', name: '姓名', dataType: 'string' },
16-
{ id: 'age', name: '年龄', dataType: 'number' },
17-
],
18-
},
19-
];
3+
import { extract } from './mock-extract';
204

215
jest.mock('../../../src/extract/extract', () => ({
226
__esModule: true,
23-
extract: async () => {
24-
return EXTRACT_MOCK_RESULT;
25-
},
7+
extract: jest.fn().mockImplementation(async (value: string) => {
8+
return await extract(value);
9+
}),
2610
}));
2711

2812
describe('extractData', () => {
@@ -38,10 +22,21 @@ describe('extractData', () => {
3822
3. 用户ID: 1003, 姓名: 王五, 年龄: 28
3923
4. 用户ID: 1004, 姓名: 赵六, 年龄: 35
4024
`;
41-
const expectResult = EXTRACT_MOCK_RESULT;
4225

4326
const result = await extractData(input);
44-
expect(result).toEqual(expectResult);
27+
const shard = result?.[0];
28+
expect(shard.shape).toBe('plain');
29+
expect(shard.data).toEqual([
30+
{ 用户ID: 1001, 姓名: '张三', 年龄: 25 },
31+
{ 用户ID: 1002, 姓名: '李四', 年龄: 30 },
32+
{ 用户ID: 1003, 姓名: '王五', 年龄: 28 },
33+
{ 用户ID: 1004, 姓名: '赵六', 年龄: 35 },
34+
]);
35+
expect(shard.metas).toEqual([
36+
{ id: '用户ID', name: '用户ID', dataType: 'number' },
37+
{ id: '姓名', name: '姓名', dataType: 'string' },
38+
{ id: '年龄', name: '年龄', dataType: 'number' },
39+
]);
4540
});
4641

4742
it('should return plain dataType if input is plain data', async () => {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { requestTboxLLM } from '@ava/utils/llm';
2+
import { getExtractPrompt } from '@ava/extract/extract/prompt';
3+
4+
export const extract = async (value: string) => {
5+
const llmAuth = process.env.LLM_AUTH;
6+
const llmAppId = process.env.LLM_APP_ID;
7+
// @ts-ignore for debug at ci
8+
console.debug(process.env);
9+
if (process.env?.NODE_ENV === 'test' && llmAuth && llmAppId) {
10+
const query = getExtractPrompt(value);
11+
const result = await requestTboxLLM({
12+
config: {
13+
authorization: llmAuth,
14+
appId: llmAppId,
15+
},
16+
prompt: query,
17+
});
18+
return JSON.parse(result);
19+
}
20+
return {};
21+
};

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = {
2-
testRegex: '(/__tests__/.*.(test|spec)).(js?|jsx?|tsx?|ts?)$',
2+
setupFiles: ['<rootDir>/jest.setup.js'],
33
collectCoverage: false,
4+
testRegex: '(/__tests__/.*.(test|spec)).(js?|jsx?|tsx?|ts?)$',
45
coveragePathIgnorePatterns: ['(tests/.*.mock).(jsx?|tsx?)$'],
56
transform: {
67
'^.+\\.m?[tj]sx?$': [

jest.setup.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
require('dotenv').config({
3+
path: ['.env.local']
4+
});
5+
jest.setTimeout(30000);

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@
3838
]
3939
},
4040
"dependencies": {
41-
"@antv/color-schema": "^0.2.3",
42-
"@antv/smart-color": "^0.2.1",
4341
"@antv/algorithm": "^0.1.26",
42+
"@antv/color-schema": "^0.2.3",
4443
"@antv/gpt-vis": "^0.5.9",
44+
"@antv/smart-color": "^0.2.1",
45+
"axios": "^1.12.2",
4546
"bayesian-changepoint": "^1.0.1",
4647
"csstype": "^3.1.2",
4748
"heap-js": "^2.1.6",
4849
"lodash": "^4.17.21",
50+
"moment": "^2.29.4",
51+
"openai": "^6.2.0",
4952
"regression": "^2.0.1",
5053
"tapable": "^2.2.1",
51-
"tslib": "^2.3.1",
52-
"moment": "^2.29.4",
53-
"axios": "^1.12.2",
54-
"openai": "^6.2.0"
54+
"tslib": "^2.3.1"
5555
},
5656
"devDependencies": {
5757
"@antv/data-samples": "^1.0.1",
@@ -66,14 +66,15 @@
6666
"@types/numeral": "^2.0.2",
6767
"@typescript-eslint/eslint-plugin": "^4.29.2",
6868
"@typescript-eslint/parser": "^4.29.2",
69+
"dotenv": "^17.2.3",
6970
"eslint": "^7.32.0",
7071
"eslint-config-airbnb": "^18.2.1",
7172
"eslint-config-prettier": "^8.3.0",
7273
"eslint-import-resolver-typescript": "^2.4.0",
7374
"eslint-plugin-import": "^2.24.1",
7475
"eslint-plugin-react": "^7.24.0",
75-
"husky": "^7.0.1",
7676
"father": "^4.1.0",
77+
"husky": "^7.0.1",
7778
"jest": "^30.0.0",
7879
"lint-staged": "^11.0.1",
7980
"npm-run-all": "^4.1.5",

src/extract/extract/prompt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ export const getExtractPrompt = (input: string) => {
5050
shape: DATA_SHAPE;
5151
data: FieldDataType<DATA_SHAPE>;
5252
metas: Array<{
53+
id: string; // 字段在数据中的 id
5354
name: string; // 字段的名称
54-
key: string; // 字段在数据中的 key
5555
dataType: 'number' | 'string' | 'date' | 'geo'; // 字段的类型
5656
}>; // 字段元信息
5757
purpose?: {

0 commit comments

Comments
 (0)