11wqe1 1 bulan lalu
induk
melakukan
4412ecbaca

+ 142 - 44
src/components/classRoomHelper/component/languageAssistant.vue

@@ -173,6 +173,8 @@
 <script>
 import { v4 as uuidv4 } from "uuid";
 import MarkdownIt from "markdown-it";
+import { fetchEventSource } from "@microsoft/fetch-event-source";
+
 export default {
   props: {},
   data() {
@@ -193,6 +195,8 @@ export default {
       openMessage: true,
 			canTalk:true,
 			loading:false,
+      curRequestController:null,
+
     };
   },
   computed: {
@@ -321,54 +325,148 @@ export default {
                     session_name: _uuid + "-qgt",
                     uid: _uuid,
                     file_ids: [],
-                    model: "gpt-4o-2024-11-20"
+                    model: "gpt-4o-2024-11-20",
+                    stream:true
                   };
-                  this.ajax
-                    // .post("https://claude3.cocorobo.cn/chat", params)
-                    // .post("https://gpt4.cocorobo.cn/chat", params)
-                    .post(
-                      "https://gpt4.cocorobo.cn/ai_agent_park_chat_new",
-                      params
-                    )
-                    .then(res => {
-                      if (
-                        converter(res.data.FunctionResponse.result) ==
-                        converter("发送成功")
-                      ) {
-                        this.userText = "";
-												this.showIndex = 0;
-                      } else {
-                        // this.$message.warning(res.data.FunctionResponse.result);
-                        console.log(res.data.FunctionResponse.result);
+
+                  const md = new MarkdownIt();
+                  let _this = this;
+                  this.curRequestController = new AbortController();
+
+                  let _allText = "";
+                  let _mdText = "";
+                  let _talkText = "";
+
+                  fetchEventSource("https://gpt4.cocorobo.cn/chat_post_stream",{
+                    method: "POST",
+                    headers: {
+                      "Content-Type": "application/json"
+                    },
+                    body: JSON.stringify(params),
+                    signal: _this.curRequestController.signal,
+                    onmessage(_e){
+                      console.log('_e',_e)
+
+                      this.showIndex = 0;
+                      let _eData = JSON.parse(_e.data);
+                      if (_eData.content.replace("'", "").replace("'", "") == "[DONE]") {
+                        // this.source.close();
+                        // this.source = null;
+                        let _result = [];
+                        if ("result" in _eData) {
+                          _result = _eData.result;
+                          for (let i = 0; i < _result.length; i++) {
+                            _mdText = _mdText.replace(_result[i].text, _result[i].fileName);
+                          }
+                        }
+                        _mdText = _mdText.replace("_", "");
+                        this.aiText = _mdText;
+                        this.scrollBottom();
+                        if (_talkText != "") {
+                          let _resultText = this.removeMarkdown(_talkText);
+                          this.talkTextList.push(_resultText);
+                          _talkText = "";
+                          if (!this.talkLoading) this.talkText();
+                        }
                         this.chatLoading = false;
-                        this.aiStatus = 0;
+                      } else {
+                        // _talkIndex+=1;
+                        let _text = _eData.content.replace("'", "").replace("'", "");
+                        if (_allText == "") {
+                          _allText = _text.replace(/^\n+/, ""); //去掉回复消息中偶尔开头就存在的连续换行符
+                          _talkText += _text.replace(/^\n+/, "");
+                        } else {
+                          _allText += _text;
+                          _talkText += _text;
+                        }
+                        `~`;
+                        _mdText = _allText + "_";
+                        _mdText = _mdText.replace(/\\n/g, "\n");
+                        _mdText = _mdText.replace(/\\/g, "");
+                        if (_allText.split("```").length % 2 == 0) _mdText += "\n```\n";
+                        this.aiText = _mdText;
                         this.showTextIndex = 0;
-                        this.showIndex = 0;
-                        this.aiText = "对不起,我无法理解您的问题,请重新提问";
-                        // this.timer = setTimeout(() => {
-                        //   this.showTextIndex = 3;
-                        //   this.aiStatus = 2;
-                        //   this.aiText = "";
-                        //   this.userText = "";
-                        // }, 3000);
+                        this.scrollBottom();
+                        if (/[,。:;?!)]/.test(_talkText)) {
+                          let _resultText = this.removeMarkdown(_talkText);
+                          this.talkTextList.push(_resultText);
+                          _talkText = "";
+                          if (!this.talkLoading) this.talkText();
+                        }
                       }
-                    })
-                    .catch(e => {
-                      console.log(e);
-                      this.chatLoading = false;
-                      this.aiStatus = 0;
-                      this.showTextIndex = 0;
-                      this.showIndex = 0;
-                      this.aiText = "对不起,我无法理解您的问题,请重新提问";
-                      // this.timer = setTimeout(() => {
-                      //   this.showTextIndex = 3;
-                      //   this.aiStatus = 2;
-                      //   this.aiText = "";
-                      //   this.userText = "";
-                      // }, 3000);
-                    });
-                  // 通过流获取ai对话数据
-                  this.getAtAuContent(_uuid);
+
+                      // let _data = ev.data;
+                      // if(_data=='[DONE]')return;
+                      // _addText+=_data;
+                      // this.aiText = md.render(_addText)
+                      // // _this.chatList.find(i => i.uid == _uuid).aiContent =  md.render(_addText);
+                      // // _this.chatList.find(i => i.uid == _uuid).loading = false;
+                      // _this.scrollBottom();
+                      // console.log(_data)
+                    },
+                    onclose(){
+                      _this.$forceUpdate();
+                      // _this.stopTalkToken = null;
+                      // // _this.faloading = false;
+                      // _this.curRequestController = null;
+
+                      // _this.chatList.find(i => i.uid == _uuid).isalltext = true;
+                      // _this.chatList.find(i => i.uid == _uuid).isShowSynchronization = true;
+                      // _this.insertChat(_uuid);
+                      console.log("连接关闭")
+                    },
+                    onerror(err){
+
+                      console.log("连接错误",err)
+                    }
+                })
+
+                  // this.ajax
+                  //   // .post("https://claude3.cocorobo.cn/chat", params)
+                  //   // .post("https://gpt4.cocorobo.cn/chat", params)
+                  //   .post(
+                  //     "https://gpt4.cocorobo.cn/ai_agent_park_chat_new",
+                  //     params
+                  //   )
+                  //   .then(res => {
+                  //     if (
+                  //       converter(res.data.FunctionResponse.result) ==
+                  //       converter("发送成功")
+                  //     ) {
+                  //       this.userText = "";
+									// 			this.showIndex = 0;
+                  //     } else {
+                  //       // this.$message.warning(res.data.FunctionResponse.result);
+                  //       console.log(res.data.FunctionResponse.result);
+                  //       this.chatLoading = false;
+                  //       this.aiStatus = 0;
+                  //       this.showTextIndex = 0;
+                  //       this.showIndex = 0;
+                  //       this.aiText = "对不起,我无法理解您的问题,请重新提问";
+                  //       // this.timer = setTimeout(() => {
+                  //       //   this.showTextIndex = 3;
+                  //       //   this.aiStatus = 2;
+                  //       //   this.aiText = "";
+                  //       //   this.userText = "";
+                  //       // }, 3000);
+                  //     }
+                  //   })
+                  //   .catch(e => {
+                  //     console.log(e);
+                  //     this.chatLoading = false;
+                  //     this.aiStatus = 0;
+                  //     this.showTextIndex = 0;
+                  //     this.showIndex = 0;
+                  //     this.aiText = "对不起,我无法理解您的问题,请重新提问";
+                  //     // this.timer = setTimeout(() => {
+                  //     //   this.showTextIndex = 3;
+                  //     //   this.aiStatus = 2;
+                  //     //   this.aiText = "";
+                  //     //   this.userText = "";
+                  //     // }, 3000);
+                  //   });
+                  // // 通过流获取ai对话数据
+                  // this.getAtAuContent(_uuid);
                 }
               }, 5000);
             }

