diff --git a/pages/index/index.vue b/pages/index/index.vue index 819db96..5b5dadf 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -27,7 +27,7 @@ class="chat-friend" v-if="item.uid !== 'admin'"> 客服AI智能助手(正在思索……) - {{ item.msg }} + {{ item.msg }} + + + @@ -151,6 +166,7 @@ export default { acqStatus: true, gptMode: "gpt-3.5-turbo", isGPT3: true, + fileList: [], }; }, onLoad(item) {}, @@ -169,6 +185,10 @@ export default { name: GPT_MODEL, time: dateNow, msg: this.inputMsg, + fileList: + this.fileList.length > 0 + ? JSON.parse(JSON.stringify(this.fileList)) + : [], chatType: 0, //信息类型,0文字,1图片 uid: "admin", //uid }; @@ -192,8 +212,8 @@ export default { uid: "ai", //uid }; this.chatCompletion(params, chatBeforResMsg); - this.inputMsg = ""; + this.fileList = []; } else { this.$nextTick(() => { this.acqStatus = true; @@ -219,7 +239,7 @@ export default { changeMode(e) { this.acqStatus = true; this.isGPT3 = !this.isGPT3; - this.gptMode = this.isGPT3 ? "gpt-3.5-turbo" : "gpt-4"; + this.gptMode = this.isGPT3 ? "gpt-3.5-turbo" : "gpt-4-1106-preview"; // 清空对话 this.chatList = []; }, @@ -235,46 +255,65 @@ export default { params.stream = true; //新增一个空的消息 this.sendMsg(chatBeforResMsg); - + const self = this; const currentResLocation = this.chatList.length - 1; // 获取当前环境地址 const baseUrl = "http://114.218.158.24:9020/"; + const token = + "46d71a72d8d845ad7ed23eba9bdde260e635407190c2ce1bf7fd22088e41682ea07773ec65cae8946d2003f264d55961f96e0fc5da10eb96d3a348c1664e9644e756eda7154e1af9e70d1c9d2f100823a26885ea6df3249fe619995cb79dc5dbd5ead32d43b955d6b3ce83129097bb21bb8169898f48692de4f966db140c71b85a2065acfc948561c465279fc05194a79a1115f3b00170944b6c4bd6c52ada909a075c55d18d76c2ed2175602421b34b27362a05c350733ed73382471df0a08950f7f1e812a610c17bdac82d82d54be38969f6b41201af79b8d36ef177c5b94b533b1600017241188832aaee0ff1844b2560f527e9f563e3c561bffc356ffe5777a3d2030a9579e443bb04a2b565d05f9d2d3d1efaefdb703ae0575f1542aeba992ba5ba7c2db5b5573509b172bc26aaf8c05b27bc981ec23f0873a801f42c51"; try { - uni.request({ - url: baseUrl + "chat/app-completion", //你的接口地址 + await fetch(baseUrl + "chat/completion", { method: "POST", - data: JSON.stringify(params), - - header: { - "Content-Type": "text/event-stream", + timeout: 10000, + body: JSON.stringify({ + ...params, + }), + headers: { + "Content-Type": "application/json", Accept: "application/json", - Authorization: "", + Authorization: token, }, - success: (response) => { - // 在这里处理你的响应数据 - console.log(response.data, 123); - if (response.data) { - if (response.data.data.content) { - this.chatList[currentResLocation].msg = - response.data.data.content; - - this.$nextTick(() => { - this.acqStatus = true; - }); - this.scrollToBottom(); - } else { - this.chatList[currentResLocation].msg = - "我还在学习中,等我学习完再来问我吧!"; - this.$nextTick(() => { - this.acqStatus = true; - }); - this.scrollToBottom(); + }).then((response) => { + const reader = response.body.getReader(); + + function readStream(reader) { + console.log(reader, 666); + return reader.read().then(({ done, value }) => { + if (done) { + return; } - } - }, - fail: (err) => { - console.error(err); - }, + console.log(self.chatList, currentResLocation, 777); + if (!self.chatList[currentResLocation].reminder) { + self.chatList[currentResLocation].reminder = ""; + } + let decoded = new TextDecoder().decode(value); + decoded = self.chatList[currentResLocation].reminder + decoded; + let decodedArray = decoded.split("data: "); + decodedArray.forEach((decoded) => { + if (decoded !== "") { + if (decoded.trim() === "[DONE]") { + return; + } else { + const response = JSON.parse(decoded).choices[0].delta + .content + ? JSON.parse(decoded).choices[0].delta.content + : ""; + self.chatList[currentResLocation].msg = + self.chatList[currentResLocation].msg + response; + self.scrollToBottom(); + } + } + }); + return readStream(reader); + }); + } + this.chatList[currentResLocation].msg = + this.chatList[currentResLocation].msg; + + readStream(reader); + this.$nextTick(() => { + this.acqStatus = true; + }); }); } catch (error) { console.error(error); @@ -284,8 +323,28 @@ export default { contextualAssemblyData() { const conversation = []; for (var chat of this.chatList) { + let role = []; if (chat.uid == "admin") { - let my = { speaker: "user", text: chat.msg }; + let my; + if (this.gptMode === "gpt-4-1106-preview-vision-preview") { + if (chat.fileList.length > 0) { + chat.fileList.forEach((item) => { + if (item) { + role.push({ + type: "image_url", + image_url: item, + }); + } + }); + } + role.unshift({ + type: "text", + text: chat.msg, + }); + my = { speaker: "user", text: role }; + } else { + my = { speaker: "user", text: chat.msg }; + } conversation.push(my); } else if (chat.uid == "ai") { let ai = { speaker: "agent", text: chat.msg }; @@ -303,8 +362,37 @@ export default { scrollToUpper() { console.log("滚动到顶部"); }, + uploadFilePromise(url) { + return new Promise((resolve, reject) => { + const Authorization = uni.getStorageSync('token'); + let a = uni.uploadFile({ + url: 'http://114.218.158.24:9020/upload/img', + filePath: url, + name: 'file', + formData: { + source: 'gpt', + mask:'' + }, + header: { + Authorization + }, + success: res => { + resolve(res.data); + this.fileList.push( + JSON.parse(res.data).data.ori_url + ); + console.log(this.fileList, 888); + + } + }); + }); + }, + async upLoaded(file, lists, name) { + this.uploadFilePromise(file.file.url) + }, }, }; +