From 01bf39bc89f082b617b8cdd8fa5c30cfd0c8469b Mon Sep 17 00:00:00 2001 From: fxjhello <2395112364@qq.com> Date: Sat, 13 May 2023 16:59:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- views/src/api/chat.ts | 12 ++- views/src/views/chat/index.vue | 84 ++++++++----------- views/src/views/chat/layout/sider/index.vue | 65 +++++++------- .../sider/{ => knowledge-base}/filelist.vue | 71 +++++++++------- .../layout/sider/knowledge-base/index.vue | 74 ++++++++++++++++ views/vite.config.ts | 3 +- 6 files changed, 184 insertions(+), 125 deletions(-) rename views/src/views/chat/layout/sider/{ => knowledge-base}/filelist.vue (65%) create mode 100644 views/src/views/chat/layout/sider/knowledge-base/index.vue diff --git a/views/src/api/chat.ts b/views/src/api/chat.ts index fa3db29..9d664e9 100644 --- a/views/src/api/chat.ts +++ b/views/src/api/chat.ts @@ -3,7 +3,7 @@ import { api } from './api' export const chat = (params: any) => { return api({ - url: '/chat-docs/chatno', + url: '/chat', method: 'post', data: JSON.stringify(params), }) @@ -17,20 +17,18 @@ export const chatfile = (params: any) => { }) } -export const getfilelist = () => { +export const getfilelist = (knowledge_base_id: any) => { return api({ - url: '/chat-docs/list', + url: '/local_doc_qa/list_files', method: 'get', - params: { - knowledge_base_id: '123', - }, + params: { knowledge_base_id }, }) } export const deletefile = (params: any) => { return api({ - url: '/chat-docs/delete', + url: '/local_doc_qa/delete_file', method: 'post', data: JSON.stringify(params), }) diff --git a/views/src/views/chat/index.vue b/views/src/views/chat/index.vue index dc0393a..be82813 100644 --- a/views/src/views/chat/index.vue +++ b/views/src/views/chat/index.vue @@ -13,19 +13,18 @@ import HeaderComponent from './components/Header/index.vue' import { HoverButton, SvgIcon } from '@/components/common' import { useBasicLayout } from '@/hooks/useBasicLayout' import { useChatStore, usePromptStore } from '@/store' -import { fetchChatAPIProcess } from '@/api' import { t } from '@/locales' import { chat, chatfile } from '@/api/chat' let controller = new AbortController() -const openLongReply = import.meta.env.VITE_GLOB_OPEN_LONG_REPLY === 'true' +// const openLongReply = import.meta.env.VITE_GLOB_OPEN_LONG_REPLY === 'true' const route = useRoute() const dialog = useDialog() const ms = useMessage() const chatStore = useChatStore() - +const history = ref([]) const { isMobile } = useBasicLayout() const { addChat, updateChat, updateChatSome, getChatByUuidAndIndex } = useChat() const { scrollRef, scrollToBottom, scrollToBottomIfAtBottom } = useScroll() @@ -61,7 +60,9 @@ function handleSubmit() { async function onConversation() { const message = prompt.value - + dataSources.value.forEach((item) => { + console.log(item) + }) if (loading.value) return @@ -110,13 +111,13 @@ async function onConversation() { const lastText = '' const fetchChatAPIOnce = async () => { const res = active.value - ? await chatfile({ message }) + ? await chatfile({ + question: message, + history: history.value, + }) : await chat({ question: message, - history: [[ - '工伤保险是什么?', - '工伤保险是指用人单位按照国家规定,为本单位的职工和用人单位的其他人员,缴纳工伤保险费,由保险机构按照国家规定的标准,给予工伤保险待遇的社会保险制度。', - ]], + history: history.value, }) const result = active.value ? res.data.response.text : res.data.response updateChat( @@ -230,7 +231,6 @@ async function onConversation() { loading.value = false } } - async function onRegenerate(index: number) { if (loading.value) return @@ -239,7 +239,7 @@ async function onRegenerate(index: number) { const { requestOptions } = dataSources.value[index] - let message = requestOptions?.prompt ?? '' + const message = requestOptions?.prompt ?? '' let options: Chat.ConversationRequest = {} @@ -263,48 +263,30 @@ async function onRegenerate(index: number) { ) try { - let lastText = '' + const lastText = '' const fetchChatAPIOnce = async () => { - await fetchChatAPIProcess({ - prompt: message, - options, - signal: controller.signal, - onDownloadProgress: ({ event }) => { - const xhr = event.target - const { responseText } = xhr - // Always process the final line - const lastIndex = responseText.lastIndexOf('\n', responseText.length - 2) - let chunk = responseText - if (lastIndex !== -1) - chunk = responseText.substring(lastIndex) - try { - const data = JSON.parse(chunk) - updateChat( - +uuid, - index, - { - dateTime: new Date().toLocaleString(), - text: lastText + (data.text ?? ''), - inversion: false, - error: false, - loading: true, - conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id }, - requestOptions: { prompt: message, options: { ...options } }, - }, - ) - - if (openLongReply && data.detail.choices[0].finish_reason === 'length') { - options.parentMessageId = data.id - lastText = data.text - message = '' - return fetchChatAPIOnce() - } - } - catch (error) { - // - } + const res = active.value + ? await chatfile({ message }) + : await chat({ + question: message, + history: history.value, + }) + const result = active.value ? res.data.response.text : res.data.response + updateChat( + +uuid, + dataSources.value.length - 1, + { + dateTime: new Date().toLocaleString(), + text: lastText + (result ?? ''), + inversion: false, + error: false, + loading: false, + conversationOptions: null, + requestOptions: { prompt: message, options: { ...options } }, }, - }) + ) + scrollToBottomIfAtBottom() + loading.value = false updateChatSome(+uuid, index, { loading: false }) } await fetchChatAPIOnce() diff --git a/views/src/views/chat/layout/sider/index.vue b/views/src/views/chat/layout/sider/index.vue index 8a49409..8629c38 100644 --- a/views/src/views/chat/layout/sider/index.vue +++ b/views/src/views/chat/layout/sider/index.vue @@ -1,9 +1,9 @@