index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <template>
  2. <div class="rightBox" v-loading="loading">
  3. <!-- <div class="title">
  4. <div class="c_info_title">创建表单内容</div>
  5. </div> -->
  6. <div class="edit_top">
  7. <div class="e_t_left">
  8. <el-button type="primary" size="small" @click="useAiBtn()"
  9. >按照文档创建</el-button
  10. >
  11. <el-button :type="viewFile?'primary':''" size="small" v-if="fileData" @click="viewUploadFile()"
  12. >预览</el-button
  13. >
  14. </div>
  15. <div class="edit_btn">
  16. <span class="edit" :class="{ active: type == 1 }" @click="type = 1"
  17. >编辑</span
  18. >
  19. <span class="check" :class="{ active: type == 2 }" @click="type = 2"
  20. >预览</span
  21. >
  22. <!-- <span :class="{ active: type == 3 }" @click="type = 3">回答</span>
  23. <span :class="{ active: type == 4 }" @click="type = 4">统计</span> -->
  24. </div>
  25. <div class="op_btn">
  26. <!-- <el-button type="primary" size="small" @click="lastSteps">上一步</el-button> -->
  27. <el-button type="primary" size="small" @click="save">保存</el-button>
  28. <el-button type="primary" size="small" @click="publish">发布</el-button>
  29. </div>
  30. </div>
  31. <div class="e_box">
  32. <editBox
  33. v-if="type == 1"
  34. :checkJson="checkJson"
  35. @changeJson="changeJson"
  36. :title="title"
  37. :brief="brief"
  38. :fileData="fileData"
  39. :viewFile="viewFile"
  40. @updateTitle="updateTitle"
  41. @updateBrief="updateBrief"
  42. ></editBox>
  43. <checkBox v-if="type == 2" :cJson="checkJson" :title="title"></checkBox>
  44. </div>
  45. <div class="ajaxCancel" v-if="loading && ajaxCancelToken">
  46. <el-button type="primary" @click.stop="cancelAjax">停止生成</el-button>
  47. </div>
  48. </div>
  49. </template>
  50. <script>
  51. import editBox from "./edit/index.vue";
  52. import checkBox from "./check/index.vue";
  53. import { v4 as uuidv4 } from "uuid";
  54. import ConvertApi from 'convertapi-js'
  55. let convertApi = ConvertApi.auth('secret_8jZzewWvAJluEjTR')
  56. export default {
  57. components: {
  58. editBox,
  59. checkBox
  60. },
  61. props: {
  62. title: {
  63. type: String
  64. },
  65. brief: {
  66. type: String
  67. },
  68. testType: {
  69. type: Array
  70. },
  71. see: {
  72. type: Boolean
  73. },
  74. steps: {
  75. type: Number
  76. },
  77. cJson: {
  78. type: Array
  79. },
  80. fileData: {
  81. type: Object
  82. }
  83. },
  84. data() {
  85. return {
  86. type: 1,
  87. checkJson: [],
  88. loading: false,
  89. userId: this.$route.query.userid,
  90. viewFile:false,
  91. ajaxCancelToken:null,
  92. };
  93. },
  94. watch: {
  95. cJson: {
  96. handler: function(newVal, oldVal) {
  97. this.checkJson = this.depthCopy(newVal);
  98. },
  99. deep: true
  100. },
  101. },
  102. methods: {
  103. updateTitle(value) {
  104. console.log(value);
  105. this.$emit("update:title", value);
  106. },
  107. updateBrief(value) {
  108. this.$emit("update:brief", value);
  109. },
  110. updateFileData(value){
  111. this.$emit("update:fileData",value)
  112. },
  113. lastSteps() {
  114. this.$emit("update:steps", this.steps - 1);
  115. },
  116. save() {
  117. this.$emit("save", 4);
  118. },
  119. publish() {
  120. this.$emit("publish");
  121. },
  122. depthCopy(s) {
  123. return s ? JSON.parse(JSON.stringify(s)) : "";
  124. },
  125. changeJson(json) {
  126. console.log(json);
  127. this.$emit("update:cJson", json);
  128. },
  129. async useAiBtn(type = 0) {
  130. if (type == 0 && this.checkJson.length > 0) {
  131. return this.$confirm(
  132. "使用智能检索功能会覆盖已有表单,确定使用吗?",
  133. "提示",
  134. {
  135. confirmButtonText: "确定",
  136. cancelButtonText: "取消",
  137. type: "warning"
  138. }
  139. )
  140. .then(() => {
  141. this.useAiBtn(1);
  142. })
  143. .catch(() => {
  144. console.log("不使用");
  145. });
  146. }
  147. let input = document.createElement("input");
  148. input.type = "file";
  149. // input.accept = ".wav";
  150. // input.accept = "audio/*, .txt, .pdf, .xlsx";
  151. input.accept = ".docx,.doc";
  152. input.click();
  153. input.onchange = async () => {
  154. this.loading = true;
  155. let file = input.files[0];
  156. if(/\.(doc)$/i.test(file.name)){
  157. let params = convertApi.createParams()
  158. params.add('file', file)
  159. let result = await convertApi.convert('doc', 'docx', params)
  160. // Get result file URL
  161. file = await fetch(result.files[0].Url).then(res => res.blob()).then(blob => {
  162. return new File([blob], result.files[0].FileName, { type: 'application/octet-stream' });
  163. });
  164. }
  165. console.log("file",file)
  166. if (!/\.(docx)$/i.test(file.name)) {
  167. this.loading = false;
  168. return this.$message.error("请上传.docx格式的文件");
  169. }
  170. let uploadData = await this.uploadFile(file);
  171. if (uploadData == 1) {
  172. this.loading = false;
  173. return this.$message.error("文件上传失败");
  174. }
  175. let _fileData = {
  176. fileName: uploadData.Key,
  177. url: uploadData.Location,
  178. fileId: await this.fileGetFileId(uploadData.Location)
  179. };
  180. if (_fileData.fileId == 1) {
  181. this.loading = false;
  182. return this.$message.error("文件获取fileId失败");
  183. }
  184. let testData = await this.getCheckJSon(_fileData.fileId);
  185. if(testData==2){
  186. this.loading = false;
  187. return;
  188. }
  189. if (testData == 1) {
  190. this.loading = false;
  191. return this.$message.error("生成题目失败");
  192. }
  193. let _titleData = testData.find(i=>(i.formName || i.illustrate))
  194. testData = testData.filter(i=>!(i.formName || i.illustrate))
  195. this.fileData = _fileData;
  196. this.updateFileData(_fileData);
  197. if(_titleData){
  198. this.updateTitle(_titleData.formName)
  199. this.updateBrief(_titleData.illustrate)
  200. }
  201. this.changeJson(testData)
  202. this.viewFile = true;
  203. console.log(_fileData);
  204. console.log(_titleData)
  205. console.log(testData)
  206. this.loading = false;
  207. // this.uploadWavFileAndGetText(file);
  208. };
  209. },
  210. uploadFile(file) {
  211. return new Promise(resolve => {
  212. var credentials = {
  213. accessKeyId: "AKIATLPEDU37QV5CHLMH",
  214. secretAccessKey: "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"
  215. }; //秘钥形式的登录上传
  216. window.AWS.config.update(credentials);
  217. window.AWS.config.region = "cn-northwest-1"; //设置区域
  218. var bucket = new window.AWS.S3({ params: { Bucket: "ccrb" } }); //选择桶
  219. var _this = this;
  220. if (file) {
  221. var params = {
  222. Key:
  223. file.name.split(".")[0] +
  224. new Date().getTime() +
  225. "." +
  226. file.name.split(".")[file.name.split(".").length - 1],
  227. ContentType: file.type,
  228. Body: file,
  229. "Access-Control-Allow-Credentials": "*",
  230. ACL: "public-read"
  231. }; //key可以设置为桶的相抵路径,Body为文件, ACL最好要设置
  232. var options = {
  233. partSize: 2048 * 1024 * 1024,
  234. queueSize: 2,
  235. leavePartsOnError: true
  236. };
  237. bucket
  238. .upload(params, options)
  239. .on("httpUploadProgress", function(evt) {
  240. //这里可以写进度条
  241. // _this.progressData.value = parseInt((evt.loaded * 100) / evt.total);
  242. // console.log("Uploaded : " + parseInt((evt.loaded * 80) / evt.total) + '%');
  243. })
  244. .send(function(err, data) {
  245. if (err) {
  246. resolve(1);
  247. } else {
  248. resolve(data);
  249. }
  250. });
  251. }
  252. });
  253. },
  254. fileGetFileId(url) {
  255. return new Promise(resolve => {
  256. this.ajax
  257. .put("https://gpt4.cocorobo.cn/upload_file_knowledge", { url: url })
  258. .then(res => {
  259. let _data = res.data.FunctionResponse;
  260. if (_data.result && _data.result.id) {
  261. resolve(_data.result.id);
  262. } else {
  263. resolve(1);
  264. }
  265. })
  266. .catch(e => {
  267. console.log(e);
  268. resolve(1);
  269. });
  270. });
  271. },
  272. getCheckJSon(fileId) {
  273. return new Promise(resolve => {
  274. let _msg = `Language: Please use the same language as the user requirement, if the user speaks Chinese, the specific text of your answer should also be in Chinese.
  275. ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example".
  276. Instruction: Based on the context, follow "Format example", write content
  277. #Context
  278. ##角色描述
  279. 你是一位行政文员,你需要提取文档中需要收集的信息,梳理出需要收集的信息表格,确定信息收表的题目内容与题目类型。
  280. ##任务描述
  281. 你的任务是阅读上传文档中的内容,提取并输出文档中需要填写的信息,并输出收集信息表单,输出格式为## Format example。你的任务分为三步:
  282. 第一步:提取文档中需要收集的信息内容。信息内容使用文件中的原文,并直接“## Format example”的形式罗列出需要收集的信息清单,不需要二次的分类和总结。通常提取文档中表格中的内容,其他描述性的文字、项目背景、审核意见等内容不应该出现在需要收集的表单内。
  283. 第二步:判断这些需要需要收集的信息以什么题目类型的进行收集,除“年 月 日/日期”等内容使用日期外,其他题目都默认使用文本。
  284. 第三步:输出信息收集表单,你仅仅只需要输出一份## Format example格式的数据,不需要增加任何描述性文字。
  285. ##信息补充
  286. 表单的题目类型包含问答、单选、多选
  287. 文本:需要收集的信息为文本格式的内容,例如“姓名、年级、项目经历、获奖等文本内容”
  288. 单选:需要收集的信息提供了选项,例如文档的表格中出现“性别 男 女” 。或文档中某项收集信息中提供了“是 否”或其他具有选择符号的内容,需要关联上下文判断。
  289. 日期:文档中单独出现需要收集的信息中设计到“年 月 日”“日期”“时间”等,与日期相关的内容。如果文档中出现多个日期相关的题目,只保留一个日期题目类型即可。
  290. 标题:文档中的总标题
  291. 说明:文档中的总标题下的说明性文字
  292. ##题目格式
  293. 问答题:{"ttype":1,"type":3,"json":{"title":"标题","type":1,"answer":""}}
  294. 单选题:{"ttype":1,"type":1,"json":{"title":"标题","type":1,"array":[{"option":"选项1","img":""}, {"option":"选项2","img":""}],"answer":""}}
  295. 多选题:{"ttype":1,"type":1,"json":{"title":"标题","type":2,"array":[{"option":"选项1","img":""},{"option":"选项2","img":""}],"answer":""}}
  296. 标题和说明:{"formName":"标题","illustrate":"说明"}
  297. ## Format example
  298. [{"formName":"标题","illustrate":"说明"},{"ttype":1,"type":3,"json":{"title":"标题","type":1,"answer":""}},{"ttype":1,"type":1,"json":{"title":"标题","type":1,"array":[{"option":"选项1","img":""},{"option":"选项2","img":""}],"answer":""}},{"ttype":1,"type":1,"json":{"title":"标题","type":2,"array":[{"option":"选项1","img":""},{"option":"选项2","img":""}],"answer":""}}]
  299. `;
  300. // 日期:{"ttype":1,"type":8,"json":{"title":"标题","detail":""}}
  301. let params = {
  302. assistant_id: "6063369f-289a-11ef-8bf4-12e77c4cb76b",
  303. message: [{ type: "text", text: _msg }],
  304. session_name: uuidv4(),
  305. userId: this.userId,
  306. file_ids: [fileId],
  307. model: "gpt-4o-2024-08-06"
  308. // model: "gpt-4o-2024-08-06"
  309. };
  310. console.log(params)
  311. this.ajaxCancelToken = this.ajax.setCancelSource()
  312. this.ajax
  313. // .post("https://gpt4.cocorobo.cn/chat", params)
  314. // .post("https://claude3.cocorobo.cn/chat", params)
  315. .post("https://gpt4.cocorobo.cn/ai_agent_park_chat", params,this.ajaxCancelToken)
  316. .then(res => {
  317. let _data = res.data.FunctionResponse.message;
  318. _data = _data.replaceAll("```json", "").replaceAll("```", "");
  319. // const match = _data.match(/\[\s*\{[\s\S]*?\}\s*\]/);
  320. // console.log("👇")
  321. // console.log(match[0])
  322. let _result = JSON.parse(_data) || [];
  323. if (_result.length > 0) {
  324. resolve(_result);
  325. } else {
  326. resolve(1);
  327. }
  328. this.ajaxCancelToken = null;
  329. })
  330. .catch(e => {
  331. console.log(e);
  332. this.ajaxCancelToken = null;
  333. resolve(2);
  334. });
  335. });
  336. },
  337. viewUploadFile(){
  338. this.viewFile = !this.viewFile;
  339. },
  340. cancelAjax(){
  341. if(this.ajaxCancelToken){
  342. this.ajaxCancelToken.cancel('Request canceled by the user.');
  343. this.ajaxCancelToken = null;
  344. this.$message.info("已停止文档生成");
  345. this.loading = false;
  346. }
  347. }
  348. },
  349. mounted() {
  350. this.checkJson = this.depthCopy(this.cJson);
  351. if(this.fileData){
  352. this.viewFile = true;
  353. }
  354. }
  355. };
  356. </script>
  357. <style scoped>
  358. .c_info_title {
  359. padding: 15px 0 15px 0;
  360. font-size: 16px;
  361. font-weight: bold;
  362. margin: 0 0 0 0;
  363. box-sizing: border-box;
  364. display: flex;
  365. align-items: center;
  366. line-height: 20px;
  367. }
  368. .c_info_title::before {
  369. content: "";
  370. display: block;
  371. width: 3px;
  372. height: 20px;
  373. background: #0061ff;
  374. border-radius: 3px;
  375. margin: 0 5px 0 0;
  376. }
  377. .rightBox {
  378. width: calc(100%);
  379. /* background: #F0F2F5; */
  380. /* background: #fff; */
  381. overflow: auto;
  382. height: calc(100%);
  383. margin: 0 auto;
  384. position: relative;
  385. box-sizing: border-box;
  386. }
  387. .rightBox > .title {
  388. background: #fff;
  389. width: 100%;
  390. padding: 0 20px 0;
  391. box-sizing: border-box;
  392. }
  393. .edit_top {
  394. height: 50px;
  395. background: #fff;
  396. display: flex;
  397. align-items: center;
  398. justify-content: center;
  399. position: relative;
  400. }
  401. .edit_top > .edit_btn {
  402. display: flex;
  403. height: 40px;
  404. align-items: center;
  405. border: 1px solid #e5e5e5;
  406. box-sizing: border-box;
  407. padding: 4px;
  408. border-radius: 4px;
  409. }
  410. .edit_top > .edit_btn > span {
  411. cursor: pointer;
  412. padding-bottom: 5px;
  413. display: block;
  414. width: 90px;
  415. padding: 0 15px;
  416. height: 100%;
  417. font-size: 14px;
  418. box-sizing: border-box;
  419. display: flex;
  420. align-items: center;
  421. justify-content: center;
  422. border-radius: 4px;
  423. }
  424. .edit_top > .edit_btn > .active {
  425. color: #3e88f4;
  426. /* border-bottom: 2px solid #2f80f3; */
  427. background: rgb(224, 234, 251);
  428. }
  429. .edit_top > .edit_btn > span + span {
  430. margin-left: 10px;
  431. }
  432. .edit_top > .edit_btn > span::before {
  433. content: "";
  434. display: block;
  435. background-size: 100% 100%;
  436. margin-right: 8px;
  437. }
  438. .edit_top > .edit_btn > .check::before {
  439. width: 15px;
  440. height: 15px;
  441. background-image: url(../../../../../assets/icon/test/add_check_icon.png);
  442. }
  443. .edit_top > .edit_btn > .edit::before {
  444. width: 15px;
  445. height: 16px;
  446. background-image: url(../../../../../assets/icon/test/add_edit_icon.png);
  447. }
  448. .edit_top > .edit_btn > .active.check::before {
  449. background-image: url(../../../../../assets/icon/test/add_check_icon_active.png);
  450. }
  451. .edit_top > .edit_btn > .active.edit::before {
  452. background-image: url(../../../../../assets/icon/test/add_edit_icon_active.png);
  453. }
  454. .edit_top > .op_btn {
  455. position: absolute;
  456. right: 30px;
  457. }
  458. .e_box {
  459. height: calc(100% - 90px);
  460. width: calc(100% - 40px);
  461. overflow: hidden;
  462. /* background: rgb(196, 226, 241); */
  463. /* background: #fff; */
  464. /* border: 1px solid #E5E5E5; */
  465. -webkit-box-sizing: border-box;
  466. box-sizing: border-box;
  467. margin: 20px auto 0;
  468. border-radius: 5px;
  469. }
  470. .e_t_left {
  471. position: absolute;
  472. left: 30px;
  473. }
  474. .ajaxCancel{
  475. position: fixed;
  476. top: calc(50vh + 100px);
  477. left: 50vw;
  478. transform: translate(-50%,-50%);
  479. z-index: 9999;
  480. width: 150px;
  481. height: 80px;
  482. background: #fff;
  483. border-radius: 5px;
  484. display: flex;
  485. flex-direction: column;
  486. align-items: center;
  487. justify-content: center;
  488. }
  489. </style>