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 16KB

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