textarea.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import React, { useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import {
  4. RiEqualizer2Line,
  5. } from '@remixicon/react'
  6. import Button from '../../base/button'
  7. import Tag from '../../base/tag'
  8. import { getIcon } from '../common/retrieval-method-info'
  9. import s from './style.module.css'
  10. import ModifyExternalRetrievalModal from './modify-external-retrieval-modal'
  11. import Tooltip from '@/app/components/base/tooltip'
  12. import cn from '@/utils/classnames'
  13. import type { ExternalKnowledgeBaseHitTestingResponse, HitTestingResponse } from '@/models/datasets'
  14. import { externalKnowledgeBaseHitTesting, hitTesting } from '@/service/datasets'
  15. import { asyncRunSafe } from '@/utils'
  16. import { RETRIEVE_METHOD, type RetrievalConfig } from '@/types/app'
  17. type TextAreaWithButtonIProps = {
  18. datasetId: string
  19. onUpdateList: () => void
  20. setHitResult: (res: HitTestingResponse) => void
  21. setExternalHitResult: (res: ExternalKnowledgeBaseHitTestingResponse) => void
  22. loading: boolean
  23. setLoading: (v: boolean) => void
  24. text: string
  25. setText: (v: string) => void
  26. isExternal?: boolean
  27. onClickRetrievalMethod: () => void
  28. retrievalConfig: RetrievalConfig
  29. isEconomy: boolean
  30. onSubmit?: () => void
  31. }
  32. const TextAreaWithButton = ({
  33. datasetId,
  34. onUpdateList,
  35. setHitResult,
  36. setExternalHitResult,
  37. setLoading,
  38. loading,
  39. text,
  40. setText,
  41. isExternal = false,
  42. onClickRetrievalMethod,
  43. retrievalConfig,
  44. isEconomy,
  45. onSubmit: _onSubmit,
  46. }: TextAreaWithButtonIProps) => {
  47. const { t } = useTranslation()
  48. const [isSettingsOpen, setIsSettingsOpen] = useState(false)
  49. const [externalRetrievalSettings, setExternalRetrievalSettings] = useState({
  50. top_k: 2,
  51. score_threshold: 0.5,
  52. score_threshold_enabled: false,
  53. })
  54. const handleSaveExternalRetrievalSettings = (data: { top_k: number; score_threshold: number; score_threshold_enabled: boolean }) => {
  55. setExternalRetrievalSettings(data)
  56. setIsSettingsOpen(false)
  57. }
  58. function handleTextChange(event: any) {
  59. setText(event.target.value)
  60. }
  61. const onSubmit = async () => {
  62. setLoading(true)
  63. const [e, res] = await asyncRunSafe<HitTestingResponse>(
  64. hitTesting({
  65. datasetId,
  66. queryText: text,
  67. retrieval_model: {
  68. ...retrievalConfig,
  69. search_method: isEconomy ? RETRIEVE_METHOD.keywordSearch : retrievalConfig.search_method,
  70. },
  71. }) as Promise<HitTestingResponse>,
  72. )
  73. if (!e) {
  74. setHitResult(res)
  75. onUpdateList?.()
  76. }
  77. setLoading(false)
  78. _onSubmit && _onSubmit()
  79. }
  80. const externalRetrievalTestingOnSubmit = async () => {
  81. const [e, res] = await asyncRunSafe<ExternalKnowledgeBaseHitTestingResponse>(
  82. externalKnowledgeBaseHitTesting({
  83. datasetId,
  84. query: text,
  85. external_retrieval_model: {
  86. top_k: externalRetrievalSettings.top_k,
  87. score_threshold: externalRetrievalSettings.score_threshold,
  88. score_threshold_enabled: externalRetrievalSettings.score_threshold_enabled,
  89. },
  90. }) as Promise<ExternalKnowledgeBaseHitTestingResponse>,
  91. )
  92. if (!e) {
  93. setExternalHitResult(res)
  94. onUpdateList?.()
  95. }
  96. setLoading(false)
  97. }
  98. const retrievalMethod = isEconomy ? RETRIEVE_METHOD.invertedIndex : retrievalConfig.search_method
  99. const Icon = getIcon(retrievalMethod)
  100. return (
  101. <>
  102. <div className={s.wrapper}>
  103. <div className='relative pt-2 rounded-tl-xl rounded-tr-xl bg-[#EEF4FF]'>
  104. <div className="px-4 pb-2 flex justify-between h-8 items-center">
  105. <span className="text-gray-800 font-semibold text-sm">
  106. {t('datasetHitTesting.input.title')}
  107. </span>
  108. {isExternal
  109. ? <Button
  110. variant='secondary'
  111. size='small'
  112. onClick={() => setIsSettingsOpen(!isSettingsOpen)}
  113. >
  114. <RiEqualizer2Line className='text-components-button-secondary-text w-3.5 h-3.5' />
  115. <div className='flex px-[3px] justify-center items-center gap-1'>
  116. <span className='text-components-button-secondary-text system-xs-medium'>{t('datasetHitTesting.settingTitle')}</span>
  117. </div>
  118. </Button>
  119. : <Tooltip
  120. popupContent={t('dataset.retrieval.changeRetrievalMethod')}
  121. >
  122. <div
  123. onClick={onClickRetrievalMethod}
  124. className='flex px-2 h-7 items-center space-x-1 bg-white hover:bg-[#ECE9FE] rounded-md shadow-sm cursor-pointer text-[#6927DA]'
  125. >
  126. <Icon className='w-3.5 h-3.5'></Icon>
  127. <div className='text-xs font-medium'>{t(`dataset.retrieval.${retrievalMethod}.title`)}</div>
  128. </div>
  129. </Tooltip>
  130. }
  131. </div>
  132. {
  133. isSettingsOpen && (
  134. <ModifyExternalRetrievalModal
  135. onClose={() => setIsSettingsOpen(false)}
  136. onSave={handleSaveExternalRetrievalSettings}
  137. initialTopK={externalRetrievalSettings.top_k}
  138. initialScoreThreshold={externalRetrievalSettings.score_threshold}
  139. initialScoreThresholdEnabled={externalRetrievalSettings.score_threshold_enabled}
  140. />
  141. )
  142. }
  143. <div className='h-2 rounded-tl-xl rounded-tr-xl bg-white'></div>
  144. </div>
  145. <div className='px-4 pb-11'>
  146. <textarea
  147. className='h-[220px] border-none resize-none font-normal caret-primary-600 text-gray-700 text-sm w-full focus-visible:outline-none placeholder:text-gray-300 placeholder:text-sm placeholder:font-normal'
  148. value={text}
  149. onChange={handleTextChange}
  150. placeholder={t('datasetHitTesting.input.placeholder') as string}
  151. />
  152. <div className="absolute inset-x-0 bottom-0 flex items-center justify-between mx-4 mt-2 mb-2">
  153. {text?.length > 200
  154. ? (
  155. <Tooltip
  156. popupContent={t('datasetHitTesting.input.countWarning')}
  157. >
  158. <div>
  159. <Tag color="red" className="!text-red-600">
  160. {text?.length}
  161. <span className="text-red-300 mx-0.5">/</span>
  162. 200
  163. </Tag>
  164. </div>
  165. </Tooltip>
  166. )
  167. : (
  168. <Tag
  169. color="gray"
  170. className={cn('!text-gray-500', text?.length ? '' : 'opacity-50')}
  171. >
  172. {text?.length}
  173. <span className="text-gray-300 mx-0.5">/</span>
  174. 200
  175. </Tag>
  176. )}
  177. <div>
  178. <Button
  179. onClick={isExternal ? externalRetrievalTestingOnSubmit : onSubmit}
  180. variant="primary"
  181. loading={loading}
  182. disabled={(!text?.length || text?.length > 200)}
  183. >
  184. {t('datasetHitTesting.input.testing')}
  185. </Button>
  186. </div>
  187. </div>
  188. </div>
  189. </div>
  190. </>
  191. )
  192. }
  193. export default TextAreaWithButton