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 type { ChatMessageVo, GetChatListParams, SendDTO } from './types';
import { get, post } from '@/utils/request'; import { get, post } from '@/utils/request';
// 发送消息
export const send = (data: SendDTO) => post<null>('/chat/send', data).stream(); 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) { export function getChatList(params: GetChatListParams) {
return get<ChatMessageVo[]>('/system/message/list', params); return get<ChatMessageVo[]>('/system/message/list', params);
} }

View File

@ -1,5 +1,3 @@
import type { ModelType } from '@/constants/enums';
/** /**
* ChatRequest * ChatRequest
*/ */
@ -21,7 +19,8 @@ export interface SendDTO {
*/ */
kid?: string; kid?: string;
messages: Message[]; messages: Message[];
model: ModelType; // model: ModelType;
model?: string;
/** /**
* *
*/ */
@ -161,7 +160,7 @@ export interface GetChatListParams {
/** /**
* id * id
*/ */
sessionId?: number; sessionId?: string;
/** /**
* Tokens * 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 { Loading, Position } from '@element-plus/icons-vue';
import { useXStream } from 'vue-element-plus-x'; import { useXStream } from 'vue-element-plus-x';
import { useRoute } from 'vue-router'; import { useRoute } from 'vue-router';
import { send } from '@/api/chat/index';
import { useChatStore } from '@/stores/modules/chat'; import { useChatStore } from '@/stores/modules/chat';
import { useModelStore } from '@/stores/modules/model';
type MessageItem = BubbleProps & { type MessageItem = BubbleProps & {
key: number; key: number;
@ -16,15 +18,16 @@ type MessageItem = BubbleProps & {
expanded?: boolean; 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 API_KEY = 'sk-vfjyscildobjnrijtcllnkhtcouidcxdgjxtldzqzeowrbga';
const MODEL = 'THUDM/GLM-Z1-9B-0414'; // const MODEL = 'THUDM/GLM-Z1-9B-0414';
const route = useRoute(); const route = useRoute();
const chatStore = useChatStore(); const chatStore = useChatStore();
const modelStore = useModelStore();
const isDeepThinking = computed(() => chatStore.isDeepThinking); const isDeepThinking = computed(() => chatStore.isDeepThinking);
const inputValue = ref('帮我写一篇小米手机介绍'); const inputValue = ref('帮我写一篇小米手机介绍');
const senderRef = ref<any>(null); const senderRef = ref<any>(null);
@ -34,9 +37,29 @@ const processedIndex = ref(0);
watch( watch(
() => route.params?.id, () => route.params?.id,
(_id_) => { async (_id_) => {
if (_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'); const v = localStorage.getItem('chatContent');
if (v) { if (v) {
inputValue.value = v; inputValue.value = v;
@ -122,28 +145,42 @@ async function startSSE() {
// BubbleList // BubbleList
bubbleListRef.value!.scrollToBottom(); bubbleListRef.value!.scrollToBottom();
const response = await fetch(BASE_URL, { const res = await send({
method: 'POST', messages: bubbleItems.value
headers: { .filter((item: any) => item.role === 'user')
'Authorization': `Bearer ${API_KEY}`, .map((item: any) => ({
'Content-Type': 'application/json', role: item.role,
'Accept': 'text/event-stream', content: item.content,
}, })),
body: JSON.stringify({ sessionId: Number(route.params?.id),
model: MODEL, userId: 1,
messages: bubbleItems.value model: modelStore.currentModelInfo.modelName ?? '',
.filter((item: any) => item.role === 'user')
.map((item: any) => ({
role: item.role,
content: item.content,
})),
stream: true,
}),
}); });
const readableStream = response.body!;
// console.log('res', res);
processedIndex.value = 0;
await startStream({ readableStream }); // 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) { catch (err) {
handleError(err); handleError(err);

View File

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

View File

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