diff --git a/src/renderer/src/components/template-study/container/right.vue b/src/renderer/src/components/template-study/container/right.vue index 6d5b0e0..6330ddb 100644 --- a/src/renderer/src/components/template-study/container/right.vue +++ b/src/renderer/src/components/template-study/container/right.vue @@ -25,7 +25,7 @@ {{ curTemplate.ex3 == 1 ? '复制并创建个人模板' : '添加提示词' }} - 一键研读 + 一键研读 @@ -68,7 +68,7 @@
- + 重新研读 @@ -207,12 +207,114 @@ const getTempResult = () => { } }) }) - }) } + + +const prompt = ref('') + +// 是否重新研读 +const isAgain = ref(false) + +// 研读 +const getModelResult = async (index, item) => { + + let str = prompt.value + // 重新研读 + if(index && item){ + isAgain.value = true + // 清空研读结果 + childTempList.value[index].answer = '' + // 设置需要打字机效果 + childTempList.value[index].showTypewriter = true + // 滚动到该项的位置 + if (messageElements.value[index]) { + messageElements.value[index].scrollIntoView({ + behavior: 'smooth', + block: 'start', + }); + } + // 设置当前模板prompt + str = str.replace('{模板标题}',item.name) + str = str.replace('{模板内容}',item.prompt) + params.prompt = str + params.template = item.prompt + // 请求数据 + try { + childTempList.value[index].loading = true + const data = await requestModelData() + childTempList.value[index].answer = data.answer; + // 保存重新研读后的结果 + onEditResult(data.answer, index) + } finally { + childTempList.value[index].loading = false + } + + } + // 一键研读 + else{ + // 清空所有模板结果 + childTempList.value.forEach(item => { + if (item.answer) { + item.answer = '' + item.showTypewriter = true + } + }) + // 设置滚动到第一个位置 + messageElements.value[0].scrollIntoView({ + behavior: 'smooth', + block: 'start', + }) + for (let item of childTempList.value) { + try { + item.loading = true + + str = str.replace('{模板标题}',item.name) + str = str.replace('{模板内容}',item.prompt) + params.prompt = str + params.template = item.prompt + // 请求数据 + const data = await requestModelData() + + item.answer = data.answer + await onSaveTemp(item) + } finally { + item.loading = false + } + } + } + +} + +// 设置一个延迟函数 ms: 毫秒 +const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)); + +// 请求大模型接口获取模板研读结果 知识库模型 or 教学大模型 +const requestModelData = async () =>{ + // 教学大模型 + let data = null + if (curMode.value == 1) { + const res = await sendChart({ + content: params.prompt, + conversationId: conversation_id.value, + stream: false + }) + // 一键研读 设置一个1s 延迟 防止百度千帆请求过于频繁 + if(!isAdjust.value){ + await delay(1000); // 每个请求之间延迟 1 秒 + } + data = res.data + } + // 知识库模型 + else { + const res = await completion(params) + data = res.data + } + return data +} + + const messageElements = ref([]); // 存储所有消息元素的引用 - - // 处理页面滚动 const outerContainer = ref() let lastScrollTime = 0; @@ -223,8 +325,6 @@ const handleScroll = (index) => { const container = document.querySelector('.container-right-list'); if(isAgain.value){ scrollToTmp(index) - // const item = messageElements.value[index] - // item.scrollIntoView({ behavior: 'smooth', block: 'end' }) } else{ @@ -244,38 +344,29 @@ const handleScroll = (index) => { }); }); } - } } - lastScrollTime = now; } - } -// const tmpViewContainer = document.querySelector('.container-right-list') +// 处理重新研读 滚动 const scrollToTmp = (index) =>{ - - const now = Date.now(); - if (now - lastScrollTime >= scrollInterval) { - // 最后一个 滚到底部 - if(index == childTempList.value.length - 1){ - console.log(outerContainer.value.scrollHeight) - requestAnimationFrame(() => { - outerContainer.value.scrollTo({ - top: outerContainer.value.scrollHeight, - behavior: 'smooth', - }); + // 最后一个 滚到底部 + if(index == childTempList.value.length - 1){ + requestAnimationFrame(() => { + outerContainer.value.scrollTo({ + top: outerContainer.value.scrollHeight, + behavior: 'smooth', }); + }); + } + else{ + const item = messageElements.value[index] + if(item.clientHeight > outerContainer.value.clientHeight ){ + item.scrollIntoView({ behavior: 'smooth', block: 'end' }) } - else{ - const item = messageElements.value[index] - if(item.clientHeight > outerContainer.value.clientHeight ){ - item.scrollIntoView({ behavior: 'smooth', block: 'end' }) - } - } - lastScrollTime = now; - } + } } // 打字机完成后触发 @@ -285,16 +376,6 @@ const handleDone = () =>{ } } - -const handleComplete = (index) => { - // if (index < childTempList.value.length - 1) { - // document.querySelector(`.template-item:nth-child(${index + 2})`).scrollIntoView({ behavior: 'smooth' }); - // } else { - // outerContainer.value.scrollTop = outerContainer.value.scrollHeight; - // } -} - - // 模板切换 const changeTemplate = (val) => { ElMessageBox.confirm( @@ -362,106 +443,7 @@ const onEdit = (index, item) => { isEdit.value = true } -// 重新研读 -const prompt = ref('') -// 重新研读 -const isAgain = ref(false) -const againResult = async (index, item) => { - - isAgain.value = true - childTempList.value[index].answer = '' - childTempList.value[index].showTypewriter = true - return - // 滚动到该项的位置 - if (messageElements.value[index]) { - messageElements.value[index].scrollIntoView({ - behavior: 'smooth', - block: 'start', - }); - } - - try { - await nextTick() - childTempList.value[index].loading = true - item.aiShow = true - - let str = cloneDeep(prompt.value) - str = str.replace('{模板标题}',item.name) - str = str.replace('{模板内容}',item.prompt) - params.prompt = str - params.template = item.prompt - - let data = null; - // 教学大模型 - if (curMode.value == 1) { - const res = await sendChart({ - content: params.prompt, - conversationId: conversation_id.value, - stream: false - }) - data = res.data - } else { - // 知识库模型 - const res = await completion(params) - data = res.data - } - - childTempList.value[index].answer = data.answer; - - onEditResult(data.answer, index) - - } finally { - childTempList.value[index].loading = false - } -} - - -const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)); -// 一键研读 -const getCompletion = async () => { - - childTempList.value.forEach(item => { - if (item.answer) { - item.answer = '' - item.showTypewriter = true - } - }) - - for (let item of childTempList.value) { - try { - item.loading = true - item.aiShow = true - let str = cloneDeep(prompt.value) - str = str.replace('{模板标题}',item.name) - str = str.replace('{模板内容}',item.prompt) - params.prompt = str - params.template = item.prompt - // 教学大模型 - let data = null - if (curMode.value == 1) { - const res = await sendChart({ - content: params.prompt, - conversationId: conversation_id.value, - stream: false - }) - await delay(1000); // 每个请求之间延迟 1 秒 - data = res.data - } - // 知识库模型 - else { - const res = await completion(params) - data = res.data - } - - item.answer = data.answer - - await onSaveTemp(item) - } finally { - item.loading = false - } - } -} // 保存编辑后的研读内容 const onEditResult = async (answer, index) => {