【新增】AI:流式发送消息的微调,统一成单接口

This commit is contained in:
YunaiV 2024-05-15 23:06:27 +08:00
parent 83b9332125
commit 4d3c5ef7c1
2 changed files with 33 additions and 25 deletions

View File

@ -31,19 +31,16 @@ export const ChatMessageApi = {
})
},
// 发送 add 消息
add: async (data: ChatMessageSendVO) => {
return await request.post({ url: `/ai/chat/message/add`, data })
},
// 发送 send 消息
send: async (data: ChatMessageSendVO) => {
return await request.post({ url: `/ai/chat/message/send`, data })
},
// 发送 send stream 消息
// TODO axios 可以么? https://apifox.com/apiskills/how-to-create-axios-stream/
sendStream: async (id: string, ctrl, onMessage, onError, onClose) => {
sendStream: async (
conversationId: number,
content: string,
ctrl,
onMessage,
onError,
onClose
) => {
const token = getAccessToken()
return fetchEventSource(`${config.base_url}/ai/chat/message/send-stream`, {
method: 'post',
@ -53,7 +50,8 @@ export const ChatMessageApi = {
},
openWhenHidden: true,
body: JSON.stringify({
id: id
conversationId,
content
}),
onmessage: onMessage,
onerror: onError,

View File

@ -90,6 +90,7 @@
<div class="message-container" ref="messageContainer">
<div class="chat-list" v-for="(item, index) in list" :key="index">
<!-- 靠左 message -->
<!-- TODO 芋艿类型判断 -->
<div class="left-message message-item" v-if="item.type === 'system'">
<div class="avatar">
<el-avatar
@ -297,24 +298,28 @@ const onSend = async () => {
return
}
const content = prompt.value?.trim()
if (content?.length < 2) {
if (content.length < 2) {
ElMessage({
message: '请输入内容!',
type: 'error'
})
return
}
// TODO UI
//
prompt.value = ''
const requestParams = {
// const requestParams = {
// conversationId: conversationId.value,
// content: content
// } as unknown as ChatMessageSendVO
// // message
const userMessage = {
conversationId: conversationId.value,
content: content
} as unknown as ChatMessageSendVO
// message
const userMessage = (await ChatMessageApi.add(requestParams)) as unknown as ChatMessageVO
list.value.push(userMessage)
//
scrollToBottom()
}
// list.value.push(userMessage)
// //
// scrollToBottom()
// stream
await doSendStream(userMessage)
//
@ -330,13 +335,15 @@ const doSendStream = async (userMessage: ChatMessageVO) => {
let isFirstMessage = true
let content = ''
ChatMessageApi.sendStream(
userMessage.id,
userMessage.conversationId, // TODO
userMessage.content,
conversationInAbortController.value,
(message) => {
console.log('message', message)
const data = JSON.parse(message.data) as unknown as ChatMessageVO
const data = JSON.parse(message.data) // TODO
// debugger
//
if (data.content === '') {
if (data.receive.content === '') {
//
conversationInProgress.value = false
// stream
@ -345,9 +352,12 @@ const doSendStream = async (userMessage: ChatMessageVO) => {
// message
if (isFirstMessage) {
isFirstMessage = false
list.value.push(data)
// debugger
list.value.push(data.send)
list.value.push(data.receive)
} else {
content = content + data.content
// debugger
content = content + data.receive.content
const lastMessage = list.value[list.value.length - 1]
lastMessage.content = marked(content) as unknown as string
list.value[list.value - 1] = lastMessage