dataChartEditChat.vue 11 KB

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