+ 139 - 43
src/components/classRoomHelper/component/levitatedSphere.vue

@@ -65,6 +65,8 @@
 <script>
 import { v4 as uuidv4 } from "uuid";
 import MarkdownIt from "markdown-it";
+import { fetchEventSource } from "@microsoft/fetch-event-source";
+
 export default {
   data() {
     return {
@@ -245,51 +247,145 @@ export default {
                   uid: _uuid,
                   file_ids: [],
 									model: "gpt-4o-2024-11-20",
+                  stream:true
+
                 };
-                this.ajax
-                  // .post("https://claude3.cocorobo.cn/chat", params)
-                  // .post("https://gpt4.cocorobo.cn/chat", params)
-                  .post(
-                    "https://gpt4.cocorobo.cn/ai_agent_park_chat_new",
-                    params
-                  )
-                  .then(res => {
-                    if (
-                      converter(res.data.FunctionResponse.result) ==
-                      converter("发送成功")
-                    ) {
-                      this.userText = "";
-											this.showIndex = 0;
-                    } else {
-                      // this.$message.warning(res.data.FunctionResponse.result);
-                      console.log(res.data.FunctionResponse.result);
-                      this.chatLoading = false;
-                      this.aiStatus = 0;
-                      this.showTextIndex = 0;
-                      this.aiText = "对不起,我无法理解您的问题,请重新提问";
-                      this.timer = setTimeout(() => {
-                        this.showTextIndex = 3;
-                        this.aiStatus = 2;
-                        this.aiText = "";
-                        this.userText = "";
-                      }, 3000);
+
+                  const md = new MarkdownIt();
+                  let _this = this;
+                  this.curRequestController = new AbortController();
+
+                  let _allText = "";
+                  let _mdText = "";
+                  let _talkText = "";
+
+                  fetchEventSource("https://gpt4.cocorobo.cn/chat_post_stream",{
+                    method: "POST",
+                    headers: {
+                      "Content-Type": "application/json"
+                    },
+                    body: JSON.stringify(params),
+                    signal: _this.curRequestController.signal,
+                    onmessage(_e){
+                      console.log('_e',_e)
+
+                      this.showIndex = 0;
+                      let _eData = JSON.parse(_e.data);
+                      if (_eData.content.replace("'", "").replace("'", "") == "[DONE]") {
+                        // this.source.close();
+                        // this.source = null;
+                        let _result = [];
+                        if ("result" in _eData) {
+                          _result = _eData.result;
+                          for (let i = 0; i < _result.length; i++) {
+                            _mdText = _mdText.replace(_result[i].text, _result[i].fileName);
+                          }
+                        }
+                        _mdText = _mdText.replace("_", "");
+                        this.aiText = _mdText;
+                        this.scrollBottom();
+                        if (_talkText != "") {
+                          let _resultText = this.removeMarkdown(_talkText);
+                          this.talkTextList.push(_resultText);
+                          _talkText = "";
+                          if (!this.talkLoading) this.talkText();
+                        }
+                        this.chatLoading = false;
+                      } else {
+                        // _talkIndex+=1;
+                        let _text = _eData.content.replace("'", "").replace("'", "");
+                        if (_allText == "") {
+                          _allText = _text.replace(/^\n+/, ""); //去掉回复消息中偶尔开头就存在的连续换行符
+                          _talkText += _text.replace(/^\n+/, "");
+                        } else {
+                          _allText += _text;
+                          _talkText += _text;
+                        }
+                        `~`;
+                        _mdText = _allText + "_";
+                        _mdText = _mdText.replace(/\\n/g, "\n");
+                        _mdText = _mdText.replace(/\\/g, "");
+                        if (_allText.split("```").length % 2 == 0) _mdText += "\n```\n";
+                        this.aiText = _mdText;
+                        this.showTextIndex = 0;
+                        this.scrollBottom();
+                        if (/[,。:;?!)]/.test(_talkText)) {
+                          let _resultText = this.removeMarkdown(_talkText);
+                          this.talkTextList.push(_resultText);
+                          _talkText = "";
+                          if (!this.talkLoading) this.talkText();
+                        }
+                      }
+
+                      // let _data = ev.data;
+                      // if(_data=='[DONE]')return;
+                      // _addText+=_data;
+                      // this.aiText = md.render(_addText)
+                      // // _this.chatList.find(i => i.uid == _uuid).aiContent =  md.render(_addText);
+                      // // _this.chatList.find(i => i.uid == _uuid).loading = false;
+                      // _this.scrollBottom();
+                      // console.log(_data)
+                    },
+                    onclose(){
+                      _this.$forceUpdate();
+                      // _this.stopTalkToken = null;
+                      // // _this.faloading = false;
+                      // _this.curRequestController = null;
+
+                      // _this.chatList.find(i => i.uid == _uuid).isalltext = true;
+                      // _this.chatList.find(i => i.uid == _uuid).isShowSynchronization = true;
+                      // _this.insertChat(_uuid);
+                      console.log("连接关闭")
+                    },
+                    onerror(err){
+
+                      console.log("连接错误",err)
                     }
-                  })
-                  .catch(e => {
-                    console.log(e);
-                    this.chatLoading = false;
-                    this.aiStatus = 0;
-                    this.showTextIndex = 0;
-                    this.aiText = "对不起,我无法理解您的问题,请重新提问";
-                    this.timer = setTimeout(() => {
-                      this.showTextIndex = 3;
-                      this.aiStatus = 2;
-                      this.aiText = "";
-                      this.userText = "";
-                    }, 3000);
-                  });
-                // 通过流获取ai对话数据
-                this.getAtAuContent(_uuid);
+                })
+                // this.ajax
+                //   // .post("https://claude3.cocorobo.cn/chat", params)
+                //   // .post("https://gpt4.cocorobo.cn/chat", params)
+                //   .post(
+                //     "https://gpt4.cocorobo.cn/ai_agent_park_chat_new",
+                //     params
+                //   )
+                //   .then(res => {
+                //     if (
+                //       converter(res.data.FunctionResponse.result) ==
+                //       converter("发送成功")
+                //     ) {
+                //       this.userText = "";
+								// 			this.showIndex = 0;
+                //     } else {
+                //       // this.$message.warning(res.data.FunctionResponse.result);
+                //       console.log(res.data.FunctionResponse.result);
+                //       this.chatLoading = false;
+                //       this.aiStatus = 0;
+                //       this.showTextIndex = 0;
+                //       this.aiText = "对不起,我无法理解您的问题,请重新提问";
+                //       this.timer = setTimeout(() => {
+                //         this.showTextIndex = 3;
+                //         this.aiStatus = 2;
+                //         this.aiText = "";
+                //         this.userText = "";
+                //       }, 3000);
+                //     }
+                //   })
+                //   .catch(e => {
+                //     console.log(e);
+                //     this.chatLoading = false;
+                //     this.aiStatus = 0;
+                //     this.showTextIndex = 0;
+                //     this.aiText = "对不起,我无法理解您的问题,请重新提问";
+                //     this.timer = setTimeout(() => {
+                //       this.showTextIndex = 3;
+                //       this.aiStatus = 2;
+                //       this.aiText = "";
+                //       this.userText = "";
+                //     }, 3000);
+                //   });
+                // // 通过流获取ai对话数据
+                // this.getAtAuContent(_uuid);
               }
             }, 5000);
           }

