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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import { formatFileSize, formatNumber, formatTime } from '@/utils/format'
  4. import { ChunkingMode, type DocType } from '@/models/datasets'
  5. import useTimestamp from '@/hooks/use-timestamp'
  6. export type inputType = 'input' | 'select' | 'textarea'
  7. export type metadataType = DocType | 'originInfo' | 'technicalParameters'
  8. type MetadataMap
  9. = Record<
  10. metadataType,
  11. {
  12. text: string
  13. allowEdit?: boolean
  14. icon?: React.ReactNode
  15. iconName?: string
  16. subFieldsMap: Record<
  17. string,
  18. {
  19. label: string
  20. inputType?: inputType
  21. field?: string
  22. render?: (value: any, total?: number) => React.ReactNode | string
  23. }
  24. >
  25. }
  26. >
  27. const fieldPrefix = 'datasetDocuments.metadata.field'
  28. export const useMetadataMap = (): MetadataMap => {
  29. const { t } = useTranslation()
  30. const { formatTime: formatTimestamp } = useTimestamp()
  31. return {
  32. book: {
  33. text: t('datasetDocuments.metadata.type.book'),
  34. iconName: 'bookOpen',
  35. subFieldsMap: {
  36. title: { label: t(`${fieldPrefix}.book.title`) },
  37. language: {
  38. label: t(`${fieldPrefix}.book.language`),
  39. inputType: 'select',
  40. },
  41. author: { label: t(`${fieldPrefix}.book.author`) },
  42. publisher: { label: t(`${fieldPrefix}.book.publisher`) },
  43. publication_date: { label: t(`${fieldPrefix}.book.publicationDate`) },
  44. isbn: { label: t(`${fieldPrefix}.book.ISBN`) },
  45. category: {
  46. label: t(`${fieldPrefix}.book.category`),
  47. inputType: 'select',
  48. },
  49. },
  50. },
  51. web_page: {
  52. text: t('datasetDocuments.metadata.type.webPage'),
  53. iconName: 'globe',
  54. subFieldsMap: {
  55. 'title': { label: t(`${fieldPrefix}.webPage.title`) },
  56. 'url': { label: t(`${fieldPrefix}.webPage.url`) },
  57. 'language': {
  58. label: t(`${fieldPrefix}.webPage.language`),
  59. inputType: 'select',
  60. },
  61. 'author/publisher': { label: t(`${fieldPrefix}.webPage.authorPublisher`) },
  62. 'publish_date': { label: t(`${fieldPrefix}.webPage.publishDate`) },
  63. 'topic/keywords': { label: t(`${fieldPrefix}.webPage.topicKeywords`) },
  64. 'description': { label: t(`${fieldPrefix}.webPage.description`) },
  65. },
  66. },
  67. paper: {
  68. text: t('datasetDocuments.metadata.type.paper'),
  69. iconName: 'graduationHat',
  70. subFieldsMap: {
  71. 'title': { label: t(`${fieldPrefix}.paper.title`) },
  72. 'language': {
  73. label: t(`${fieldPrefix}.paper.language`),
  74. inputType: 'select',
  75. },
  76. 'author': { label: t(`${fieldPrefix}.paper.author`) },
  77. 'publish_date': { label: t(`${fieldPrefix}.paper.publishDate`) },
  78. 'journal/conference_name': {
  79. label: t(`${fieldPrefix}.paper.journalConferenceName`),
  80. },
  81. 'volume/issue/page_numbers': { label: t(`${fieldPrefix}.paper.volumeIssuePage`) },
  82. 'doi': { label: t(`${fieldPrefix}.paper.DOI`) },
  83. 'topic/keywords': { label: t(`${fieldPrefix}.paper.topicKeywords`) },
  84. 'abstract': {
  85. label: t(`${fieldPrefix}.paper.abstract`),
  86. inputType: 'textarea',
  87. },
  88. },
  89. },
  90. social_media_post: {
  91. text: t('datasetDocuments.metadata.type.socialMediaPost'),
  92. iconName: 'atSign',
  93. subFieldsMap: {
  94. 'platform': { label: t(`${fieldPrefix}.socialMediaPost.platform`) },
  95. 'author/username': {
  96. label: t(`${fieldPrefix}.socialMediaPost.authorUsername`),
  97. },
  98. 'publish_date': { label: t(`${fieldPrefix}.socialMediaPost.publishDate`) },
  99. 'post_url': { label: t(`${fieldPrefix}.socialMediaPost.postURL`) },
  100. 'topics/tags': { label: t(`${fieldPrefix}.socialMediaPost.topicsTags`) },
  101. },
  102. },
  103. personal_document: {
  104. text: t('datasetDocuments.metadata.type.personalDocument'),
  105. iconName: 'file',
  106. subFieldsMap: {
  107. 'title': { label: t(`${fieldPrefix}.personalDocument.title`) },
  108. 'author': { label: t(`${fieldPrefix}.personalDocument.author`) },
  109. 'creation_date': {
  110. label: t(`${fieldPrefix}.personalDocument.creationDate`),
  111. },
  112. 'last_modified_date': {
  113. label: t(`${fieldPrefix}.personalDocument.lastModifiedDate`),
  114. },
  115. 'document_type': {
  116. label: t(`${fieldPrefix}.personalDocument.documentType`),
  117. inputType: 'select',
  118. },
  119. 'tags/category': {
  120. label: t(`${fieldPrefix}.personalDocument.tagsCategory`),
  121. },
  122. },
  123. },
  124. business_document: {
  125. text: t('datasetDocuments.metadata.type.businessDocument'),
  126. iconName: 'briefcase',
  127. subFieldsMap: {
  128. 'title': { label: t(`${fieldPrefix}.businessDocument.title`) },
  129. 'author': { label: t(`${fieldPrefix}.businessDocument.author`) },
  130. 'creation_date': {
  131. label: t(`${fieldPrefix}.businessDocument.creationDate`),
  132. },
  133. 'last_modified_date': {
  134. label: t(`${fieldPrefix}.businessDocument.lastModifiedDate`),
  135. },
  136. 'document_type': {
  137. label: t(`${fieldPrefix}.businessDocument.documentType`),
  138. inputType: 'select',
  139. },
  140. 'department/team': {
  141. label: t(`${fieldPrefix}.businessDocument.departmentTeam`),
  142. },
  143. },
  144. },
  145. im_chat_log: {
  146. text: t('datasetDocuments.metadata.type.IMChat'),
  147. iconName: 'messageTextCircle',
  148. subFieldsMap: {
  149. 'chat_platform': { label: t(`${fieldPrefix}.IMChat.chatPlatform`) },
  150. 'chat_participants/group_name': {
  151. label: t(`${fieldPrefix}.IMChat.chatPartiesGroupName`),
  152. },
  153. 'start_date': { label: t(`${fieldPrefix}.IMChat.startDate`) },
  154. 'end_date': { label: t(`${fieldPrefix}.IMChat.endDate`) },
  155. 'participants': { label: t(`${fieldPrefix}.IMChat.participants`) },
  156. 'topicKeywords': {
  157. label: t(`${fieldPrefix}.IMChat.topicKeywords`),
  158. inputType: 'textarea',
  159. },
  160. 'fileType': { label: t(`${fieldPrefix}.IMChat.fileType`) },
  161. },
  162. },
  163. wikipedia_entry: {
  164. text: t('datasetDocuments.metadata.type.wikipediaEntry'),
  165. allowEdit: false,
  166. subFieldsMap: {
  167. 'title': { label: t(`${fieldPrefix}.wikipediaEntry.title`) },
  168. 'language': {
  169. label: t(`${fieldPrefix}.wikipediaEntry.language`),
  170. inputType: 'select',
  171. },
  172. 'web_page_url': { label: t(`${fieldPrefix}.wikipediaEntry.webpageURL`) },
  173. 'editor/contributor': {
  174. label: t(`${fieldPrefix}.wikipediaEntry.editorContributor`),
  175. },
  176. 'last_edit_date': {
  177. label: t(`${fieldPrefix}.wikipediaEntry.lastEditDate`),
  178. },
  179. 'summary/introduction': {
  180. label: t(`${fieldPrefix}.wikipediaEntry.summaryIntroduction`),
  181. inputType: 'textarea',
  182. },
  183. },
  184. },
  185. synced_from_notion: {
  186. text: t('datasetDocuments.metadata.type.notion'),
  187. allowEdit: false,
  188. subFieldsMap: {
  189. 'title': { label: t(`${fieldPrefix}.notion.title`) },
  190. 'language': { label: t(`${fieldPrefix}.notion.lang`), inputType: 'select' },
  191. 'author/creator': { label: t(`${fieldPrefix}.notion.author`) },
  192. 'creation_date': { label: t(`${fieldPrefix}.notion.createdTime`) },
  193. 'last_modified_date': {
  194. label: t(`${fieldPrefix}.notion.lastModifiedTime`),
  195. },
  196. 'notion_page_link': { label: t(`${fieldPrefix}.notion.url`) },
  197. 'category/tags': { label: t(`${fieldPrefix}.notion.tag`) },
  198. 'description': { label: t(`${fieldPrefix}.notion.desc`) },
  199. },
  200. },
  201. synced_from_github: {
  202. text: t('datasetDocuments.metadata.type.github'),
  203. allowEdit: false,
  204. subFieldsMap: {
  205. 'repository_name': { label: t(`${fieldPrefix}.github.repoName`) },
  206. 'repository_description': { label: t(`${fieldPrefix}.github.repoDesc`) },
  207. 'repository_owner/organization': { label: t(`${fieldPrefix}.github.repoOwner`) },
  208. 'code_filename': { label: t(`${fieldPrefix}.github.fileName`) },
  209. 'code_file_path': { label: t(`${fieldPrefix}.github.filePath`) },
  210. 'programming_language': { label: t(`${fieldPrefix}.github.programmingLang`) },
  211. 'github_link': { label: t(`${fieldPrefix}.github.url`) },
  212. 'open_source_license': { label: t(`${fieldPrefix}.github.license`) },
  213. 'commit_date': { label: t(`${fieldPrefix}.github.lastCommitTime`) },
  214. 'commit_author': {
  215. label: t(`${fieldPrefix}.github.lastCommitAuthor`),
  216. },
  217. },
  218. },
  219. originInfo: {
  220. text: '',
  221. allowEdit: false,
  222. subFieldsMap: {
  223. 'name': { label: t(`${fieldPrefix}.originInfo.originalFilename`) },
  224. 'data_source_info.upload_file.size': {
  225. label: t(`${fieldPrefix}.originInfo.originalFileSize`),
  226. render: value => formatFileSize(value),
  227. },
  228. 'created_at': {
  229. label: t(`${fieldPrefix}.originInfo.uploadDate`),
  230. render: value => formatTimestamp(value, t('datasetDocuments.metadata.dateTimeFormat') as string),
  231. },
  232. 'completed_at': {
  233. label: t(`${fieldPrefix}.originInfo.lastUpdateDate`),
  234. render: value => formatTimestamp(value, t('datasetDocuments.metadata.dateTimeFormat') as string),
  235. },
  236. 'data_source_type': {
  237. label: t(`${fieldPrefix}.originInfo.source`),
  238. render: value => t(`datasetDocuments.metadata.source.${value === 'notion_import' ? 'notion' : value}`),
  239. },
  240. },
  241. },
  242. technicalParameters: {
  243. text: t('datasetDocuments.metadata.type.technicalParameters'),
  244. allowEdit: false,
  245. subFieldsMap: {
  246. 'doc_form': {
  247. label: t(`${fieldPrefix}.technicalParameters.segmentSpecification`),
  248. render: (value) => {
  249. if (value === ChunkingMode.text)
  250. return t('dataset.chunkingMode.general')
  251. if (value === ChunkingMode.qa)
  252. return t('dataset.chunkingMode.qa')
  253. if (value === ChunkingMode.parentChild)
  254. return t('dataset.chunkingMode.parentChild')
  255. return '--'
  256. },
  257. },
  258. 'dataset_process_rule.rules.segmentation.max_tokens': {
  259. label: t(`${fieldPrefix}.technicalParameters.segmentLength`),
  260. render: value => formatNumber(value),
  261. },
  262. 'average_segment_length': {
  263. label: t(`${fieldPrefix}.technicalParameters.avgParagraphLength`),
  264. render: value => `${formatNumber(value)} characters`,
  265. },
  266. 'segment_count': {
  267. label: t(`${fieldPrefix}.technicalParameters.paragraphs`),
  268. render: value => `${formatNumber(value)} paragraphs`,
  269. },
  270. 'hit_count': {
  271. label: t(`${fieldPrefix}.technicalParameters.hitCount`),
  272. render: (value, total) => {
  273. const v = value || 0
  274. return `${!total ? 0 : ((v / total) * 100).toFixed(2)}% (${v}/${total})`
  275. },
  276. },
  277. 'indexing_latency': {
  278. label: t(`${fieldPrefix}.technicalParameters.embeddingTime`),
  279. render: value => formatTime(value),
  280. },
  281. 'tokens': {
  282. label: t(`${fieldPrefix}.technicalParameters.embeddedSpend`),
  283. render: value => `${formatNumber(value)} tokens`,
  284. },
  285. },
  286. },
  287. }
  288. }
  289. const langPrefix = 'datasetDocuments.metadata.languageMap.'
  290. export const useLanguages = () => {
  291. const { t } = useTranslation()
  292. return {
  293. zh: t(`${langPrefix}zh`),
  294. en: t(`${langPrefix}en`),
  295. es: t(`${langPrefix}es`),
  296. fr: t(`${langPrefix}fr`),
  297. de: t(`${langPrefix}de`),
  298. ja: t(`${langPrefix}ja`),
  299. ko: t(`${langPrefix}ko`),
  300. ru: t(`${langPrefix}ru`),
  301. ar: t(`${langPrefix}ar`),
  302. pt: t(`${langPrefix}pt`),
  303. it: t(`${langPrefix}it`),
  304. nl: t(`${langPrefix}nl`),
  305. pl: t(`${langPrefix}pl`),
  306. sv: t(`${langPrefix}sv`),
  307. tr: t(`${langPrefix}tr`),
  308. he: t(`${langPrefix}he`),
  309. hi: t(`${langPrefix}hi`),
  310. da: t(`${langPrefix}da`),
  311. fi: t(`${langPrefix}fi`),
  312. no: t(`${langPrefix}no`),
  313. hu: t(`${langPrefix}hu`),
  314. el: t(`${langPrefix}el`),
  315. cs: t(`${langPrefix}cs`),
  316. th: t(`${langPrefix}th`),
  317. id: t(`${langPrefix}id`),
  318. ro: t(`${langPrefix}ro`),
  319. }
  320. }
  321. const bookCategoryPrefix = 'datasetDocuments.metadata.categoryMap.book.'
  322. export const useBookCategories = () => {
  323. const { t } = useTranslation()
  324. return {
  325. fiction: t(`${bookCategoryPrefix}fiction`),
  326. biography: t(`${bookCategoryPrefix}biography`),
  327. history: t(`${bookCategoryPrefix}history`),
  328. science: t(`${bookCategoryPrefix}science`),
  329. technology: t(`${bookCategoryPrefix}technology`),
  330. education: t(`${bookCategoryPrefix}education`),
  331. philosophy: t(`${bookCategoryPrefix}philosophy`),
  332. religion: t(`${bookCategoryPrefix}religion`),
  333. socialSciences: t(`${bookCategoryPrefix}socialSciences`),
  334. art: t(`${bookCategoryPrefix}art`),
  335. travel: t(`${bookCategoryPrefix}travel`),
  336. health: t(`${bookCategoryPrefix}health`),
  337. selfHelp: t(`${bookCategoryPrefix}selfHelp`),
  338. businessEconomics: t(`${bookCategoryPrefix}businessEconomics`),
  339. cooking: t(`${bookCategoryPrefix}cooking`),
  340. childrenYoungAdults: t(`${bookCategoryPrefix}childrenYoungAdults`),
  341. comicsGraphicNovels: t(`${bookCategoryPrefix}comicsGraphicNovels`),
  342. poetry: t(`${bookCategoryPrefix}poetry`),
  343. drama: t(`${bookCategoryPrefix}drama`),
  344. other: t(`${bookCategoryPrefix}other`),
  345. }
  346. }
  347. const personalDocCategoryPrefix
  348. = 'datasetDocuments.metadata.categoryMap.personalDoc.'
  349. export const usePersonalDocCategories = () => {
  350. const { t } = useTranslation()
  351. return {
  352. notes: t(`${personalDocCategoryPrefix}notes`),
  353. blogDraft: t(`${personalDocCategoryPrefix}blogDraft`),
  354. diary: t(`${personalDocCategoryPrefix}diary`),
  355. researchReport: t(`${personalDocCategoryPrefix}researchReport`),
  356. bookExcerpt: t(`${personalDocCategoryPrefix}bookExcerpt`),
  357. schedule: t(`${personalDocCategoryPrefix}schedule`),
  358. list: t(`${personalDocCategoryPrefix}list`),
  359. projectOverview: t(`${personalDocCategoryPrefix}projectOverview`),
  360. photoCollection: t(`${personalDocCategoryPrefix}photoCollection`),
  361. creativeWriting: t(`${personalDocCategoryPrefix}creativeWriting`),
  362. codeSnippet: t(`${personalDocCategoryPrefix}codeSnippet`),
  363. designDraft: t(`${personalDocCategoryPrefix}designDraft`),
  364. personalResume: t(`${personalDocCategoryPrefix}personalResume`),
  365. other: t(`${personalDocCategoryPrefix}other`),
  366. }
  367. }
  368. const businessDocCategoryPrefix
  369. = 'datasetDocuments.metadata.categoryMap.businessDoc.'
  370. export const useBusinessDocCategories = () => {
  371. const { t } = useTranslation()
  372. return {
  373. meetingMinutes: t(`${businessDocCategoryPrefix}meetingMinutes`),
  374. researchReport: t(`${businessDocCategoryPrefix}researchReport`),
  375. proposal: t(`${businessDocCategoryPrefix}proposal`),
  376. employeeHandbook: t(`${businessDocCategoryPrefix}employeeHandbook`),
  377. trainingMaterials: t(`${businessDocCategoryPrefix}trainingMaterials`),
  378. requirementsDocument: t(`${businessDocCategoryPrefix}requirementsDocument`),
  379. designDocument: t(`${businessDocCategoryPrefix}designDocument`),
  380. productSpecification: t(`${businessDocCategoryPrefix}productSpecification`),
  381. financialReport: t(`${businessDocCategoryPrefix}financialReport`),
  382. marketAnalysis: t(`${businessDocCategoryPrefix}marketAnalysis`),
  383. projectPlan: t(`${businessDocCategoryPrefix}projectPlan`),
  384. teamStructure: t(`${businessDocCategoryPrefix}teamStructure`),
  385. policiesProcedures: t(`${businessDocCategoryPrefix}policiesProcedures`),
  386. contractsAgreements: t(`${businessDocCategoryPrefix}contractsAgreements`),
  387. emailCorrespondence: t(`${businessDocCategoryPrefix}emailCorrespondence`),
  388. other: t(`${businessDocCategoryPrefix}other`),
  389. }
  390. }