From 210141ede849d4cb4c18bce0e5b7f77c2b852015 Mon Sep 17 00:00:00 2001 From: tiantian <1012874180@qq.com> Date: Sat, 11 Jan 2025 15:02:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?add:=20=E7=9F=A5=E8=AF=86=E5=BA=93=E5=85=B3?= =?UTF-8?q?=E8=81=94=E5=AF=B9=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/localchat/AiChatMain.vue | 17 +- frontend/src/stores/aichat.ts | 405 ++++++++++-------- 2 files changed, 230 insertions(+), 192 deletions(-) diff --git a/frontend/src/components/localchat/AiChatMain.vue b/frontend/src/components/localchat/AiChatMain.vue index 4561504..09b97a0 100644 --- a/frontend/src/components/localchat/AiChatMain.vue +++ b/frontend/src/components/localchat/AiChatMain.vue @@ -21,6 +21,7 @@ const messageContainerRef = ref>(); const messageInnerRef = ref(); // User Input Message const userMessage = ref(""); +const knowledgeId = ref(0) const promptMessage = computed(() => { return [ { @@ -34,7 +35,9 @@ const promptMessage = computed(() => { ]; }); onMounted(async () => { - await chatStore.initChat() + knowledgeId.value = win?.config?.knowledgeId || 0 + console.log(knowledgeId.value, 'knowledgeId') + await chatStore.initChat(knowledgeId.value) //await aiStore.initChat() }); const requestMessages = computed(() => { @@ -95,7 +98,7 @@ const sendMessage = async () => { if (userMessage.value) { // Add the message to the list if (isPadding.value === true) return; - let saveMessage:any = { + let saveMessage: any = { content: userMessage.value, chatId: chatStore.activeId, role: "user", @@ -103,7 +106,7 @@ const sendMessage = async () => { createdAt: Date.now(), }; chatStore.messageList.push(saveMessage); - + await chatStore.addMessages(chatStore.activeId, saveMessage); // Clear the input @@ -129,7 +132,7 @@ const createCompletion = async () => { }; const chatConfig = modelStore.chatConfig.chat; - const knowledgeId = win?.config?.knowledgeId*1 || 0; + const knowledgeId = win?.config?.knowledgeId * 1 || 0; let postMsg: any = { messages: requestMessages.value, model: chatStore.chatInfo.model, @@ -139,7 +142,7 @@ const createCompletion = async () => { fileContent: fileContent.value, fileName: fileName.value, options: chatConfig, - knowledgeId:knowledgeId, + knowledgeId: knowledgeId, }; if (imageData.value != "") { const img2txtModel = await modelStore.getModel("img2txt"); @@ -158,10 +161,10 @@ const createCompletion = async () => { images: [imageData.value], }, ], - knowledgeId:knowledgeId, + knowledgeId: knowledgeId, }; } - + const postData: any = { method: "POST", body: JSON.stringify(postMsg), diff --git a/frontend/src/stores/aichat.ts b/frontend/src/stores/aichat.ts index a1fb28a..cb7138d 100644 --- a/frontend/src/stores/aichat.ts +++ b/frontend/src/stores/aichat.ts @@ -1,202 +1,237 @@ -import { defineStore } from "pinia" -import { db } from "./db.js" -import { t } from "@/i18n/index.ts" -import { useAssistantStore } from "./assistant.ts"; -import { useModelStore } from "@/stores/model"; -import { ref } from "vue"; -export const useAiChatStore = defineStore('aichat', () => { - const modelStore = useModelStore() - const promptStore = useAssistantStore() - const activeId: any = ref(0) - const currentMessage: any = ref({}) - // 聊天列表 - const chatList: any = ref([]) - const chatInfo: any = ref({}) - const messageList: any = ref([]) - const modelList:any = ref([]) - const promptList: any = ref([]) - const searchInput: any = ref('') - const showInfo = ref(false) - const editInfo: any = ref({}); //编辑聊天信息 - const isEditor = ref(true); +import { defineStore } from 'pinia' +import { db } from './db.js' +import { t } from '@/i18n/index.ts' +import { useAssistantStore } from './assistant.ts' +import { useModelStore } from '@/stores/model' +import { ref } from 'vue' +export const useAiChatStore = defineStore( + 'aichat', + () => { + const modelStore = useModelStore() + const promptStore = useAssistantStore() + const activeId: any = ref(0) + const currentMessage: any = ref({}) + // 聊天列表 + const chatList: any = ref([]) + const chatInfo: any = ref({}) + const messageList: any = ref([]) + const modelList: any = ref([]) + const promptList: any = ref([]) + const searchInput: any = ref('') + const showInfo = ref(false) + const editInfo: any = ref({}) //编辑聊天信息 + const isEditor = ref(true) - const newChat = async () => { - const currentModel = await modelStore.getModel('chat') - if (!currentModel) { - return false + const newChat = async (knowledgeId: any) => { + const currentModel = await modelStore.getModel('chat') + if (!currentModel) { + return false + } + const promptData = await promptStore.getPrompt('chat') + if (knowledgeId) { + return await addChat( + '知识库对话' + knowledgeId, + currentModel, + promptData, + knowledgeId + ) + } + return await addChat( + t('aichat.newchat'), + currentModel, + promptData, + knowledgeId + ) } - const promptData = await promptStore.getPrompt('chat') - return await addChat(t('aichat.newchat'), currentModel, promptData, "") - } - const getPrompt = async (type: string) => { - return await promptStore.getPrompt(type) - } - const initChat = async () => { - if (activeId.value === 0) { - await newChat() - } - modelList.value = await modelStore.getModelAction('chat') - const promptRes = await promptStore.getPrompts('chat') - promptList.value = promptRes.list - chatList.value = await db.getAll('aichatlist') - if(activeId.value > 0){ - messageList.value = await db.getByField('aichatmsg', 'chatId', activeId.value) + const getPrompt = async (type: string) => { + return await promptStore.getPrompt(type) + } + const initChat = async (knowledgeId: any) => { + if (knowledgeId) { + const chat = await db.getByField( + 'aichatlist', + 'knowledgeId', + knowledgeId + ) + if (chat.length) { + activeId.value = chat[0].id + } else { + await newChat(knowledgeId) + } + } + if (activeId.value === 0) { + await newChat(knowledgeId) + } + modelList.value = await modelStore.getModelAction('chat') + const promptRes = await promptStore.getPrompts('chat') + promptList.value = promptRes.list + chatList.value = await db.getAll('aichatlist') + if (activeId.value > 0) { + messageList.value = await db.getByField( + 'aichatmsg', + 'chatId', + activeId.value + ) + chatInfo.value = await db.getOne('aichatlist', activeId.value) + } + } + const getActiveChat = async () => { chatInfo.value = await db.getOne('aichatlist', activeId.value) + messageList.value = await db.getByField( + 'aichatmsg', + 'chatId', + activeId.value + ) + chatList.value = await db.getAll('aichatlist') + return { chatInfo, messageList, chatList } } - } - const getActiveChat = async () => { - chatInfo.value = await db.getOne('aichatlist', activeId.value) - messageList.value = await db.getByField('aichatmsg', 'chatId', activeId.value) - chatList.value = await db.getAll('aichatlist') - return { chatInfo, messageList, chatList } - } - const getChatList = async () => { - const list = await db.getAll('aichatlist') - chatList.value = list - return list - } - // 添加聊天 - async function addChat(title: string, modelData: any, promptData: any, knowledgeId: string) { - const newChat = { - title, - prompt: promptData.prompt, - promptId: promptData.id, - model: modelData.model, - engine: modelData.info.engine, - createdAt: Date.now(), - knowledgeId - } - //console.log(newChat) - activeId.value = await db.addOne('aichatlist', newChat) - return activeId.value - - } - async function setActiveId(newId: number) { - activeId.value = newId - chatInfo.value = await db.getOne('aichatlist', newId) - messageList.value = await db.getByField('aichatmsg', 'chatId', newId) - } - // 删除单个聊天 - async function deleteChat(chatId: number) { - await db.delete('aichatlist', chatId) - await db.deleteByField('aichatmsg', 'chatId', chatId) - //如果删除的id是当前id - let id; - if (chatId == activeId.value) { - // + const getChatList = async () => { const list = await db.getAll('aichatlist') - if (list.length > 0) { - id = list[0]['id'] - - } else { - id = await newChat() + chatList.value = list + return list + } + // 添加聊天 + async function addChat( + title: string, + modelData: any, + promptData: any, + knowledgeId: string + ) { + const newChat = { + title, + prompt: promptData.prompt, + promptId: promptData.id, + model: modelData.model, + engine: modelData.info.engine, + createdAt: Date.now(), + knowledgeId, } - setActiveId(id) + //console.log(newChat) + activeId.value = await db.addOne('aichatlist', newChat) + return activeId.value + } + async function setActiveId(newId: number) { + activeId.value = newId + chatInfo.value = await db.getOne('aichatlist', newId) + messageList.value = await db.getByField('aichatmsg', 'chatId', newId) + } + // 删除单个聊天 + async function deleteChat(chatId: number) { + await db.delete('aichatlist', chatId) + await db.deleteByField('aichatmsg', 'chatId', chatId) + //如果删除的id是当前id + let id + if (chatId == activeId.value) { + // + const list = await db.getAll('aichatlist') + if (list.length > 0) { + id = list[0]['id'] + } else { + id = await newChat(0) + } + setActiveId(id) + } + chatList.value = await db.getAll('aichatlist') } - chatList.value = await db.getAll('aichatlist') - } - - // 更新聊天菜单标题 - async function updateTitle(chatId: number, title: string) { - await db.update('aichatlist', chatId, { title }) - } - - // 清空所有Chat - async function clearChat() { - await db.clear('aichatlist') - await db.clear('aichatmsg') - } + // 更新聊天菜单标题 + async function updateTitle(chatId: number, title: string) { + await db.update('aichatlist', chatId, { title }) + } - // 新增历史消息 - async function addMessages(chatId: number, message: any) { - const currentChat = await db.getOne('aichatlist', chatId) - //console.log(currentChat) - if (currentChat) { - return await db.addOne('aichatmsg', message) + // 清空所有Chat + async function clearChat() { + await db.clear('aichatlist') + await db.clear('aichatmsg') } - } - async function getChat(chatId: number) { - const chats = await db.getOne('aichatlist', chatId) - //console.log(chats) - const messages = await db.getByField('aichatmsg', 'chatId', chatId) - return { chats, messages } - } + // 新增历史消息 + async function addMessages(chatId: number, message: any) { + const currentChat = await db.getOne('aichatlist', chatId) + //console.log(currentChat) + if (currentChat) { + return await db.addOne('aichatmsg', message) + } + } - // 获取指定id的聊天的历史记录 + async function getChat(chatId: number) { + const chats = await db.getOne('aichatlist', chatId) + //console.log(chats) + const messages = await db.getByField('aichatmsg', 'chatId', chatId) + return { chats, messages } + } - async function getChatHistory(chatId: number) { - return await db.getByField('aichatmsg', 'chatId', chatId) - } + // 获取指定id的聊天的历史记录 - // 删除指定id的聊天的历史记录 - async function clearChatHistory() { - if(activeId.value > 0){ - await db.deleteByField('aichatmsg', 'chatId', activeId.value) - messageList.value = [] + async function getChatHistory(chatId: number) { + return await db.getByField('aichatmsg', 'chatId', chatId) } - - } - // 更新聊天配置 - async function updateChat(config: any, chatId: number) { - //console.log(config) - return await db.update('aichatlist', chatId, config) - } - const showBox = (flag: any) => { - isEditor.value = flag; - if (flag === true) { - editInfo.value = toRaw(chatInfo.value); - } else { - editInfo.value = { - title: "", - model: "", - prompt: "", - promptId: "", - }; - } - showInfo.value = true; - }; - return { - activeId, - chatList, - messageList, - chatInfo, - currentMessage, - searchInput, - showInfo, - editInfo, - isEditor, - getPrompt, - modelList, - promptList, - initChat, - setActiveId, - getActiveChat, - getChatList, - addChat, - updateTitle, - deleteChat, - clearChat, - addMessages, - getChat, - getChatHistory, - clearChatHistory, - updateChat, - showBox - } + // 删除指定id的聊天的历史记录 + async function clearChatHistory() { + if (activeId.value > 0) { + await db.deleteByField('aichatmsg', 'chatId', activeId.value) + messageList.value = [] + } + } -}, { - persist: { - enabled: true, - strategies: [ - { - storage: localStorage, - paths: [ - "activeId" - ] - }, // name 字段用localstorage存储 - ], - } -}) + // 更新聊天配置 + async function updateChat(config: any, chatId: number) { + //console.log(config) + return await db.update('aichatlist', chatId, config) + } + const showBox = (flag: any) => { + isEditor.value = flag + if (flag === true) { + editInfo.value = toRaw(chatInfo.value) + } else { + editInfo.value = { + title: '', + model: '', + prompt: '', + promptId: '', + } + } + showInfo.value = true + } + return { + activeId, + chatList, + messageList, + chatInfo, + currentMessage, + searchInput, + showInfo, + editInfo, + isEditor, + getPrompt, + modelList, + promptList, + initChat, + setActiveId, + getActiveChat, + getChatList, + addChat, + updateTitle, + deleteChat, + clearChat, + addMessages, + getChat, + getChatHistory, + clearChatHistory, + updateChat, + showBox, + } + }, + { + persist: { + enabled: true, + strategies: [ + { + storage: localStorage, + paths: ['activeId'], + }, // name 字段用localstorage存储 + ], + }, + } +) From d9d58e5213a0386f7956c9f76188da786541eb4c Mon Sep 17 00:00:00 2001 From: tiantian <1012874180@qq.com> Date: Sat, 11 Jan 2025 15:19:37 +0800 Subject: [PATCH 2/2] change knowledgeId --- frontend/src/components/localchat/AiChatMain.vue | 2 +- frontend/src/stores/aichat.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/localchat/AiChatMain.vue b/frontend/src/components/localchat/AiChatMain.vue index 09b97a0..a4892fe 100644 --- a/frontend/src/components/localchat/AiChatMain.vue +++ b/frontend/src/components/localchat/AiChatMain.vue @@ -132,7 +132,7 @@ const createCompletion = async () => { }; const chatConfig = modelStore.chatConfig.chat; - const knowledgeId = win?.config?.knowledgeId * 1 || 0; + const knowledgeId = chatStore.chatInfo.knowledgeId; let postMsg: any = { messages: requestMessages.value, model: chatStore.chatInfo.model, diff --git a/frontend/src/stores/aichat.ts b/frontend/src/stores/aichat.ts index cb7138d..2a42d1d 100644 --- a/frontend/src/stores/aichat.ts +++ b/frontend/src/stores/aichat.ts @@ -104,7 +104,7 @@ export const useAiChatStore = defineStore( model: modelData.model, engine: modelData.info.engine, createdAt: Date.now(), - knowledgeId, + knowledgeId: knowledgeId || 0, } //console.log(newChat) activeId.value = await db.addOne('aichatlist', newChat)