Browse Source

add: 知识库关联对话

master
tiantian 5 months ago
parent
commit
210141ede8
  1. 5
      frontend/src/components/localchat/AiChatMain.vue
  2. 115
      frontend/src/stores/aichat.ts

5
frontend/src/components/localchat/AiChatMain.vue

@ -21,6 +21,7 @@ const messageContainerRef = ref<InstanceType<typeof ElScrollbar>>();
const messageInnerRef = ref<HTMLDivElement>(); const messageInnerRef = ref<HTMLDivElement>();
// User Input Message // User Input Message
const userMessage = ref(""); const userMessage = ref("");
const knowledgeId = ref(0)
const promptMessage = computed(() => { const promptMessage = computed(() => {
return [ return [
{ {
@ -34,7 +35,9 @@ const promptMessage = computed(() => {
]; ];
}); });
onMounted(async () => { onMounted(async () => {
await chatStore.initChat() knowledgeId.value = win?.config?.knowledgeId || 0
console.log(knowledgeId.value, 'knowledgeId')
await chatStore.initChat(knowledgeId.value)
//await aiStore.initChat() //await aiStore.initChat()
}); });
const requestMessages = computed(() => { const requestMessages = computed(() => {

115
frontend/src/stores/aichat.ts

@ -1,10 +1,12 @@
import { defineStore } from "pinia" import { defineStore } from 'pinia'
import { db } from "./db.js" import { db } from './db.js'
import { t } from "@/i18n/index.ts" import { t } from '@/i18n/index.ts'
import { useAssistantStore } from "./assistant.ts"; import { useAssistantStore } from './assistant.ts'
import { useModelStore } from "@/stores/model"; import { useModelStore } from '@/stores/model'
import { ref } from "vue"; import { ref } from 'vue'
export const useAiChatStore = defineStore('aichat', () => { export const useAiChatStore = defineStore(
'aichat',
() => {
const modelStore = useModelStore() const modelStore = useModelStore()
const promptStore = useAssistantStore() const promptStore = useAssistantStore()
const activeId: any = ref(0) const activeId: any = ref(0)
@ -17,36 +19,69 @@ export const useAiChatStore = defineStore('aichat', () => {
const promptList: any = ref([]) const promptList: any = ref([])
const searchInput: any = ref('') const searchInput: any = ref('')
const showInfo = ref(false) const showInfo = ref(false)
const editInfo: any = ref({}); //编辑聊天信息 const editInfo: any = ref({}) //编辑聊天信息
const isEditor = ref(true); const isEditor = ref(true)
const newChat = async () => { const newChat = async (knowledgeId: any) => {
const currentModel = await modelStore.getModel('chat') const currentModel = await modelStore.getModel('chat')
if (!currentModel) { if (!currentModel) {
return false return false
} }
const promptData = await promptStore.getPrompt('chat') const promptData = await promptStore.getPrompt('chat')
return await addChat(t('aichat.newchat'), currentModel, promptData, "") if (knowledgeId) {
return await addChat(
'知识库对话' + knowledgeId,
currentModel,
promptData,
knowledgeId
)
}
return await addChat(
t('aichat.newchat'),
currentModel,
promptData,
knowledgeId
)
} }
const getPrompt = async (type: string) => { const getPrompt = async (type: string) => {
return await promptStore.getPrompt(type) return await promptStore.getPrompt(type)
} }
const initChat = async () => { 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) { if (activeId.value === 0) {
await newChat() await newChat(knowledgeId)
} }
modelList.value = await modelStore.getModelAction('chat') modelList.value = await modelStore.getModelAction('chat')
const promptRes = await promptStore.getPrompts('chat') const promptRes = await promptStore.getPrompts('chat')
promptList.value = promptRes.list promptList.value = promptRes.list
chatList.value = await db.getAll('aichatlist') chatList.value = await db.getAll('aichatlist')
if (activeId.value > 0) { if (activeId.value > 0) {
messageList.value = await db.getByField('aichatmsg', 'chatId', activeId.value) messageList.value = await db.getByField(
'aichatmsg',
'chatId',
activeId.value
)
chatInfo.value = await db.getOne('aichatlist', activeId.value) chatInfo.value = await db.getOne('aichatlist', activeId.value)
} }
} }
const getActiveChat = async () => { const getActiveChat = async () => {
chatInfo.value = await db.getOne('aichatlist', activeId.value) chatInfo.value = await db.getOne('aichatlist', activeId.value)
messageList.value = await db.getByField('aichatmsg', 'chatId', activeId.value) messageList.value = await db.getByField(
'aichatmsg',
'chatId',
activeId.value
)
chatList.value = await db.getAll('aichatlist') chatList.value = await db.getAll('aichatlist')
return { chatInfo, messageList, chatList } return { chatInfo, messageList, chatList }
} }
@ -56,7 +91,12 @@ export const useAiChatStore = defineStore('aichat', () => {
return list return list
} }
// 添加聊天 // 添加聊天
async function addChat(title: string, modelData: any, promptData: any, knowledgeId: string) { async function addChat(
title: string,
modelData: any,
promptData: any,
knowledgeId: string
) {
const newChat = { const newChat = {
title, title,
prompt: promptData.prompt, prompt: promptData.prompt,
@ -64,12 +104,11 @@ export const useAiChatStore = defineStore('aichat', () => {
model: modelData.model, model: modelData.model,
engine: modelData.info.engine, engine: modelData.info.engine,
createdAt: Date.now(), createdAt: Date.now(),
knowledgeId knowledgeId,
} }
//console.log(newChat) //console.log(newChat)
activeId.value = await db.addOne('aichatlist', newChat) activeId.value = await db.addOne('aichatlist', newChat)
return activeId.value return activeId.value
} }
async function setActiveId(newId: number) { async function setActiveId(newId: number) {
activeId.value = newId activeId.value = newId
@ -81,15 +120,14 @@ export const useAiChatStore = defineStore('aichat', () => {
await db.delete('aichatlist', chatId) await db.delete('aichatlist', chatId)
await db.deleteByField('aichatmsg', 'chatId', chatId) await db.deleteByField('aichatmsg', 'chatId', chatId)
//如果删除的id是当前id //如果删除的id是当前id
let id; let id
if (chatId == activeId.value) { if (chatId == activeId.value) {
// //
const list = await db.getAll('aichatlist') const list = await db.getAll('aichatlist')
if (list.length > 0) { if (list.length > 0) {
id = list[0]['id'] id = list[0]['id']
} else { } else {
id = await newChat() id = await newChat(0)
} }
setActiveId(id) setActiveId(id)
} }
@ -107,7 +145,6 @@ export const useAiChatStore = defineStore('aichat', () => {
await db.clear('aichatmsg') await db.clear('aichatmsg')
} }
// 新增历史消息 // 新增历史消息
async function addMessages(chatId: number, message: any) { async function addMessages(chatId: number, message: any) {
const currentChat = await db.getOne('aichatlist', chatId) const currentChat = await db.getOne('aichatlist', chatId)
@ -136,7 +173,6 @@ export const useAiChatStore = defineStore('aichat', () => {
await db.deleteByField('aichatmsg', 'chatId', activeId.value) await db.deleteByField('aichatmsg', 'chatId', activeId.value)
messageList.value = [] messageList.value = []
} }
} }
// 更新聊天配置 // 更新聊天配置
@ -145,19 +181,19 @@ export const useAiChatStore = defineStore('aichat', () => {
return await db.update('aichatlist', chatId, config) return await db.update('aichatlist', chatId, config)
} }
const showBox = (flag: any) => { const showBox = (flag: any) => {
isEditor.value = flag; isEditor.value = flag
if (flag === true) { if (flag === true) {
editInfo.value = toRaw(chatInfo.value); editInfo.value = toRaw(chatInfo.value)
} else { } else {
editInfo.value = { editInfo.value = {
title: "", title: '',
model: "", model: '',
prompt: "", prompt: '',
promptId: "", promptId: '',
}; }
} }
showInfo.value = true; showInfo.value = true
}; }
return { return {
activeId, activeId,
chatList, chatList,
@ -184,19 +220,18 @@ export const useAiChatStore = defineStore('aichat', () => {
getChatHistory, getChatHistory,
clearChatHistory, clearChatHistory,
updateChat, updateChat,
showBox showBox,
} }
},
}, { {
persist: { persist: {
enabled: true, enabled: true,
strategies: [ strategies: [
{ {
storage: localStorage, storage: localStorage,
paths: [ paths: ['activeId'],
"activeId"
]
}, // name 字段用localstorage存储 }, // name 字段用localstorage存储
], ],
},
} }
}) )

Loading…
Cancel
Save