dataChartEditChat.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <template>
  2. <div class="chart-content-chat heightFull d-flex flex-column">
  3. <div class="chart-content-chat-box overflow-y-auto position-relative element" ref="chatBox" v-loading="loading">
  4. <div class="pa-3">
  5. <div
  6. v-for="(item, index) in items"
  7. :key="index"
  8. :class="['d-flex', 'mb-3', item.type === 1 ? 'flex-row' : 'flex-row-reverse' ]"
  9. >
  10. <v-avatar color="indigo" size="36">
  11. <span class="white--text">{{ item.type === 1 ? 'AI' : 'T' }}</span>
  12. </v-avatar>
  13. <div :class="[item.type === 1 ? 'ml-3 flex-grow-1 flex-shrink-1' : 'mr-3 box-length-70']">
  14. <div
  15. :class="['d-flex align-center', `justify-${item.type === 1 ? 'start' : 'end'}`]"
  16. >
  17. {{ item.type === 1 ? 'AI助手' : '游客' }}
  18. <template v-if="item.type === 1">
  19. <v-btn
  20. v-if="item.content?.sql"
  21. class="ml-3"
  22. small
  23. elevation="0"
  24. depressed
  25. @click="item.showSnackbar = !item.showSnackbar"
  26. >
  27. {{ !item.showSnackbar ? '查看SQL' : '收起SQL'}}
  28. </v-btn>
  29. </template>
  30. </div>
  31. <div class="mt-2" :class="{ 'indigo lighten-5 pa-3 rounded': item.type === 2 }">
  32. <template v-if="typeof item.content === 'string'">
  33. <div>
  34. <span v-if="item.welcome" class="mdi mdi-hand-wave"></span>
  35. {{item.content}}
  36. </div>
  37. <div v-if="item.welcome">
  38. <div class="mt-1" v-for="_item in item.items" :key="_item">
  39. <span class="defaultLink" @click="onSend(_item)">{{_item}}</span>
  40. </div>
  41. </div>
  42. </template>
  43. <template v-else-if="Object.keys(item.content).length === 0">
  44. <span>
  45. 正在思考中
  46. <v-progress-circular
  47. indeterminate
  48. size="14"
  49. class="ml-1"
  50. width="2"
  51. color="primary"
  52. ></v-progress-circular>
  53. </span>
  54. </template>
  55. <template v-else>
  56. <div>
  57. {{ item.content.summary }}
  58. </div>
  59. <div v-if="item.showSnackbar" class="pa-3 blue-grey lighten-3 mt-3">
  60. {{ item.content.sql }}
  61. </div>
  62. <div class="mt-3" v-if="item.content.columns.length">
  63. <div>
  64. <v-menu
  65. v-model="item.showMenu"
  66. :close-on-content-click="false"
  67. :close-on-click="false"
  68. max-width="300"
  69. attach=".chart-content-chat-box"
  70. >
  71. <template v-slot:activator>
  72. <v-btn
  73. @click="item.showMenu = true"
  74. text
  75. color="primary"
  76. >我要画图</v-btn>
  77. </template>
  78. <div class="white">
  79. <v-banner>画图配置</v-banner>
  80. <div class="pa-3">
  81. <v-autocomplete
  82. v-model="item.model.typeAxis"
  83. :items="item.content.columns"
  84. class="mb-3"
  85. outlined
  86. dense
  87. hide-details
  88. label="类型轴"
  89. ></v-autocomplete>
  90. <v-autocomplete
  91. v-model="item.model.dataAxis"
  92. :items="item.content.columns"
  93. class="mb-3"
  94. outlined
  95. dense
  96. hide-details
  97. label="数据轴"
  98. multiple
  99. chips
  100. small-chips
  101. ></v-autocomplete>
  102. <div class="text-right">
  103. <v-btn small class="mr-3" @click="item.showMenu = false">关闭</v-btn>
  104. <v-btn small color="primary" @click="onRender(item)">图表预览</v-btn>
  105. </div>
  106. </div>
  107. </div>
  108. </v-menu>
  109. </div>
  110. <v-card flat outlined height="324">
  111. <div class="pa-3">
  112. <v-simple-table
  113. fixed-header
  114. dense
  115. height="300px"
  116. >
  117. <template v-slot:default>
  118. <thead>
  119. <tr>
  120. <th
  121. v-for="header in item.content.columns"
  122. :key="header"
  123. class="text-left"
  124. >{{ header }}</th>
  125. </tr>
  126. </thead>
  127. <tbody>
  128. <tr
  129. v-for="(row, index) in item.content.rows"
  130. :key="index"
  131. >
  132. <td
  133. v-for="header in item.content.columns"
  134. :key="header"
  135. class="text-left"
  136. >{{ row[header] }}</td>
  137. </tr>
  138. </tbody>
  139. </template>
  140. </v-simple-table>
  141. </div>
  142. </v-card>
  143. </div>
  144. <div class="d-flex align-center" v-if="!item.dataValidation">
  145. 您认为结果是否正确
  146. <v-btn
  147. class="ma-2"
  148. text
  149. icon
  150. small
  151. color="blue lighten-2"
  152. @click="onAddTrain(item, true)"
  153. >
  154. <v-icon>mdi-thumb-up</v-icon>
  155. </v-btn>
  156. <v-btn
  157. class="ma-2"
  158. text
  159. icon
  160. small
  161. color="red lighten-2"
  162. @click="onAddTrain(item, false)"
  163. >
  164. <v-icon>mdi-thumb-down</v-icon>
  165. </v-btn>
  166. </div>
  167. </template>
  168. </div>
  169. </div>
  170. </div>
  171. </div>
  172. </div>
  173. <div class="pa-3 chart-content-chat-btn">
  174. <div class="send d-flex align-center justify-center">
  175. <div class="send-box">
  176. <v-textarea
  177. v-model="question"
  178. class="send-box-area"
  179. auto-grow
  180. placeholder="请输入您想问的内容,按 Ctrl+Enter 换行"
  181. outlined
  182. hide-details
  183. no-resize
  184. rows="1"
  185. @keydown.enter="handleKeyCode($event)"
  186. >
  187. </v-textarea>
  188. <v-btn icon color="primary" class="btn" :disabled="!question || disabled" @click="handleSendMsg">
  189. <v-icon>mdi-send</v-icon>
  190. </v-btn>
  191. </div>
  192. </div>
  193. </div>
  194. </div>
  195. </template>
  196. <script>
  197. import {
  198. getAsk,
  199. submitTrainingCorrect,
  200. submitTrainingError
  201. } from '@/api/dataChart'
  202. export default {
  203. name: 'dataChartEditChat',
  204. data () {
  205. return {
  206. loading: false,
  207. disabled: false,
  208. question: '',
  209. items: [
  210. {
  211. type: 1,
  212. welcome: true,
  213. content: '你好,我是您的数据查询小助手,支持查询“高速公路服务区”的相关信息。您可以这样提问: ',
  214. items: [
  215. '现在一共有多少个服务区?分别归属于哪些管理公司?',
  216. '哪个服务区的档口数量最多?',
  217. '赣州分公司下,餐饮类档口的日均订单量是多少?'
  218. ]
  219. }
  220. ]
  221. // trueData: false
  222. }
  223. },
  224. methods: {
  225. handleKeyCode (event) {
  226. if (event.keyCode === 13) {
  227. if (!event.ctrlKey) {
  228. event.preventDefault()
  229. this.handleSendMsg()
  230. } else {
  231. this.question += '\n'
  232. }
  233. }
  234. },
  235. onSend (str) {
  236. if (this.disabled) {
  237. return
  238. }
  239. this.question = str
  240. this.handleSendMsg()
  241. },
  242. async handleSendMsg () {
  243. if (!this.question || this.disabled) {
  244. return
  245. }
  246. this.disabled = true
  247. const question = this.question
  248. this.items.push({
  249. type: 2,
  250. user: '游客',
  251. content: question
  252. })
  253. this.scrollToBottom()
  254. const ask = {
  255. type: 1,
  256. content: {},
  257. showSnackbar: false,
  258. dataValidation: false,
  259. question, // 记录当前问题
  260. showMenu: false,
  261. model: {
  262. dataAxis: null,
  263. typeAxis: null
  264. }
  265. }
  266. this.items.push(ask)
  267. this.question = ''
  268. try {
  269. const { data } = await getAsk({ question })
  270. ask.content = data
  271. this.scrollToBottom()
  272. } catch (error) {
  273. ask.content = error.message
  274. } finally {
  275. this.disabled = false
  276. }
  277. },
  278. scrollToBottom () {
  279. this.$nextTick(() => {
  280. const box = this.$refs.chatBox
  281. if (!box) {
  282. return
  283. }
  284. box.scrollTop = box.scrollHeight
  285. })
  286. },
  287. async onAddTrain (item, bool) {
  288. this.loading = true
  289. const subApi = bool ? submitTrainingCorrect : submitTrainingError
  290. try {
  291. await subApi({
  292. question: item.question,
  293. sql: item.content.sql
  294. })
  295. item.dataValidation = true
  296. // this.$snackbar.success('操作成功')
  297. } catch (error) {
  298. this.$snackbar.error(error)
  299. } finally {
  300. this.loading = false
  301. }
  302. },
  303. onRender ({ model, content }) {
  304. if (!model.dataAxis || !model.typeAxis) {
  305. this.$snackbar.error('请选择数据轴和类型轴')
  306. return
  307. }
  308. const { typeAxis, dataAxis } = model
  309. const data = {
  310. type: typeAxis ? content.rows.map(e => e[typeAxis]) : [],
  311. data: dataAxis ? dataAxis.map(e => content.rows.map(r => r[e])) : []
  312. }
  313. this.$emit('render', data)
  314. }
  315. }
  316. }
  317. </script>
  318. <style lang="scss" scoped>
  319. .heightFull {
  320. height: 100%;
  321. }
  322. .widthFull {
  323. width: 100%;
  324. }
  325. .chart-content {
  326. &-chat {
  327. // border: 1px solid #ccc;
  328. &-box {
  329. height: 0;
  330. flex: 1;
  331. max-width: 800px;
  332. width: 100%;
  333. margin: 0 auto;
  334. }
  335. &-btn {
  336. .send {
  337. margin: 0 auto;
  338. max-width: 800px;
  339. }
  340. }
  341. }
  342. }
  343. .box-length-70 {
  344. max-width: 70%;
  345. }
  346. .send {
  347. // height: 130px;
  348. // margin: 20px 0;
  349. padding: 20px;
  350. &-box {
  351. width: 100%;
  352. // max-width: 800px;
  353. position: relative;
  354. .btn {
  355. position: absolute;
  356. right: 20px;
  357. bottom: 12px;
  358. }
  359. &-area {
  360. position: relative;
  361. bottom: 0;
  362. ::v-deep textarea {
  363. padding: 15px 70px 15px 0 !important;
  364. max-height: 300px;
  365. min-height: 60px;
  366. overflow: auto;
  367. margin: 0 !important;
  368. }
  369. }
  370. }
  371. }
  372. .position {
  373. &-relative {
  374. position: relative;
  375. }
  376. }
  377. .element {
  378. overflow: auto;
  379. scrollbar-width: none; /* Firefox */
  380. -ms-overflow-style: none; /* IE/Edge */
  381. }
  382. .element::-webkit-scrollbar {
  383. display: none; /* Chrome/Safari/Opera */
  384. }
  385. </style>