balibabu
commited on
Commit
·
c939fd2
1
Parent(s):
4e603f5
feat: run flow (#1076)
Browse files### What problem does this PR solve?
feat: run flow #918
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
- web/src/hooks/flow-hooks.ts +19 -0
- web/src/pages/flow/constant.ts +9 -4
- web/src/pages/flow/header/index.tsx +3 -2
- web/src/pages/flow/hooks.ts +20 -2
- web/src/pages/flow/mock.tsx +1 -1
- web/src/pages/flow/utils.ts +29 -10
- web/src/services/flow-service.ts +5 -0
- web/src/utils/api.ts +1 -0
- web/src/utils/form.ts +1 -1
web/src/hooks/flow-hooks.ts
CHANGED
@@ -90,3 +90,22 @@ export const useDeleteFlow = () => {
|
|
90 |
|
91 |
return { data, loading, deleteFlow: mutateAsync };
|
92 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
return { data, loading, deleteFlow: mutateAsync };
|
92 |
};
|
93 |
+
|
94 |
+
export const useRunFlow = () => {
|
95 |
+
const {
|
96 |
+
data,
|
97 |
+
isPending: loading,
|
98 |
+
mutateAsync,
|
99 |
+
} = useMutation({
|
100 |
+
mutationKey: ['runFlow'],
|
101 |
+
mutationFn: async (params: { id: string; dsl: DSL }) => {
|
102 |
+
const { data } = await flowService.runCanvas(params);
|
103 |
+
if (data.retcode === 0) {
|
104 |
+
message.success(i18n.t(`message.modified`));
|
105 |
+
}
|
106 |
+
return data?.data ?? {};
|
107 |
+
},
|
108 |
+
});
|
109 |
+
|
110 |
+
return { data, loading, runFlow: mutateAsync };
|
111 |
+
};
|
web/src/pages/flow/constant.ts
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
import { ModelVariableType } from '@/constants/knowledge';
|
2 |
-
|
3 |
export enum Operator {
|
4 |
Begin = 'Begin',
|
5 |
Retrieval = 'Retrieval',
|
@@ -18,8 +16,8 @@ export const initialBeginValues = {
|
|
18 |
};
|
19 |
|
20 |
export const initialGenerateValues = {
|
21 |
-
parameters: ModelVariableType.Precise,
|
22 |
-
temperatureEnabled:
|
23 |
temperature: 0.1,
|
24 |
top_p: 0.3,
|
25 |
frequency_penalty: 0.7,
|
@@ -30,3 +28,10 @@ export const initialGenerateValues = {
|
|
30 |
The above is the content you need to summarize.`,
|
31 |
cite: true,
|
32 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
export enum Operator {
|
2 |
Begin = 'Begin',
|
3 |
Retrieval = 'Retrieval',
|
|
|
16 |
};
|
17 |
|
18 |
export const initialGenerateValues = {
|
19 |
+
// parameters: ModelVariableType.Precise,
|
20 |
+
// temperatureEnabled: true,
|
21 |
temperature: 0.1,
|
22 |
top_p: 0.3,
|
23 |
frequency_penalty: 0.7,
|
|
|
28 |
The above is the content you need to summarize.`,
|
29 |
cite: true,
|
30 |
};
|
31 |
+
|
32 |
+
export const initialFormValuesMap = {
|
33 |
+
[Operator.Begin]: initialBeginValues,
|
34 |
+
[Operator.Retrieval]: initialRetrievalValues,
|
35 |
+
[Operator.Generate]: initialGenerateValues,
|
36 |
+
[Operator.Answer]: {},
|
37 |
+
};
|
web/src/pages/flow/header/index.tsx
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
import { Button, Flex } from 'antd';
|
2 |
|
3 |
-
import { useSaveGraph } from '../hooks';
|
4 |
import styles from './index.less';
|
5 |
|
6 |
const FlowHeader = () => {
|
7 |
const { saveGraph } = useSaveGraph();
|
|
|
8 |
|
9 |
return (
|
10 |
<Flex
|
@@ -13,7 +14,7 @@ const FlowHeader = () => {
|
|
13 |
gap={'large'}
|
14 |
className={styles.flowHeader}
|
15 |
>
|
16 |
-
<Button>
|
17 |
<b>Debug</b>
|
18 |
</Button>
|
19 |
<Button type="primary" onClick={saveGraph}>
|
|
|
1 |
import { Button, Flex } from 'antd';
|
2 |
|
3 |
+
import { useRunGraph, useSaveGraph } from '../hooks';
|
4 |
import styles from './index.less';
|
5 |
|
6 |
const FlowHeader = () => {
|
7 |
const { saveGraph } = useSaveGraph();
|
8 |
+
const { runGraph } = useRunGraph();
|
9 |
|
10 |
return (
|
11 |
<Flex
|
|
|
14 |
gap={'large'}
|
15 |
className={styles.flowHeader}
|
16 |
>
|
17 |
+
<Button onClick={runGraph}>
|
18 |
<b>Debug</b>
|
19 |
</Button>
|
20 |
<Button type="primary" onClick={saveGraph}>
|
web/src/pages/flow/hooks.ts
CHANGED
@@ -2,6 +2,7 @@ import { useSetModalState } from '@/hooks/commonHooks';
|
|
2 |
import {
|
3 |
useFetchFlow,
|
4 |
useFetchFlowTemplates,
|
|
|
5 |
useSetFlow,
|
6 |
} from '@/hooks/flow-hooks';
|
7 |
import { useFetchLlmList } from '@/hooks/llmHooks';
|
@@ -83,7 +84,9 @@ export const useHandleDrop = () => {
|
|
83 |
x: 0,
|
84 |
y: 0,
|
85 |
},
|
86 |
-
data: {
|
|
|
|
|
87 |
sourcePosition: Position.Right,
|
88 |
targetPosition: Position.Left,
|
89 |
};
|
@@ -141,7 +144,6 @@ export const useSaveGraph = () => {
|
|
141 |
const { nodes, edges } = useStore((state) => state);
|
142 |
const saveGraph = useCallback(() => {
|
143 |
const dslComponents = buildDslComponentsByGraph(nodes, edges);
|
144 |
-
console.info('components:', dslComponents);
|
145 |
setFlow({
|
146 |
id,
|
147 |
title: data.title,
|
@@ -198,3 +200,19 @@ export const useFetchDataOnMount = () => {
|
|
198 |
export const useFlowIsFetching = () => {
|
199 |
return useIsFetching({ queryKey: ['flowDetail'] }) > 0;
|
200 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import {
|
3 |
useFetchFlow,
|
4 |
useFetchFlowTemplates,
|
5 |
+
useRunFlow,
|
6 |
useSetFlow,
|
7 |
} from '@/hooks/flow-hooks';
|
8 |
import { useFetchLlmList } from '@/hooks/llmHooks';
|
|
|
84 |
x: 0,
|
85 |
y: 0,
|
86 |
},
|
87 |
+
data: {
|
88 |
+
label: `${type}`,
|
89 |
+
},
|
90 |
sourcePosition: Position.Right,
|
91 |
targetPosition: Position.Left,
|
92 |
};
|
|
|
144 |
const { nodes, edges } = useStore((state) => state);
|
145 |
const saveGraph = useCallback(() => {
|
146 |
const dslComponents = buildDslComponentsByGraph(nodes, edges);
|
|
|
147 |
setFlow({
|
148 |
id,
|
149 |
title: data.title,
|
|
|
200 |
export const useFlowIsFetching = () => {
|
201 |
return useIsFetching({ queryKey: ['flowDetail'] }) > 0;
|
202 |
};
|
203 |
+
|
204 |
+
export const useRunGraph = () => {
|
205 |
+
const { data } = useFetchFlow();
|
206 |
+
const { runFlow } = useRunFlow();
|
207 |
+
const { id } = useParams();
|
208 |
+
const { nodes, edges } = useStore((state) => state);
|
209 |
+
const runGraph = useCallback(() => {
|
210 |
+
const dslComponents = buildDslComponentsByGraph(nodes, edges);
|
211 |
+
runFlow({
|
212 |
+
id: id!!,
|
213 |
+
dsl: { ...data.dsl, components: dslComponents },
|
214 |
+
});
|
215 |
+
}, [nodes, edges, runFlow, id, data]);
|
216 |
+
|
217 |
+
return { runGraph };
|
218 |
+
};
|
web/src/pages/flow/mock.tsx
CHANGED
@@ -181,6 +181,6 @@ export const dsl = {
|
|
181 |
},
|
182 |
},
|
183 |
history: [],
|
184 |
-
path: [
|
185 |
answer: [],
|
186 |
};
|
|
|
181 |
},
|
182 |
},
|
183 |
history: [],
|
184 |
+
path: [],
|
185 |
answer: [],
|
186 |
};
|
web/src/pages/flow/utils.ts
CHANGED
@@ -1,8 +1,11 @@
|
|
1 |
import { DSLComponents } from '@/interfaces/database/flow';
|
2 |
import { removeUselessFieldsFromValues } from '@/utils/form';
|
3 |
import dagre from 'dagre';
|
|
|
|
|
4 |
import { Edge, MarkerType, Node, Position } from 'reactflow';
|
5 |
import { v4 as uuidv4 } from 'uuid';
|
|
|
6 |
import { NodeData } from './interface';
|
7 |
|
8 |
const buildEdges = (
|
@@ -109,15 +112,27 @@ const buildComponentDownstreamOrUpstream = (
|
|
109 |
.map((y) => y[isBuildDownstream ? 'target' : 'source']);
|
110 |
};
|
111 |
|
112 |
-
const removeUselessDataInTheOperator = (
|
113 |
-
operatorName: string,
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
}
|
119 |
-
return
|
120 |
-
};
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
// construct a dsl based on the node information of the graph
|
123 |
export const buildDslComponentsByGraph = (
|
@@ -132,9 +147,13 @@ export const buildDslComponentsByGraph = (
|
|
132 |
components[id] = {
|
133 |
obj: {
|
134 |
component_name: operatorName,
|
|
|
|
|
|
|
|
|
|
|
135 |
params:
|
136 |
-
|
137 |
-
operatorName,
|
138 |
x.data.form as Record<string, unknown>,
|
139 |
) ?? {},
|
140 |
},
|
|
|
1 |
import { DSLComponents } from '@/interfaces/database/flow';
|
2 |
import { removeUselessFieldsFromValues } from '@/utils/form';
|
3 |
import dagre from 'dagre';
|
4 |
+
import { curry, isEmpty } from 'lodash';
|
5 |
+
import pipe from 'lodash/fp/pipe';
|
6 |
import { Edge, MarkerType, Node, Position } from 'reactflow';
|
7 |
import { v4 as uuidv4 } from 'uuid';
|
8 |
+
import { Operator, initialFormValuesMap } from './constant';
|
9 |
import { NodeData } from './interface';
|
10 |
|
11 |
const buildEdges = (
|
|
|
112 |
.map((y) => y[isBuildDownstream ? 'target' : 'source']);
|
113 |
};
|
114 |
|
115 |
+
const removeUselessDataInTheOperator = curry(
|
116 |
+
(operatorName: string, params: Record<string, unknown>) => {
|
117 |
+
if (operatorName === Operator.Generate) {
|
118 |
+
return removeUselessFieldsFromValues(params, '');
|
119 |
+
}
|
120 |
+
return params;
|
121 |
+
},
|
122 |
+
);
|
123 |
+
// initialize data for operators without parameters
|
124 |
+
const initializeOperatorParams = curry((operatorName: string, values: any) => {
|
125 |
+
if (isEmpty(values)) {
|
126 |
+
return initialFormValuesMap[operatorName as Operator];
|
127 |
}
|
128 |
+
return values;
|
129 |
+
});
|
130 |
+
|
131 |
+
const buildOperatorParams = (operatorName: string) =>
|
132 |
+
pipe(
|
133 |
+
removeUselessDataInTheOperator(operatorName),
|
134 |
+
initializeOperatorParams(operatorName), // Final processing, for guarantee
|
135 |
+
);
|
136 |
|
137 |
// construct a dsl based on the node information of the graph
|
138 |
export const buildDslComponentsByGraph = (
|
|
|
147 |
components[id] = {
|
148 |
obj: {
|
149 |
component_name: operatorName,
|
150 |
+
// params:
|
151 |
+
// removeUselessDataInTheOperator(
|
152 |
+
// operatorName,
|
153 |
+
// x.data.form as Record<string, unknown>,
|
154 |
+
// ) ?? {},
|
155 |
params:
|
156 |
+
buildOperatorParams(operatorName)(
|
|
|
157 |
x.data.form as Record<string, unknown>,
|
158 |
) ?? {},
|
159 |
},
|
web/src/services/flow-service.ts
CHANGED
@@ -8,6 +8,7 @@ const {
|
|
8 |
listCanvas,
|
9 |
resetCanvas,
|
10 |
removeCanvas,
|
|
|
11 |
listTemplates,
|
12 |
} = api;
|
13 |
|
@@ -32,6 +33,10 @@ const methods = {
|
|
32 |
url: removeCanvas,
|
33 |
method: 'post',
|
34 |
},
|
|
|
|
|
|
|
|
|
35 |
listTemplates: {
|
36 |
url: listTemplates,
|
37 |
method: 'get',
|
|
|
8 |
listCanvas,
|
9 |
resetCanvas,
|
10 |
removeCanvas,
|
11 |
+
runCanvas,
|
12 |
listTemplates,
|
13 |
} = api;
|
14 |
|
|
|
33 |
url: removeCanvas,
|
34 |
method: 'post',
|
35 |
},
|
36 |
+
runCanvas: {
|
37 |
+
url: runCanvas,
|
38 |
+
method: 'post',
|
39 |
+
},
|
40 |
listTemplates: {
|
41 |
url: listTemplates,
|
42 |
method: 'get',
|
web/src/utils/api.ts
CHANGED
@@ -89,4 +89,5 @@ export default {
|
|
89 |
removeCanvas: `${api_host}/canvas/rm`,
|
90 |
setCanvas: `${api_host}/canvas/set`,
|
91 |
resetCanvas: `${api_host}/canvas/reset`,
|
|
|
92 |
};
|
|
|
89 |
removeCanvas: `${api_host}/canvas/rm`,
|
90 |
setCanvas: `${api_host}/canvas/set`,
|
91 |
resetCanvas: `${api_host}/canvas/reset`,
|
92 |
+
runCanvas: `${api_host}/canvas/run`,
|
93 |
};
|
web/src/utils/form.ts
CHANGED
@@ -3,7 +3,7 @@ import omit from 'lodash/omit';
|
|
3 |
|
4 |
// chat model setting and generate operator
|
5 |
export const excludeUnEnabledVariables = (
|
6 |
-
values: any,
|
7 |
prefix = 'llm_setting.',
|
8 |
) => {
|
9 |
const unEnabledFields: Array<keyof typeof variableEnabledFieldMap> =
|
|
|
3 |
|
4 |
// chat model setting and generate operator
|
5 |
export const excludeUnEnabledVariables = (
|
6 |
+
values: any = {},
|
7 |
prefix = 'llm_setting.',
|
8 |
) => {
|
9 |
const unEnabledFields: Array<keyof typeof variableEnabledFieldMap> =
|