chore: init chat params

This commit is contained in:
jialin
2025-03-03 10:13:06 +08:00
parent 35fe459420
commit 1bb6c5fbfc
6 changed files with 365 additions and 325 deletions
@@ -18,7 +18,7 @@ type ParamsSettingsProps = {
ref?: any; ref?: any;
parametersTitle?: React.ReactNode; parametersTitle?: React.ReactNode;
showModelSelector?: boolean; showModelSelector?: boolean;
modelList: Global.BaseOption<string>[]; modelList?: Global.BaseOption<string>[];
onValuesChange?: (changeValues: any, value: Record<string, any>) => void; onValuesChange?: (changeValues: any, value: Record<string, any>) => void;
onModelChange?: (model: string) => void; onModelChange?: (model: string) => void;
paramsConfig?: ParamsSchema[]; paramsConfig?: ParamsSchema[];
@@ -143,6 +143,7 @@ const ParamsSettings: React.FC<ParamsSettingsProps> = forwardRef(
</span> </span>
)} )}
</h3> </h3>
{showModelSelector && (
<Form.Item <Form.Item
name="model" name="model"
rules={[ rules={[
@@ -164,6 +165,7 @@ const ParamsSettings: React.FC<ParamsSettingsProps> = forwardRef(
label={intl.formatMessage({ id: 'playground.model' })} label={intl.formatMessage({ id: 'playground.model' })}
></SealSelect> ></SealSelect>
</Form.Item> </Form.Item>
)}
</> </>
} }
{renderFields} {renderFields}
+29 -22
View File
@@ -1,25 +1,26 @@
import useOverlayScroller from '@/hooks/use-overlay-scroller'; import { useIntl } from '@umijs/max';
import { useIntl, useSearchParams } from '@umijs/max';
import { Spin } from 'antd'; import { Spin } from 'antd';
import classNames from 'classnames'; import classNames from 'classnames';
import 'overlayscrollbars/overlayscrollbars.css'; import 'overlayscrollbars/overlayscrollbars.css';
import { import {
forwardRef, forwardRef,
useEffect,
useImperativeHandle, useImperativeHandle,
useMemo, useMemo,
useRef, useRef,
useState useState
} from 'react'; } from 'react';
import { OpenAIViewCode, Roles, generateMessages } from '../config'; import { OpenAIViewCode, Roles, generateMessages } from '../config';
import { ChatParamsConfig } from '../config/params-config';
import { MessageItem, MessageItemAction } from '../config/types'; import { MessageItem, MessageItemAction } from '../config/types';
import { LLM_METAKEYS, llmInitialValues } from '../hooks/config';
import useChatCompletion from '../hooks/use-chat-completion'; import useChatCompletion from '../hooks/use-chat-completion';
import { useInitLLmMeta } from '../hooks/use-init-meta';
import '../style/ground-left.less'; import '../style/ground-left.less';
import '../style/system-message-wrap.less'; import '../style/system-message-wrap.less';
import DynamicParams from './dynamic-params';
import MessageInput from './message-input'; import MessageInput from './message-input';
import MessageContent from './multiple-chat/message-content'; import MessageContent from './multiple-chat/message-content';
import SystemMessage from './multiple-chat/system-message'; import SystemMessage from './multiple-chat/system-message';
import ParamsSettings from './params-settings';
import ReferenceParams from './reference-params'; import ReferenceParams from './reference-params';
import ViewCodeModal from './view-code-modal'; import ViewCodeModal from './view-code-modal';
@@ -32,21 +33,16 @@ interface MessageProps {
const GroundLeft: React.FC<MessageProps> = forwardRef((props, ref) => { const GroundLeft: React.FC<MessageProps> = forwardRef((props, ref) => {
const { modelList } = props; const { modelList } = props;
const intl = useIntl(); const intl = useIntl();
const [searchParams] = useSearchParams();
const selectModel = searchParams.get('model') || '';
const [parameters, setParams] = useState<any>({});
const [systemMessage, setSystemMessage] = useState(''); const [systemMessage, setSystemMessage] = useState('');
const [show, setShow] = useState(false); const [show, setShow] = useState(false);
const [collapse, setCollapse] = useState(false); const [collapse, setCollapse] = useState(false);
const scroller = useRef<any>(null); const scroller = useRef<any>(null);
const paramsRef = useRef<any>(null);
const [actions, setActions] = useState<MessageItemAction[]>([ const [actions, setActions] = useState<MessageItemAction[]>([
'upload', 'upload',
'delete', 'delete',
'copy' 'copy'
]); ]);
const { initialize: innitializeParams } = useOverlayScroller();
const { const {
submitMessage, submitMessage,
handleStopConversation, handleStopConversation,
@@ -57,6 +53,24 @@ const GroundLeft: React.FC<MessageProps> = forwardRef((props, ref) => {
messageList, messageList,
loading loading
} = useChatCompletion(scroller); } = useChatCompletion(scroller);
const {
handleOnValuesChange,
formRef,
paramsRef,
paramsConfig,
initialValues,
parameters
} = useInitLLmMeta(
{ modelList, isChat: true },
{
defaultValues: {
...llmInitialValues,
model: modelList[0]?.value
},
defaultParamsConfig: ChatParamsConfig,
metaKeys: LLM_METAKEYS
}
);
useImperativeHandle(ref, () => { useImperativeHandle(ref, () => {
return { return {
@@ -93,8 +107,6 @@ const GroundLeft: React.FC<MessageProps> = forwardRef((props, ref) => {
setShow(false); setShow(false);
}; };
const handleSelectModel = () => {};
const handleOnCheck = (e: any) => { const handleOnCheck = (e: any) => {
const checked = e.target.checked; const checked = e.target.checked;
if (checked) { if (checked) {
@@ -104,12 +116,6 @@ const GroundLeft: React.FC<MessageProps> = forwardRef((props, ref) => {
} }
}; };
useEffect(() => {
if (paramsRef.current) {
innitializeParams(paramsRef.current);
}
}, [innitializeParams]);
return ( return (
<div className="ground-left-wrapper"> <div className="ground-left-wrapper">
<div className="ground-left"> <div className="ground-left">
@@ -176,7 +182,6 @@ const GroundLeft: React.FC<MessageProps> = forwardRef((props, ref) => {
addMessage={handleAddNewMessage} addMessage={handleAddNewMessage}
handleAbortFetch={handleStopConversation} handleAbortFetch={handleStopConversation}
clearAll={handleClear} clearAll={handleClear}
setModelSelections={handleSelectModel}
/> />
</div> </div>
</div> </div>
@@ -187,11 +192,13 @@ const GroundLeft: React.FC<MessageProps> = forwardRef((props, ref) => {
ref={paramsRef} ref={paramsRef}
> >
<div className="box"> <div className="box">
<ParamsSettings <DynamicParams
setParams={setParams} ref={formRef}
params={parameters} onValuesChange={handleOnValuesChange}
selectedModel={selectModel} paramsConfig={paramsConfig}
initialValues={initialValues}
modelList={modelList} modelList={modelList}
showModelSelector={true}
/> />
</div> </div>
</div> </div>
@@ -28,13 +28,7 @@ const MultiCompare: React.FC<MultiCompareProps> = ({ modelList, loaded }) => {
const [modelSelections, setModelSelections] = useState<ModelSelectionItem[]>( const [modelSelections, setModelSelections] = useState<ModelSelectionItem[]>(
[] []
); );
const [globalParams, setGlobalParams] = useState<Record<string, any>>({ const [globalParams, setGlobalParams] = useState<Record<string, any>>({});
seed: null,
stop: null,
temperature: 1,
top_p: 1,
max_tokens: 1024
});
const [spans, setSpans] = useState<{ const [spans, setSpans] = useState<{
span: number; span: number;
count: number; count: number;
@@ -23,10 +23,13 @@ import React, {
import 'simplebar-react/dist/simplebar.min.css'; import 'simplebar-react/dist/simplebar.min.css';
import { OpenAIViewCode, Roles, generateMessages } from '../../config'; import { OpenAIViewCode, Roles, generateMessages } from '../../config';
import CompareContext from '../../config/compare-context'; import CompareContext from '../../config/compare-context';
import { ChatParamsConfig } from '../../config/params-config';
import { MessageItem, ModelSelectionItem } from '../../config/types'; import { MessageItem, ModelSelectionItem } from '../../config/types';
import { LLM_METAKEYS, llmInitialValues } from '../../hooks/config';
import useChatCompletion from '../../hooks/use-chat-completion'; import useChatCompletion from '../../hooks/use-chat-completion';
import { useInitLLmMeta } from '../../hooks/use-init-meta';
import '../../style/model-item.less'; import '../../style/model-item.less';
import ParamsSettings from '../params-settings'; import DynamicParams from '../dynamic-params';
import ReferenceParams from '../reference-params'; import ReferenceParams from '../reference-params';
import ViewCodeModal from '../view-code-modal'; import ViewCodeModal from '../view-code-modal';
import MessageContent from './message-content'; import MessageContent from './message-content';
@@ -39,8 +42,8 @@ interface ModelItemProps {
ref: any; ref: any;
} }
const ModelItem: React.FC<ModelItemProps> = forwardRef( const ModelItem: React.FC<ModelItemProps> = forwardRef((props, ref) => {
({ model, modelList, instanceId }, ref) => { const { modelList, model, instanceId } = props;
const { const {
globalParams, globalParams,
setGlobalParams, setGlobalParams,
@@ -50,12 +53,26 @@ const ModelItem: React.FC<ModelItemProps> = forwardRef(
modelFullList, modelFullList,
actions actions
} = useContext(CompareContext); } = useContext(CompareContext);
const {
handleOnValuesChange,
handleOnModelChange,
setParams,
setInitialValues,
formRef,
paramsConfig,
initialValues,
parameters
} = useInitLLmMeta(props, {
defaultValues: {
...llmInitialValues,
model: model
},
defaultParamsConfig: ChatParamsConfig,
metaKeys: LLM_METAKEYS
});
const intl = useIntl(); const intl = useIntl();
const isApplyToAllModels = useRef(false); const isApplyToAllModels = useRef(false);
const [systemMessage, setSystemMessage] = useState<string>(''); const [systemMessage, setSystemMessage] = useState<string>('');
const [params, setParams] = useState<Record<string, any>>({
model: model
});
const [show, setShow] = useState(false); const [show, setShow] = useState(false);
const scroller = useRef<any>(null); const scroller = useRef<any>(null);
@@ -97,7 +114,7 @@ const ModelItem: React.FC<ModelItemProps> = forwardRef(
? { role: Roles.System, content: systemMessage } ? { role: Roles.System, content: systemMessage }
: undefined, : undefined,
current: currentMsg, current: currentMsg,
parameters: params parameters: parameters
}); });
}; };
@@ -105,40 +122,32 @@ const ModelItem: React.FC<ModelItemProps> = forwardRef(
isApplyToAllModels.current = e.target.checked; isApplyToAllModels.current = e.target.checked;
if (e.target.checked) { if (e.target.checked) {
setGlobalParams({ setGlobalParams({
..._.omit(params, 'model') ..._.omit(parameters, 'model')
}); });
} }
}; };
const handleOnValuesChange = useCallback( const OnValuesChange = useCallback(
(changeValues: any, allValues: Record<string, any>) => { (changeValues: any, allValues: Record<string, any>) => {
if (isApplyToAllModels.current) { handleOnValuesChange(changeValues, {
setParams({ ...allValues,
...params, model: parameters.model
...allValues
}); });
if (isApplyToAllModels.current) {
setGlobalParams({ setGlobalParams({
...allValues ...allValues
}); });
} else {
setParams({
...params,
...changeValues
});
} }
}, },
[params, isApplyToAllModels.current] [parameters, isApplyToAllModels.current]
); );
const handleCloseViewCode = () => { const handleCloseViewCode = () => {
setShow(false); setShow(false);
}; };
const handleModelChange = (value: string) => { const onModelChange = (value: string) => {
setParams({ handleOnModelChange(value);
...params,
model: value
});
handleClear(); handleClear();
}; };
@@ -179,9 +188,17 @@ const ModelItem: React.FC<ModelItemProps> = forwardRef(
}, [modelList, intl]); }, [modelList, intl]);
useEffect(() => { useEffect(() => {
setParams({ setParams((prev: any) => {
...params, return {
...prev,
...globalParams ...globalParams
};
});
setInitialValues((prev: any) => {
return {
...prev,
...globalParams
};
}); });
}, [globalParams]); }, [globalParams]);
@@ -211,8 +228,8 @@ const ModelItem: React.FC<ModelItemProps> = forwardRef(
style={{ width: '100%' }} style={{ width: '100%' }}
variant="borderless" variant="borderless"
options={modelFullList} options={modelFullList}
onChange={handleModelChange} onChange={onModelChange}
value={params.model} value={parameters.model}
labelRender={(data) => { labelRender={(data) => {
return ( return (
<AutoTooltip <AutoTooltip
@@ -259,14 +276,14 @@ const ModelItem: React.FC<ModelItemProps> = forwardRef(
</Dropdown> </Dropdown>
<Popover <Popover
placement="bottomRight" placement="bottomRight"
overlayInnerStyle={{ width: 384 }}
content={ content={
<ParamsSettings <DynamicParams
ref={formRef}
onValuesChange={OnValuesChange}
paramsConfig={paramsConfig}
initialValues={initialValues}
showModelSelector={false} showModelSelector={false}
setParams={setParams}
modelList={modelList}
globalParams={globalParams}
selectedModel={params.model}
onValuesChange={handleOnValuesChange}
/> />
} }
trigger={['click']} trigger={['click']}
@@ -313,13 +330,12 @@ const ModelItem: React.FC<ModelItemProps> = forwardRef(
payload={{ payload={{
messages: viewCodeMessage messages: viewCodeMessage
}} }}
parameters={params} parameters={parameters}
onCancel={handleCloseViewCode} onCancel={handleCloseViewCode}
title={intl.formatMessage({ id: 'playground.viewcode' })} title={intl.formatMessage({ id: 'playground.viewcode' })}
></ViewCodeModal> ></ViewCodeModal>
</div> </div>
); );
} });
);
export default React.memo(ModelItem); export default React.memo(ModelItem);
+1 -1
View File
@@ -23,7 +23,7 @@ export const llmInitialValues = {
stop: null, stop: null,
temperature: 1, temperature: 1,
top_p: 1, top_p: 1,
max_tokens: 1024 max_tokens: null
}; };
export const advancedFieldsDefaultValus = { export const advancedFieldsDefaultValus = {
+32 -11
View File
@@ -28,7 +28,9 @@ import {
interface MessageProps { interface MessageProps {
modelList: Global.BaseOption<string>[]; modelList: Global.BaseOption<string>[];
model?: string;
loaded?: boolean; loaded?: boolean;
isChat?: boolean;
ref?: any; ref?: any;
} }
@@ -43,7 +45,7 @@ export const useInitLLmMeta = (
props: MessageProps, props: MessageProps,
options: InitMetaOptions options: InitMetaOptions
) => { ) => {
const { modelList } = props; const { modelList, model, isChat } = props;
const { const {
metaKeys = {}, metaKeys = {},
defaultValues = {}, defaultValues = {},
@@ -51,7 +53,9 @@ export const useInitLLmMeta = (
} = options; } = options;
const formRef = useRef<any>(null); const formRef = useRef<any>(null);
const [searchParams] = useSearchParams(); const [searchParams] = useSearchParams();
const defaultModel = searchParams.get('model') || modelList?.[0]?.value || ''; const defaultModel =
searchParams.get('model') ||
(isChat ? model ?? modelList?.[0]?.value : model);
const [modelMeta, setModelMeta] = useState<any>({}); const [modelMeta, setModelMeta] = useState<any>({});
const [initialValues, setInitialValues] = useState<any>({ const [initialValues, setInitialValues] = useState<any>({
...defaultValues, ...defaultValues,
@@ -103,18 +107,12 @@ export const useInitLLmMeta = (
return fields?.join(','); return fields?.join(',');
}, [paramsConfig]); }, [paramsConfig]);
const handleOnValuesChange = useCallback(
(changeValues: Record<string, any>, allValues: Record<string, any>) => {
setParams(allValues);
},
[]
);
const handleOnModelChange = useCallback( const handleOnModelChange = useCallback(
(val: string) => { (val: string) => {
if (!val) return; if (!val) return;
const model = modelList.find((item) => item.value === val); const model = modelList.find((item) => item.value === val);
const { form: initialData, meta } = extractLLMMeta(model?.meta); const { form: initialData, meta } = extractLLMMeta(model?.meta);
setModelMeta(meta || {}); setModelMeta(meta || {});
setInitialValues({ setInitialValues({
...initialData, ...initialData,
@@ -124,8 +122,31 @@ export const useInitLLmMeta = (
...initialData, ...initialData,
model: val model: val
}); });
const config = defaultParamsConfig.map((item) => {
return {
...item,
attrs:
item.name === 'max_tokens'
? { ...item.attrs, max: meta.max_tokens || 16 * 1024 }
: {
...item.attrs
}
};
});
setParamsConfig(config);
}, },
[modelList] [modelList, defaultParamsConfig]
);
const handleOnValuesChange = useCallback(
(changeValues: Record<string, any>, allValues: Record<string, any>) => {
if (changeValues.model) {
handleOnModelChange(changeValues.model);
return;
}
setParams(allValues);
},
[handleOnModelChange]
); );
useEffect(() => { useEffect(() => {
@@ -138,7 +159,7 @@ export const useInitLLmMeta = (
if (paramsRef.current) { if (paramsRef.current) {
innitializeParams(paramsRef.current); innitializeParams(paramsRef.current);
} }
}, [paramsRef.current, innitializeParams]); }, [innitializeParams]);
return { return {
extractLLMMeta, extractLLMMeta,