2024-02-20 18:10:20 +08:00
|
|
|
|
import SimilaritySlider from '@/components/similarity-slider';
|
2024-02-19 19:16:23 +08:00
|
|
|
|
import { DeleteOutlined } from '@ant-design/icons';
|
|
|
|
|
|
import {
|
|
|
|
|
|
Button,
|
|
|
|
|
|
Col,
|
|
|
|
|
|
Divider,
|
|
|
|
|
|
Form,
|
|
|
|
|
|
Input,
|
|
|
|
|
|
Row,
|
2024-02-20 18:10:20 +08:00
|
|
|
|
Slider,
|
2024-02-19 19:16:23 +08:00
|
|
|
|
Switch,
|
|
|
|
|
|
Table,
|
|
|
|
|
|
TableProps,
|
|
|
|
|
|
} from 'antd';
|
|
|
|
|
|
import classNames from 'classnames';
|
2024-02-20 18:10:20 +08:00
|
|
|
|
import {
|
|
|
|
|
|
ForwardedRef,
|
|
|
|
|
|
forwardRef,
|
|
|
|
|
|
useEffect,
|
|
|
|
|
|
useImperativeHandle,
|
|
|
|
|
|
useState,
|
|
|
|
|
|
} from 'react';
|
2024-02-19 19:16:23 +08:00
|
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
|
|
import { EditableCell, EditableRow } from './editable-cell';
|
|
|
|
|
|
import { ISegmentedContentProps } from './interface';
|
|
|
|
|
|
|
|
|
|
|
|
import styles from './index.less';
|
|
|
|
|
|
|
|
|
|
|
|
interface DataType {
|
|
|
|
|
|
key: string;
|
2024-02-20 18:10:20 +08:00
|
|
|
|
variable: string;
|
2024-02-19 19:16:23 +08:00
|
|
|
|
optional: boolean;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-20 18:10:20 +08:00
|
|
|
|
type FieldType = {
|
|
|
|
|
|
similarity_threshold?: number;
|
|
|
|
|
|
vector_similarity_weight?: number;
|
|
|
|
|
|
top_n?: number;
|
|
|
|
|
|
};
|
2024-02-19 19:16:23 +08:00
|
|
|
|
|
2024-02-20 18:10:20 +08:00
|
|
|
|
const PromptEngine = (
|
|
|
|
|
|
{ show, form }: ISegmentedContentProps,
|
|
|
|
|
|
ref: ForwardedRef<Array<Omit<DataType, 'variable'>>>,
|
|
|
|
|
|
) => {
|
2024-02-19 19:16:23 +08:00
|
|
|
|
const [dataSource, setDataSource] = useState<DataType[]>([]);
|
|
|
|
|
|
|
|
|
|
|
|
const components = {
|
|
|
|
|
|
body: {
|
|
|
|
|
|
row: EditableRow,
|
|
|
|
|
|
cell: EditableCell,
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleRemove = (key: string) => () => {
|
|
|
|
|
|
const newData = dataSource.filter((item) => item.key !== key);
|
|
|
|
|
|
setDataSource(newData);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleSave = (row: DataType) => {
|
|
|
|
|
|
const newData = [...dataSource];
|
|
|
|
|
|
const index = newData.findIndex((item) => row.key === item.key);
|
|
|
|
|
|
const item = newData[index];
|
|
|
|
|
|
newData.splice(index, 1, {
|
|
|
|
|
|
...item,
|
|
|
|
|
|
...row,
|
|
|
|
|
|
});
|
|
|
|
|
|
setDataSource(newData);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-02-20 18:10:20 +08:00
|
|
|
|
const handleAdd = () => {
|
|
|
|
|
|
setDataSource((state) => [
|
|
|
|
|
|
...state,
|
|
|
|
|
|
{
|
|
|
|
|
|
key: uuid(),
|
|
|
|
|
|
variable: '',
|
|
|
|
|
|
optional: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
]);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleOptionalChange = (row: DataType) => (checked: boolean) => {
|
|
|
|
|
|
const newData = [...dataSource];
|
|
|
|
|
|
const index = newData.findIndex((item) => row.key === item.key);
|
|
|
|
|
|
const item = newData[index];
|
|
|
|
|
|
newData.splice(index, 1, {
|
|
|
|
|
|
...item,
|
|
|
|
|
|
optional: checked,
|
|
|
|
|
|
});
|
|
|
|
|
|
setDataSource(newData);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
useImperativeHandle(
|
|
|
|
|
|
ref,
|
|
|
|
|
|
() => {
|
|
|
|
|
|
return dataSource
|
|
|
|
|
|
.filter((x) => x.variable.trim() !== '')
|
|
|
|
|
|
.map((x) => ({ key: x.variable, optional: x.optional }));
|
|
|
|
|
|
},
|
|
|
|
|
|
[dataSource],
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
form.setFieldValue(['prompt_config', 'parameters'], dataSource);
|
|
|
|
|
|
const x = form.getFieldValue(['prompt_config', 'parameters']);
|
|
|
|
|
|
console.info(x);
|
|
|
|
|
|
}, [dataSource, form]);
|
|
|
|
|
|
|
2024-02-19 19:16:23 +08:00
|
|
|
|
const columns: TableProps<DataType>['columns'] = [
|
|
|
|
|
|
{
|
|
|
|
|
|
title: 'key',
|
|
|
|
|
|
dataIndex: 'variable',
|
|
|
|
|
|
key: 'variable',
|
|
|
|
|
|
onCell: (record: DataType) => ({
|
|
|
|
|
|
record,
|
|
|
|
|
|
editable: true,
|
|
|
|
|
|
dataIndex: 'variable',
|
|
|
|
|
|
title: 'key',
|
|
|
|
|
|
handleSave,
|
|
|
|
|
|
}),
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
title: 'optional',
|
|
|
|
|
|
dataIndex: 'optional',
|
|
|
|
|
|
key: 'optional',
|
|
|
|
|
|
width: 40,
|
|
|
|
|
|
align: 'center',
|
2024-02-20 18:10:20 +08:00
|
|
|
|
render(text, record) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Switch
|
|
|
|
|
|
size="small"
|
|
|
|
|
|
checked={text}
|
|
|
|
|
|
onChange={handleOptionalChange(record)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
);
|
2024-02-19 19:16:23 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
title: 'operation',
|
|
|
|
|
|
dataIndex: 'operation',
|
|
|
|
|
|
width: 30,
|
|
|
|
|
|
key: 'operation',
|
|
|
|
|
|
align: 'center',
|
|
|
|
|
|
render(_, record) {
|
|
|
|
|
|
return <DeleteOutlined onClick={handleRemove(record.key)} />;
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<section
|
|
|
|
|
|
className={classNames({
|
|
|
|
|
|
[styles.segmentedHidden]: !show,
|
|
|
|
|
|
})}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Form.Item
|
|
|
|
|
|
label="Orchestrate"
|
|
|
|
|
|
rules={[{ required: true, message: 'Please input!' }]}
|
2024-02-20 18:10:20 +08:00
|
|
|
|
name={['prompt_config', 'system']}
|
|
|
|
|
|
initialValue={`你是一个智能助手,请总结知识库的内容来回答问题,请列举知识库中的数据详细回答。当所有知识库内容都与问题无关时,你的回答必须包括“知识库中未找到您要的答案!”这句话。回答需要考虑聊天历史。
|
|
|
|
|
|
以下是知识库:
|
|
|
|
|
|
{knowledge}
|
|
|
|
|
|
以上是知识库。`}
|
2024-02-19 19:16:23 +08:00
|
|
|
|
>
|
|
|
|
|
|
<Input.TextArea autoSize={{ maxRows: 5, minRows: 5 }} />
|
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
<Divider></Divider>
|
2024-02-20 18:10:20 +08:00
|
|
|
|
<SimilaritySlider></SimilaritySlider>
|
|
|
|
|
|
<Form.Item<FieldType> label="Top n" name={'top_n'} initialValue={0}>
|
|
|
|
|
|
<Slider max={30} />
|
|
|
|
|
|
</Form.Item>
|
2024-02-19 19:16:23 +08:00
|
|
|
|
<section className={classNames(styles.variableContainer)}>
|
|
|
|
|
|
<Row align={'middle'} justify="end">
|
|
|
|
|
|
<Col span={6} className={styles.variableAlign}>
|
|
|
|
|
|
<label className={styles.variableLabel}>Variables</label>
|
|
|
|
|
|
</Col>
|
|
|
|
|
|
<Col span={18} className={styles.variableAlign}>
|
|
|
|
|
|
<Button size="small" onClick={handleAdd}>
|
|
|
|
|
|
Add
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</Col>
|
|
|
|
|
|
</Row>
|
|
|
|
|
|
{dataSource.length > 0 && (
|
|
|
|
|
|
<Row>
|
|
|
|
|
|
<Col span={6}></Col>
|
|
|
|
|
|
<Col span={18}>
|
|
|
|
|
|
<Table
|
|
|
|
|
|
dataSource={dataSource}
|
|
|
|
|
|
columns={columns}
|
|
|
|
|
|
rowKey={'key'}
|
|
|
|
|
|
className={styles.variableTable}
|
|
|
|
|
|
components={components}
|
|
|
|
|
|
rowClassName={() => styles.editableRow}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</Col>
|
|
|
|
|
|
</Row>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</section>
|
|
|
|
|
|
</section>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-02-20 18:10:20 +08:00
|
|
|
|
export default forwardRef(PromptEngine);
|