index.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <template>
  2. <h2
  3. style="
  4. text-align: center;
  5. margin: 18px 0;
  6. font-size: 25px;
  7. font-family: 'Source Sans Pro', 'ui-sans-serif', 'system-ui', sans-serif;
  8. "
  9. >
  10. ChatGLM
  11. </h2>
  12. <div class="chatbg tool">
  13. <div
  14. style="
  15. float: left;
  16. font-size: 12px;
  17. padding: 4px 8px;
  18. border-right: 1px solid #e5e7eb;
  19. border-bottom: 1px solid #e5e7eb;
  20. border-bottom-right-radius: 7px;
  21. "
  22. >
  23. <el-icon><ChatDotRound /></el-icon>
  24. Chatbot
  25. </div>
  26. <div style="clear: both"></div>
  27. <div class="chatGptChat" ref="chatEle">
  28. <div v-for="(item, index) in chat" :key="index">
  29. <div v-if="item.type === 'robot'" class="chat">
  30. <div class="cahtText">
  31. <textShow :text="item.text" />
  32. </div>
  33. </div>
  34. <div v-if="item.type === 'user'" class="chat chatRight">
  35. <div class="cahtText" v-text="item.text"></div>
  36. </div>
  37. <div style="clear: both"></div>
  38. </div>
  39. </div>
  40. </div>
  41. <div class="tool">
  42. <el-row :gutter="15">
  43. <el-col :span="16">
  44. <el-input
  45. v-model="inputText"
  46. :rows="10"
  47. type="textarea"
  48. placeholder="请输入您的问题"
  49. />
  50. <el-button
  51. style="
  52. width: 100%;
  53. color: #ea580c;
  54. font-weight: 600;
  55. font-size: 16px;
  56. margin-top: 1em;
  57. "
  58. :loading="load"
  59. color="#fdba74"
  60. size="large"
  61. @click="saveText"
  62. >
  63. 提交
  64. </el-button>
  65. </el-col>
  66. <el-col :span="8">
  67. <el-button
  68. style="width: 100%; margin-bottom: 1em"
  69. color="#f3f4f6"
  70. size="large"
  71. @click="chat = []"
  72. >
  73. 清除历史记录
  74. </el-button>
  75. <div class="tools">
  76. <div class="toolsItem">
  77. 最大长度
  78. <el-input-number
  79. style="float: right"
  80. v-model="num"
  81. :min="0"
  82. :max="4096"
  83. @change="change"
  84. controls-position="right"
  85. size="small"
  86. />
  87. <input
  88. :value="0"
  89. type="range"
  90. id="num"
  91. style="width: 100%"
  92. @input="input"
  93. />
  94. </div>
  95. <div class="line"></div>
  96. <div class="toolsItem">
  97. Top P
  98. <el-input-number
  99. style="float: right"
  100. v-model="num1"
  101. :min="0"
  102. :max="1"
  103. :step="0.01"
  104. @change="change1"
  105. controls-position="right"
  106. size="small"
  107. />
  108. <input
  109. :value="0"
  110. type="range"
  111. id="num1"
  112. style="width: 100%"
  113. @input="input1"
  114. />
  115. </div>
  116. <div class="line"></div>
  117. <div class="toolsItem">
  118. 感情度
  119. <el-input-number
  120. style="float: right"
  121. v-model="num2"
  122. :min="0"
  123. :max="1"
  124. :step="0.01"
  125. @change="change2"
  126. controls-position="right"
  127. size="small"
  128. />
  129. <input
  130. :value="0"
  131. type="range"
  132. id="num2"
  133. style="width: 100%"
  134. @input="input2"
  135. />
  136. </div>
  137. </div>
  138. </el-col>
  139. </el-row>
  140. </div>
  141. </template>
  142. <script setup>
  143. import { createSocket } from '@/utils/socket';
  144. import { ChatDotRound } from '@element-plus/icons-vue';
  145. import { ref, nextTick, onMounted } from 'vue';
  146. import textShow from './textShow.vue';
  147. const chat = ref([]);
  148. const load = ref(false);
  149. const chatEle = ref(null);
  150. const inputText = ref('');
  151. const num = ref(2048);
  152. const num1 = ref(0.7);
  153. const num2 = ref(0.95);
  154. let chatlist = ref([]);
  155. function saveText() {
  156. load.value = true;
  157. nextTick(() => {
  158. // 滚动到最底层
  159. if (chatEle.value.scrollHeight > chatEle.value.clientHeight) {
  160. chatEle.value.scrollTop = chatEle.value.scrollHeight;
  161. }
  162. });
  163. chat.value.push({
  164. text: inputText.value,
  165. type: 'user',
  166. });
  167. chat.value.push({
  168. text: '',
  169. type: 'robot',
  170. });
  171. // const idnex = chatlist.value.length === 0 ? 0 : chatlist.value.length - 1;
  172. const t = inputText.value;
  173. inputText.value = '';
  174. let session_hash = Math.random().toString(36).substring(2);
  175. createSocket(ws => {
  176. const data = ws.data || {};
  177. if (data.msg == 'send_hash')
  178. ws.ws.send(
  179. JSON.stringify({
  180. fn_index: 0,
  181. session_hash,
  182. })
  183. );
  184. if (data.msg == 'send_data')
  185. ws.ws.send(
  186. JSON.stringify({
  187. fn_index: 0,
  188. data: [t, chatlist.value, num.value, num1.value, num2.value, null],
  189. event_data: null,
  190. session_hash,
  191. })
  192. );
  193. if (data.msg == 'process_generating') {
  194. const li = data.output.data[0] || [];
  195. const olist = li[li.length - 1];
  196. chat.value[chat.value.length - 1].text = olist[1];
  197. }
  198. if (data.msg == 'process_completed') {
  199. chatlist.value = data.output.data[0] || [];
  200. load.value = false;
  201. }
  202. });
  203. }
  204. const input = e => {
  205. num.value = Math.floor((e.target.value / 100) * 4096);
  206. };
  207. const change = () => {
  208. document.querySelector('#num').value =
  209. ((num.value / 4096) * 100).toFixed(0) - 0;
  210. };
  211. const input1 = e => {
  212. num1.value = e.target.value / 100;
  213. };
  214. const change1 = () => {
  215. document.querySelector('#num1').value = num1.value * 100;
  216. };
  217. const input2 = e => {
  218. num2.value = e.target.value / 100;
  219. };
  220. const change2 = () => {
  221. document.querySelector('#num2').value = num2.value * 100;
  222. };
  223. onMounted(() => {
  224. change();
  225. change1();
  226. change2();
  227. });
  228. </script>
  229. <style>
  230. .chatbg {
  231. min-height: 3em;
  232. border-radius: 8px;
  233. overflow: hidden;
  234. border: 1px solid #e5e7eb;
  235. }
  236. .chatGptChat {
  237. padding-top: 5px;
  238. box-sizing: border-box;
  239. font-weight: 500;
  240. margin: 1em auto 0 auto;
  241. width: 100%;
  242. height: auto;
  243. max-height: 480px;
  244. position: relative;
  245. overflow-y: auto;
  246. padding: 0 1em;
  247. }
  248. .chatGptChat .van-field {
  249. background-color: #eee;
  250. position: fixed;
  251. padding-bottom: 1em;
  252. bottom: 0;
  253. left: 0;
  254. }
  255. .chatGptChat .van-field .van-field__control {
  256. background-color: #ffffff;
  257. padding: 0.4em;
  258. }
  259. .chatGptChat .chat {
  260. padding: 8px 12px;
  261. background-color: #f4f4f680;
  262. border-radius: 22px;
  263. border: 1px solid #e5e7eb;
  264. width: 98%;
  265. border-bottom-left-radius: 0;
  266. margin-bottom: 1em;
  267. }
  268. .chatGptChat .chat .cahtText {
  269. text-align: left;
  270. line-height: 1.5em;
  271. border-radius: 5px;
  272. padding: 0.5em;
  273. display: inline-block;
  274. max-width: calc(100% - 40px);
  275. vertical-align: middle;
  276. }
  277. .chatGptChat .chat .van-image {
  278. vertical-align: top;
  279. }
  280. .chatGptChat .chat .cahtText:not(:last-child),
  281. .chatGptChat .chat .van-image:not(:last-child) {
  282. margin-right: 5px;
  283. }
  284. .chatGptChat .chatRight {
  285. border-color: #fdba74;
  286. background-color: #fff7ed;
  287. border-bottom-right-radius: 0;
  288. border-bottom-left-radius: 22px;
  289. float: right;
  290. }
  291. .tool {
  292. width: 100%;
  293. margin: 1em auto 0 auto;
  294. }
  295. @media (min-width: 640px) {
  296. .chatGptChat,
  297. .tool {
  298. max-width: 608px;
  299. }
  300. }
  301. @media (min-width: 768px) {
  302. .chatGptChat,
  303. .tool {
  304. max-width: 736px;
  305. }
  306. }
  307. @media (min-width: 1024px) {
  308. .chatGptChat,
  309. .tool {
  310. max-width: 992px;
  311. }
  312. }
  313. @media (min-width: 1280px) {
  314. .chatGptChat,
  315. .tool {
  316. max-width: 1248px;
  317. }
  318. }
  319. @media (min-width: 1536px) {
  320. .chatGptChat,
  321. .tool {
  322. max-width: 1504px;
  323. }
  324. }
  325. .tools {
  326. border-radius: 8px;
  327. border: 1px solid #e5e7eb;
  328. line-height: 1.5em;
  329. }
  330. .toolsItem {
  331. padding: 10px 12px;
  332. font-size: 14px;
  333. font-weight: 400;
  334. color: #6b7280;
  335. }
  336. .line {
  337. height: 1px;
  338. background-color: #e5e7eb;
  339. }
  340. </style>