You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

datasets.ts 17KB

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