您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

panel.tsx 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import type { FC } from 'react'
  2. import {
  3. memo,
  4. useMemo,
  5. } from 'react'
  6. import { intersectionBy } from 'lodash-es'
  7. import { useTranslation } from 'react-i18next'
  8. import VarReferencePicker from '../_base/components/variable/var-reference-picker'
  9. import useConfig from './use-config'
  10. import RetrievalConfig from './components/retrieval-config'
  11. import AddKnowledge from './components/add-dataset'
  12. import DatasetList from './components/dataset-list'
  13. import MetadataFilter from './components/metadata/metadata-filter'
  14. import type { KnowledgeRetrievalNodeType } from './types'
  15. import Field from '@/app/components/workflow/nodes/_base/components/field'
  16. import Split from '@/app/components/workflow/nodes/_base/components/split'
  17. import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
  18. import type { NodePanelProps } from '@/app/components/workflow/types'
  19. const i18nPrefix = 'workflow.nodes.knowledgeRetrieval'
  20. const Panel: FC<NodePanelProps<KnowledgeRetrievalNodeType>> = ({
  21. id,
  22. data,
  23. }) => {
  24. const { t } = useTranslation()
  25. const {
  26. readOnly,
  27. inputs,
  28. handleQueryVarChange,
  29. filterVar,
  30. handleModelChanged,
  31. handleCompletionParamsChange,
  32. handleRetrievalModeChange,
  33. handleMultipleRetrievalConfigChange,
  34. selectedDatasets,
  35. selectedDatasetsLoaded,
  36. handleOnDatasetsChange,
  37. rerankModelOpen,
  38. setRerankModelOpen,
  39. handleAddCondition,
  40. handleMetadataFilterModeChange,
  41. handleRemoveCondition,
  42. handleToggleConditionLogicalOperator,
  43. handleUpdateCondition,
  44. handleMetadataModelChange,
  45. handleMetadataCompletionParamsChange,
  46. availableStringVars,
  47. availableStringNodesWithParent,
  48. availableNumberVars,
  49. availableNumberNodesWithParent,
  50. } = useConfig(id, data)
  51. const metadataList = useMemo(() => {
  52. return intersectionBy(...selectedDatasets.filter((dataset) => {
  53. return !!dataset.doc_metadata
  54. }).map((dataset) => {
  55. return dataset.doc_metadata!
  56. }), 'name')
  57. }, [selectedDatasets])
  58. return (
  59. <div className='pt-2'>
  60. <div className='space-y-4 px-4 pb-2'>
  61. <Field
  62. title={t(`${i18nPrefix}.queryVariable`)}
  63. required
  64. >
  65. <VarReferencePicker
  66. nodeId={id}
  67. readonly={readOnly}
  68. isShowNodeName
  69. value={inputs.query_variable_selector}
  70. onChange={handleQueryVarChange}
  71. filterVar={filterVar}
  72. />
  73. </Field>
  74. <Field
  75. title={t(`${i18nPrefix}.knowledge`)}
  76. required
  77. operations={
  78. <div className='flex items-center space-x-1'>
  79. <RetrievalConfig
  80. payload={{
  81. retrieval_mode: inputs.retrieval_mode,
  82. multiple_retrieval_config: inputs.multiple_retrieval_config,
  83. single_retrieval_config: inputs.single_retrieval_config,
  84. }}
  85. onRetrievalModeChange={handleRetrievalModeChange}
  86. onMultipleRetrievalConfigChange={handleMultipleRetrievalConfigChange}
  87. singleRetrievalModelConfig={inputs.single_retrieval_config?.model}
  88. onSingleRetrievalModelChange={handleModelChanged as any}
  89. onSingleRetrievalModelParamsChange={handleCompletionParamsChange}
  90. readonly={readOnly || !selectedDatasets.length}
  91. rerankModalOpen={rerankModelOpen}
  92. onRerankModelOpenChange={setRerankModelOpen}
  93. selectedDatasets={selectedDatasets}
  94. />
  95. {!readOnly && (<div className='h-3 w-px bg-divider-regular'></div>)}
  96. {!readOnly && (
  97. <AddKnowledge
  98. selectedIds={inputs.dataset_ids}
  99. onChange={handleOnDatasetsChange}
  100. />
  101. )}
  102. </div>
  103. }
  104. >
  105. <DatasetList
  106. list={selectedDatasets}
  107. onChange={handleOnDatasetsChange}
  108. readonly={readOnly}
  109. />
  110. </Field>
  111. </div>
  112. <div className='mb-2 py-2'>
  113. <MetadataFilter
  114. metadataList={metadataList}
  115. selectedDatasetsLoaded={selectedDatasetsLoaded}
  116. metadataFilterMode={inputs.metadata_filtering_mode}
  117. metadataFilteringConditions={inputs.metadata_filtering_conditions}
  118. handleAddCondition={handleAddCondition}
  119. handleMetadataFilterModeChange={handleMetadataFilterModeChange}
  120. handleRemoveCondition={handleRemoveCondition}
  121. handleToggleConditionLogicalOperator={handleToggleConditionLogicalOperator}
  122. handleUpdateCondition={handleUpdateCondition}
  123. metadataModelConfig={inputs.metadata_model_config}
  124. handleMetadataModelChange={handleMetadataModelChange}
  125. handleMetadataCompletionParamsChange={handleMetadataCompletionParamsChange}
  126. availableStringVars={availableStringVars}
  127. availableStringNodesWithParent={availableStringNodesWithParent}
  128. availableNumberVars={availableNumberVars}
  129. availableNumberNodesWithParent={availableNumberNodesWithParent}
  130. />
  131. </div>
  132. <Split />
  133. <div>
  134. <OutputVars>
  135. <>
  136. <VarItem
  137. name='result'
  138. type='Array[Object]'
  139. description={t(`${i18nPrefix}.outputVars.output`)}
  140. subItems={[
  141. {
  142. name: 'content',
  143. type: 'string',
  144. description: t(`${i18nPrefix}.outputVars.content`),
  145. },
  146. // url, title, link like bing search reference result: link, link page title, link page icon
  147. {
  148. name: 'title',
  149. type: 'string',
  150. description: t(`${i18nPrefix}.outputVars.title`),
  151. },
  152. {
  153. name: 'url',
  154. type: 'string',
  155. description: t(`${i18nPrefix}.outputVars.url`),
  156. },
  157. {
  158. name: 'icon',
  159. type: 'string',
  160. description: t(`${i18nPrefix}.outputVars.icon`),
  161. },
  162. {
  163. name: 'metadata',
  164. type: 'object',
  165. description: t(`${i18nPrefix}.outputVars.metadata`),
  166. },
  167. ]}
  168. />
  169. </>
  170. </OutputVars>
  171. </div>
  172. </div>
  173. )
  174. }
  175. export default memo(Panel)