utils.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. validRerankModel?: { provider?: string; model?: string },
  89. ) => {
  90. const shouldSetWeightDefaultValue = xorBy(selectedDatasets, originalDatasets, 'id').length > 0
  91. const rerankModelIsValid = validRerankModel?.provider && validRerankModel?.model
  92. const {
  93. allHighQuality,
  94. allHighQualityVectorSearch,
  95. allHighQualityFullTextSearch,
  96. allEconomic,
  97. mixtureHighQualityAndEconomic,
  98. allInternal,
  99. allExternal,
  100. mixtureInternalAndExternal,
  101. inconsistentEmbeddingModel,
  102. } = getSelectedDatasetsMode(selectedDatasets)
  103. const {
  104. top_k = DATASET_DEFAULT.top_k,
  105. score_threshold,
  106. reranking_mode,
  107. reranking_model,
  108. weights,
  109. reranking_enable,
  110. } = multipleRetrievalConfig || { top_k: DATASET_DEFAULT.top_k }
  111. const result = {
  112. top_k,
  113. score_threshold,
  114. reranking_mode,
  115. reranking_model,
  116. weights,
  117. reranking_enable: ((allInternal && allEconomic) || allExternal) ? reranking_enable : true,
  118. }
  119. if (!rerankModelIsValid)
  120. result.reranking_model = undefined
  121. const setDefaultWeights = () => {
  122. result.weights = {
  123. vector_setting: {
  124. vector_weight: allHighQualityVectorSearch
  125. ? DEFAULT_WEIGHTED_SCORE.allHighQualityVectorSearch.semantic
  126. : allHighQualityFullTextSearch
  127. ? DEFAULT_WEIGHTED_SCORE.allHighQualityFullTextSearch.semantic
  128. : DEFAULT_WEIGHTED_SCORE.other.semantic,
  129. embedding_provider_name: selectedDatasets[0].embedding_model_provider,
  130. embedding_model_name: selectedDatasets[0].embedding_model,
  131. },
  132. keyword_setting: {
  133. keyword_weight: allHighQualityVectorSearch
  134. ? DEFAULT_WEIGHTED_SCORE.allHighQualityVectorSearch.keyword
  135. : allHighQualityFullTextSearch
  136. ? DEFAULT_WEIGHTED_SCORE.allHighQualityFullTextSearch.keyword
  137. : DEFAULT_WEIGHTED_SCORE.other.keyword,
  138. },
  139. }
  140. }
  141. if (allEconomic || mixtureHighQualityAndEconomic || inconsistentEmbeddingModel || allExternal || mixtureInternalAndExternal) {
  142. result.reranking_mode = RerankingModeEnum.RerankingModel
  143. if (rerankModelIsValid) {
  144. result.reranking_mode = RerankingModeEnum.RerankingModel
  145. result.reranking_model = {
  146. provider: validRerankModel?.provider || '',
  147. model: validRerankModel?.model || '',
  148. }
  149. }
  150. else {
  151. result.reranking_model = undefined
  152. }
  153. }
  154. if (allHighQuality && !inconsistentEmbeddingModel && allInternal) {
  155. if (!reranking_mode) {
  156. if (validRerankModel?.provider && validRerankModel?.model) {
  157. result.reranking_mode = RerankingModeEnum.RerankingModel
  158. result.reranking_model = {
  159. provider: validRerankModel.provider,
  160. model: validRerankModel.model,
  161. }
  162. }
  163. else {
  164. result.reranking_mode = RerankingModeEnum.WeightedScore
  165. setDefaultWeights()
  166. }
  167. }
  168. if (reranking_mode === RerankingModeEnum.WeightedScore && !weights)
  169. setDefaultWeights()
  170. if (reranking_mode === RerankingModeEnum.WeightedScore && weights && shouldSetWeightDefaultValue) {
  171. if (rerankModelIsValid) {
  172. result.reranking_mode = RerankingModeEnum.RerankingModel
  173. result.reranking_model = {
  174. provider: validRerankModel.provider || '',
  175. model: validRerankModel.model || '',
  176. }
  177. }
  178. else {
  179. setDefaultWeights()
  180. }
  181. }
  182. if (reranking_mode === RerankingModeEnum.RerankingModel && !rerankModelIsValid && shouldSetWeightDefaultValue) {
  183. result.reranking_mode = RerankingModeEnum.WeightedScore
  184. setDefaultWeights()
  185. }
  186. }
  187. return result
  188. }
  189. export const checkoutRerankModelConfigedInRetrievalSettings = (
  190. datasets: DataSet[],
  191. multipleRetrievalConfig?: MultipleRetrievalConfig,
  192. ) => {
  193. if (!multipleRetrievalConfig)
  194. return true
  195. const {
  196. allEconomic,
  197. allExternal,
  198. } = getSelectedDatasetsMode(datasets)
  199. const {
  200. reranking_enable,
  201. reranking_mode,
  202. reranking_model,
  203. } = multipleRetrievalConfig
  204. if (reranking_mode === RerankingModeEnum.RerankingModel && (!reranking_model?.provider || !reranking_model?.model)) {
  205. if ((allEconomic || allExternal) && !reranking_enable)
  206. return true
  207. return false
  208. }
  209. return true
  210. }