test(api): 测试接口,梳理逻辑

This commit is contained in:
何嘉悦 2025-05-29 01:27:35 +08:00
parent 4b39bbb2a5
commit d2275d5932
5 changed files with 86 additions and 42 deletions

View File

@ -1,8 +1,15 @@
import type { ChatMessageVo, GetChatListParams, SendDTO } from './types';
import { get, post } from '@/utils/request';
// 发送消息
export const send = (data: SendDTO) => post<null>('/chat/send', data).stream();
// 新增对应会话聊天记录
export function addChat(data: ChatMessageVo) {
return post('/system/message', data);
}
// 获取当前会话的聊天记录
export function getChatList(params: GetChatListParams) {
return get<ChatMessageVo[]>('/system/message/list', params);
}

View File

@ -1,5 +1,3 @@
import type { ModelType } from '@/constants/enums';
/**
* ChatRequest
*/
@ -21,7 +19,8 @@ export interface SendDTO {
*/
kid?: string;
messages: Message[];
model: ModelType;
// model: ModelType;
model?: string;
/**
*
*/
@ -161,7 +160,7 @@ export interface GetChatListParams {
/**
* id
*/
sessionId?: number;
sessionId?: string;
/**
* Tokens
*/

View File

@ -6,7 +6,9 @@ import type { ThinkingStatus } from 'vue-element-plus-x/types/Thinking';
import { Loading, Position } from '@element-plus/icons-vue';
import { useXStream } from 'vue-element-plus-x';
import { useRoute } from 'vue-router';
import { send } from '@/api/chat/index';
import { useChatStore } from '@/stores/modules/chat';
import { useModelStore } from '@/stores/modules/model';
type MessageItem = BubbleProps & {
key: number;
@ -16,15 +18,16 @@ type MessageItem = BubbleProps & {
expanded?: boolean;
};
const { startStream, cancel, data, error, isLoading } = useXStream();
const { startStream: _, cancel, data, error, isLoading } = useXStream();
const BASE_URL = 'https://api.siliconflow.cn/v1/chat/completions';
// const BASE_URL = 'https://api.siliconflow.cn/v1/chat/completions';
//
const API_KEY = 'sk-vfjyscildobjnrijtcllnkhtcouidcxdgjxtldzqzeowrbga';
const MODEL = 'THUDM/GLM-Z1-9B-0414';
// const API_KEY = 'sk-vfjyscildobjnrijtcllnkhtcouidcxdgjxtldzqzeowrbga';
// const MODEL = 'THUDM/GLM-Z1-9B-0414';
const route = useRoute();
const chatStore = useChatStore();
const modelStore = useModelStore();
const isDeepThinking = computed(() => chatStore.isDeepThinking);
const inputValue = ref('帮我写一篇小米手机介绍');
const senderRef = ref<any>(null);
@ -34,9 +37,29 @@ const processedIndex = ref(0);
watch(
() => route.params?.id,
(_id_) => {
async (_id_) => {
if (_id_) {
chatStore.requestChatList(Number(_id_));
await chatStore.requestChatList(`${_id_}`);
// id
if (chatStore.chatMap[`${_id_}`] && chatStore.chatMap[`${_id_}`].length) {
bubbleItems.value = chatStore.chatMap[`${_id_}`].map((item: any) => ({
key: item.id,
avatar:
item.role === 'user'
? 'https://avatars.githubusercontent.com/u/76239030?v=4'
: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
content: item.content,
avatarSize: '32px',
role: item.role,
typing: false,
}));
//
setTimeout(() => {
bubbleListRef.value!.scrollToBottom();
}, 350);
}
const v = localStorage.getItem('chatContent');
if (v) {
inputValue.value = v;
@ -122,28 +145,42 @@ async function startSSE() {
// BubbleList
bubbleListRef.value!.scrollToBottom();
const response = await fetch(BASE_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
'Accept': 'text/event-stream',
},
body: JSON.stringify({
model: MODEL,
messages: bubbleItems.value
.filter((item: any) => item.role === 'user')
.map((item: any) => ({
role: item.role,
content: item.content,
})),
stream: true,
}),
const res = await send({
messages: bubbleItems.value
.filter((item: any) => item.role === 'user')
.map((item: any) => ({
role: item.role,
content: item.content,
})),
sessionId: Number(route.params?.id),
userId: 1,
model: modelStore.currentModelInfo.modelName ?? '',
});
const readableStream = response.body!;
//
processedIndex.value = 0;
await startStream({ readableStream });
console.log('res', res);
// const response = await fetch(BASE_URL, {
// method: 'POST',
// headers: {
// 'Authorization': `Bearer ${API_KEY}`,
// 'Content-Type': 'application/json',
// 'Accept': 'text/event-stream',
// },
// body: JSON.stringify({
// model: MODEL,
// messages: bubbleItems.value
// .filter((item: any) => item.role === 'user')
// .map((item: any) => ({
// role: item.role,
// content: item.content,
// })),
// stream: true,
// }),
// });
// const readableStream = response.body!;
// //
// processedIndex.value = 0;
// await startStream({ readableStream });
}
catch (err) {
handleError(err);

View File

@ -6,13 +6,22 @@ import { useUserStore } from './user';
export const useChatStore = defineStore('chat', () => {
const userStore = useUserStore();
const chatMap = ref<Record<number, ChatMessageVo[]>>({});
// 是否开启深度思考
const isDeepThinking = ref<boolean>(false);
const setChatMap = (id: number, data: ChatMessageVo[]) => {
const setDeepThinking = (value: boolean) => {
isDeepThinking.value = value;
};
// 会议ID对应-聊天记录 map对象
const chatMap = ref<Record<string, ChatMessageVo[]>>({});
const setChatMap = (id: string, data: ChatMessageVo[]) => {
chatMap.value[id] = data;
};
const requestChatList = async (sessionId: number) => {
// 获取当前会话的聊天记录
const requestChatList = async (sessionId: string) => {
// 如果没有 token 则不查询聊天记录
if (!userStore.token)
return;
@ -30,13 +39,6 @@ export const useChatStore = defineStore('chat', () => {
}
};
// 是否开启深度思考
const isDeepThinking = ref<boolean>(false);
const setDeepThinking = (value: boolean) => {
isDeepThinking.value = value;
};
return {
chatMap,
requestChatList,

View File

@ -19,7 +19,6 @@ export const useModelStore = defineStore('model', () => {
try {
const res = await getModelList();
modelList.value = res.data;
console.log('模型列表', res.data);
}
catch (error) {
console.error('requestModelList错误', error);