index.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <script setup lang="ts">
  2. import { computed, onMounted, ref, toRaw } from 'vue';
  3. import SvgIcon from '@/components/SvgIcon/index.vue';
  4. import Chat from './components/Chat.vue';
  5. import { list as getKnowledgeList } from '@/api/aigc/knowledge';
  6. import { list as getPromptList } from '@/api/aigc/prompt';
  7. import { modelList } from '@/api/models';
  8. import { getMessages, clean } from '@/api/aigc/chat';
  9. import { useChatStore } from './components/store/useChatStore';
  10. import { useDialog, useMessage } from 'naive-ui';
  11. import DataTable from './components/DataTable.vue';
  12. const dialog = useDialog();
  13. const ms = useMessage();
  14. const checked = ref();
  15. const activeTab = ref('knowledge');
  16. const list = ref();
  17. const knowledgeList = ref();
  18. const promptList = ref();
  19. const value = ref('');
  20. const loading = ref(true);
  21. const chatLoading = ref(false);
  22. const model = ref('gpt-4');
  23. const chatStore = useChatStore();
  24. onMounted(async () => {
  25. loading.value = true;
  26. const data = await getKnowledgeList({});
  27. let arr: any[] = [];
  28. data.forEach((item) => {
  29. item.docs = item.docs.map((opt) => ({
  30. label: opt.name,
  31. value: opt.id,
  32. }));
  33. arr.push(item);
  34. });
  35. knowledgeList.value = arr;
  36. console.log('xxx', knowledgeList.value);
  37. promptList.value = await getPromptList({});
  38. list.value = knowledgeList.value;
  39. loading.value = false;
  40. });
  41. async function onCheck(item: any) {
  42. checked.value = item;
  43. chatLoading.value = true;
  44. chatStore.messages = [];
  45. chatStore.conversationId = item.id;
  46. chatStore.knowledge = null;
  47. chatStore.prompt = null;
  48. chatStore.messages = await getMessages(item.id);
  49. if (activeTab.value === 'knowledge') {
  50. chatStore.knowledge = item;
  51. }
  52. if (activeTab.value === 'prompt') {
  53. chatStore.prompt = item;
  54. }
  55. chatLoading.value = false;
  56. }
  57. async function onUpdate(val: string) {
  58. activeTab.value = val;
  59. if (val === 'knowledge') {
  60. list.value = knowledgeList.value;
  61. }
  62. if (val === 'prompt') {
  63. list.value = promptList.value;
  64. }
  65. }
  66. function onUpdateSelect(val, opt) {
  67. console.log(val, opt);
  68. }
  69. // 清除
  70. function handleClear() {
  71. if (loading.value || chatStore.conversationId == null) {
  72. return;
  73. }
  74. dialog.warning({
  75. title: '清除聊天',
  76. content: '确认清除聊天',
  77. positiveText: '是',
  78. negativeText: '否',
  79. onPositiveClick: async () => {
  80. await clean(chatStore.conversationId);
  81. ms.success('聊天记录清除成功');
  82. },
  83. });
  84. }
  85. </script>
  86. <template>
  87. <n-layout has-sider class="h-full w-full">
  88. <n-layout-sider :width="350" bordered :native-scrollbar="false">
  89. <div class="px-2 py-2 pb-0">
  90. <n-tabs type="segment" animated @update:value="onUpdate">
  91. <n-tab-pane name="knowledge" tab="知识库" />
  92. <n-tab-pane name="prompt" tab="提示词库" />
  93. </n-tabs>
  94. </div>
  95. <n-spin :show="loading">
  96. <div class="flex justify-center items-center p-4 pt-0 rounded overflow-y-auto">
  97. <ul class="mt-2 space-y-3 w-full">
  98. <li v-for="(item, idx) in list" :key="idx" @click="onCheck(item)">
  99. <n-popselect
  100. v-model:value="value"
  101. :options="item.docs"
  102. placement="right"
  103. @update:value="onUpdateSelect"
  104. :show="item.isStruct !== undefined && item.isStruct && item == checked"
  105. >
  106. <label :for="item.name" class="block relative">
  107. <input
  108. :id="item.name"
  109. type="radio"
  110. :checked="item == checked"
  111. name="payment"
  112. class="sr-only peer"
  113. />
  114. <div
  115. class="w-full flex gap-x-3 items-start p-4 pb-2.5 cursor-pointer rounded-lg border bg-white shadow-sm ring-indigo-600 peer-checked:bg-indigo-50/75 peer-checked:ring-1 duration-200"
  116. >
  117. <div class="flex-none">
  118. <SvgIcon
  119. v-if="activeTab === 'prompt'"
  120. class="text-3xl"
  121. icon="solar:document-bold-duotone"
  122. />
  123. <template v-if="activeTab === 'knowledge'">
  124. <n-avatar v-if="item.cover !== null" :src="item.cover" />
  125. <SvgIcon v-else class="text-3xl" icon="fa-solid:book" />
  126. </template>
  127. </div>
  128. <div>
  129. <div class="leading-none text-gray-800 font-medium text-[15px] pr-3">
  130. {{ item.name }}
  131. </div>
  132. <p class="text-xs text-gray-600 mt-[9px]">
  133. <n-ellipsis :line-clamp="2" :tooltip="false" expand-trigger="click">
  134. {{ item.des == undefined ? item.prompt : item.des }}
  135. </n-ellipsis>
  136. </p>
  137. </div>
  138. </div>
  139. </label>
  140. </n-popselect>
  141. </li>
  142. </ul>
  143. </div>
  144. </n-spin>
  145. </n-layout-sider>
  146. <div class="flex flex-col gap-1 items-center w-full mt-0">
  147. <n-split direction="vertical" :default-size="0" :resize-trigger-size="0">
  148. <template #1>
  149. <div class="w-full p-2 mb-4 h-full">
  150. <span
  151. class="inline-flex items-center mb-2 gap-x-2 rounded-full bg-green-600/20 px-2.5 py-1 text-sm font-semibold leading-5 text-green-600"
  152. >
  153. <span class="inline-block h-1.5 w-1.5 rounded-full bg-green-600"></span>
  154. Approved
  155. </span>
  156. <DataTable />
  157. </div>
  158. </template>
  159. <template #2>
  160. <div class="p-8 pt-6 w-full h-full mb-2">
  161. <div class="mb-2 flex flex-wrap justify-between items-center">
  162. <div class="font-bold flex justify-center items-center flex-wrap gap-2">
  163. <SvgIcon class="text-lg" icon="ion:sparkles-outline" />
  164. <span>AI对话</span>
  165. </div>
  166. <n-space align="center">
  167. <n-select
  168. size="small"
  169. v-model:value="chatStore.model"
  170. :options="modelList"
  171. :consistent-menu-width="false"
  172. class="!w-auto"
  173. />
  174. <n-button @click="handleClear" size="small" type="success" secondary>
  175. <template #icon>
  176. <SvgIcon class="text-[14px]" icon="fluent:delete-12-regular" />
  177. </template>
  178. 清空聊天
  179. </n-button>
  180. </n-space>
  181. </div>
  182. <div class="w-full h-full rounded-md p-2 flex items-center justify-center">
  183. <n-spin :show="chatLoading">
  184. <Chat
  185. :id="checked.id"
  186. :model="model"
  187. v-if="checked !== undefined && checked.id !== undefined"
  188. />
  189. <div v-else class="w-full h-full flex items-center justify-center">
  190. <n-empty description="请先选中左侧的知识库或者提示词列表开始聊天!">
  191. <template #extra>
  192. <n-button size="small" type="success"> 立即开始 </n-button>
  193. </template>
  194. </n-empty>
  195. </div>
  196. </n-spin>
  197. </div>
  198. </div>
  199. </template>
  200. </n-split>
  201. </div>
  202. </n-layout>
  203. </template>
  204. <style scoped lang="less">
  205. ::v-deep(.n-tabs.n-tabs--top .n-tab-pane) {
  206. padding: 0 !important;
  207. }
  208. </style>