chore: rerank init params

This commit is contained in:
jialin
2025-02-23 13:32:03 +08:00
parent 8c7eebe76c
commit 21cd4b67f0
3 changed files with 44 additions and 90 deletions
@@ -9,7 +9,7 @@ import {
PlusOutlined, PlusOutlined,
SendOutlined SendOutlined
} from '@ant-design/icons'; } from '@ant-design/icons';
import { useIntl, useSearchParams } from '@umijs/max'; import { useIntl } from '@umijs/max';
import { Button, Checkbox, Form, Input, Spin, Tag, Tooltip } from 'antd'; import { Button, Checkbox, Form, Input, Spin, Tag, Tooltip } from 'antd';
import classNames from 'classnames'; import classNames from 'classnames';
import _ from 'lodash'; import _ from 'lodash';
@@ -26,7 +26,9 @@ import {
} from 'react'; } from 'react';
import { useHotkeys } from 'react-hotkeys-hook'; import { useHotkeys } from 'react-hotkeys-hook';
import { rerankerQuery } from '../apis'; import { rerankerQuery } from '../apis';
import { MessageItem, ParamsSchema } from '../config/types'; import { ParamsSchema } from '../config/types';
import { LLM_METAKEYS } from '../hooks/config';
import { useInitLLmMeta } from '../hooks/use-init-meta';
import '../style/ground-left.less'; import '../style/ground-left.less';
import '../style/rerank.less'; import '../style/rerank.less';
import '../style/system-message-wrap.less'; import '../style/system-message-wrap.less';
@@ -41,7 +43,7 @@ interface MessageProps {
ref?: any; ref?: any;
} }
const paramsConfig: ParamsSchema[] = [ const fieldConfig: ParamsSchema[] = [
{ {
type: 'InputNumber', type: 'InputNumber',
name: 'top_n', name: 'top_n',
@@ -61,32 +63,20 @@ const paramsConfig: ParamsSchema[] = [
} }
]; ];
const initialValues = {
top_n: 3
};
const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => { const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => {
const { modelList } = props; const { modelList } = props;
const acceptType =
'.txt, .doc, .docx, .xls, .xlsx, .csv, .md, .pdf, .eml, .msg, .ppt, .pptx, .xml, .epub, .html';
const messageId = useRef<number>(0);
const [messageList, setMessageList] = useState<MessageItem[]>([]);
const messageId = useRef<number>(0);
const intl = useIntl(); const intl = useIntl();
const requestSource = useRequestToken(); const requestSource = useRequestToken();
const [searchParams] = useSearchParams();
const selectModel = searchParams.get('model') || '';
const [parameters, setParams] = useState<any>({});
const [show, setShow] = useState(false); const [show, setShow] = useState(false);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [tokenResult, setTokenResult] = useState<any>(null); const [tokenResult, setTokenResult] = useState<any>(null);
const [collapse, setCollapse] = useState(false); const [collapse, setCollapse] = useState(false);
const scroller = useRef<any>(null); const scroller = useRef<any>(null);
const inputListRef = useRef<any>(null); const inputListRef = useRef<any>(null);
const paramsRef = useRef<any>(null);
const messageListLengthCache = useRef<number>(0); const messageListLengthCache = useRef<number>(0);
const requestToken = useRef<any>(null); const requestToken = useRef<any>(null);
const formRef = useRef<any>(null);
const multiplePasteEnable = useRef<boolean>(true); const multiplePasteEnable = useRef<boolean>(true);
const [isEmptyText, setIsEmptyText] = useState<boolean>(false); const [isEmptyText, setIsEmptyText] = useState<boolean>(false);
const [fileList, setFileList] = useState< const [fileList, setFileList] = useState<
@@ -130,8 +120,21 @@ const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => {
const { initialize, updateScrollerPosition: updateDocumentScrollerPosition } = const { initialize, updateScrollerPosition: updateDocumentScrollerPosition } =
useOverlayScroller(); useOverlayScroller();
const { initialize: innitializeParams, updateScrollerPosition } =
useOverlayScroller(); const {
handleOnValuesChange,
formRef,
paramsConfig,
initialValues,
parameters,
paramsRef,
modelMeta,
formFields
} = useInitLLmMeta(props, {
defaultValues: { top_n: 3 },
defaultParamsConfig: fieldConfig,
metaKeys: LLM_METAKEYS
});
useImperativeHandle(ref, () => { useImperativeHandle(ref, () => {
return { return {
@@ -148,14 +151,14 @@ const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => {
return generateRerankCode({ return generateRerankCode({
api: '/v1/rerank', api: '/v1/rerank',
parameters: { parameters: {
...parameters, ..._.pick(parameters, ['model', ..._.split(formFields, ',')]),
query: queryValue, query: queryValue,
documents: [...textList, ...fileList] documents: [...textList, ...fileList]
.map((item) => item.text) .map((item) => item.text)
.filter((text) => text) .filter((text) => text)
} }
}); });
}, [parameters, queryValue, textList, fileList]); }, [parameters, formFields, queryValue, textList, fileList]);
// [0.1, 1.0] // [0.1, 1.0]
const normalizValue = (data: { min: number; max: number; value: number }) => { const normalizValue = (data: { min: number; max: number; value: number }) => {
@@ -290,30 +293,6 @@ const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => {
newTextList = _.sortBy(newTextList, 'rank'); newTextList = _.sortBy(newTextList, 'rank');
setSortIndexMap(sortMap); setSortIndexMap(sortMap);
setTextList(newTextList); setTextList(newTextList);
setMessageList([
{
title: 'Results',
role: '',
content: result.results?.map((item: any) => {
const percent: number = normalizValue({
min: minValue,
max: maxValue,
value: item.relevance_score
});
return {
uid: setMessageId(),
text: `${item.document?.text?.slice(0, 500) || ''}`,
docIndex: item.index,
title: documentList[item.index]?.name || '',
score: item.relevance_score,
extra: renderPercent(percent),
normalizValue: percent
};
}),
uid: setMessageId()
}
]);
} catch (error: any) { } catch (error: any) {
setTokenResult({ setTokenResult({
error: true, error: true,
@@ -336,20 +315,6 @@ const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => {
setShow(false); setShow(false);
}; };
const handleUpdateFileList = (
files: { text: string; name: string; uid: number | string }[]
) => {
console.log('files:', files);
setFileList((preList) => {
return [...preList, ...files];
});
};
const handleDeleteFile = (uid: number | string) => {
setFileList((preList) => {
return preList.filter((item) => item.uid !== uid);
});
};
const handleAddText = () => { const handleAddText = () => {
inputListRef.current?.handleAdd(); inputListRef.current?.handleAdd();
}; };
@@ -428,12 +393,20 @@ const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => {
[] []
); );
const handleModelChange = (value: string) => { const renderExtra = useMemo(() => {
const model = modelList.find((item) => item.value === value); if (modelMeta?.n_ctx && modelMeta?.n_slot) {
if (model) { return (
setMetaData(model.meta || {}); <Form.Item>
<SealInputNumber
disabled
label="Max Tokens"
value={_.divide(modelMeta?.n_ctx, modelMeta?.n_slot)}
></SealInputNumber>
</Form.Item>
);
} }
}; return null;
}, modelMeta);
const handleClearDocuments = () => { const handleClearDocuments = () => {
setTextList([ setTextList([
@@ -467,7 +440,6 @@ const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => {
useEffect(() => { useEffect(() => {
setMessageId(); setMessageId();
setMessageList([]);
setTokenResult(null); setTokenResult(null);
}, [parameters.model]); }, [parameters.model]);
@@ -477,16 +449,6 @@ const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => {
} }
}, [scroller.current, initialize]); }, [scroller.current, initialize]);
useEffect(() => {
if (paramsRef.current) {
innitializeParams(paramsRef.current);
}
}, [paramsRef.current, innitializeParams]);
useEffect(() => {
updateScrollerPosition();
}, [messageList]);
useEffect(() => { useEffect(() => {
if (textList.length + fileList.length > messageListLengthCache.current) { if (textList.length + fileList.length > messageListLengthCache.current) {
updateDocumentScrollerPosition(); updateDocumentScrollerPosition();
@@ -630,25 +592,11 @@ const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => {
<div className="box"> <div className="box">
<DynamicParams <DynamicParams
ref={formRef} ref={formRef}
setParams={setParams} onValuesChange={handleOnValuesChange}
params={parameters}
onModelChange={handleModelChange}
paramsConfig={paramsConfig} paramsConfig={paramsConfig}
initialValues={initialValues} initialValues={initialValues}
selectedModel={selectModel}
modelList={modelList} modelList={modelList}
extra={ extra={renderExtra}
metaData?.n_ctx &&
metaData?.n_slot && (
<Form.Item>
<SealInputNumber
disabled
label="Max Tokens"
value={_.divide(metaData?.n_ctx, metaData?.n_slot)}
></SealInputNumber>
</Form.Item>
)
}
/> />
</div> </div>
</div> </div>
+3
View File
@@ -18,6 +18,9 @@ export const playGroundRoles = [
} }
]; ];
export const acceptType =
'.txt, .doc, .docx, .xls, .xlsx, .csv, .md, .pdf, .eml, .msg, .ppt, .pptx, .xml, .epub, .html';
export const formatMessageParams = (messageList: any[]) => { export const formatMessageParams = (messageList: any[]) => {
const result: any[] = []; const result: any[] = [];
@@ -143,6 +143,9 @@ export const useInitLLmMeta = (
extractLLMMeta, extractLLMMeta,
handleOnModelChange, handleOnModelChange,
handleOnValuesChange, handleOnValuesChange,
setModelMeta,
setInitialValues,
setParams,
formRef, formRef,
paramsConfig, paramsConfig,
initialValues, initialValues,