document.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. from drf_spectacular.types import OpenApiTypes
  2. from drf_spectacular.utils import OpenApiParameter
  3. from common.mixins.api_mixin import APIMixin
  4. from common.result import DefaultResultSerializer
  5. from knowledge.serializers.common import BatchSerializer
  6. from knowledge.serializers.document import DocumentInstanceSerializer, DocumentWebInstanceSerializer, \
  7. CancelInstanceSerializer, BatchCancelInstanceSerializer, DocumentRefreshSerializer, BatchEditHitHandlingSerializer, \
  8. DocumentBatchRefreshSerializer, DocumentBatchGenerateRelatedSerializer, DocumentMigrateSerializer
  9. class DocumentSplitAPI(APIMixin):
  10. @staticmethod
  11. def get_parameters():
  12. return [
  13. OpenApiParameter(
  14. name="workspace_id",
  15. description="工作空间id",
  16. type=OpenApiTypes.STR,
  17. location='path',
  18. required=True,
  19. ),
  20. ]
  21. @staticmethod
  22. def get_request():
  23. return {
  24. 'multipart/form-data': {
  25. 'type': 'object',
  26. 'properties': {
  27. 'file': {
  28. 'type': 'string',
  29. 'format': 'binary' # Tells Swagger it's a file
  30. },
  31. 'limit': {
  32. 'type': 'integer',
  33. 'description': '分段长度'
  34. },
  35. 'patterns': {
  36. 'type': 'string',
  37. 'description': '分段正则列表'
  38. },
  39. 'with_filter': {
  40. 'type': 'boolean',
  41. 'description': '是否清除特殊字符'
  42. }
  43. }
  44. }
  45. }
  46. class DocumentBatchAPI(APIMixin):
  47. @staticmethod
  48. def get_parameters():
  49. return [
  50. OpenApiParameter(
  51. name="workspace_id",
  52. description="工作空间id",
  53. type=OpenApiTypes.STR,
  54. location='path',
  55. required=True,
  56. ),
  57. OpenApiParameter(
  58. name="knowledge_id",
  59. description="知识库id",
  60. type=OpenApiTypes.STR,
  61. location='path',
  62. required=True,
  63. ),
  64. ]
  65. @staticmethod
  66. def get_request():
  67. return BatchSerializer
  68. @staticmethod
  69. def get_response():
  70. return DefaultResultSerializer
  71. class DocumentBatchCreateAPI(APIMixin):
  72. @staticmethod
  73. def get_parameters():
  74. return [
  75. OpenApiParameter(
  76. name="workspace_id",
  77. description="工作空间id",
  78. type=OpenApiTypes.STR,
  79. location='path',
  80. required=True,
  81. ),
  82. OpenApiParameter(
  83. name="knowledge_id",
  84. description="知识库id",
  85. type=OpenApiTypes.STR,
  86. location='path',
  87. required=True,
  88. ),
  89. ]
  90. @staticmethod
  91. def get_request():
  92. return DocumentInstanceSerializer(many=True)
  93. @staticmethod
  94. def get_response():
  95. return DefaultResultSerializer
  96. class DocumentCreateAPI(APIMixin):
  97. @staticmethod
  98. def get_parameters():
  99. return [
  100. OpenApiParameter(
  101. name="workspace_id",
  102. description="工作空间id",
  103. type=OpenApiTypes.STR,
  104. location='path',
  105. required=True,
  106. ),
  107. OpenApiParameter(
  108. name="knowledge_id",
  109. description="知识库id",
  110. type=OpenApiTypes.STR,
  111. location='path',
  112. required=True,
  113. ),
  114. ]
  115. @staticmethod
  116. def get_request():
  117. return DocumentInstanceSerializer
  118. @staticmethod
  119. def get_response():
  120. return DefaultResultSerializer
  121. class DocumentReadAPI(APIMixin):
  122. @staticmethod
  123. def get_parameters():
  124. return [
  125. OpenApiParameter(
  126. name="workspace_id",
  127. description="工作空间id",
  128. type=OpenApiTypes.STR,
  129. location='path',
  130. required=True,
  131. ),
  132. OpenApiParameter(
  133. name="knowledge_id",
  134. description="知识库id",
  135. type=OpenApiTypes.STR,
  136. location='path',
  137. required=True,
  138. ),
  139. OpenApiParameter(
  140. name="document_id",
  141. description="文档id",
  142. type=OpenApiTypes.STR,
  143. location='path',
  144. required=True,
  145. ),
  146. ]
  147. @staticmethod
  148. def get_response():
  149. return DefaultResultSerializer
  150. class DocumentEditAPI(DocumentReadAPI):
  151. @staticmethod
  152. def get_request():
  153. return DocumentInstanceSerializer
  154. class DocumentDeleteAPI(DocumentReadAPI):
  155. pass
  156. class TableDocumentCreateAPI(APIMixin):
  157. @staticmethod
  158. def get_parameters():
  159. return [
  160. OpenApiParameter(
  161. name="workspace_id",
  162. description="工作空间id",
  163. type=OpenApiTypes.STR,
  164. location='path',
  165. required=True,
  166. ),
  167. OpenApiParameter(
  168. name="knowledge_id",
  169. description="知识库id",
  170. type=OpenApiTypes.STR,
  171. location='path',
  172. required=True,
  173. ),
  174. ]
  175. @staticmethod
  176. def get_request():
  177. return {
  178. 'multipart/form-data': {
  179. 'type': 'object',
  180. 'properties': {
  181. 'file': {
  182. 'type': 'string',
  183. 'format': 'binary' # Tells Swagger it's a file
  184. }
  185. }
  186. }
  187. }
  188. @staticmethod
  189. def get_response():
  190. return DefaultResultSerializer
  191. class QaDocumentCreateAPI(TableDocumentCreateAPI):
  192. pass
  193. class WebDocumentCreateAPI(APIMixin):
  194. @staticmethod
  195. def get_request():
  196. return DocumentWebInstanceSerializer
  197. class CancelTaskAPI(DocumentReadAPI):
  198. @staticmethod
  199. def get_request():
  200. return CancelInstanceSerializer
  201. class BatchCancelTaskAPI(DocumentReadAPI):
  202. @staticmethod
  203. def get_request():
  204. return BatchCancelInstanceSerializer
  205. class SyncWebAPI(DocumentReadAPI):
  206. pass
  207. class RefreshAPI(DocumentReadAPI):
  208. @staticmethod
  209. def get_request():
  210. return DocumentRefreshSerializer
  211. class BatchEditHitHandlingAPI(APIMixin):
  212. @staticmethod
  213. def get_parameters():
  214. return [
  215. OpenApiParameter(
  216. name="workspace_id",
  217. description="工作空间id",
  218. type=OpenApiTypes.STR,
  219. location='path',
  220. required=True,
  221. ),
  222. OpenApiParameter(
  223. name="knowledge_id",
  224. description="知识库id",
  225. type=OpenApiTypes.STR,
  226. location='path',
  227. required=True,
  228. ),
  229. ]
  230. @staticmethod
  231. def get_request():
  232. return BatchEditHitHandlingSerializer
  233. class DocumentTreeReadAPI(APIMixin):
  234. @staticmethod
  235. def get_parameters():
  236. return [
  237. OpenApiParameter(
  238. name="workspace_id",
  239. description="工作空间id",
  240. type=OpenApiTypes.STR,
  241. location='path',
  242. required=True,
  243. ),
  244. OpenApiParameter(
  245. name="knowledge_id",
  246. description="知识库id",
  247. type=OpenApiTypes.STR,
  248. location='path',
  249. required=True,
  250. ),
  251. OpenApiParameter(
  252. name="folder_id",
  253. description="文件夹id",
  254. type=OpenApiTypes.STR,
  255. location='query',
  256. required=False,
  257. ),
  258. OpenApiParameter(
  259. name="user_id",
  260. description="用户id",
  261. type=OpenApiTypes.STR,
  262. location='query',
  263. required=False,
  264. ),
  265. OpenApiParameter(
  266. name="name",
  267. description="名称",
  268. type=OpenApiTypes.STR,
  269. location='query',
  270. required=False,
  271. ),
  272. OpenApiParameter(
  273. name="desc",
  274. description="描述",
  275. type=OpenApiTypes.STR,
  276. location='query',
  277. required=False,
  278. ),
  279. ]
  280. class DocumentSplitPatternAPI(APIMixin):
  281. @staticmethod
  282. def get_parameters():
  283. return [
  284. OpenApiParameter(
  285. name="workspace_id",
  286. description="工作空间id",
  287. type=OpenApiTypes.STR,
  288. location='path',
  289. required=True,
  290. ),
  291. OpenApiParameter(
  292. name="knowledge_id",
  293. description="知识库id",
  294. type=OpenApiTypes.STR,
  295. location='path',
  296. required=True,
  297. ),
  298. ]
  299. @staticmethod
  300. def get_response():
  301. return DefaultResultSerializer
  302. class BatchRefreshAPI(APIMixin):
  303. @staticmethod
  304. def get_parameters():
  305. return [
  306. OpenApiParameter(
  307. name="workspace_id",
  308. description="工作空间id",
  309. type=OpenApiTypes.STR,
  310. location='path',
  311. required=True,
  312. ),
  313. OpenApiParameter(
  314. name="knowledge_id",
  315. description="知识库id",
  316. type=OpenApiTypes.STR,
  317. location='path',
  318. required=True,
  319. ),
  320. ]
  321. @staticmethod
  322. def get_request():
  323. return DocumentBatchRefreshSerializer
  324. class BatchGenerateRelatedAPI(APIMixin):
  325. @staticmethod
  326. def get_parameters():
  327. return [
  328. OpenApiParameter(
  329. name="workspace_id",
  330. description="工作空间id",
  331. type=OpenApiTypes.STR,
  332. location='path',
  333. required=True,
  334. ),
  335. OpenApiParameter(
  336. name="knowledge_id",
  337. description="知识库id",
  338. type=OpenApiTypes.STR,
  339. location='path',
  340. required=True,
  341. ),
  342. ]
  343. @staticmethod
  344. def get_request():
  345. return DocumentBatchGenerateRelatedSerializer
  346. class TemplateExportAPI(APIMixin):
  347. @staticmethod
  348. def get_parameters():
  349. return [
  350. OpenApiParameter(
  351. name="workspace_id",
  352. description="工作空间id",
  353. type=OpenApiTypes.STR,
  354. location='path',
  355. required=True,
  356. ),
  357. OpenApiParameter(
  358. name="knowledge_id",
  359. description="知识库id",
  360. type=OpenApiTypes.STR,
  361. location='path',
  362. required=True,
  363. ),
  364. OpenApiParameter(
  365. name="type",
  366. description="Export template type csv|excel",
  367. type=OpenApiTypes.STR,
  368. location='query',
  369. required=True,
  370. ),
  371. ]
  372. @staticmethod
  373. def get_response():
  374. return DefaultResultSerializer
  375. class DocumentExportAPI(APIMixin):
  376. @staticmethod
  377. def get_parameters():
  378. return [
  379. OpenApiParameter(
  380. name="workspace_id",
  381. description="工作空间id",
  382. type=OpenApiTypes.STR,
  383. location='path',
  384. required=True,
  385. ),
  386. OpenApiParameter(
  387. name="knowledge_id",
  388. description="知识库id",
  389. type=OpenApiTypes.STR,
  390. location='path',
  391. required=True,
  392. ),
  393. OpenApiParameter(
  394. name="document_id",
  395. description="文档id",
  396. type=OpenApiTypes.STR,
  397. location='path',
  398. required=True,
  399. ),
  400. ]
  401. @staticmethod
  402. def get_response():
  403. return DefaultResultSerializer
  404. class DocumentMigrateAPI(APIMixin):
  405. @staticmethod
  406. def get_parameters():
  407. return [
  408. OpenApiParameter(
  409. name="workspace_id",
  410. description="工作空间id",
  411. type=OpenApiTypes.STR,
  412. location='path',
  413. required=True,
  414. ),
  415. OpenApiParameter(
  416. name="knowledge_id",
  417. description="知识库id",
  418. type=OpenApiTypes.STR,
  419. location='path',
  420. required=True,
  421. ),
  422. OpenApiParameter(
  423. name="target_knowledge_id",
  424. description="目标知识库id",
  425. type=OpenApiTypes.STR,
  426. location='path',
  427. required=True,
  428. ),
  429. ]
  430. @staticmethod
  431. def get_request():
  432. return DocumentMigrateSerializer
  433. class DocumentDownloadSourceAPI(APIMixin):
  434. @staticmethod
  435. def get_parameters():
  436. return [
  437. OpenApiParameter(
  438. name="workspace_id",
  439. description="工作空间id",
  440. type=OpenApiTypes.STR,
  441. location='path',
  442. required=True,
  443. ),
  444. OpenApiParameter(
  445. name="knowledge_id",
  446. description="知识库id",
  447. type=OpenApiTypes.STR,
  448. location='path',
  449. required=True,
  450. ),
  451. OpenApiParameter(
  452. name="document_id",
  453. description="文档id",
  454. type=OpenApiTypes.STR,
  455. location='path',
  456. required=True,
  457. ),
  458. ]
  459. @staticmethod
  460. def get_response():
  461. return DefaultResultSerializer
  462. class DocumentTagsAPI(APIMixin):
  463. @staticmethod
  464. def get_parameters():
  465. return [
  466. OpenApiParameter(
  467. name="workspace_id",
  468. description="工作空间id",
  469. type=OpenApiTypes.STR,
  470. location='path',
  471. required=True,
  472. ),
  473. OpenApiParameter(
  474. name="knowledge_id",
  475. description="知识库id",
  476. type=OpenApiTypes.STR,
  477. location='path',
  478. required=True,
  479. ),
  480. OpenApiParameter(
  481. name="document_id",
  482. description="文档id",
  483. type=OpenApiTypes.STR,
  484. location='path',
  485. required=True,
  486. ),
  487. ]
  488. @staticmethod
  489. def get_request():
  490. return None
  491. @staticmethod
  492. def get_response():
  493. return DefaultResultSerializer