调整内容显示

This commit is contained in:
aixianling
2023-05-12 18:04:58 +08:00
parent 795f6cd652
commit cd8e0f57aa
7 changed files with 5440 additions and 28 deletions

View File

@@ -46,8 +46,9 @@ export default {
methods: {
handleSend() {
if (!!this.inputText) {
const ai = this.settings.model
const myMsg = {
headImg: USER_AVATAR,
avatar: USER_AVATAR,
name: "我",
time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
msg: this.inputText,
@@ -56,11 +57,67 @@ export default {
}
this.chatHistory.push(myMsg)
this.loading = true
this.settings.model.chat().finally(() => this.loading = false)
const aiMsg = {
avatar: ai.avatar,
name: ai.name,
time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
msg: '',
chatType: 0, //信息类型0文字1图片
uid: "ai", //uid
}
this.chatHistory.push(aiMsg)
ai.chat(this.chatHistory).finally(() => this.loading = false)
} else {
this.$message.error("请不要发送空消息!")
}
}
},
readStream(reader, _this, currentResLocation, type) {
return reader.read().then(({done, value}) => {
if (done) {
return;
}
if (!_this.chatList[currentResLocation].reminder) {
_this.chatList[currentResLocation].reminder = "";
}
let decoded = new TextDecoder().decode(value);
decoded = _this.chatList[currentResLocation].reminder + decoded;
let decodedArray = decoded.split("data: ");
let longstr = "";
decodedArray.forEach(decoded => {
try {
decoded = decoded.trim();
if (longstr == "") {
JSON.parse(decoded);
} else {
decoded = longstr + decoded;
longstr = "";
JSON.parse(decoded);
}
} catch (e) {
longstr = decoded;
decoded = "";
}
if (decoded !== "") {
if (decoded.trim() === "[DONE]") {
return;
} else {
const choices = JSON.parse(decoded).choices
if (choices && choices.length > 0) {
if (type === "chat") {
const response = choices[0].delta.content ? choices[0].delta.content : "";
_this.chatList[currentResLocation].msg = _this.chatList[currentResLocation].msg + response
_this.scrollBottom();
} else {
const response = choices[0].text;
_this.chatList[currentResLocation].msg = _this.chatList[currentResLocation].msg + response
}
}
}
}
})
return this.readStream(reader, _this, currentResLocation, type);
});
},
}
}
</script>