File size: 6,060 Bytes
8c4ec99 84f80c5 452020d 8c4ec99 452020d 84f80c5 452020d 8c4ec99 452020d 058cd84 452020d 058cd84 452020d 8c4ec99 452020d 8c4ec99 058cd84 8c4ec99 452020d 058cd84 452020d 8c4ec99 452020d 8c4ec99 452020d 058cd84 452020d 058cd84 452020d b7adc24 8c4ec99 452020d 058cd84 452020d 830bf29 b7adc24 830bf29 b7adc24 830bf29 8c4ec99 452020d 84f80c5 b7adc24 84f80c5 452020d 84f80c5 452020d 84f80c5 452020d 8c4ec99 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
import SimilaritySlider from '@/components/similarity-slider';
import { DeleteOutlined, QuestionCircleOutlined } from '@ant-design/icons';
import {
Button,
Col,
Divider,
Form,
Input,
Row,
Slider,
Switch,
Table,
TableProps,
Tooltip,
} from 'antd';
import classNames from 'classnames';
import {
ForwardedRef,
forwardRef,
useEffect,
useImperativeHandle,
useState,
} from 'react';
import { v4 as uuid } from 'uuid';
import {
VariableTableDataType as DataType,
IPromptConfigParameters,
ISegmentedContentProps,
} from '../interface';
import { EditableCell, EditableRow } from './editable-cell';
import { useSelectPromptConfigParameters } from '../hooks';
import styles from './index.less';
type FieldType = {
similarity_threshold?: number;
vector_similarity_weight?: number;
top_n?: number;
};
const PromptEngine = (
{ show }: ISegmentedContentProps,
ref: ForwardedRef<Array<IPromptConfigParameters>>,
) => {
const [dataSource, setDataSource] = useState<DataType[]>([]);
const parameters = useSelectPromptConfigParameters();
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);
};
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],
);
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',
render(text, record) {
return (
<Switch
size="small"
checked={text}
onChange={handleOptionalChange(record)}
/>
);
},
},
{
title: 'operation',
dataIndex: 'operation',
width: 30,
key: 'operation',
align: 'center',
render(_, record) {
return <DeleteOutlined onClick={handleRemove(record.key)} />;
},
},
];
useEffect(() => {
setDataSource(parameters);
}, [parameters]);
return (
<section
className={classNames({
[styles.segmentedHidden]: !show,
})}
>
<Form.Item
label="System"
rules={[{ required: true, message: 'Please input!' }]}
tooltip="Instructions you need LLM to follow when LLM answers questions, like charactor design, answer length and answer language etc."
name={['prompt_config', 'system']}
initialValue={`你是一个智能助手,请总结知识库的内容来回答问题,请列举知识库中的数据详细回答。当所有知识库内容都与问题无关时,你的回答必须包括“知识库中未找到您要的答案!”这句话。回答需要考虑聊天历史。
以下是知识库:
{knowledge}
以上是知识库。`}
>
<Input.TextArea autoSize={{ maxRows: 8, minRows: 5 }} />
</Form.Item>
<Divider></Divider>
<SimilaritySlider isTooltipShown></SimilaritySlider>
<Form.Item<FieldType>
label="Top N"
name={'top_n'}
initialValue={8}
tooltip={`Not all the chunks whose similarity score is above the 'simialrity threashold' will be feed to LLMs. LLM can only see these 'Top N' chunks.`}
>
<Slider max={30} />
</Form.Item>
<section className={classNames(styles.variableContainer)}>
<Row align={'middle'} justify="end">
<Col span={7} className={styles.variableAlign}>
<label className={styles.variableLabel}>
Variables
<Tooltip title="If you use dialog APIs, the varialbes might help you chat with your clients with different strategies.
The variables are used to fill-in the 'System' part in prompt in order to give LLM a hint.
The 'knowledge' is a very special variable which will be filled-in with the retrieved chunks.
All the variables in 'System' should be curly bracketed.">
<QuestionCircleOutlined className={styles.variableIcon} />
</Tooltip>
</label>
</Col>
<Col span={17} className={styles.variableAlign}>
<Button size="small" onClick={handleAdd}>
Add
</Button>
</Col>
</Row>
{dataSource.length > 0 && (
<Row>
<Col span={7}> </Col>
<Col span={17}>
<Table
dataSource={dataSource}
columns={columns}
rowKey={'key'}
className={styles.variableTable}
components={components}
rowClassName={() => styles.editableRow}
/>
</Col>
</Row>
)}
</section>
</section>
);
};
export default forwardRef(PromptEngine);
|