Commit 051a610e by YunaiV

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

parent 3d15eadb
import request from '@/config/axios' import request from '@/config/axios'
// 聊天VO // AI 聊天会话 VO
export interface ChatConversationVO { export interface ChatConversationVO {
id: string // 会话编号 id: number // ID 编号
userId: string // 用户编号 userId: number // 用户编号
title: string // 会话标题 title: string // 会话标题
pinned: string // 是否置顶 pinned: boolean // 是否置顶
roleId: string // 角色编号 roleId: number // 角色编号
model: number // 模型标志
modelId: number // 模型编号 modelId: number // 模型编号
temperature: string // 温度参数 model: string // 模型标志
maxTokens: string // 单条回复的最大 Token 数量 temperature: number // 温度参数
maxContexts: string // 上下文的最大 Message 数量 maxTokens: number // 单条回复的最大 Token 数量
maxContexts: number // 上下文的最大 Message 数量
// 额外字段
roleAvatar?: string // 角色头像
modelMaxTokens?: string // 模型的单条回复的最大 Token 数量
modelMaxContexts?: string // 模型的上下文的最大 Message 数量
} }
export interface ChatConversationUpdateVO { export interface ChatConversationUpdateVO {
...@@ -24,7 +28,7 @@ export interface ChatConversationUpdateVO { ...@@ -24,7 +28,7 @@ export interface ChatConversationUpdateVO {
maxContexts: string // 上下文的最大 Message 数量 maxContexts: string // 上下文的最大 Message 数量
} }
// AI chat 聊天 // AI 聊天会话 API
export const ChatConversationApi = { export const ChatConversationApi = {
// 获取 Conversation // 获取 Conversation
get: async (id: string) => { get: async (id: string) => {
...@@ -34,4 +38,14 @@ export const ChatConversationApi = { ...@@ -34,4 +38,14 @@ export const ChatConversationApi = {
update: async (data: ChatConversationUpdateVO) => { update: async (data: ChatConversationUpdateVO) => {
return await request.put({ url: `/ai/chat/conversation/update`, data}) 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` })
}
} }
import request from '@/config/axios' import request from '@/config/axios'
import {fetchEventSource} from '@microsoft/fetch-event-source'; import { fetchEventSource } from '@microsoft/fetch-event-source'
import {getAccessToken} from '@/utils/auth' import { getAccessToken } from '@/utils/auth'
import {config} from '@/config/axios/config' import { config } from '@/config/axios/config'
// 聊天VO // 聊天VO
export interface ChatMessageVO { export interface ChatMessageVO {
...@@ -24,26 +24,28 @@ export interface ChatMessageSendVO { ...@@ -24,26 +24,28 @@ export interface ChatMessageSendVO {
// AI chat 聊天 // AI chat 聊天
export const ChatMessageApi = { export const ChatMessageApi = {
// 消息列表 // 消息列表
messageList: async (conversationId: string) => { 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 消息
add: async (data: ChatMessageSendVO) => { 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 消息
send: async (data: ChatMessageSendVO) => { 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 消息 // 发送 send stream 消息
// TODO axios 可以么? https://apifox.com/apiskills/how-to-create-axios-stream/
sendStream: async (id: string, ctrl, onMessage, onError, onClose) => { sendStream: async (id: string, ctrl, onMessage, onError, onClose) => {
const token = getAccessToken() 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', method: 'post',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
...@@ -51,18 +53,17 @@ export const ChatMessageApi = { ...@@ -51,18 +53,17 @@ export const ChatMessageApi = {
}, },
openWhenHidden: true, openWhenHidden: true,
body: JSON.stringify({ body: JSON.stringify({
id: id, id: id
}), }),
onmessage: onMessage, onmessage: onMessage,
onerror:onError, onerror: onError,
onclose: onClose, onclose: onClose,
signal: ctrl.signal, signal: ctrl.signal
}); })
}, },
// 发送 send 消息 // 发送 send 消息
delete: async (id: string) => { delete: async (id: string) => {
return await request.delete({ url: `/ai/chat/message/delete?id=${id}` }) return await request.delete({ url: `/ai/chat/message/delete?id=${id}` })
}, }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment