main
齐斌 10 months ago
parent f0deb900de
commit 7587ce69d0

@ -27,7 +27,7 @@
class="chat-friend" class="chat-friend"
v-if="item.uid !== 'admin'"> v-if="item.uid !== 'admin'">
<span style="color: #175abd" <span style="color: #175abd"
>客服AI<span >智能助手<span
style="font-size: 20rpx" style="font-size: 20rpx"
v-show="!acqStatus && index === chatList.length - 1" v-show="!acqStatus && index === chatList.length - 1"
>正在思索</span >正在思索</span
@ -45,7 +45,11 @@
<view <view
class="chat-text" class="chat-text"
v-if="item.chatType == 0"> v-if="item.chatType == 0">
<view v-if="item.msg" class="chat-word">{{ item.msg }}</view> <view
v-if="item.msg"
class="chat-word"
>{{ item.msg }}</view
>
</view> </view>
<view <view
class="chat-image" class="chat-image"
@ -120,13 +124,24 @@
<u-button <u-button
style=" style="
border-radius: 40rpx; border-radius: 40rpx;
width: 120rpx; width: 140rpx;
height: 64rpx; height: 64rpx;
background-color: #175abd; background-color: #175abd;
" "
@click="sendText" @click="sendText"
type="primary" type="primary"
text="发送"></u-button> text="发送"></u-button>
<u-upload
:fileList="fileList"
name="6"
accept="image"
@afterRead="upLoaded"
:maxCount="1">
<u-icon
size="30"
style="margin-bottom: 30rpx; margin-left: 30rpx"
name="plus-circle-fill"></u-icon>
</u-upload>
</view> </view>
</view> </view>
</template> </template>
@ -151,6 +166,7 @@ export default {
acqStatus: true, acqStatus: true,
gptMode: "gpt-3.5-turbo", gptMode: "gpt-3.5-turbo",
isGPT3: true, isGPT3: true,
fileList: [],
}; };
}, },
onLoad(item) {}, onLoad(item) {},
@ -169,6 +185,10 @@ export default {
name: GPT_MODEL, name: GPT_MODEL,
time: dateNow, time: dateNow,
msg: this.inputMsg, msg: this.inputMsg,
fileList:
this.fileList.length > 0
? JSON.parse(JSON.stringify(this.fileList))
: [],
chatType: 0, //01 chatType: 0, //01
uid: "admin", //uid uid: "admin", //uid
}; };
@ -192,8 +212,8 @@ export default {
uid: "ai", //uid uid: "ai", //uid
}; };
this.chatCompletion(params, chatBeforResMsg); this.chatCompletion(params, chatBeforResMsg);
this.inputMsg = ""; this.inputMsg = "";
this.fileList = [];
} else { } else {
this.$nextTick(() => { this.$nextTick(() => {
this.acqStatus = true; this.acqStatus = true;
@ -219,7 +239,7 @@ export default {
changeMode(e) { changeMode(e) {
this.acqStatus = true; this.acqStatus = true;
this.isGPT3 = !this.isGPT3; 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 = []; this.chatList = [];
}, },
@ -235,46 +255,65 @@ export default {
params.stream = true; params.stream = true;
// //
this.sendMsg(chatBeforResMsg); this.sendMsg(chatBeforResMsg);
const self = this;
const currentResLocation = this.chatList.length - 1; const currentResLocation = this.chatList.length - 1;
// //
const baseUrl = "http://114.218.158.24:9020/"; const baseUrl = "http://114.218.158.24:9020/";
const token =
"46d71a72d8d845ad7ed23eba9bdde260e635407190c2ce1bf7fd22088e41682ea07773ec65cae8946d2003f264d55961f96e0fc5da10eb96d3a348c1664e9644e756eda7154e1af9e70d1c9d2f100823a26885ea6df3249fe619995cb79dc5dbd5ead32d43b955d6b3ce83129097bb21bb8169898f48692de4f966db140c71b85a2065acfc948561c465279fc05194a79a1115f3b00170944b6c4bd6c52ada909a075c55d18d76c2ed2175602421b34b27362a05c350733ed73382471df0a08950f7f1e812a610c17bdac82d82d54be38969f6b41201af79b8d36ef177c5b94b533b1600017241188832aaee0ff1844b2560f527e9f563e3c561bffc356ffe5777a3d2030a9579e443bb04a2b565d05f9d2d3d1efaefdb703ae0575f1542aeba992ba5ba7c2db5b5573509b172bc26aaf8c05b27bc981ec23f0873a801f42c51";
try { try {
uni.request({ await fetch(baseUrl + "chat/completion", {
url: baseUrl + "chat/app-completion", //
method: "POST", method: "POST",
data: JSON.stringify(params), timeout: 10000,
body: JSON.stringify({
header: { ...params,
"Content-Type": "text/event-stream", }),
headers: {
"Content-Type": "application/json",
Accept: "application/json", Accept: "application/json",
Authorization: "", Authorization: token,
}, },
success: (response) => { }).then((response) => {
// const reader = response.body.getReader();
console.log(response.data, 123);
if (response.data) { function readStream(reader) {
if (response.data.data.content) { console.log(reader, 666);
return reader.read().then(({ done, value }) => {
if (done) {
return;
}
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 =
response.data.data.content; this.chatList[currentResLocation].msg;
readStream(reader);
this.$nextTick(() => { this.$nextTick(() => {
this.acqStatus = true; this.acqStatus = true;
}); });
this.scrollToBottom();
} else {
this.chatList[currentResLocation].msg =
"我还在学习中,等我学习完再来问我吧!";
this.$nextTick(() => {
this.acqStatus = true;
});
this.scrollToBottom();
}
}
},
fail: (err) => {
console.error(err);
},
}); });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@ -284,8 +323,28 @@ export default {
contextualAssemblyData() { contextualAssemblyData() {
const conversation = []; const conversation = [];
for (var chat of this.chatList) { for (var chat of this.chatList) {
let role = [];
if (chat.uid == "admin") { 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); conversation.push(my);
} else if (chat.uid == "ai") { } else if (chat.uid == "ai") {
let ai = { speaker: "agent", text: chat.msg }; let ai = { speaker: "agent", text: chat.msg };
@ -303,8 +362,37 @@ export default {
scrollToUpper() { scrollToUpper() {
console.log("滚动到顶部"); 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)
},
}, },
}; };
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
@ -324,6 +412,9 @@ page {
color: #fff; color: #fff;
padding-left: 40rpx; padding-left: 40rpx;
} }
/deep/ .u-upload{
flex: none !important;
}
.text-area { .text-area {
// position: fixed; // position: fixed;
width: 95%; width: 95%;
@ -642,7 +733,7 @@ page {
} }
.chat-word { .chat-word {
// //
animation: word 1s linear; animation: word 0.5s linear;
} }
@keyframes word { @keyframes word {
0% { 0% {

Loading…
Cancel
Save