SearchParams.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import { makeStyles, Theme } from '@material-ui/core';
  2. import { FC, useCallback, useContext, 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_NLIST_VALUE,
  10. DEFAULT_SEARCH_PARAM_VALUE_MAP,
  11. INDEX_CONFIG,
  12. METRIC_OPTIONS_MAP,
  13. searchKeywordsType,
  14. } from '@/consts';
  15. import { rootContext } from '@/context';
  16. import { useFormValidation } from '@/hooks';
  17. import { formatForm } from '@/utils';
  18. import { SearchParamInputConfig, SearchParamsProps } from './Types';
  19. const getStyles = makeStyles((theme: Theme) => ({
  20. selector: {
  21. width: '100%',
  22. marginBottom: theme.spacing(2),
  23. },
  24. input: {
  25. marginBottom: theme.spacing(2),
  26. },
  27. inlineInput: {
  28. width: '48%',
  29. },
  30. inlineInputWrapper: {
  31. display: 'flex',
  32. justifyContent: 'space-between',
  33. },
  34. }));
  35. const SearchParams: FC<SearchParamsProps> = ({
  36. indexType,
  37. indexParams,
  38. searchParamsForm,
  39. handleFormChange,
  40. handleMetricTypeChange,
  41. embeddingType,
  42. metricType,
  43. topK,
  44. setParamsDisabled,
  45. wrapperClass = '',
  46. }) => {
  47. const { t: indexTrans } = useTranslation('index');
  48. const { t: warningTrans } = useTranslation('warning');
  49. const classes = getStyles();
  50. const { openSnackBar } = useContext(rootContext);
  51. const metricOptions: Option[] = METRIC_OPTIONS_MAP[embeddingType];
  52. // search params key list, depends on index type
  53. // e.g. ['nprobe']
  54. const searchParams = useMemo((): searchKeywordsType[] => {
  55. const isSupportedType = Object.keys(INDEX_CONFIG).includes(indexType);
  56. // show warning snackbar for unsupported type
  57. if (!isSupportedType) {
  58. indexType !== '' &&
  59. openSnackBar(
  60. warningTrans('noSupportIndexType', { type: indexType }),
  61. 'warning'
  62. );
  63. }
  64. return indexType !== '' && isSupportedType
  65. ? [...INDEX_CONFIG[indexType].search, 'round_decimal']
  66. : ['round_decimal'];
  67. }, [indexType, openSnackBar, warningTrans]);
  68. const handleInputChange = useCallback(
  69. (key: string, value: number) => {
  70. const form = { ...searchParamsForm, [key]: value };
  71. handleFormChange(form);
  72. },
  73. [handleFormChange, searchParamsForm]
  74. );
  75. /**
  76. * function to transfer search params to CustomInput need config type
  77. */
  78. const getNumberInputConfig = useCallback(
  79. (params: SearchParamInputConfig): ITextfieldConfig => {
  80. const {
  81. label,
  82. key,
  83. min,
  84. max,
  85. value,
  86. handleChange,
  87. isInt = true,
  88. } = params;
  89. // search_k range is special compared to others,need to be handled separately
  90. // range: {-1} ∪ [top_k, n × n_trees]
  91. const isSearchK = label === 'search_k';
  92. const config: ITextfieldConfig = {
  93. label,
  94. key,
  95. onChange: value => {
  96. handleChange(value);
  97. },
  98. className: classes.inlineInput,
  99. variant: 'filled',
  100. type: 'number',
  101. value,
  102. validations: [
  103. {
  104. rule: 'require',
  105. errorText: warningTrans('required', { name: label }),
  106. },
  107. ],
  108. };
  109. if (!isSearchK && min && max) {
  110. config.validations?.push({
  111. rule: 'range',
  112. errorText: warningTrans('range', { min, max }),
  113. extraParam: {
  114. min,
  115. max,
  116. type: 'number',
  117. },
  118. });
  119. }
  120. if (isInt) {
  121. config.validations?.push({
  122. rule: 'integer',
  123. errorText: warningTrans('integer', { name: label }),
  124. });
  125. }
  126. // search_k
  127. if (isSearchK) {
  128. config.validations?.push({
  129. rule: 'specValueOrRange',
  130. errorText: warningTrans('specValueOrRange', {
  131. name: label,
  132. min,
  133. max,
  134. specValue: -1,
  135. }),
  136. extraParam: {
  137. min,
  138. max,
  139. compareValue: -1,
  140. type: 'number',
  141. },
  142. });
  143. }
  144. return config;
  145. },
  146. [classes.inlineInput, warningTrans]
  147. );
  148. const getSearchInputConfig = useCallback(
  149. (paramKey: searchKeywordsType): ITextfieldConfig => {
  150. const nlist = Number(
  151. // nlist range is [1, 65536], if user didn't create index, we set 1024 as default nlist value
  152. indexParams.find(p => p.key === 'nlist')?.value || DEFAULT_NLIST_VALUE
  153. );
  154. const configParamMap: {
  155. [key in searchKeywordsType]: SearchParamInputConfig;
  156. } = {
  157. round_decimal: {
  158. label: 'Round Decimals',
  159. key: 'round_decimal',
  160. value: searchParamsForm['round_decimal'] || '',
  161. min: -1,
  162. max: 10,
  163. isInt: true,
  164. handleChange: value => {
  165. handleInputChange('round_decimal', Number(value));
  166. },
  167. className: classes.inlineInput,
  168. },
  169. nprobe: {
  170. label: 'nprobe',
  171. key: 'nprobe',
  172. value: searchParamsForm['nprobe'] || '',
  173. min: 1,
  174. max: nlist,
  175. isInt: true,
  176. handleChange: value => {
  177. handleInputChange('nprobe', Number(value));
  178. },
  179. className: classes.inlineInput,
  180. },
  181. ef: {
  182. label: 'ef',
  183. key: 'ef',
  184. value: searchParamsForm['ef'] || '',
  185. min: topK,
  186. max: 32768,
  187. isInt: true,
  188. handleChange: value => {
  189. handleInputChange('ef', Number(value));
  190. },
  191. },
  192. level: {
  193. label: 'level',
  194. key: 'level',
  195. value: searchParamsForm['level'] || '',
  196. min: 1,
  197. max: 3,
  198. isInt: true,
  199. handleChange: value => {
  200. handleInputChange('level', Number(value));
  201. },
  202. },
  203. search_k: {
  204. label: 'search_k',
  205. key: 'search_k',
  206. value: searchParamsForm['search_k'] || '',
  207. min: topK,
  208. // n * n_trees can be infinity
  209. max: Infinity,
  210. isInt: true,
  211. handleChange: value => {
  212. handleInputChange('search_k', Number(value));
  213. },
  214. },
  215. search_length: {
  216. label: 'search_length',
  217. key: 'search_length',
  218. value: searchParamsForm['search_length'] || '',
  219. min: 10,
  220. max: 300,
  221. isInt: true,
  222. handleChange: value => {
  223. handleInputChange('search_length', Number(value));
  224. },
  225. },
  226. search_list: {
  227. label: 'search_list',
  228. key: 'search_list',
  229. value: searchParamsForm['search_list'] || '',
  230. min: 20,
  231. max: 65535,
  232. isInt: true,
  233. handleChange: value => {
  234. handleInputChange('search_list', Number(value));
  235. },
  236. },
  237. };
  238. const param = configParamMap[paramKey];
  239. return getNumberInputConfig(param);
  240. },
  241. [
  242. indexParams,
  243. searchParamsForm,
  244. classes.inlineInput,
  245. topK,
  246. getNumberInputConfig,
  247. handleInputChange,
  248. ]
  249. );
  250. useEffect(() => {
  251. // generate different form according to search params
  252. const form = searchParams.reduce(
  253. (paramsForm, param) => ({
  254. ...paramsForm,
  255. [param]: DEFAULT_SEARCH_PARAM_VALUE_MAP[param],
  256. }),
  257. {}
  258. );
  259. handleFormChange(form);
  260. }, [searchParams, handleFormChange]);
  261. const checkedForm = useMemo(() => {
  262. const { ...needCheckItems } = searchParamsForm;
  263. return formatForm(needCheckItems);
  264. }, [searchParamsForm]);
  265. const { validation, checkIsValid, disabled } = useFormValidation(checkedForm);
  266. useEffect(() => {
  267. setParamsDisabled(disabled);
  268. }, [disabled, setParamsDisabled]);
  269. return (
  270. <div className={wrapperClass}>
  271. {/* metric type */}
  272. <CustomSelector
  273. options={metricOptions}
  274. value={metricType}
  275. label={indexTrans('metric')}
  276. wrapperClass={classes.selector}
  277. variant="filled"
  278. onChange={(e: { target: { value: unknown } }) => {
  279. const metricType = e.target.value as string;
  280. handleMetricTypeChange(metricType);
  281. }}
  282. />
  283. <div className={classes.inlineInputWrapper}>
  284. {/* dynamic params, now every type only has one param except metric type */}
  285. {searchParams.map(param => (
  286. <CustomInput
  287. key={param}
  288. type="text"
  289. textConfig={getSearchInputConfig(param)}
  290. checkValid={checkIsValid}
  291. validInfo={validation}
  292. />
  293. ))}
  294. </div>
  295. </div>
  296. );
  297. };
  298. export default SearchParams;