基本功能已完成

This commit is contained in:
2023-05-15 01:24:18 +08:00
parent 65b4d57675
commit b99f837c72
7 changed files with 75 additions and 57 deletions

View File

@@ -17,7 +17,7 @@
<thinking-bar v-show="loading"/>
<chat-content class="fill" :list="chatHistory"/>
<el-row class="mar-t8" align="middle">
<chat-input class="fill mar-r8" v-model="inputText"/>
<chat-input class="fill mar-r8" v-model="inputText" @enter="handleSend"/>
<el-button type="primary" @click="handleSend">发送</el-button>
</el-row>
</section>
@@ -46,7 +46,7 @@ export default {
methods: {
handleSend() {
if (!!this.inputText) {
const ai = this.settings.model
const ai = this.config.model
const myMsg = {
avatar: USER_AVATAR,
name: "我",
@@ -54,6 +54,7 @@ export default {
msg: this.inputText,
chatType: 0, //信息类型0文字1图片
uid: "me", //uid
role: "user"
}
this.chatHistory.push(myMsg)
this.loading = true
@@ -61,63 +62,58 @@ export default {
avatar: ai.avatar,
name: ai.name,
time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
msg: '',
msg: "",
chatType: 0, //信息类型0文字1图片
uid: "ai", //uid
role: "assistant"
}
this.chatHistory.push(aiMsg)
ai.chat(this.chatHistory).finally(() => this.loading = false)
if (this.config.stream) {
ai.chatStream(this.chatHistory).then(reader => this.streamOutput(reader, this.chatHistory.at(-1))).finally(() => this.loading = false)
} else {
ai.chat(this.chatHistory).then(reply => {
const decodeArr = reply.split("")
decodeArr.forEach(e => this.chatHistory.at(-1).msg += e)
}).finally(() => this.loading = false)
}
} else {
this.$message.error("请不要发送空消息!")
}
},
readStream(reader, _this, currentResLocation, type) {
streamOutput(reader, chat) {
return reader.read().then(({done, value}) => {
if (done) {
return;
}
if (!_this.chatList[currentResLocation].reminder) {
_this.chatList[currentResLocation].reminder = "";
if (!chat.reminder) {
chat.reminder = ""
}
let decoded = new TextDecoder().decode(value);
decoded = _this.chatList[currentResLocation].reminder + decoded;
let decodedArray = decoded.split("data: ");
let decode = new TextDecoder().decode(value)
decode = chat.reminder + decode
let decodedArray = decode.split("data: ");
let longstr = "";
decodedArray.forEach(decoded => {
decoded = decoded.trim();
try {
decoded = decoded.trim();
if (longstr == "") {
JSON.parse(decoded);
} else {
if (longstr != "") {
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
}
}
if (!!decoded && decoded !== "[DONE]") {
const choices = JSON.parse(decoded).choices
if (choices?.length > 0) {
const response = choices[0].delta.content || "";
chat.msg += response
}
}
})
return this.readStream(reader, _this, currentResLocation, type);
});
},
return this.streamOutput(reader, chat)
})
}
}
}
</script>