talk.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <div class="d-flex flex-column" style="height: 100%">
  3. <v-banner single-line>
  4. <div class="my-3">
  5. {{ title }}
  6. </div>
  7. <template #actions>
  8. <slot name="title"></slot>
  9. </template>
  10. </v-banner>
  11. <div style="flex: 1; overflow: auto;" class="pa-3" ref="box">
  12. <div style="height: 100%;">
  13. <v-list three-line>
  14. <!-- 欢迎语 -->
  15. <v-list-item>
  16. <v-list-item-content>
  17. <v-list-item-title class="d-flex align-center" :class="system.nameColor + '--text'">
  18. <v-icon class="mr-3" :color="system.iconColor" >
  19. {{ system.icon }}
  20. </v-icon>
  21. {{ system.name }}
  22. </v-list-item-title>
  23. <v-list-item-subtitle class="d-flex">
  24. <div class="lighten-5 pa-3 round" :class="system.bgColor">
  25. {{ system.welcome }}
  26. </div>
  27. </v-list-item-subtitle>
  28. </v-list-item-content>
  29. </v-list-item>
  30. <v-list-item v-for="talk in talks" :key="talk.id">
  31. <v-list-item-content>
  32. <v-list-item-title
  33. class="d-flex align-center"
  34. :class="
  35. `${talk.type === 'question' ? 'justify-end ' + user.nameColor + '--text' : system.nameColor + '--text'}`
  36. "
  37. >
  38. {{ talk.type === 'question' ? formatName(baseInfo?.name ?? baseInfo?.phone) : '' }}
  39. <template v-if="talk.type === 'question'">
  40. <v-chip v-for="tag in talk.tags" :key="tag" x-small class="ml-2">{{ tag }}</v-chip>
  41. </template>
  42. <v-icon
  43. :class="talk.type === 'question' ? 'ml-3' : 'mr-3'"
  44. :color="talk.type === 'question' ? user.iconColor : system.iconColor"
  45. >
  46. {{ talk.type === 'question' ? user.icon : system.icon }}
  47. </v-icon>
  48. {{ talk.type === 'question' ? '' : system.name }}
  49. <template v-if="talk.type !== 'question'">
  50. <v-chip v-for="tag in talk.tags" :key="tag" x-small class="ml-2">{{ tag }}</v-chip>
  51. </template>
  52. </v-list-item-title>
  53. <v-list-item-subtitle class="d-flex" :class="{'justify-end': talk.type === 'question'}">
  54. <div class="lighten-5 pa-3 round" :class=" talk.type === 'question' ? user.bgColor : system.bgColor">
  55. <template v-if="talk.view === 'table'">
  56. <v-simple-table fixed-header dense height="350">
  57. <template v-slot:default>
  58. <thead>
  59. <tr>
  60. <th v-for="header in talk.content.headers" :key="header" class="text-left">
  61. {{header}}
  62. </th>
  63. </tr>
  64. </thead>
  65. <tbody>
  66. <tr
  67. v-for="(body, i) in talk.content.body"
  68. :key="i"
  69. >
  70. <td v-for="header in talk.content.headers" :key="header">{{ body[header] }}</td>
  71. </tr>
  72. </tbody>
  73. </template>
  74. </v-simple-table>
  75. </template>
  76. <template v-else>
  77. <div v-for="(content, index) in talk.content" :key="index + content">{{ content }}</div>
  78. </template>
  79. </div>
  80. </v-list-item-subtitle>
  81. </v-list-item-content>
  82. </v-list-item>
  83. <!-- loading -->
  84. <v-list-item style="min-height: 10px" v-show="loading">
  85. <v-list-item-content>
  86. <v-list-item-title class="d-flex align-center" :class="system.nameColor + '--text'">
  87. <v-icon class="mr-3" :color="system.iconColor">
  88. {{ system.icon }}
  89. </v-icon>
  90. <div class="mr-3">{{ system.name }} 搜索中 </div>
  91. <v-progress-circular
  92. size="18"
  93. width="2"
  94. indeterminate
  95. :color="system.iconColor"
  96. ></v-progress-circular>
  97. </v-list-item-title>
  98. </v-list-item-content>
  99. </v-list-item>
  100. </v-list>
  101. </div>
  102. </div>
  103. <div class="send d-flex align-center justify-center">
  104. <div class="send-box">
  105. <v-textarea
  106. v-model="question"
  107. dense
  108. class="send-box-area"
  109. auto-grow
  110. label="请输入您想问的内容,按 Ctrl+Enter 换行"
  111. placeholder="请输入您想问的内容,按 Ctrl+Enter 换行"
  112. solo
  113. hide-details
  114. no-resize
  115. rows="1"
  116. @keydown.enter="handleKeyCode($event)"
  117. >
  118. </v-textarea>
  119. <v-btn icon color="primary" class="btn" :disabled="!question" @click="handleSendMsg">
  120. <v-icon>mdi-send</v-icon>
  121. </v-btn>
  122. </div>
  123. </div>
  124. </div>
  125. </template>
  126. <script setup>
  127. import { ref, watch, nextTick } from 'vue'
  128. import { api } from '@/api/dataGovernance'
  129. import { formatName } from '@/utils/getText'
  130. import Snackbar from '@/plugins/snackbar'
  131. const props = defineProps({
  132. // 1 手册探索 2 图表实验室 3 产品知识库 (RAG + Graph) 4 产品知识库 (RAG)
  133. type: {
  134. type: Number,
  135. default: 1
  136. },
  137. title: {
  138. type: String,
  139. default: '数据探索'
  140. },
  141. welcome: {
  142. type: String,
  143. default: '您好,我是您的智能助手,有什么问题可以问我哦!'
  144. }
  145. })
  146. // 企业基本信息
  147. const baseInfo = ref(JSON.parse(localStorage.getItem('entBaseInfo')) || {})
  148. const box = ref(null)
  149. const loading = ref(false)
  150. const talks = ref([])
  151. const question = ref('')
  152. const id = ref(0)
  153. // 系统端信息配置
  154. const system = ref({
  155. name: '智能助手',
  156. nameColor: '#00B760',
  157. icon: 'mdi-face-agent',
  158. iconColor: '#00B760',
  159. bgColor: '#00B760',
  160. welcome: props.welcome
  161. })
  162. // 用户端信息配置
  163. const user = ref({
  164. name: formatName(baseInfo.value?.name ?? baseInfo.value?.phone),
  165. nameColor: 'blue',
  166. icon: 'mdi-account-circle',
  167. iconColor: '#00B760',
  168. bgColor: '#00B760'
  169. })
  170. // 监听talks变化,自动滚动到底部
  171. watch(talks, () => {
  172. nextTick(() => {
  173. box.value.scrollTo({
  174. top: box.value.scrollHeight,
  175. behavior: 'smooth'
  176. })
  177. })
  178. }, { deep: true, immediate: true })
  179. const handleSendMsg = () => {
  180. if (!question.value) {
  181. return
  182. }
  183. switch (props.type) {
  184. case 2:
  185. getSql()
  186. break
  187. case 4:
  188. talkTo(3, true)
  189. break
  190. default:
  191. talkTo(props.type)
  192. break
  193. }
  194. }
  195. const handleKeyCode = (event) => {
  196. if (event.keyCode === 13) {
  197. if (!event.ctrlKey) {
  198. event.preventDefault()
  199. handleSendMsg()
  200. } else {
  201. question.value += '\n'
  202. }
  203. }
  204. }
  205. const getSql = async () => {
  206. loading.value = true
  207. talks.value.push({
  208. id: id.value++,
  209. type: 'question',
  210. tags: [],
  211. content: question.value.split('\n')
  212. })
  213. const params = {
  214. question: question.value
  215. }
  216. question.value = ''
  217. try {
  218. const { data } = await api.getSql(params)
  219. talks.value.push({
  220. id: id.value++,
  221. type: 'response',
  222. tags: [],
  223. content: data.text.split('\n')
  224. })
  225. getTable(data.id)
  226. } catch (error) {
  227. Snackbar.error(error)
  228. } finally {
  229. loading.value = false
  230. }
  231. }
  232. const getTable = async (id) => {
  233. loading.value = true
  234. const params = { id }
  235. try {
  236. const { data } = await api.getToTable(params)
  237. if (data.type === 'error') {
  238. return
  239. }
  240. const _table = JSON.parse(data.df)
  241. talks.value.push({
  242. id: id.value++,
  243. type: 'response',
  244. tags: [],
  245. view: 'table',
  246. content: _table
  247. })
  248. } catch (error) {
  249. Snackbar.error(error)
  250. } finally {
  251. loading.value = false
  252. }
  253. }
  254. const talkTo = async (type, isRag = false) => {
  255. loading.value = true
  256. talks.value.push({
  257. id: id.value++,
  258. type: 'question',
  259. tags: [],
  260. content: question.value.split('\n')
  261. })
  262. const params = {
  263. question: question.value,
  264. type,
  265. isRag
  266. }
  267. question.value = ''
  268. try {
  269. const { data } = await api.talkTo(params)
  270. talks.value.push({
  271. id: id.value++,
  272. type: 'response',
  273. tags: data.tags || [],
  274. content: data.text.split('\n')
  275. })
  276. } catch (error) {
  277. Snackbar.error(error)
  278. } finally {
  279. loading.value = false
  280. }
  281. }
  282. </script>
  283. <style lang="scss" scoped>
  284. .send {
  285. padding: 10px;
  286. border-top: 1px solid #e0e0e0;
  287. .send-box {
  288. width: 100%;
  289. display: flex;
  290. align-items: center;
  291. .send-box-area {
  292. flex: 1;
  293. margin-right: 10px;
  294. }
  295. .btn {
  296. flex-shrink: 0;
  297. }
  298. }
  299. }
  300. .round {
  301. border-radius: 8px;
  302. }
  303. </style>