main
齐斌 10 months ago
parent f0deb900de
commit 7587ce69d0

@ -27,7 +27,7 @@
class="chat-friend"
v-if="item.uid !== 'admin'">
<span style="color: #175abd"
>客服AI<span
>智能助手<span
style="font-size: 20rpx"
v-show="!acqStatus && index === chatList.length - 1"
>正在思索</span
@ -45,7 +45,11 @@
<view
class="chat-text"
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
class="chat-image"
@ -120,13 +124,24 @@
<u-button
style="
border-radius: 40rpx;
width: 120rpx;
width: 140rpx;
height: 64rpx;
background-color: #175abd;
"
@click="sendText"
type="primary"
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>
</template>
@ -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, //01
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) {
}).then((response) => {
const reader = response.body.getReader();
function readStream(reader) {
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 =
response.data.data.content;
this.chatList[currentResLocation].msg;
readStream(reader);
this.$nextTick(() => {
this.acqStatus = true;
});
this.scrollToBottom();
} else {
this.chatList[currentResLocation].msg =
"我还在学习中,等我学习完再来问我吧!";
this.$nextTick(() => {
this.acqStatus = true;
});
this.scrollToBottom();
}
}
},
fail: (err) => {
console.error(err);
},
});
} 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)
},
},
};
</script>
<style lang="scss" scoped>
@ -324,6 +412,9 @@ page {
color: #fff;
padding-left: 40rpx;
}
/deep/ .u-upload{
flex: none !important;
}
.text-area {
// position: fixed;
width: 95%;
@ -642,7 +733,7 @@ page {
}
.chat-word {
//
animation: word 1s linear;
animation: word 0.5s linear;
}
@keyframes word {
0% {

Loading…
Cancel
Save