mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-05-28 00:58:20 +08:00

Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: AkaraChen <akarachen@outlook.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Warren Chen <warren.chen830@gmail.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: Yi Xiao <54782454+YIXIAO0@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: KVOJJJin <jzongcode@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: JzoNgKVO <27049666+JzoNgKVO@users.noreply.github.com> Co-authored-by: Charlie.Wei <luowei@cvte.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: huayaoyue6 <huayaoyue@163.com> Co-authored-by: kurokobo <kuro664@gmail.com> Co-authored-by: Matsuda <yiyth.fcb6@gmail.com> Co-authored-by: shirochan <s.yusuke0711@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: Huỳnh Gia Bôi <boihuynh147@gmail.com> Co-authored-by: Julian Huynh <julian.huynh@immersio.io> Co-authored-by: Hash Brown <hi@xzd.me> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Kazuki Takamatsu <kazuki.takamatsu@chowagiken.co.jp> Co-authored-by: Trey Dong <1346650911@qq.com> Co-authored-by: VoidIsVoid <343750470@qq.com> Co-authored-by: Gimling <huangjl@ruyi.ai> Co-authored-by: xiandan-erizo <xiandan.erizo@gmail.com> Co-authored-by: Muneyuki Noguchi <nogu.dev@gmail.com> Co-authored-by: zhaobingshuang <1475195565@qq.com> Co-authored-by: zhaobs <zhaobs@cailian.net> Co-authored-by: suzuki.sh <s2terminal@users.noreply.github.com> Co-authored-by: Yingchun Lai <laiyingchun@apache.org> Co-authored-by: huanshare <huanshare@live.com> Co-authored-by: huanshare <liuhuan101@longfor.com> Co-authored-by: orangeclk <orangeclk@users.noreply.github.com> Co-authored-by: 문정현 <120004247+JungHyunMoon@users.noreply.github.com> Co-authored-by: barabicu <kztk533@gmail.com> Co-authored-by: Wei Mingzhi <whistler_wmz@users.sf.net> Co-authored-by: Paul van Oorschot <20116814+pvoo@users.noreply.github.com> Co-authored-by: zkyTech <zhangkunyuan@hotmail.com> Co-authored-by: zhangkunyuan <zhangkunyuan@cmhi.chinamobile.com> Co-authored-by: Tommy <34446820+Asterovim@users.noreply.github.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Novice <857526207@qq.com> Co-authored-by: Novice Lee <novicelee@NovicedeMacBook-Pro.local> Co-authored-by: Novice Lee <novicelee@NoviPro.local> Co-authored-by: zxhlyh <16177003+zxhlyh@users.noreply.github.com> Co-authored-by: liuzhenghua <1090179900@qq.com> Co-authored-by: Jiang <65766008+AlwaysBluer@users.noreply.github.com> Co-authored-by: jiangzhijie <jiangzhijie.jzj@alibaba-inc.com> Co-authored-by: Joe <79627742+ZhouhaoJiang@users.noreply.github.com> Co-authored-by: Alok Shrivastwa <alok.shrivastwa@gmail.com> Co-authored-by: Alok Shrivastwa <Alok.Shrivastwa@microland.com> Co-authored-by: JasonVV <jasonwangiii@outlook.com> Co-authored-by: Hiroshi Fujita <fujita-h@users.noreply.github.com> Co-authored-by: Kevin9703 <51311316+Kevin9703@users.noreply.github.com> Co-authored-by: NFish <douxc512@gmail.com> Co-authored-by: Junyan Qin <1010553892@qq.com> Co-authored-by: IWAI, Masaharu <iwaim.sub@gmail.com> Co-authored-by: IWAI, Masaharu <iwai_masaharu@funkit.co.jp> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: luckylhb90 <luckylhb90@gmail.com> Co-authored-by: hobo.l <hobo.l@binance.com> Co-authored-by: douxc <7553076+douxc@users.noreply.github.com>
212 lines
9.0 KiB
TypeScript
212 lines
9.0 KiB
TypeScript
'use client'
|
|
import type { FC } from 'react'
|
|
import React, { useEffect, useState } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import useSWR from 'swr'
|
|
import { omit } from 'lodash-es'
|
|
import { useBoolean } from 'ahooks'
|
|
import { useContext } from 'use-context-selector'
|
|
import { RiApps2Line, RiFocus2Line } from '@remixicon/react'
|
|
import SegmentCard from '../documents/detail/completed/SegmentCard'
|
|
import Textarea from './textarea'
|
|
import s from './style.module.css'
|
|
import ModifyRetrievalModal from './modify-retrieval-modal'
|
|
import ResultItem from './components/result-item'
|
|
import cn from '@/utils/classnames'
|
|
import type { ExternalKnowledgeBaseHitTestingResponse, HitTestingResponse } from '@/models/datasets'
|
|
import Loading from '@/app/components/base/loading'
|
|
import Drawer from '@/app/components/base/drawer'
|
|
import Pagination from '@/app/components/base/pagination'
|
|
import FloatRightContainer from '@/app/components/base/float-right-container'
|
|
import { fetchTestingRecords } from '@/service/datasets'
|
|
import DatasetDetailContext from '@/context/dataset-detail'
|
|
import type { RetrievalConfig } from '@/types/app'
|
|
import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
|
|
import useTimestamp from '@/hooks/use-timestamp'
|
|
import docStyle from '@/app/components/datasets/documents/detail/completed/style.module.css'
|
|
|
|
const limit = 10
|
|
|
|
type Props = {
|
|
datasetId: string
|
|
}
|
|
|
|
const RecordsEmpty: FC = () => {
|
|
const { t } = useTranslation()
|
|
return <div className='bg-gray-50 rounded-2xl p-5'>
|
|
<div className={s.clockWrapper}>
|
|
<div className={cn(s.clockIcon, 'w-5 h-5')}></div>
|
|
</div>
|
|
<div className='my-2 text-gray-500 text-sm'>{t('datasetHitTesting.noRecentTip')}</div>
|
|
</div>
|
|
}
|
|
|
|
const HitTesting: FC<Props> = ({ datasetId }: Props) => {
|
|
const { t } = useTranslation()
|
|
const { formatTime } = useTimestamp()
|
|
|
|
const media = useBreakpoints()
|
|
const isMobile = media === MediaType.mobile
|
|
|
|
const [hitResult, setHitResult] = useState<HitTestingResponse | undefined>() // 初始化记录为空数组
|
|
const [externalHitResult, setExternalHitResult] = useState<ExternalKnowledgeBaseHitTestingResponse | undefined>()
|
|
const [submitLoading, setSubmitLoading] = useState(false)
|
|
const [text, setText] = useState('')
|
|
|
|
const [currPage, setCurrPage] = React.useState<number>(0)
|
|
const { data: recordsRes, error, mutate: recordsMutate } = useSWR({
|
|
action: 'fetchTestingRecords',
|
|
datasetId,
|
|
params: { limit, page: currPage + 1 },
|
|
}, apiParams => fetchTestingRecords(omit(apiParams, 'action')))
|
|
|
|
const total = recordsRes?.total || 0
|
|
|
|
const { dataset: currentDataset } = useContext(DatasetDetailContext)
|
|
const isExternal = currentDataset?.provider === 'external'
|
|
|
|
const [retrievalConfig, setRetrievalConfig] = useState(currentDataset?.retrieval_model_dict as RetrievalConfig)
|
|
const [isShowModifyRetrievalModal, setIsShowModifyRetrievalModal] = useState(false)
|
|
const [isShowRightPanel, { setTrue: showRightPanel, setFalse: hideRightPanel, set: setShowRightPanel }] = useBoolean(!isMobile)
|
|
const renderHitResults = (results: any[]) => (
|
|
<div className='h-full flex flex-col py-3 px-4 rounded-t-2xl bg-background-body'>
|
|
<div className='shrink-0 pl-2 text-text-primary font-semibold leading-6 mb-2'>
|
|
{t('datasetHitTesting.hit.title', { num: results.length })}
|
|
</div>
|
|
<div className='grow overflow-y-auto space-y-2'>
|
|
{results.map((record, idx) => (
|
|
<ResultItem
|
|
key={idx}
|
|
payload={record}
|
|
isExternal={isExternal}
|
|
/>
|
|
))}
|
|
</div>
|
|
</div>
|
|
)
|
|
|
|
const renderEmptyState = () => (
|
|
<div className='h-full flex flex-col justify-center items-center py-3 px-4 rounded-t-2xl bg-background-body'>
|
|
<div className={cn(docStyle.commonIcon, docStyle.targetIcon, '!bg-text-quaternary !h-14 !w-14')} />
|
|
<div className='text-text-quaternary text-[13px] mt-3'>
|
|
{t('datasetHitTesting.hit.emptyTip')}
|
|
</div>
|
|
</div>
|
|
)
|
|
|
|
useEffect(() => {
|
|
setShowRightPanel(!isMobile)
|
|
}, [isMobile, setShowRightPanel])
|
|
|
|
return (
|
|
<div className={s.container}>
|
|
<div className='px-6 py-3 flex flex-col'>
|
|
<div className='flex flex-col justify-center mb-4'>
|
|
<h1 className='text-base font-semibold text-text-primary'>{t('datasetHitTesting.title')}</h1>
|
|
<p className='mt-0.5 text-[13px] leading-4 font-normal text-text-tertiary'>{t('datasetHitTesting.desc')}</p>
|
|
</div>
|
|
<Textarea
|
|
datasetId={datasetId}
|
|
setHitResult={setHitResult}
|
|
setExternalHitResult={setExternalHitResult}
|
|
onSubmit={showRightPanel}
|
|
onUpdateList={recordsMutate}
|
|
loading={submitLoading}
|
|
setLoading={setSubmitLoading}
|
|
setText={setText}
|
|
text={text}
|
|
isExternal={isExternal}
|
|
onClickRetrievalMethod={() => setIsShowModifyRetrievalModal(true)}
|
|
retrievalConfig={retrievalConfig}
|
|
isEconomy={currentDataset?.indexing_technique === 'economy'}
|
|
/>
|
|
<div className='text-base font-semibold text-text-primary mt-6 mb-3'>{t('datasetHitTesting.records')}</div>
|
|
{(!recordsRes && !error)
|
|
? (
|
|
<div className='flex-1'><Loading type='app' /></div>
|
|
)
|
|
: recordsRes?.data?.length
|
|
? (
|
|
<>
|
|
<div className='grow overflow-y-auto'>
|
|
<table className={'w-full border-collapse border-0 text-[13px] leading-4 text-text-secondary '}>
|
|
<thead className="sticky top-0 h-7 leading-7 text-xs text-text-tertiary font-medium uppercase">
|
|
<tr>
|
|
<td className='pl-3 w-[128px] rounded-l-lg bg-background-section-burn'>{t('datasetHitTesting.table.header.source')}</td>
|
|
<td className='bg-background-section-burn'>{t('datasetHitTesting.table.header.text')}</td>
|
|
<td className='pl-2 w-48 rounded-r-lg bg-background-section-burn'>{t('datasetHitTesting.table.header.time')}</td>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{recordsRes?.data?.map((record) => {
|
|
const SourceIcon = record.source === 'app' ? RiApps2Line : RiFocus2Line
|
|
return <tr
|
|
key={record.id}
|
|
className='group border-b border-divider-subtle h-10 hover:bg-background-default-hover cursor-pointer'
|
|
onClick={() => setText(record.content)}
|
|
>
|
|
<td className='pl-3 w-[128px]'>
|
|
<div className='flex items-center'>
|
|
<SourceIcon className='mr-1 size-4 text-text-tertiary' />
|
|
<span className='capitalize'>{record.source.replace('_', ' ').replace('hit testing', 'retrieval test')}</span>
|
|
</div>
|
|
</td>
|
|
<td className='max-w-xs py-2'>{record.content}</td>
|
|
<td className='pl-2 w-36'>
|
|
{formatTime(record.created_at, t('datasetHitTesting.dateTimeFormat') as string)}
|
|
</td>
|
|
</tr>
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
{(total && total > limit)
|
|
? <Pagination current={currPage} onChange={setCurrPage} total={total} limit={limit} />
|
|
: null}
|
|
</>
|
|
)
|
|
: (
|
|
<RecordsEmpty />
|
|
)}
|
|
</div>
|
|
<FloatRightContainer panelClassname='!justify-start !overflow-y-auto' showClose isMobile={isMobile} isOpen={isShowRightPanel} onClose={hideRightPanel} footer={null}>
|
|
<div className='flex flex-col pt-3'>
|
|
{/* {renderHitResults(generalResultData)} */}
|
|
{submitLoading
|
|
? <SegmentCard
|
|
loading={true}
|
|
scene='hitTesting'
|
|
className='h-[216px]'
|
|
/>
|
|
: (
|
|
(() => {
|
|
if (!hitResult?.records.length && !externalHitResult?.records.length)
|
|
return renderEmptyState()
|
|
|
|
if (hitResult?.records.length)
|
|
return renderHitResults(hitResult.records)
|
|
|
|
return renderHitResults(externalHitResult?.records || [])
|
|
})()
|
|
)
|
|
}
|
|
</div>
|
|
</FloatRightContainer>
|
|
<Drawer isOpen={isShowModifyRetrievalModal} onClose={() => setIsShowModifyRetrievalModal(false)} footer={null} mask={isMobile} panelClassname='mt-16 mx-2 sm:mr-2 mb-3 !p-0 !max-w-[640px] rounded-xl'>
|
|
<ModifyRetrievalModal
|
|
indexMethod={currentDataset?.indexing_technique || ''}
|
|
value={retrievalConfig}
|
|
isShow={isShowModifyRetrievalModal}
|
|
onHide={() => setIsShowModifyRetrievalModal(false)}
|
|
onSave={(value) => {
|
|
setRetrievalConfig(value)
|
|
setIsShowModifyRetrievalModal(false)
|
|
}}
|
|
/>
|
|
</Drawer>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default HitTesting
|