feat: 模型思维链展示
This commit is contained in:
@@ -14,14 +14,17 @@ export const useChatStore = defineStore("chat", () => {
|
||||
const historyMessages = ref<IChatWithLLMRequest["messages"]>([]);
|
||||
// 是否正在响应
|
||||
const completing = ref<boolean>(false);
|
||||
// 是否正在思考
|
||||
const thinking = ref<boolean>(false);
|
||||
// 在线人数
|
||||
const onlineCount = ref<number>(0);
|
||||
|
||||
// 与 LLM 聊天
|
||||
const chatWithLLM = async (
|
||||
request: IChatWithLLMRequest,
|
||||
onProgress: (content: string) => void, // 接收进度回调
|
||||
getUsageInfo: (object: UsageInfo) => void = () => {}
|
||||
onProgress: (content: string) => void, // 接收内容进度回调
|
||||
getUsageInfo: (object: UsageInfo) => void = () => {}, // 接收使用信息回调
|
||||
getThinking: (thinkingContent: string) => void = () => {} // 接收思维链内容回调
|
||||
) => {
|
||||
if (completing.value) throw new Error("正在响应中");
|
||||
|
||||
@@ -35,6 +38,9 @@ export const useChatStore = defineStore("chat", () => {
|
||||
},
|
||||
(object: UsageInfo) => {
|
||||
getUsageInfo(object);
|
||||
},
|
||||
(thinkingContent: string) => {
|
||||
getThinking(thinkingContent);
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
@@ -60,6 +66,20 @@ export const useChatStore = defineStore("chat", () => {
|
||||
historyMessages.value = [];
|
||||
};
|
||||
|
||||
// 确保最后一条消息是助手消息,如果最后一条消息不是,就加一条空的占位,不然后面的思维链会丢失
|
||||
const ensureAssistantMessage = () => {
|
||||
if (
|
||||
historyMessages.value.length === 0 ||
|
||||
historyMessages.value[historyMessages.value.length - 1].role !==
|
||||
"assistant"
|
||||
) {
|
||||
historyMessages.value.push({
|
||||
role: "assistant",
|
||||
content: ""
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
watch(
|
||||
historyMessages,
|
||||
(newVal) => {
|
||||
@@ -72,23 +92,15 @@ export const useChatStore = defineStore("chat", () => {
|
||||
messages: newVal,
|
||||
model: modelInfo.value?.model_id
|
||||
},
|
||||
// 处理进度回调,文本
|
||||
(content) => {
|
||||
// 处理进度回调
|
||||
if (
|
||||
historyMessages.value.length === 0 ||
|
||||
historyMessages.value[historyMessages.value.length - 1].role !==
|
||||
"assistant"
|
||||
) {
|
||||
historyMessages.value.push({
|
||||
role: "assistant",
|
||||
content: ""
|
||||
});
|
||||
}
|
||||
ensureAssistantMessage();
|
||||
thinking.value = false;
|
||||
historyMessages.value[historyMessages.value.length - 1].content =
|
||||
content;
|
||||
},
|
||||
// 处理使用usage信息回调
|
||||
(usageInfo: UsageInfo) => {
|
||||
// 处理使用usage信息回调
|
||||
// 如果最后一条消息是助手的回复,则更新使用信息
|
||||
if (
|
||||
historyMessages.value.length > 0 &&
|
||||
@@ -98,6 +110,13 @@ export const useChatStore = defineStore("chat", () => {
|
||||
historyMessages.value[historyMessages.value.length - 1].usage =
|
||||
usageInfo;
|
||||
}
|
||||
},
|
||||
// 处理思维链内容回调
|
||||
(thinkingContent: string) => {
|
||||
ensureAssistantMessage();
|
||||
thinking.value = true;
|
||||
historyMessages.value[historyMessages.value.length - 1].thinking =
|
||||
thinkingContent;
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -129,6 +148,7 @@ export const useChatStore = defineStore("chat", () => {
|
||||
getModelList,
|
||||
modelList,
|
||||
modelInfo,
|
||||
onlineCount
|
||||
onlineCount,
|
||||
thinking
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user