utils.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import {
  2. uniq,
  3. xorBy,
  4. } from 'lodash-es'
  5. import type { MultipleRetrievalConfig } from './types'
  6. import type {
  7. DataSet,
  8. SelectedDatasetsMode,
  9. } from '@/models/datasets'
  10. import {
  11. DEFAULT_WEIGHTED_SCORE,
  12. RerankingModeEnum,
  13. } from '@/models/datasets'
  14. import { RETRIEVE_METHOD } from '@/types/app'
  15. import { DATASET_DEFAULT } from '@/config'
  16. export const checkNodeValid = () => {
  17. return true
  18. }
  19. export const getSelectedDatasetsMode = (datasets: DataSet[] = []) => {
  20. if (datasets === null)
  21. datasets = []
  22. let allHighQuality = true
  23. let allHighQualityVectorSearch = true
  24. let allHighQualityFullTextSearch = true
  25. let allEconomic = true
  26. let mixtureHighQualityAndEconomic = true
  27. let allExternal = true
  28. let allInternal = true
  29. let mixtureInternalAndExternal = true
  30. let inconsistentEmbeddingModel = false
  31. if (!datasets.length) {
  32. allHighQuality = false
  33. allHighQualityVectorSearch = false
  34. allHighQualityFullTextSearch = false
  35. allEconomic = false
  36. mixtureHighQualityAndEconomic = false
  37. inconsistentEmbeddingModel = false
  38. allExternal = false
  39. allInternal = false
  40. mixtureInternalAndExternal = false
  41. }
  42. datasets.forEach((dataset) => {
  43. if (dataset.indexing_technique === 'economy') {
  44. allHighQuality = false
  45. allHighQualityVectorSearch = false
  46. allHighQualityFullTextSearch = false
  47. }
  48. if (dataset.indexing_technique === 'high_quality') {
  49. allEconomic = false
  50. if (dataset.retrieval_model_dict.search_method !== RETRIEVE_METHOD.semantic)
  51. allHighQualityVectorSearch = false
  52. if (dataset.retrieval_model_dict.search_method !== RETRIEVE_METHOD.fullText)
  53. allHighQualityFullTextSearch = false
  54. }
  55. if (dataset.provider !== 'external') {
  56. allExternal = false
  57. }
  58. else {
  59. allInternal = false
  60. allHighQuality = false
  61. allHighQualityVectorSearch = false
  62. allHighQualityFullTextSearch = false
  63. mixtureHighQualityAndEconomic = false
  64. }
  65. })
  66. if (allExternal || allInternal)
  67. mixtureInternalAndExternal = false
  68. if (allHighQuality || allEconomic)
  69. mixtureHighQualityAndEconomic = false
  70. if (allHighQuality)
  71. inconsistentEmbeddingModel = uniq(datasets.map(item => item.embedding_model)).length > 1
  72. return {
  73. allHighQuality,
  74. allHighQualityVectorSearch,
  75. allHighQualityFullTextSearch,
  76. allEconomic,
  77. mixtureHighQualityAndEconomic,
  78. allInternal,
  79. allExternal,
  80. mixtureInternalAndExternal,
  81. inconsistentEmbeddingModel,
  82. } as SelectedDatasetsMode
  83. }
  84. export const getMultipleRetrievalConfig = (
  85. multipleRetrievalConfig: MultipleRetrievalConfig,
  86. selectedDatasets: DataSet[],
  87. originalDatasets: DataSet[],
  88. isValidRerankModel?: boolean,
  89. ) => {
  90. const shouldSetWeightDefaultValue = xorBy(selectedDatasets, originalDatasets, 'id').length > 0
  91. const {
  92. allHighQuality,
  93. allHighQualityVectorSearch,
  94. allHighQualityFullTextSearch,
  95. allEconomic,
  96. mixtureHighQualityAndEconomic,
  97. allInternal,
  98. allExternal,
  99. mixtureInternalAndExternal,
  100. inconsistentEmbeddingModel,
  101. } = getSelectedDatasetsMode(selectedDatasets)
  102. const {
  103. top_k = DATASET_DEFAULT.top_k,
  104. score_threshold,
  105. reranking_mode,
  106. reranking_model,
  107. weights,
  108. reranking_enable,
  109. } = multipleRetrievalConfig || { top_k: DATASET_DEFAULT.top_k }
  110. const result = {
  111. top_k,
  112. score_threshold,
  113. reranking_mode,
  114. reranking_model,
  115. weights,
  116. reranking_enable: ((allInternal && allEconomic) || allExternal) ? reranking_enable : true,
  117. }
  118. if (allEconomic || mixtureHighQualityAndEconomic || inconsistentEmbeddingModel || allExternal || mixtureInternalAndExternal)
  119. result.reranking_mode = RerankingModeEnum.RerankingModel
  120. if (allHighQuality && !inconsistentEmbeddingModel && reranking_mode === undefined && allInternal)
  121. result.reranking_mode = RerankingModeEnum.WeightedScore
  122. if (allHighQuality && !inconsistentEmbeddingModel && (reranking_mode === RerankingModeEnum.WeightedScore || reranking_mode === undefined) && allInternal && !weights) {
  123. if (!isValidRerankModel)
  124. result.reranking_mode = RerankingModeEnum.WeightedScore
  125. else
  126. result.reranking_mode = RerankingModeEnum.RerankingModel
  127. result.weights = {
  128. vector_setting: {
  129. vector_weight: allHighQualityVectorSearch
  130. ? DEFAULT_WEIGHTED_SCORE.allHighQualityVectorSearch.semantic
  131. : allHighQualityFullTextSearch
  132. ? DEFAULT_WEIGHTED_SCORE.allHighQualityFullTextSearch.semantic
  133. : DEFAULT_WEIGHTED_SCORE.other.semantic,
  134. embedding_provider_name: selectedDatasets[0].embedding_model_provider,
  135. embedding_model_name: selectedDatasets[0].embedding_model,
  136. },
  137. keyword_setting: {
  138. keyword_weight: allHighQualityVectorSearch
  139. ? DEFAULT_WEIGHTED_SCORE.allHighQualityVectorSearch.keyword
  140. : allHighQualityFullTextSearch
  141. ? DEFAULT_WEIGHTED_SCORE.allHighQualityFullTextSearch.keyword
  142. : DEFAULT_WEIGHTED_SCORE.other.keyword,
  143. },
  144. }
  145. }
  146. if (shouldSetWeightDefaultValue && allHighQuality && !inconsistentEmbeddingModel && (reranking_mode === RerankingModeEnum.WeightedScore || reranking_mode === undefined || !isValidRerankModel) && allInternal && weights) {
  147. if (!isValidRerankModel)
  148. result.reranking_mode = RerankingModeEnum.WeightedScore
  149. else
  150. result.reranking_mode = RerankingModeEnum.RerankingModel
  151. result.weights = {
  152. vector_setting: {
  153. vector_weight: allHighQualityVectorSearch
  154. ? DEFAULT_WEIGHTED_SCORE.allHighQualityVectorSearch.semantic
  155. : allHighQualityFullTextSearch
  156. ? DEFAULT_WEIGHTED_SCORE.allHighQualityFullTextSearch.semantic
  157. : DEFAULT_WEIGHTED_SCORE.other.semantic,
  158. embedding_provider_name: selectedDatasets[0].embedding_model_provider,
  159. embedding_model_name: selectedDatasets[0].embedding_model,
  160. },
  161. keyword_setting: {
  162. keyword_weight: allHighQualityVectorSearch
  163. ? DEFAULT_WEIGHTED_SCORE.allHighQualityVectorSearch.keyword
  164. : allHighQualityFullTextSearch
  165. ? DEFAULT_WEIGHTED_SCORE.allHighQualityFullTextSearch.keyword
  166. : DEFAULT_WEIGHTED_SCORE.other.keyword,
  167. },
  168. }
  169. }
  170. return result
  171. }