datasets.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import type { Fetcher } from 'swr'
  2. import qs from 'qs'
  3. import { del, get, patch, post, put } from './base'
  4. import type {
  5. CreateDocumentReq,
  6. DataSet,
  7. DataSetListResponse,
  8. DocumentDetailResponse,
  9. DocumentListResponse,
  10. ErrorDocsResponse,
  11. ExternalAPIDeleteResponse,
  12. ExternalAPIItem,
  13. ExternalAPIListResponse,
  14. ExternalAPIUsage,
  15. ExternalKnowledgeBaseHitTestingResponse,
  16. ExternalKnowledgeItem,
  17. FileIndexingEstimateResponse,
  18. HitTestingRecordsResponse,
  19. HitTestingResponse,
  20. IndexingEstimateParams,
  21. IndexingEstimateResponse,
  22. IndexingStatusBatchResponse,
  23. IndexingStatusResponse,
  24. ProcessRuleResponse,
  25. RelatedAppResponse,
  26. SegmentDetailModel,
  27. SegmentUpdater,
  28. SegmentsQuery,
  29. SegmentsResponse,
  30. createDocumentResponse,
  31. } from '@/models/datasets'
  32. import type { CreateKnowledgeBaseReq } from '@/app/components/datasets/external-knowledge-base/create/declarations'
  33. import type { CreateExternalAPIReq } from '@/app/components/datasets/external-api/declarations'
  34. import type { CommonResponse, DataSourceNotionWorkspace } from '@/models/common'
  35. import { DataSourceProvider } from '@/models/common'
  36. import type {
  37. ApiKeysListResponse,
  38. CreateApiKeyResponse,
  39. } from '@/models/app'
  40. import type { RetrievalConfig } from '@/types/app'
  41. // apis for documents in a dataset
  42. type CommonDocReq = {
  43. datasetId: string
  44. documentId: string
  45. }
  46. type BatchReq = {
  47. datasetId: string
  48. batchId: string
  49. }
  50. export type SortType = 'created_at' | 'hit_count' | '-created_at' | '-hit_count'
  51. export type MetadataType = 'all' | 'only' | 'without'
  52. export const fetchDatasetDetail: Fetcher<DataSet, string> = (datasetId: string) => {
  53. return get<DataSet>(`/datasets/${datasetId}`)
  54. }
  55. export const updateDatasetSetting: Fetcher<DataSet, {
  56. datasetId: string
  57. body: Partial<Pick<DataSet,
  58. 'name' | 'description' | 'permission' | 'partial_member_list' | 'indexing_technique' | 'retrieval_model' | 'embedding_model' | 'embedding_model_provider'
  59. >>
  60. }> = ({ datasetId, body }) => {
  61. return patch<DataSet>(`/datasets/${datasetId}`, { body })
  62. }
  63. export const fetchDatasetRelatedApps: Fetcher<RelatedAppResponse, string> = (datasetId: string) => {
  64. return get<RelatedAppResponse>(`/datasets/${datasetId}/related-apps`)
  65. }
  66. export const fetchDatasets: Fetcher<DataSetListResponse, { url: string; params: { page: number; ids?: string[]; limit?: number } }> = ({ url, params }) => {
  67. const urlParams = qs.stringify(params, { indices: false })
  68. return get<DataSetListResponse>(`${url}?${urlParams}`)
  69. }
  70. export const createEmptyDataset: Fetcher<DataSet, { name: string }> = ({ name }) => {
  71. return post<DataSet>('/datasets', { body: { name } })
  72. }
  73. export const checkIsUsedInApp: Fetcher<{ is_using: boolean }, string> = (id) => {
  74. return get<{ is_using: boolean }>(`/datasets/${id}/use-check`, {}, {
  75. silent: true,
  76. })
  77. }
  78. export const deleteDataset: Fetcher<DataSet, string> = (datasetID) => {
  79. return del<DataSet>(`/datasets/${datasetID}`)
  80. }
  81. export const fetchExternalAPIList: Fetcher<ExternalAPIListResponse, { url: string }> = ({ url }) => {
  82. return get<ExternalAPIListResponse>(url)
  83. }
  84. export const fetchExternalAPI: Fetcher<ExternalAPIItem, { apiTemplateId: string }> = ({ apiTemplateId }) => {
  85. return get<ExternalAPIItem>(`/datasets/external-knowledge-api/${apiTemplateId}`)
  86. }
  87. export const updateExternalAPI: Fetcher<ExternalAPIItem, { apiTemplateId: string; body: ExternalAPIItem }> = ({ apiTemplateId, body }) => {
  88. return patch<ExternalAPIItem>(`/datasets/external-knowledge-api/${apiTemplateId}`, { body })
  89. }
  90. export const deleteExternalAPI: Fetcher<ExternalAPIDeleteResponse, { apiTemplateId: string }> = ({ apiTemplateId }) => {
  91. return del<ExternalAPIDeleteResponse>(`/datasets/external-knowledge-api/${apiTemplateId}`)
  92. }
  93. export const checkUsageExternalAPI: Fetcher<ExternalAPIUsage, { apiTemplateId: string }> = ({ apiTemplateId }) => {
  94. return get<ExternalAPIUsage>(`/datasets/external-knowledge-api/${apiTemplateId}/use-check`)
  95. }
  96. export const createExternalAPI: Fetcher<ExternalAPIItem, { body: CreateExternalAPIReq }> = ({ body }) => {
  97. return post<ExternalAPIItem>('/datasets/external-knowledge-api', { body })
  98. }
  99. export const createExternalKnowledgeBase: Fetcher<ExternalKnowledgeItem, { body: CreateKnowledgeBaseReq }> = ({ body }) => {
  100. return post<ExternalKnowledgeItem>('/datasets/external', { body })
  101. }
  102. export const fetchDefaultProcessRule: Fetcher<ProcessRuleResponse, { url: string }> = ({ url }) => {
  103. return get<ProcessRuleResponse>(url)
  104. }
  105. export const fetchProcessRule: Fetcher<ProcessRuleResponse, { params: { documentId: string } }> = ({ params: { documentId } }) => {
  106. return get<ProcessRuleResponse>('/datasets/process-rule', { params: { document_id: documentId } })
  107. }
  108. export const fetchDocuments: Fetcher<DocumentListResponse, { datasetId: string; params: { keyword: string; page: number; limit: number; sort?: SortType } }> = ({ datasetId, params }) => {
  109. return get<DocumentListResponse>(`/datasets/${datasetId}/documents`, { params })
  110. }
  111. export const createFirstDocument: Fetcher<createDocumentResponse, { body: CreateDocumentReq }> = ({ body }) => {
  112. return post<createDocumentResponse>('/datasets/init', { body })
  113. }
  114. export const createDocument: Fetcher<createDocumentResponse, { datasetId: string; body: CreateDocumentReq }> = ({ datasetId, body }) => {
  115. return post<createDocumentResponse>(`/datasets/${datasetId}/documents`, { body })
  116. }
  117. export const fetchIndexingEstimate: Fetcher<IndexingEstimateResponse, CommonDocReq> = ({ datasetId, documentId }) => {
  118. return get<IndexingEstimateResponse>(`/datasets/${datasetId}/documents/${documentId}/indexing-estimate`, {})
  119. }
  120. export const fetchIndexingEstimateBatch: Fetcher<IndexingEstimateResponse, BatchReq> = ({ datasetId, batchId }) => {
  121. return get<IndexingEstimateResponse>(`/datasets/${datasetId}/batch/${batchId}/indexing-estimate`, {})
  122. }
  123. export const fetchIndexingStatus: Fetcher<IndexingStatusResponse, CommonDocReq> = ({ datasetId, documentId }) => {
  124. return get<IndexingStatusResponse>(`/datasets/${datasetId}/documents/${documentId}/indexing-status`, {})
  125. }
  126. export const fetchIndexingStatusBatch: Fetcher<IndexingStatusBatchResponse, BatchReq> = ({ datasetId, batchId }) => {
  127. return get<IndexingStatusBatchResponse>(`/datasets/${datasetId}/batch/${batchId}/indexing-status`, {})
  128. }
  129. export const fetchDocumentDetail: Fetcher<DocumentDetailResponse, CommonDocReq & { params: { metadata?: MetadataType } }> = ({ datasetId, documentId, params }) => {
  130. return get<DocumentDetailResponse>(`/datasets/${datasetId}/documents/${documentId}`, { params })
  131. }
  132. export const renameDocumentName: Fetcher<CommonResponse, CommonDocReq & { name: string }> = ({ datasetId, documentId, name }) => {
  133. return post<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/rename`, {
  134. body: { name },
  135. })
  136. }
  137. export const pauseDocIndexing: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
  138. return patch<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/processing/pause`)
  139. }
  140. export const resumeDocIndexing: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
  141. return patch<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/processing/resume`)
  142. }
  143. export const deleteDocument: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
  144. return del<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}`)
  145. }
  146. export const archiveDocument: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
  147. return patch<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/status/archive`)
  148. }
  149. export const unArchiveDocument: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
  150. return patch<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/status/un_archive`)
  151. }
  152. export const enableDocument: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
  153. return patch<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/status/enable`)
  154. }
  155. export const disableDocument: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
  156. return patch<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/status/disable`)
  157. }
  158. export const syncDocument: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
  159. return get<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/notion/sync`)
  160. }
  161. export const syncWebsite: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
  162. return get<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/website-sync`)
  163. }
  164. export const preImportNotionPages: Fetcher<{ notion_info: DataSourceNotionWorkspace[] }, { url: string; datasetId?: string }> = ({ url, datasetId }) => {
  165. return get<{ notion_info: DataSourceNotionWorkspace[] }>(url, { params: { dataset_id: datasetId } })
  166. }
  167. export const modifyDocMetadata: Fetcher<CommonResponse, CommonDocReq & { body: { doc_type: string; doc_metadata: Record<string, any> } }> = ({ datasetId, documentId, body }) => {
  168. return put<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/metadata`, { body })
  169. }
  170. // apis for segments in a document
  171. export const fetchSegments: Fetcher<SegmentsResponse, CommonDocReq & { params: SegmentsQuery }> = ({ datasetId, documentId, params }) => {
  172. return get<SegmentsResponse>(`/datasets/${datasetId}/documents/${documentId}/segments`, { params })
  173. }
  174. export const enableSegment: Fetcher<CommonResponse, { datasetId: string; segmentId: string }> = ({ datasetId, segmentId }) => {
  175. return patch<CommonResponse>(`/datasets/${datasetId}/segments/${segmentId}/enable`)
  176. }
  177. export const disableSegment: Fetcher<CommonResponse, { datasetId: string; segmentId: string }> = ({ datasetId, segmentId }) => {
  178. return patch<CommonResponse>(`/datasets/${datasetId}/segments/${segmentId}/disable`)
  179. }
  180. export const updateSegment: Fetcher<{ data: SegmentDetailModel; doc_form: string }, { datasetId: string; documentId: string; segmentId: string; body: SegmentUpdater }> = ({ datasetId, documentId, segmentId, body }) => {
  181. return patch<{ data: SegmentDetailModel; doc_form: string }>(`/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}`, { body })
  182. }
  183. export const addSegment: Fetcher<{ data: SegmentDetailModel; doc_form: string }, { datasetId: string; documentId: string; body: SegmentUpdater }> = ({ datasetId, documentId, body }) => {
  184. return post<{ data: SegmentDetailModel; doc_form: string }>(`/datasets/${datasetId}/documents/${documentId}/segment`, { body })
  185. }
  186. export const deleteSegment: Fetcher<CommonResponse, { datasetId: string; documentId: string; segmentId: string }> = ({ datasetId, documentId, segmentId }) => {
  187. return del<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}`)
  188. }
  189. export const segmentBatchImport: Fetcher<{ job_id: string; job_status: string }, { url: string; body: FormData }> = ({ url, body }) => {
  190. return post<{ job_id: string; job_status: string }>(url, { body }, { bodyStringify: false, deleteContentType: true })
  191. }
  192. export const checkSegmentBatchImportProgress: Fetcher<{ job_id: string; job_status: string }, { jobID: string }> = ({ jobID }) => {
  193. return get<{ job_id: string; job_status: string }>(`/datasets/batch_import_status/${jobID}`)
  194. }
  195. // hit testing
  196. export const hitTesting: Fetcher<HitTestingResponse, { datasetId: string; queryText: string; retrieval_model: RetrievalConfig }> = ({ datasetId, queryText, retrieval_model }) => {
  197. return post<HitTestingResponse>(`/datasets/${datasetId}/hit-testing`, { body: { query: queryText, retrieval_model } })
  198. }
  199. export const externalKnowledgeBaseHitTesting: Fetcher<ExternalKnowledgeBaseHitTestingResponse, { datasetId: string; query: string; external_retrieval_model: { top_k: number; score_threshold: number; score_threshold_enabled: boolean } }> = ({ datasetId, query, external_retrieval_model }) => {
  200. return post<ExternalKnowledgeBaseHitTestingResponse>(`/datasets/${datasetId}/external-hit-testing`, { body: { query, external_retrieval_model } })
  201. }
  202. export const fetchTestingRecords: Fetcher<HitTestingRecordsResponse, { datasetId: string; params: { page: number; limit: number } }> = ({ datasetId, params }) => {
  203. return get<HitTestingRecordsResponse>(`/datasets/${datasetId}/queries`, { params })
  204. }
  205. export const fetchFileIndexingEstimate: Fetcher<FileIndexingEstimateResponse, IndexingEstimateParams> = (body: IndexingEstimateParams) => {
  206. return post<FileIndexingEstimateResponse>('/datasets/indexing-estimate', { body })
  207. }
  208. export const fetchNotionPagePreview: Fetcher<{ content: string }, { workspaceID: string; pageID: string; pageType: string }> = ({ workspaceID, pageID, pageType }) => {
  209. return get<{ content: string }>(`notion/workspaces/${workspaceID}/pages/${pageID}/${pageType}/preview`)
  210. }
  211. export const fetchApiKeysList: Fetcher<ApiKeysListResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  212. return get<ApiKeysListResponse>(url, params)
  213. }
  214. export const delApikey: Fetcher<CommonResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  215. return del<CommonResponse>(url, params)
  216. }
  217. export const createApikey: Fetcher<CreateApiKeyResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  218. return post<CreateApiKeyResponse>(url, body)
  219. }
  220. export const fetchDatasetApiBaseUrl: Fetcher<{ api_base_url: string }, string> = (url) => {
  221. return get<{ api_base_url: string }>(url)
  222. }
  223. export const fetchDataSources = () => {
  224. return get<CommonResponse>('api-key-auth/data-source')
  225. }
  226. export const createDataSourceApiKeyBinding: Fetcher<CommonResponse, Record<string, any>> = (body) => {
  227. return post<CommonResponse>('api-key-auth/data-source/binding', { body })
  228. }
  229. export const removeDataSourceApiKeyBinding: Fetcher<CommonResponse, string> = (id: string) => {
  230. return del<CommonResponse>(`api-key-auth/data-source/${id}`)
  231. }
  232. export const createFirecrawlTask: Fetcher<CommonResponse, Record<string, any>> = (body) => {
  233. return post<CommonResponse>('website/crawl', {
  234. body: {
  235. ...body,
  236. provider: DataSourceProvider.fireCrawl,
  237. },
  238. })
  239. }
  240. export const checkFirecrawlTaskStatus: Fetcher<CommonResponse, string> = (jobId: string) => {
  241. return get<CommonResponse>(`website/crawl/status/${jobId}`, {
  242. params: {
  243. provider: DataSourceProvider.fireCrawl,
  244. },
  245. }, {
  246. silent: true,
  247. })
  248. }
  249. export const createJinaReaderTask: Fetcher<CommonResponse, Record<string, any>> = (body) => {
  250. return post<CommonResponse>('website/crawl', {
  251. body: {
  252. ...body,
  253. provider: DataSourceProvider.jinaReader,
  254. },
  255. })
  256. }
  257. export const checkJinaReaderTaskStatus: Fetcher<CommonResponse, string> = (jobId: string) => {
  258. return get<CommonResponse>(`website/crawl/status/${jobId}`, {
  259. params: {
  260. provider: 'jinareader',
  261. },
  262. }, {
  263. silent: true,
  264. })
  265. }
  266. type FileTypesRes = {
  267. allowed_extensions: string[]
  268. }
  269. export const fetchSupportFileTypes: Fetcher<FileTypesRes, { url: string }> = ({ url }) => {
  270. return get<FileTypesRes>(url)
  271. }
  272. export const getErrorDocs: Fetcher<ErrorDocsResponse, { datasetId: string }> = ({ datasetId }) => {
  273. return get<ErrorDocsResponse>(`/datasets/${datasetId}/error-docs`)
  274. }
  275. export const retryErrorDocs: Fetcher<CommonResponse, { datasetId: string; document_ids: string[] }> = ({ datasetId, document_ids }) => {
  276. return post<CommonResponse>(`/datasets/${datasetId}/retry`, { body: { document_ids } })
  277. }