datasets.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. import type { DataSourceNotionPage, DataSourceProvider } from './common'
  2. import type { AppIconType, AppMode, RetrievalConfig } from '@/types/app'
  3. import type { Tag } from '@/app/components/base/tag-management/constant'
  4. import type { IndexingType } from '@/app/components/datasets/create/step-two'
  5. export enum DataSourceType {
  6. FILE = 'upload_file',
  7. NOTION = 'notion_import',
  8. WEB = 'website_crawl',
  9. }
  10. export enum DatasetPermission {
  11. 'onlyMe' = 'only_me',
  12. 'allTeamMembers' = 'all_team_members',
  13. 'partialMembers' = 'partial_members',
  14. }
  15. export enum ChunkingMode {
  16. 'text' = 'text_model', // General text
  17. 'qa' = 'qa_model', // General QA
  18. 'parentChild' = 'hierarchical_model', // Parent-Child
  19. }
  20. export type DataSet = {
  21. id: string
  22. name: string
  23. icon: string
  24. icon_background: string
  25. description: string
  26. permission: DatasetPermission
  27. data_source_type: DataSourceType
  28. indexing_technique: IndexingType
  29. created_by: string
  30. updated_by: string
  31. updated_at: number
  32. app_count: number
  33. doc_form: ChunkingMode
  34. document_count: number
  35. word_count: number
  36. provider: string
  37. embedding_model: string
  38. embedding_model_provider: string
  39. embedding_available: boolean
  40. retrieval_model_dict: RetrievalConfig
  41. retrieval_model: RetrievalConfig
  42. tags: Tag[]
  43. partial_member_list?: string[]
  44. external_knowledge_info: {
  45. external_knowledge_id: string
  46. external_knowledge_api_id: string
  47. external_knowledge_api_name: string
  48. external_knowledge_api_endpoint: string
  49. }
  50. external_retrieval_model: {
  51. top_k: number
  52. score_threshold: number
  53. score_threshold_enabled: boolean
  54. }
  55. }
  56. export type ExternalAPIItem = {
  57. id: string
  58. tenant_id: string
  59. name: string
  60. description: string
  61. settings: {
  62. endpoint: string
  63. api_key: string
  64. }
  65. dataset_bindings: { id: string; name: string }[]
  66. created_by: string
  67. created_at: string
  68. }
  69. export type ExternalKnowledgeItem = {
  70. id: string
  71. name: string
  72. description: string | null
  73. provider: 'external'
  74. permission: DatasetPermission
  75. data_source_type: null
  76. indexing_technique: null
  77. app_count: number
  78. document_count: number
  79. word_count: number
  80. created_by: string
  81. created_at: string
  82. updated_by: string
  83. updated_at: string
  84. tags: Tag[]
  85. }
  86. export type ExternalAPIDeleteResponse = {
  87. result: 'success' | 'error'
  88. }
  89. export type ExternalAPIUsage = {
  90. is_using: boolean
  91. count: number
  92. }
  93. export type CustomFile = File & {
  94. id?: string
  95. extension?: string
  96. mime_type?: string
  97. created_by?: string
  98. created_at?: number
  99. }
  100. export type DocumentItem = {
  101. id: string
  102. name: string
  103. extension: string
  104. }
  105. export type CrawlOptions = {
  106. crawl_sub_pages: boolean
  107. only_main_content: boolean
  108. includes: string
  109. excludes: string
  110. limit: number | string
  111. max_depth: number | string
  112. use_sitemap: boolean
  113. }
  114. export type CrawlResultItem = {
  115. title: string
  116. markdown: string
  117. description: string
  118. source_url: string
  119. }
  120. export type FileItem = {
  121. fileID: string
  122. file: CustomFile
  123. progress: number
  124. }
  125. export type FetchDatasetsParams = {
  126. url: string
  127. params: {
  128. page: number
  129. tag_ids?: string[]
  130. limit: number
  131. include_all: boolean
  132. keyword?: string
  133. }
  134. }
  135. export type DataSetListResponse = {
  136. data: DataSet[]
  137. has_more: boolean
  138. limit: number
  139. page: number
  140. total: number
  141. }
  142. export type ExternalAPIListResponse = {
  143. data: ExternalAPIItem[]
  144. has_more: boolean
  145. limit: number
  146. page: number
  147. total: number
  148. }
  149. export type QA = {
  150. question: string
  151. answer: string
  152. }
  153. export type IndexingEstimateResponse = {
  154. tokens: number
  155. total_price: number
  156. currency: string
  157. total_segments: number
  158. preview: Array<{ content: string; child_chunks: string[] }>
  159. qa_preview?: QA[]
  160. }
  161. export type FileIndexingEstimateResponse = {
  162. total_nodes: number
  163. } & IndexingEstimateResponse
  164. export type IndexingStatusResponse = {
  165. id: string
  166. indexing_status: DocumentIndexingStatus
  167. processing_started_at: number
  168. parsing_completed_at: number
  169. cleaning_completed_at: number
  170. splitting_completed_at: number
  171. completed_at: any
  172. paused_at: any
  173. error: any
  174. stopped_at: any
  175. completed_segments: number
  176. total_segments: number
  177. }
  178. export type IndexingStatusBatchResponse = {
  179. data: IndexingStatusResponse[]
  180. }
  181. export enum ProcessMode {
  182. general = 'custom',
  183. parentChild = 'hierarchical',
  184. }
  185. export type ParentMode = 'full-doc' | 'paragraph'
  186. export type ProcessRuleResponse = {
  187. mode: ProcessMode
  188. rules: Rules
  189. limits: Limits
  190. }
  191. export type Rules = {
  192. pre_processing_rules: PreProcessingRule[]
  193. segmentation: Segmentation
  194. parent_mode: ParentMode
  195. subchunk_segmentation: Segmentation
  196. }
  197. export type Limits = {
  198. indexing_max_segmentation_tokens_length: number
  199. }
  200. export type PreProcessingRule = {
  201. id: string
  202. enabled: boolean
  203. }
  204. export type Segmentation = {
  205. separator: string
  206. max_tokens: number
  207. chunk_overlap?: number
  208. }
  209. export const DocumentIndexingStatusList = [
  210. 'waiting',
  211. 'parsing',
  212. 'cleaning',
  213. 'splitting',
  214. 'indexing',
  215. 'paused',
  216. 'error',
  217. 'completed',
  218. ] as const
  219. export type DocumentIndexingStatus = typeof DocumentIndexingStatusList[number]
  220. export const DisplayStatusList = [
  221. 'queuing',
  222. 'indexing',
  223. 'paused',
  224. 'error',
  225. 'available',
  226. 'enabled',
  227. 'disabled',
  228. 'archived',
  229. ] as const
  230. export type DocumentDisplayStatus = typeof DisplayStatusList[number]
  231. export type DataSourceInfo = {
  232. upload_file: {
  233. id: string
  234. name: string
  235. size: number
  236. mime_type: string
  237. created_at: number
  238. created_by: string
  239. extension: string
  240. }
  241. notion_page_icon?: string
  242. notion_workspace_id?: string
  243. notion_page_id?: string
  244. provider?: DataSourceProvider
  245. job_id: string
  246. url: string
  247. }
  248. export type InitialDocumentDetail = {
  249. id: string
  250. batch: string
  251. position: number
  252. dataset_id: string
  253. data_source_type: DataSourceType
  254. data_source_info: DataSourceInfo
  255. dataset_process_rule_id: string
  256. name: string
  257. created_from: 'api' | 'web'
  258. created_by: string
  259. created_at: number
  260. indexing_status: DocumentIndexingStatus
  261. display_status: DocumentDisplayStatus
  262. completed_segments?: number
  263. total_segments?: number
  264. doc_form: ChunkingMode
  265. doc_language: string
  266. }
  267. export type SimpleDocumentDetail = InitialDocumentDetail & {
  268. enabled: boolean
  269. word_count: number
  270. is_qa: boolean // TODO waiting for backend to add this field
  271. error?: string | null
  272. archived: boolean
  273. updated_at: number
  274. hit_count: number
  275. dataset_process_rule_id?: string
  276. data_source_detail_dict?: {
  277. upload_file: {
  278. name: string
  279. extension: string
  280. }
  281. }
  282. }
  283. export type DocumentListResponse = {
  284. data: SimpleDocumentDetail[]
  285. has_more: boolean
  286. total: number
  287. page: number
  288. limit: number
  289. }
  290. export type DocumentReq = {
  291. original_document_id?: string
  292. indexing_technique?: string
  293. doc_form: ChunkingMode
  294. doc_language: string
  295. process_rule: ProcessRule
  296. }
  297. export type CreateDocumentReq = DocumentReq & {
  298. data_source: DataSource
  299. retrieval_model: RetrievalConfig
  300. embedding_model: string
  301. embedding_model_provider: string
  302. }
  303. export type IndexingEstimateParams = DocumentReq & Partial<DataSource> & {
  304. dataset_id: string
  305. }
  306. export type DataSource = {
  307. type: DataSourceType
  308. info_list: {
  309. data_source_type: DataSourceType
  310. notion_info_list?: NotionInfo[]
  311. file_info_list?: {
  312. file_ids: string[]
  313. }
  314. website_info_list?: {
  315. provider: string
  316. job_id: string
  317. urls: string[]
  318. }
  319. }
  320. }
  321. export type NotionInfo = {
  322. workspace_id: string
  323. pages: DataSourceNotionPage[]
  324. }
  325. export type NotionPage = {
  326. page_id: string
  327. type: string
  328. }
  329. export type ProcessRule = {
  330. mode: ProcessMode
  331. rules: Rules
  332. }
  333. export type createDocumentResponse = {
  334. dataset?: DataSet
  335. batch: string
  336. documents: InitialDocumentDetail[]
  337. }
  338. export type PrecessRule = {
  339. mode: ProcessMode
  340. rules: Rules
  341. }
  342. export type FullDocumentDetail = SimpleDocumentDetail & {
  343. batch: string
  344. created_api_request_id: string
  345. processing_started_at: number
  346. parsing_completed_at: number
  347. cleaning_completed_at: number
  348. splitting_completed_at: number
  349. tokens: number
  350. indexing_latency: number
  351. completed_at: number
  352. paused_by: string
  353. paused_at: number
  354. stopped_at: number
  355. indexing_status: string
  356. disabled_at: number
  357. disabled_by: string
  358. archived_reason: 'rule_modified' | 're_upload'
  359. archived_by: string
  360. archived_at: number
  361. doc_type?: DocType | null | 'others'
  362. doc_metadata?: DocMetadata | null
  363. segment_count: number
  364. dataset_process_rule: PrecessRule
  365. document_process_rule: ProcessRule
  366. [key: string]: any
  367. }
  368. export type DocMetadata = {
  369. title: string
  370. language: string
  371. author: string
  372. publisher: string
  373. publicationDate: string
  374. ISBN: string
  375. category: string
  376. [key: string]: string
  377. }
  378. export const CUSTOMIZABLE_DOC_TYPES = [
  379. 'book',
  380. 'web_page',
  381. 'paper',
  382. 'social_media_post',
  383. 'personal_document',
  384. 'business_document',
  385. 'im_chat_log',
  386. ] as const
  387. export const FIXED_DOC_TYPES = ['synced_from_github', 'synced_from_notion', 'wikipedia_entry'] as const
  388. export type CustomizableDocType = typeof CUSTOMIZABLE_DOC_TYPES[number]
  389. export type FixedDocType = typeof FIXED_DOC_TYPES[number]
  390. export type DocType = CustomizableDocType | FixedDocType
  391. export type DocumentDetailResponse = FullDocumentDetail
  392. export const SEGMENT_STATUS_LIST = ['waiting', 'completed', 'error', 'indexing']
  393. export type SegmentStatus = typeof SEGMENT_STATUS_LIST[number]
  394. export type SegmentsQuery = {
  395. page?: string
  396. limit: number
  397. // status?: SegmentStatus
  398. hit_count_gte?: number
  399. keyword?: string
  400. enabled?: boolean | 'all'
  401. }
  402. export type SegmentDetailModel = {
  403. id: string
  404. position: number
  405. document_id: string
  406. content: string
  407. word_count: number
  408. tokens: number
  409. keywords: string[]
  410. index_node_id: string
  411. index_node_hash: string
  412. hit_count: number
  413. enabled: boolean
  414. disabled_at: number
  415. disabled_by: string
  416. status: SegmentStatus
  417. created_by: string
  418. created_at: number
  419. indexing_at: number
  420. completed_at: number
  421. error: string | null
  422. stopped_at: number
  423. answer?: string
  424. child_chunks?: ChildChunkDetail[]
  425. updated_at: number
  426. }
  427. export type SegmentsResponse = {
  428. data: SegmentDetailModel[]
  429. has_more: boolean
  430. limit: number
  431. total: number
  432. total_pages: number
  433. page: number
  434. }
  435. export type HitTestingRecord = {
  436. id: string
  437. content: string
  438. source: 'app' | 'hit_testing' | 'plugin'
  439. source_app_id: string
  440. created_by_role: 'account' | 'end_user'
  441. created_by: string
  442. created_at: number
  443. }
  444. export type HitTestingChildChunk = {
  445. id: string
  446. content: string
  447. position: number
  448. score: number
  449. }
  450. export type HitTesting = {
  451. segment: Segment
  452. content: Segment
  453. score: number
  454. tsne_position: TsnePosition
  455. child_chunks?: HitTestingChildChunk[] | null
  456. }
  457. export type ExternalKnowledgeBaseHitTesting = {
  458. content: string
  459. title: string
  460. score: number
  461. metadata: {
  462. 'x-amz-bedrock-kb-source-uri': string
  463. 'x-amz-bedrock-kb-data-source-id': string
  464. }
  465. }
  466. export type Segment = {
  467. id: string
  468. document: Document
  469. content: string
  470. position: number
  471. word_count: number
  472. tokens: number
  473. keywords: string[]
  474. hit_count: number
  475. index_node_hash: string
  476. }
  477. export type Document = {
  478. id: string
  479. data_source_type: string
  480. name: string
  481. doc_type: DocType
  482. }
  483. export type HitTestingRecordsResponse = {
  484. data: HitTestingRecord[]
  485. has_more: boolean
  486. limit: number
  487. total: number
  488. page: number
  489. }
  490. export type TsnePosition = {
  491. x: number
  492. y: number
  493. }
  494. export type HitTestingResponse = {
  495. query: {
  496. content: string
  497. tsne_position: TsnePosition
  498. }
  499. records: Array<HitTesting>
  500. }
  501. export type ExternalKnowledgeBaseHitTestingResponse = {
  502. query: {
  503. content: string
  504. }
  505. records: Array<ExternalKnowledgeBaseHitTesting>
  506. }
  507. export type RelatedApp = {
  508. id: string
  509. name: string
  510. mode: AppMode
  511. icon_type: AppIconType | null
  512. icon: string
  513. icon_background: string
  514. icon_url: string
  515. }
  516. export type RelatedAppResponse = {
  517. data: Array<RelatedApp>
  518. total: number
  519. }
  520. export type SegmentUpdater = {
  521. content: string
  522. answer?: string
  523. keywords?: string[]
  524. regenerate_child_chunks?: boolean
  525. }
  526. export type ErrorDocsResponse = {
  527. data: IndexingStatusResponse[]
  528. total: number
  529. }
  530. export type SelectedDatasetsMode = {
  531. allHighQuality: boolean
  532. allHighQualityVectorSearch: boolean
  533. allHighQualityFullTextSearch: boolean
  534. allEconomic: boolean
  535. mixtureHighQualityAndEconomic: boolean
  536. allInternal: boolean
  537. allExternal: boolean
  538. mixtureInternalAndExternal: boolean
  539. inconsistentEmbeddingModel: boolean
  540. }
  541. export enum WeightedScoreEnum {
  542. SemanticFirst = 'semantic_first',
  543. KeywordFirst = 'keyword_first',
  544. Customized = 'customized',
  545. }
  546. export enum RerankingModeEnum {
  547. RerankingModel = 'reranking_model',
  548. WeightedScore = 'weighted_score',
  549. }
  550. export const DEFAULT_WEIGHTED_SCORE = {
  551. allHighQualityVectorSearch: {
  552. semantic: 1.0,
  553. keyword: 0,
  554. },
  555. allHighQualityFullTextSearch: {
  556. semantic: 0,
  557. keyword: 1.0,
  558. },
  559. other: {
  560. semantic: 0.7,
  561. keyword: 0.3,
  562. },
  563. }
  564. export type ChildChunkType = 'automatic' | 'customized'
  565. export type ChildChunkDetail = {
  566. id: string
  567. position: number
  568. segment_id: string
  569. content: string
  570. word_count: number
  571. created_at: number
  572. updated_at: number
  573. type: ChildChunkType
  574. }
  575. export type ChildSegmentsResponse = {
  576. data: ChildChunkDetail[]
  577. total: number
  578. total_pages: number
  579. page: number
  580. limit: number
  581. }
  582. export type UpdateDocumentParams = {
  583. datasetId: string
  584. documentId: string
  585. }
  586. // Used in api url
  587. export enum DocumentActionType {
  588. enable = 'enable',
  589. disable = 'disable',
  590. archive = 'archive',
  591. unArchive = 'un_archive',
  592. delete = 'delete',
  593. }
  594. export type UpdateDocumentBatchParams = {
  595. datasetId: string
  596. documentId?: string
  597. documentIds?: string[] | string
  598. }
  599. export type BatchImportResponse = {
  600. job_id: string
  601. job_status: string
  602. }