+ 97 - 25
src/components/classRoomHelper/component/searchArea.vue

@@ -1386,6 +1386,8 @@
 import { v4 as uuidv4 } from "uuid";
 import MarkdownIt from "markdown-it";
 import { tools } from "../../../common/tools";
+import { fetchEventSource } from "@microsoft/fetch-event-source";
+
 var OpenCC = require("opencc-js");
 let converter = OpenCC.Converter({
   from: "hk",
@@ -2707,7 +2709,8 @@ Instruction: Based on the context, follow "Format example", write content
         session_name: _uuid,
         // uid: _uuid,
         file_ids: this.fileId,
-        model: "gpt-4o-2024-11-20"
+        model: "gpt-4o-2024-11-20",
+        stream:true
       };
       // let params = {
       //   message: {
@@ -2722,30 +2725,99 @@ Instruction: Based on the context, follow "Format example", write content
       // };
       this.text = "";
 
-      this.ajax
-        // .post("https://gpt4.cocorobo.cn/chat", params)
-        // .post("https://claude3.cocorobo.cn/chat", params)
-        .post("https://gpt4.cocorobo.cn/ai_agent_park_chat", params)
-        .then(res => {
-          // console.log(res);
-          let _data = res.data.FunctionResponse.message;
-          _data = _data.replaceAll("```json", "").replaceAll("```", "");
-          const match = _data.match(/\[\s*{[^]*}\s*\]/);
-          // console.log(_data);
-          // console.log(match);
-          this.chatList.find(i => i.uid == _uuid).aiContent = JSON.parse(
-            match[0]
-          );
-          this.chatList.find(i => i.uid == _uuid).isalltext = true;
-          this.chatList.find(i => i.uid == _uuid).isShowSynchronization = true;
-          this.chatList.find(i => i.uid == _uuid).loading = false;
-          this.scrollBottom();
-          // this.chatLoading = false;
-        })
-        .catch(e => {
-          this.chatLoading = false;
-          console.log(e);
-        });
+      let _allText = "";
+      let _mdText = "";
+
+        const md = new MarkdownIt();
+        let _this = this;
+        // const curRequestController = new AbortController();
+         this.curRequestController = new AbortController();
+
+          fetchEventSource("https://gpt4.cocorobo.cn/ai_agent_park_chat_new_post_stream",{
+                method: "POST",
+                headers: {
+                  "Content-Type": "application/json"
+                },
+                body: JSON.stringify(params),
+                signal: _this.curRequestController.signal,
+                onmessage(_e){
+                  if (_e.data.replace("'", "").replace("'", "") == "[DONE]") {
+                    //对话已经完成
+                    _mdText = _mdText.replace("_", "");
+                    _mdText = _mdText.replace("```json", "");
+                    _mdText = _mdText.replace("```", "");
+                    // 使用正则表达式匹配JSON数组
+                    const regex = /\[\s*{[^]*}\s*\]/;
+                    const match = _mdText.match(regex);
+                    let _result = match[0];
+                    source.close();
+                    this.chatList.find(i => i.uid == _uuid).aiContent = JSON.parse(
+                      _result
+                    );
+                    this.chatList.find(i => i.uid == _uuid).isalltext = true;
+                    this.chatList.find(i => i.uid == _uuid).isShowSynchronization = true;
+                    this.chatList.find(i => i.uid == _uuid).loading = false;
+                    this.nowChatList.push(this.chatList.find(i => i.uid == _uuid));
+                    this.scrollBottom();
+                    // 这里保存对话
+                    return;
+                  } else {
+                    //对话还在继续
+                    let _text = "";
+                    _text = _e.data.replaceAll("'", "");
+                    if (_allText == "") {
+                      _allText = _text.replace(/^\n+/, ""); //去掉回复消息中偶尔开头就存在的连续换行符
+                    } else {
+                      _allText += _text;
+                    }
+                    _mdText = _allText + "_";
+                    _mdText = _mdText.replace(/\\n/g, "\n");
+                    _mdText = _mdText.replace(/\\/g, "");
+                    if (_allText.split("```").length % 2 == 0) _mdText += "\n```\n";
+                    //转化返回的回复流数据
+                    this.scrollBottom();
+                  }
+                },
+                onclose(){
+                  _this.$forceUpdate();
+                  // _this.stopTalkToken = null;
+                  // _this.faloading = false;
+                  // _this.curRequestController = null;
+                  // _this.chatList.find(i => i.uid == _uid).isalltext = true;
+                  // _this.chatList.find(i => i.uid == _uid).isShowSynchronization = true;
+                  _this.insertChat(_uuid);
+                  console.log("连接关闭")
+                },
+                onerror(err){
+
+                  console.log("连接错误",err)
+                }
+            })
+
+      // this.ajax
+      //   // .post("https://gpt4.cocorobo.cn/chat", params)
+      //   // .post("https://claude3.cocorobo.cn/chat", params)
+      //   .post("https://gpt4.cocorobo.cn/ai_agent_park_chat", params)
+      //   .then(res => {
+      //     // console.log(res);
+      //     let _data = res.data.FunctionResponse.message;
+      //     _data = _data.replaceAll("```json", "").replaceAll("```", "");
+      //     const match = _data.match(/\[\s*{[^]*}\s*\]/);
+      //     // console.log(_data);
+      //     // console.log(match);
+      //     this.chatList.find(i => i.uid == _uuid).aiContent = JSON.parse(
+      //       match[0]
+      //     );
+      //     this.chatList.find(i => i.uid == _uuid).isalltext = true;
+      //     this.chatList.find(i => i.uid == _uuid).isShowSynchronization = true;
+      //     this.chatList.find(i => i.uid == _uuid).loading = false;
+      //     this.scrollBottom();
+      //     // this.chatLoading = false;
+      //   })
+      //   .catch(e => {
+      //     this.chatLoading = false;
+      //     console.log(e);
+      //   });
       // this.getWAntSearchContent(_uuid);
     },
     addAsk(_text) {