SearchParams.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { makeStyles, Theme } from '@material-ui/core';
  2. import { FC, useCallback, useEffect, useMemo } from 'react';
  3. import { useTranslation } from 'react-i18next';
  4. import CustomInput from '../../components/customInput/CustomInput';
  5. import { ITextfieldConfig } from '../../components/customInput/Types';
  6. import CustomSelector from '../../components/customSelector/CustomSelector';
  7. import { Option } from '../../components/customSelector/Types';
  8. import {
  9. DEFAULT_SEARCH_PARAM_VALUE_MAP,
  10. INDEX_CONFIG,
  11. METRIC_OPTIONS_MAP,
  12. searchKeywordsType,
  13. } from '../../consts/Milvus';
  14. import { useFormValidation } from '../../hooks/Form';
  15. import { formatForm } from '../../utils/Form';
  16. import { SearchParamInputConfig, SearchParamsProps } from './Types';
  17. const getStyles = makeStyles((theme: Theme) => ({
  18. selector: {
  19. width: '100%',
  20. marginTop: theme.spacing(2),
  21. },
  22. input: {
  23. marginTop: theme.spacing(2),
  24. },
  25. }));
  26. const SearchParams: FC<SearchParamsProps> = ({
  27. indexType,
  28. indexParams,
  29. searchParamsForm,
  30. handleFormChange,
  31. embeddingType,
  32. metricType,
  33. topK,
  34. setParamsDisabled,
  35. wrapperClass = '',
  36. }) => {
  37. const { t: indexTrans } = useTranslation('index');
  38. const { t: warningTrans } = useTranslation('warning');
  39. const classes = getStyles();
  40. const metricOptions: Option[] = METRIC_OPTIONS_MAP[embeddingType];
  41. // search params key list, depends on index type
  42. // e.g. ['nprobe']
  43. const searchParams = useMemo(
  44. () => (indexType !== '' ? INDEX_CONFIG[indexType].search : []),
  45. [indexType]
  46. );
  47. const handleInputChange = useCallback(
  48. (key: string, value: number) => {
  49. const form = { ...searchParamsForm, [key]: value };
  50. handleFormChange(form);
  51. },
  52. [handleFormChange, searchParamsForm]
  53. );
  54. /**
  55. * function to transfer search params to CustomInput need config type
  56. */
  57. const getNumberInputConfig = useCallback(
  58. (params: SearchParamInputConfig): ITextfieldConfig => {
  59. const {
  60. label,
  61. key,
  62. min,
  63. max,
  64. value,
  65. handleChange,
  66. isInt = true,
  67. } = params;
  68. // search_k range is special compared to others,need to be handled separately
  69. // range: {-1} ∪ [top_k, n × n_trees]
  70. const isSearchK = label === 'search_k';
  71. const config: ITextfieldConfig = {
  72. label,
  73. key,
  74. onChange: value => {
  75. handleChange(value);
  76. },
  77. className: classes.input,
  78. variant: 'filled',
  79. type: 'number',
  80. value,
  81. validations: [
  82. {
  83. rule: 'require',
  84. errorText: warningTrans('required', { name: label }),
  85. },
  86. ],
  87. };
  88. if (!isSearchK && min && max) {
  89. config.validations?.push({
  90. rule: 'range',
  91. errorText: warningTrans('range', { min, max }),
  92. extraParam: {
  93. min,
  94. max,
  95. type: 'number',
  96. },
  97. });
  98. }
  99. if (isInt) {
  100. config.validations?.push({
  101. rule: 'integer',
  102. errorText: warningTrans('integer', { name: label }),
  103. });
  104. }
  105. // search_k
  106. if (isSearchK) {
  107. config.validations?.push({
  108. rule: 'specValueOrRange',
  109. errorText: warningTrans('specValueOrRange', {
  110. name: label,
  111. min,
  112. max,
  113. specValue: -1,
  114. }),
  115. extraParam: {
  116. min,
  117. max,
  118. compareValue: -1,
  119. type: 'number',
  120. },
  121. });
  122. }
  123. return config;
  124. },
  125. [warningTrans, classes.input]
  126. );
  127. const getSearchInputConfig = useCallback(
  128. (paramKey: searchKeywordsType): ITextfieldConfig => {
  129. const nlist = Number(
  130. indexParams.find(p => p.key === 'nlist')?.value || 0
  131. );
  132. const configParamMap: {
  133. [key in searchKeywordsType]: SearchParamInputConfig;
  134. } = {
  135. nprobe: {
  136. label: 'nprobe',
  137. key: 'nprobe',
  138. value: searchParamsForm['nprobe'] || '',
  139. min: 1,
  140. max: nlist,
  141. isInt: true,
  142. handleChange: value => {
  143. handleInputChange('nprobe', value);
  144. },
  145. },
  146. ef: {
  147. label: 'ef',
  148. key: 'ef',
  149. value: searchParamsForm['ef'] || '',
  150. min: topK,
  151. max: 32768,
  152. isInt: true,
  153. handleChange: value => {
  154. handleInputChange('ef', value);
  155. },
  156. },
  157. search_k: {
  158. label: 'search_k',
  159. key: 'search_k',
  160. value: searchParamsForm['search_k'] || '',
  161. min: topK,
  162. // n * n_trees can be infinity
  163. max: Infinity,
  164. isInt: true,
  165. handleChange: value => {
  166. handleInputChange('search_k', value);
  167. },
  168. },
  169. search_length: {
  170. label: 'search_length',
  171. key: 'search_length',
  172. value: searchParamsForm['search_length'] || '',
  173. min: 10,
  174. max: 300,
  175. isInt: true,
  176. handleChange: value => {
  177. handleInputChange('search_length', value);
  178. },
  179. },
  180. };
  181. const param = configParamMap[paramKey];
  182. return getNumberInputConfig(param);
  183. },
  184. [
  185. indexParams,
  186. topK,
  187. searchParamsForm,
  188. getNumberInputConfig,
  189. handleInputChange,
  190. ]
  191. );
  192. useEffect(() => {
  193. // generate different form according to search params
  194. const form = searchParams.reduce(
  195. (paramsForm, param) => ({
  196. ...paramsForm,
  197. [param]: DEFAULT_SEARCH_PARAM_VALUE_MAP[param],
  198. }),
  199. {}
  200. );
  201. handleFormChange(form);
  202. }, [searchParams, handleFormChange]);
  203. const checkedForm = useMemo(() => {
  204. const { ...needCheckItems } = searchParamsForm;
  205. return formatForm(needCheckItems);
  206. }, [searchParamsForm]);
  207. const { validation, checkIsValid, disabled } = useFormValidation(checkedForm);
  208. useEffect(() => {
  209. setParamsDisabled(disabled);
  210. }, [disabled, setParamsDisabled]);
  211. return (
  212. <div className={wrapperClass}>
  213. {/* metric type */}
  214. <CustomSelector
  215. options={metricOptions}
  216. value={metricType}
  217. label={indexTrans('metric')}
  218. wrapperClass={classes.selector}
  219. variant="filled"
  220. onChange={(e: { target: { value: unknown } }) => {
  221. // not selectable now, so not set onChange event
  222. }}
  223. // not selectable now
  224. readOnly={true}
  225. />
  226. {/* dynamic params, now every type only has one param except metric type */}
  227. {searchParams.map(param => (
  228. <CustomInput
  229. key={param}
  230. type="text"
  231. textConfig={getSearchInputConfig(param)}
  232. checkValid={checkIsValid}
  233. validInfo={validation}
  234. />
  235. ))}
  236. </div>
  237. );
  238. };
  239. export default SearchParams;