mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-11-10 14:54:24 +00:00
feat: restrict classification operators cannot be connected to Answer and other classification #918 (#1294)
### What problem does this PR solve? feat: restrict classification operators cannot be connected to Answer and other classification #918 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
parent
0ce720a247
commit
fbb8cbfc67
@ -20,9 +20,16 @@ import {
|
|||||||
import { RagNode } from './node';
|
import { RagNode } from './node';
|
||||||
|
|
||||||
import ChatDrawer from '../chat/drawer';
|
import ChatDrawer from '../chat/drawer';
|
||||||
|
import { isValidConnection } from '../utils';
|
||||||
import styles from './index.less';
|
import styles from './index.less';
|
||||||
|
import { BeginNode } from './node/begin-node';
|
||||||
|
import { CategorizeNode } from './node/categorize-node';
|
||||||
|
|
||||||
const nodeTypes = { ragNode: RagNode };
|
const nodeTypes = {
|
||||||
|
ragNode: RagNode,
|
||||||
|
categorizeNode: CategorizeNode,
|
||||||
|
beginNode: BeginNode,
|
||||||
|
};
|
||||||
|
|
||||||
const edgeTypes = {
|
const edgeTypes = {
|
||||||
buttonEdge: ButtonEdge,
|
buttonEdge: ButtonEdge,
|
||||||
@ -76,6 +83,7 @@ function FlowCanvas({ chatDrawerVisible, hideChatDrawer }: IProps) {
|
|||||||
onKeyUp={handleKeyUp}
|
onKeyUp={handleKeyUp}
|
||||||
onSelectionChange={onSelectionChange}
|
onSelectionChange={onSelectionChange}
|
||||||
nodeOrigin={[0.5, 0]}
|
nodeOrigin={[0.5, 0]}
|
||||||
|
isValidConnection={isValidConnection}
|
||||||
onChange={(...params) => {
|
onChange={(...params) => {
|
||||||
console.info('params:', ...params);
|
console.info('params:', ...params);
|
||||||
}}
|
}}
|
||||||
|
|||||||
39
web/src/pages/flow/canvas/node/begin-node.tsx
Normal file
39
web/src/pages/flow/canvas/node/begin-node.tsx
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import { Flex, Space } from 'antd';
|
||||||
|
import classNames from 'classnames';
|
||||||
|
import { Handle, NodeProps, Position } from 'reactflow';
|
||||||
|
import { Operator } from '../../constant';
|
||||||
|
import { NodeData } from '../../interface';
|
||||||
|
import OperatorIcon from '../../operator-icon';
|
||||||
|
import NodeDropdown from './dropdown';
|
||||||
|
|
||||||
|
import styles from './index.less';
|
||||||
|
|
||||||
|
// TODO: do not allow other nodes to connect to this node
|
||||||
|
export function BeginNode({ id, data, selected }: NodeProps<NodeData>) {
|
||||||
|
return (
|
||||||
|
<section
|
||||||
|
className={classNames(styles.ragNode, {
|
||||||
|
[styles.selectedNode]: selected,
|
||||||
|
})}
|
||||||
|
>
|
||||||
|
<Handle
|
||||||
|
type="source"
|
||||||
|
position={Position.Right}
|
||||||
|
isConnectable
|
||||||
|
className={styles.handle}
|
||||||
|
></Handle>
|
||||||
|
<Flex vertical align="center" justify="center">
|
||||||
|
<Space size={6}>
|
||||||
|
<OperatorIcon
|
||||||
|
name={data.label as Operator}
|
||||||
|
fontSize={16}
|
||||||
|
></OperatorIcon>
|
||||||
|
<NodeDropdown id={id}></NodeDropdown>
|
||||||
|
</Space>
|
||||||
|
</Flex>
|
||||||
|
<section className={styles.bottomBox}>
|
||||||
|
<div className={styles.nodeName}>{id}</div>
|
||||||
|
</section>
|
||||||
|
</section>
|
||||||
|
);
|
||||||
|
}
|
||||||
51
web/src/pages/flow/canvas/node/categorize-node.tsx
Normal file
51
web/src/pages/flow/canvas/node/categorize-node.tsx
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import { Flex, Space } from 'antd';
|
||||||
|
import classNames from 'classnames';
|
||||||
|
import get from 'lodash/get';
|
||||||
|
import { Handle, NodeProps, Position } from 'reactflow';
|
||||||
|
import { CategorizeAnchorPointPositions, Operator } from '../../constant';
|
||||||
|
import { NodeData } from '../../interface';
|
||||||
|
import OperatorIcon from '../../operator-icon';
|
||||||
|
import CategorizeHandle from './categorize-handle';
|
||||||
|
import NodeDropdown from './dropdown';
|
||||||
|
|
||||||
|
import styles from './index.less';
|
||||||
|
|
||||||
|
export function CategorizeNode({ id, data, selected }: NodeProps<NodeData>) {
|
||||||
|
const categoryData = get(data, 'form.category_description') ?? {};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<section
|
||||||
|
className={classNames(styles.ragNode, {
|
||||||
|
[styles.selectedNode]: selected,
|
||||||
|
})}
|
||||||
|
>
|
||||||
|
<Handle
|
||||||
|
type="target"
|
||||||
|
position={Position.Left}
|
||||||
|
isConnectable
|
||||||
|
className={styles.handle}
|
||||||
|
></Handle>
|
||||||
|
{Object.keys(categoryData).map((x, idx) => (
|
||||||
|
<CategorizeHandle
|
||||||
|
top={CategorizeAnchorPointPositions[idx].top}
|
||||||
|
right={CategorizeAnchorPointPositions[idx].right}
|
||||||
|
key={idx}
|
||||||
|
text={x}
|
||||||
|
idx={idx}
|
||||||
|
></CategorizeHandle>
|
||||||
|
))}
|
||||||
|
<Flex vertical align="center" justify="center">
|
||||||
|
<Space size={6}>
|
||||||
|
<OperatorIcon
|
||||||
|
name={data.label as Operator}
|
||||||
|
fontSize={16}
|
||||||
|
></OperatorIcon>
|
||||||
|
<NodeDropdown id={id}></NodeDropdown>
|
||||||
|
</Space>
|
||||||
|
</Flex>
|
||||||
|
<section className={styles.bottomBox}>
|
||||||
|
<div className={styles.nodeName}>{id}</div>
|
||||||
|
</section>
|
||||||
|
</section>
|
||||||
|
);
|
||||||
|
}
|
||||||
47
web/src/pages/flow/canvas/node/dropdown.tsx
Normal file
47
web/src/pages/flow/canvas/node/dropdown.tsx
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import OperateDropdown from '@/components/operate-dropdown';
|
||||||
|
import { CopyOutlined } from '@ant-design/icons';
|
||||||
|
import { Flex, MenuProps } from 'antd';
|
||||||
|
import { useCallback } from 'react';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import useGraphStore from '../../store';
|
||||||
|
|
||||||
|
interface IProps {
|
||||||
|
id: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const NodeDropdown = ({ id }: IProps) => {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const deleteNodeById = useGraphStore((store) => store.deleteNodeById);
|
||||||
|
const duplicateNodeById = useGraphStore((store) => store.duplicateNode);
|
||||||
|
|
||||||
|
const deleteNode = useCallback(() => {
|
||||||
|
deleteNodeById(id);
|
||||||
|
}, [id, deleteNodeById]);
|
||||||
|
|
||||||
|
const duplicateNode = useCallback(() => {
|
||||||
|
duplicateNodeById(id);
|
||||||
|
}, [id, duplicateNodeById]);
|
||||||
|
|
||||||
|
const items: MenuProps['items'] = [
|
||||||
|
{
|
||||||
|
key: '2',
|
||||||
|
onClick: duplicateNode,
|
||||||
|
label: (
|
||||||
|
<Flex justify={'space-between'}>
|
||||||
|
{t('common.copy')}
|
||||||
|
<CopyOutlined />
|
||||||
|
</Flex>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<OperateDropdown
|
||||||
|
iconFontSize={14}
|
||||||
|
deleteItem={deleteNode}
|
||||||
|
items={items}
|
||||||
|
></OperateDropdown>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default NodeDropdown;
|
||||||
@ -1,17 +1,13 @@
|
|||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import { Handle, NodeProps, Position } from 'reactflow';
|
import { Handle, NodeProps, Position } from 'reactflow';
|
||||||
|
|
||||||
import OperateDropdown from '@/components/operate-dropdown';
|
import { Flex, Space } from 'antd';
|
||||||
import { CopyOutlined } from '@ant-design/icons';
|
|
||||||
import { Flex, MenuProps, Space } from 'antd';
|
|
||||||
import get from 'lodash/get';
|
import get from 'lodash/get';
|
||||||
import { useCallback } from 'react';
|
|
||||||
import { useTranslation } from 'react-i18next';
|
|
||||||
import { CategorizeAnchorPointPositions, Operator } from '../../constant';
|
import { CategorizeAnchorPointPositions, Operator } from '../../constant';
|
||||||
import { NodeData } from '../../interface';
|
import { NodeData } from '../../interface';
|
||||||
import OperatorIcon from '../../operator-icon';
|
import OperatorIcon from '../../operator-icon';
|
||||||
import useGraphStore from '../../store';
|
|
||||||
import CategorizeHandle from './categorize-handle';
|
import CategorizeHandle from './categorize-handle';
|
||||||
|
import NodeDropdown from './dropdown';
|
||||||
import styles from './index.less';
|
import styles from './index.less';
|
||||||
|
|
||||||
export function RagNode({
|
export function RagNode({
|
||||||
@ -20,34 +16,9 @@ export function RagNode({
|
|||||||
isConnectable = true,
|
isConnectable = true,
|
||||||
selected,
|
selected,
|
||||||
}: NodeProps<NodeData>) {
|
}: NodeProps<NodeData>) {
|
||||||
const { t } = useTranslation();
|
|
||||||
const deleteNodeById = useGraphStore((store) => store.deleteNodeById);
|
|
||||||
const duplicateNodeById = useGraphStore((store) => store.duplicateNode);
|
|
||||||
|
|
||||||
const deleteNode = useCallback(() => {
|
|
||||||
deleteNodeById(id);
|
|
||||||
}, [id, deleteNodeById]);
|
|
||||||
|
|
||||||
const duplicateNode = useCallback(() => {
|
|
||||||
duplicateNodeById(id);
|
|
||||||
}, [id, duplicateNodeById]);
|
|
||||||
|
|
||||||
const isCategorize = data.label === Operator.Categorize;
|
const isCategorize = data.label === Operator.Categorize;
|
||||||
const categoryData = get(data, 'form.category_description') ?? {};
|
const categoryData = get(data, 'form.category_description') ?? {};
|
||||||
|
|
||||||
const items: MenuProps['items'] = [
|
|
||||||
{
|
|
||||||
key: '2',
|
|
||||||
onClick: duplicateNode,
|
|
||||||
label: (
|
|
||||||
<Flex justify={'space-between'}>
|
|
||||||
{t('common.copy')}
|
|
||||||
<CopyOutlined />
|
|
||||||
</Flex>
|
|
||||||
),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section
|
<section
|
||||||
className={classNames(styles.ragNode, {
|
className={classNames(styles.ragNode, {
|
||||||
@ -86,11 +57,7 @@ export function RagNode({
|
|||||||
name={data.label as Operator}
|
name={data.label as Operator}
|
||||||
fontSize={16}
|
fontSize={16}
|
||||||
></OperatorIcon>
|
></OperatorIcon>
|
||||||
<OperateDropdown
|
<NodeDropdown id={id}></NodeDropdown>
|
||||||
iconFontSize={14}
|
|
||||||
deleteItem={deleteNode}
|
|
||||||
items={items}
|
|
||||||
></OperateDropdown>
|
|
||||||
</Space>
|
</Space>
|
||||||
</Flex>
|
</Flex>
|
||||||
|
|
||||||
|
|||||||
@ -97,3 +97,27 @@ export const CategorizeAnchorPointPositions = [
|
|||||||
{ top: 91, right: 20 },
|
{ top: 91, right: 20 },
|
||||||
{ top: 98, right: 34 },
|
{ top: 98, right: 34 },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// key is the source of the edge, value is the target of the edge
|
||||||
|
// no connection lines are allowed between key and value
|
||||||
|
export const RestrictedUpstreamMap = {
|
||||||
|
[Operator.Begin]: [
|
||||||
|
Operator.Begin,
|
||||||
|
Operator.Answer,
|
||||||
|
Operator.Categorize,
|
||||||
|
Operator.Generate,
|
||||||
|
Operator.Retrieval,
|
||||||
|
],
|
||||||
|
[Operator.Categorize]: [Operator.Begin, Operator.Categorize, Operator.Answer],
|
||||||
|
[Operator.Answer]: [],
|
||||||
|
[Operator.Retrieval]: [],
|
||||||
|
[Operator.Generate]: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
export const NodeMap = {
|
||||||
|
[Operator.Begin]: 'beginNode',
|
||||||
|
[Operator.Categorize]: 'categorizeNode',
|
||||||
|
[Operator.Retrieval]: 'ragNode',
|
||||||
|
[Operator.Generate]: 'ragNode',
|
||||||
|
[Operator.Answer]: 'ragNode',
|
||||||
|
};
|
||||||
|
|||||||
@ -25,7 +25,7 @@ import { useDebounceEffect } from 'ahooks';
|
|||||||
import { FormInstance } from 'antd';
|
import { FormInstance } from 'antd';
|
||||||
import { humanId } from 'human-id';
|
import { humanId } from 'human-id';
|
||||||
import { useParams } from 'umi';
|
import { useParams } from 'umi';
|
||||||
import { Operator } from './constant';
|
import { NodeMap, Operator } from './constant';
|
||||||
import useGraphStore, { RFState } from './store';
|
import useGraphStore, { RFState } from './store';
|
||||||
import { buildDslComponentsByGraph } from './utils';
|
import { buildDslComponentsByGraph } from './utils';
|
||||||
|
|
||||||
@ -87,7 +87,7 @@ export const useHandleDrop = () => {
|
|||||||
});
|
});
|
||||||
const newNode = {
|
const newNode = {
|
||||||
id: `${type}:${humanId()}`,
|
id: `${type}:${humanId()}`,
|
||||||
type: 'ragNode',
|
type: NodeMap[type as Operator] || 'ragNode',
|
||||||
position: position || {
|
position: position || {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
|
|||||||
@ -38,7 +38,7 @@ export const dsl = {
|
|||||||
nodes: [
|
nodes: [
|
||||||
{
|
{
|
||||||
id: 'begin',
|
id: 'begin',
|
||||||
type: 'ragNode',
|
type: 'beginNode',
|
||||||
position: {
|
position: {
|
||||||
x: 50,
|
x: 50,
|
||||||
y: 200,
|
y: 200,
|
||||||
|
|||||||
@ -3,9 +3,13 @@ import { removeUselessFieldsFromValues } from '@/utils/form';
|
|||||||
import dagre from 'dagre';
|
import dagre from 'dagre';
|
||||||
import { curry, isEmpty } from 'lodash';
|
import { curry, isEmpty } from 'lodash';
|
||||||
import pipe from 'lodash/fp/pipe';
|
import pipe from 'lodash/fp/pipe';
|
||||||
import { Edge, MarkerType, Node, Position } from 'reactflow';
|
import { Connection, Edge, MarkerType, Node, Position } from 'reactflow';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
import { Operator, initialFormValuesMap } from './constant';
|
import {
|
||||||
|
Operator,
|
||||||
|
RestrictedUpstreamMap,
|
||||||
|
initialFormValuesMap,
|
||||||
|
} from './constant';
|
||||||
import { NodeData } from './interface';
|
import { NodeData } from './interface';
|
||||||
|
|
||||||
const buildEdges = (
|
const buildEdges = (
|
||||||
@ -162,3 +166,14 @@ export const buildDslComponentsByGraph = (
|
|||||||
|
|
||||||
return components;
|
return components;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getOperatorType = (id: string | null) => {
|
||||||
|
return id?.split(':')[0] as Operator | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
// restricted lines cannot be connected successfully.
|
||||||
|
export const isValidConnection = (connection: Connection) => {
|
||||||
|
return RestrictedUpstreamMap[
|
||||||
|
getOperatorType(connection.source) as Operator
|
||||||
|
]?.every((x) => x !== getOperatorType(connection.target));
|
||||||
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user