Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

panel.tsx 6.6KB

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