index.vue 16 KB

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