feat: 一些优化,以及token消耗展示
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { IChatWithLLMRequest, ModelInfo, ModelListInfo } from "@/interfaces";
|
||||
import type { IChatWithLLMRequest, ModelInfo, ModelListInfo, UsageInfo } from "@/interfaces";
|
||||
import { ChatService } from "@/services";
|
||||
|
||||
export const useChatStore = defineStore("chat", () => {
|
||||
@@ -16,6 +16,7 @@ export const useChatStore = defineStore("chat", () => {
|
||||
const chatWithLLM = async (
|
||||
request: IChatWithLLMRequest,
|
||||
onProgress: (content: string) => void, // 接收进度回调
|
||||
getUsageInfo: (object: UsageInfo) => void = () => { },
|
||||
) => {
|
||||
if (completing.value)
|
||||
throw new Error("正在响应中");
|
||||
@@ -24,6 +25,8 @@ export const useChatStore = defineStore("chat", () => {
|
||||
try {
|
||||
await ChatService.ChatWithLLM(token, request, (content) => {
|
||||
onProgress(content);
|
||||
}, (object: UsageInfo) => {
|
||||
getUsageInfo(object);
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
@@ -71,6 +74,15 @@ export const useChatStore = defineStore("chat", () => {
|
||||
});
|
||||
}
|
||||
historyMessages.value[historyMessages.value.length - 1].content = content;
|
||||
}, (usageInfo: UsageInfo) => {
|
||||
// 处理使用usage信息回调
|
||||
// 如果最后一条消息是助手的回复,则更新使用信息
|
||||
if (
|
||||
historyMessages.value.length > 0
|
||||
&& historyMessages.value[historyMessages.value.length - 1].role === "assistant"
|
||||
) {
|
||||
historyMessages.value[historyMessages.value.length - 1].usage = usageInfo;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user