【新增】AI:格式化 chat 对话的代码

This commit is contained in:
YunaiV
2024-05-14 23:55:23 +08:00
parent 3d15eadb5d
commit 051a610e56
3 changed files with 163 additions and 137 deletions

View File

@ -1,17 +1,21 @@
import request from '@/config/axios'
// 聊天VO
// AI 聊天会话 VO
export interface ChatConversationVO {
id: string // 会话编号
userId: string // 用户编号
id: number // ID 编号
userId: number // 用户编号
title: string // 会话标题
pinned: string // 是否置顶
roleId: string // 角色编号
model: number // 模型标志
pinned: boolean // 是否置顶
roleId: number // 角色编号
modelId: number // 模型编号
temperature: string // 温度参数
maxTokens: string // 单条回复的最大 Token 数量
maxContexts: string // 上下文的最大 Message 数量
model: string // 模型标志
temperature: number // 温度参数
maxTokens: number // 单条回复的最大 Token 数量
maxContexts: number // 上下文的最大 Message 数量
// 额外字段
roleAvatar?: string // 角色头像
modelMaxTokens?: string // 模型的单条回复的最大 Token 数量
modelMaxContexts?: string // 模型的上下文的最大 Message 数量
}
export interface ChatConversationUpdateVO {
@ -24,7 +28,7 @@ export interface ChatConversationUpdateVO {
maxContexts: string // 上下文的最大 Message 数量
}
// AI chat 聊天
// AI 聊天会话 API
export const ChatConversationApi = {
// 获取 Conversation
get: async (id: string) => {
@ -34,4 +38,14 @@ export const ChatConversationApi = {
update: async (data: ChatConversationUpdateVO) => {
return await request.put({ url: `/ai/chat/conversation/update`, data})
},
// 新增【我的】聊天会话
createChatConversationMy: async (data?: ChatConversationVO) => {
return await request.post({ url: `/ai/chat/conversation/create-my`, data })
},
// 获得【我的】聊天会话列表
getChatConversationMyList: async () => {
return await request.get({ url: `/ai/chat/conversation/my-list` })
}
}

View File

@ -1,7 +1,7 @@
import request from '@/config/axios'
import {fetchEventSource} from '@microsoft/fetch-event-source';
import {getAccessToken} from '@/utils/auth'
import {config} from '@/config/axios/config'
import { fetchEventSource } from '@microsoft/fetch-event-source'
import { getAccessToken } from '@/utils/auth'
import { config } from '@/config/axios/config'
// 聊天VO
export interface ChatMessageVO {
@ -24,26 +24,28 @@ export interface ChatMessageSendVO {
// AI chat 聊天
export const ChatMessageApi = {
// 消息列表
messageList: async (conversationId: string) => {
return await request.get({ url: `/ai/chat/message/list-by-conversation-id?conversationId=${conversationId}`})
return await request.get({
url: `/ai/chat/message/list-by-conversation-id?conversationId=${conversationId}`
})
},
// 发送 add 消息
add: async (data: ChatMessageSendVO) => {
return await request.post({ url: `/ai/chat/message/add`, data})
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})
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) => {
const token = getAccessToken()
return fetchEventSource(`${ config.base_url}/ai/chat/message/send-stream`, {
return fetchEventSource(`${config.base_url}/ai/chat/message/send-stream`, {
method: 'post',
headers: {
'Content-Type': 'application/json',
@ -51,18 +53,17 @@ export const ChatMessageApi = {
},
openWhenHidden: true,
body: JSON.stringify({
id: id,
id: id
}),
onmessage: onMessage,
onerror:onError,
onerror: onError,
onclose: onClose,
signal: ctrl.signal,
});
signal: ctrl.signal
})
},
// 发送 send 消息
delete: async (id: string) => {
return await request.delete({ url: `/ai/chat/message/delete?id=${id}` })
},
}
}