AnswerView.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <script setup>
  2. import { ref, unref, computed, onMounted, onUnmounted } from 'vue';
  3. import { useMessage } from 'naive-ui';
  4. import { useChatStore } from '@/stores/modules/chatStore';
  5. import { BaseButton, RecodeCardItem, TheSubMenu, TheChatView, ChatWelcome } from '@/components';
  6. import { ChatAsk, ChatAnswer, ChatAgentInput } from '@/components/Chat';
  7. import { chatApi } from '@/api/chat';
  8. import { useInfinite, useScroll, useChat, useRecommend } from '@/composables';
  9. const ANSWER_ID_KEY = '@@id@@';
  10. let controller = new AbortController();
  11. const chatStore = useChatStore();
  12. const { recordList, isFetching, onScrolltolower, onReset } = useInfinite('/front/bigModel/qa/pageList', { module: 0 });
  13. const { scrollRef, scrollToBottom, scrollToBottomIfAtBottom } = useScroll();
  14. const { chatDataSource, addChat, updateChat, clearChat, updateById } = useChat();
  15. const { recommendList } = useRecommend({ type: 0 });
  16. const message = useMessage();
  17. const switchActive = ref(false);
  18. const activeItem = ref({});
  19. const isLoading = ref(false);
  20. const inputRef = ref(null);
  21. const recordActive = ref(null);
  22. const currenSessionId = ref(null);
  23. const isExistInHistory = computed(() => (recordList.value.findIndex(({ sessionId: sId }) => sId === unref(currenSessionId)) === -1));
  24. // 新建对话
  25. const handleCreateDialog = async () => {
  26. message.destroyAll();
  27. if (unref(isLoading)) {
  28. return message.warning('当前对话生成中');
  29. }
  30. if (!unref(chatDataSource).length) {
  31. return message.info('已切换最新会话');
  32. }
  33. inputRef.value.clearInpVal();
  34. currenSessionId.value = null;
  35. recordActive.value = null;
  36. clearChat();
  37. }
  38. // 查询对话详情
  39. const handleChatDetail = async ({ sessionId }) => {
  40. isLoading.value = false;
  41. recordActive.value = sessionId;
  42. controller.abort();
  43. inputRef.value.clearInpVal();
  44. const { data } = await chatApi.getAnswerHistoryDetail({ sessionId });
  45. chatDataSource.value = data.map(item => ({ ...item, loading: false, }));
  46. currenSessionId.value = sessionId;
  47. scrollToBottom();
  48. }
  49. const onRegenerate = async ({ question, realQuestion, tools }) => {
  50. controller = new AbortController();
  51. const sessionId = unref(currenSessionId);
  52. const params = {
  53. data: {
  54. sessionId,
  55. showVal: question,
  56. question: realQuestion || question,
  57. module: 0,
  58. isStrong: Number(unref(switchActive)),
  59. tools,
  60. prompt: null
  61. // TODO: 后续大概率需要删除
  62. // topP: 0.9,
  63. // temperature: 0.7
  64. },
  65. signal: controller.signal,
  66. onDownloadProgress: ({ event }) => {
  67. const xhr = event.target;
  68. const { responseText } = xhr;
  69. const [ answer ] = responseText.split(ANSWER_ID_KEY);
  70. updateChat({
  71. sessionId,
  72. question,
  73. answer,
  74. loading: true,
  75. delayLoading: false
  76. })
  77. scrollToBottomIfAtBottom();
  78. }
  79. }
  80. try {
  81. const { data } = await chatApi.getChatStream(params);
  82. const [answer, id] = data.split(ANSWER_ID_KEY);
  83. updateChat({
  84. id,
  85. sessionId,
  86. question,
  87. answer,
  88. loading: false,
  89. delayLoading: false
  90. })
  91. scrollToBottomIfAtBottom();
  92. }
  93. catch (error) {
  94. console.log("取消了请求 - catch", error);
  95. }
  96. finally {
  97. isLoading.value = false;
  98. onReset();
  99. }
  100. }
  101. // 提交问题
  102. const handleSubmit = async ({question, selectedOption, realQuestion = ''}) => {
  103. // 用于模拟 - 内容生成前置等待状态
  104. if (unref(isExistInHistory)) {
  105. const { data: sessionId } = await chatApi.getChatSessionTag();
  106. currenSessionId.value = sessionId;
  107. }
  108. isLoading.value = true;
  109. addChat({
  110. sessionId: unref(currenSessionId),
  111. question,
  112. realQuestion,
  113. answer: '',
  114. loading: true,
  115. delayLoading: true
  116. })
  117. scrollToBottom();
  118. setTimeout(() => onRegenerate({ question, realQuestion, tools: selectedOption?.tools || null }), 2 * 1000);
  119. }
  120. // 处理推荐问题
  121. const handleWelcomeRecommend = ({ question, realQuestion }) => {
  122. handleSubmit({question, realQuestion});
  123. }
  124. // 删除历史对话
  125. const handeChatDelete = async (id) => {
  126. await chatApi.deleteHistory(id);
  127. onReset();
  128. clearChat();
  129. message.success('删除成功');
  130. }
  131. // 停止问题生成
  132. const onStopChatStream = async ({ sessionId }) => {
  133. await chatApi.getStopChatStream(sessionId);
  134. return message.warning('已停止对话生成');
  135. }
  136. // 重新生成问题
  137. const onChatResetStream = ({ question }) => {
  138. handleSubmit({question});
  139. }
  140. onMounted(() => {
  141. const question = chatStore.chatQuestion;
  142. if (Object.keys(question).length) {
  143. handleWelcomeRecommend(chatStore.chatQuestion);
  144. chatStore.clearChatQuestion();
  145. }
  146. })
  147. onUnmounted(() => {
  148. controller.abort();
  149. })
  150. </script>
  151. <template>
  152. <section class="flex items-start h-full">
  153. <TheSubMenu title="历史记录" @scrollToLower="onScrolltolower" :loading="isFetching">
  154. <template #top>
  155. <div class="create-btn px-[11px] pb-[22px]">
  156. <BaseButton @click="handleCreateDialog" icon-name="tool-add-circle">新建对话</BaseButton>
  157. </div>
  158. </template>
  159. <div class="pr-[4px] text-[#5e5e5e]">
  160. <RecodeCardItem v-for="item, index in recordList" :key="item.sessionId + index" :title="item.showVal"
  161. :time="item.createTime" :data-item="item"
  162. :class="{ 'recode-card-item_active': recordActive === item.sessionId }" @on-click="handleChatDetail"
  163. @on-delete="handeChatDelete" />
  164. </div>
  165. </TheSubMenu>
  166. <TheChatView ref="scrollRef">
  167. <ChatWelcome title="您好,我是LibraAI专家问答" card-title="您可以试着问我:" :sub-title="[
  168. '期待与您一同规划和完成未来的工作。有任何重点或需讨论的事项,随时告诉我'
  169. ]" :card-content="recommendList" v-if="!chatDataSource.length" @on-click="handleWelcomeRecommend" />
  170. <div class="conversation-item" v-if="chatDataSource.length">
  171. <template v-for="item, index in chatDataSource" :key="item.id">
  172. <ChatAsk :content="item.question" :sessionId="item.sessionId"></ChatAsk>
  173. <ChatAnswer
  174. :id="item.id"
  175. :content="item.answer"
  176. :loading="item.loading"
  177. :delay-loading="item.delayLoading"
  178. :isSatisfied="item.isSatisfied"
  179. :isVisibleResetBtn="chatDataSource.length - 1 === index"
  180. isVisibleStopBtn
  181. @on-click-stop="onStopChatStream(item)"
  182. @on-click-icon="params => updateById(params)"
  183. @on-click-reset="onChatResetStream(item)"
  184. >
  185. </ChatAnswer>
  186. </template>
  187. </div>
  188. <template #footer>
  189. <ChatAgentInput
  190. :active-item="activeItem"
  191. ref="inputRef"
  192. v-model:loading="isLoading"
  193. v-model:switch="switchActive"
  194. @on-click="handleSubmit"
  195. @on-enter="handleSubmit"
  196. ></ChatAgentInput>
  197. <!-- <ChatInput ref="inputRef" v-model:loading="isLoading" v-model:switch="switchActive" @on-click="handleSubmit"
  198. @on-enter="handleSubmit"></ChatInput> -->
  199. </template>
  200. </TheChatView>
  201. </section>
  202. </template>