import React, { useState } from 'react' import { useTranslation } from 'react-i18next' import { RiEqualizer2Line, } from '@remixicon/react' import Button from '../../base/button' import Tag from '../../base/tag' import { getIcon } from '../common/retrieval-method-info' import s from './style.module.css' import ModifyExternalRetrievalModal from './modify-external-retrieval-modal' import Tooltip from '@/app/components/base/tooltip' import cn from '@/utils/classnames' import type { ExternalKnowledgeBaseHitTestingResponse, HitTestingResponse } from '@/models/datasets' import { externalKnowledgeBaseHitTesting, hitTesting } from '@/service/datasets' import { asyncRunSafe } from '@/utils' import { RETRIEVE_METHOD, type RetrievalConfig } from '@/types/app' type TextAreaWithButtonIProps = { datasetId: string onUpdateList: () => void setHitResult: (res: HitTestingResponse) => void setExternalHitResult: (res: ExternalKnowledgeBaseHitTestingResponse) => void loading: boolean setLoading: (v: boolean) => void text: string setText: (v: string) => void isExternal?: boolean onClickRetrievalMethod: () => void retrievalConfig: RetrievalConfig isEconomy: boolean onSubmit?: () => void } const TextAreaWithButton = ({ datasetId, onUpdateList, setHitResult, setExternalHitResult, setLoading, loading, text, setText, isExternal = false, onClickRetrievalMethod, retrievalConfig, isEconomy, onSubmit: _onSubmit, }: TextAreaWithButtonIProps) => { const { t } = useTranslation() const [isSettingsOpen, setIsSettingsOpen] = useState(false) const [externalRetrievalSettings, setExternalRetrievalSettings] = useState({ top_k: 2, score_threshold: 0.5, score_threshold_enabled: false, }) const handleSaveExternalRetrievalSettings = (data: { top_k: number; score_threshold: number; score_threshold_enabled: boolean }) => { setExternalRetrievalSettings(data) setIsSettingsOpen(false) } function handleTextChange(event: any) { setText(event.target.value) } const onSubmit = async () => { setLoading(true) const [e, res] = await asyncRunSafe( hitTesting({ datasetId, queryText: text, retrieval_model: { ...retrievalConfig, search_method: isEconomy ? RETRIEVE_METHOD.keywordSearch : retrievalConfig.search_method, }, }) as Promise, ) if (!e) { setHitResult(res) onUpdateList?.() } setLoading(false) _onSubmit && _onSubmit() } const externalRetrievalTestingOnSubmit = async () => { const [e, res] = await asyncRunSafe( externalKnowledgeBaseHitTesting({ datasetId, query: text, external_retrieval_model: { top_k: externalRetrievalSettings.top_k, score_threshold: externalRetrievalSettings.score_threshold, score_threshold_enabled: externalRetrievalSettings.score_threshold_enabled, }, }) as Promise, ) if (!e) { setExternalHitResult(res) onUpdateList?.() } setLoading(false) } const retrievalMethod = isEconomy ? RETRIEVE_METHOD.invertedIndex : retrievalConfig.search_method const Icon = getIcon(retrievalMethod) return ( <>
{t('datasetHitTesting.input.title')} {isExternal ? :
{t(`dataset.retrieval.${retrievalMethod}.title`)}
}
{ isSettingsOpen && ( setIsSettingsOpen(false)} onSave={handleSaveExternalRetrievalSettings} initialTopK={externalRetrievalSettings.top_k} initialScoreThreshold={externalRetrievalSettings.score_threshold} initialScoreThresholdEnabled={externalRetrievalSettings.score_threshold_enabled} /> ) }