加载中...

VUE3 + TypeScript 仿ChatGPT前端UI

VUE3 + TypeScript 仿ChatGPT前端UI

基于html5 开发的仿ChatGPT的前端UI项目,采用了VUE3 + TypeScript等技术选型开发,实现了基本的消息会话、PDF会话、新增会话、删除会话、会话历史、Token统计等新增功能。

New Image

  • 消息会话、新建会话、会话历史、删除会话、Token统计、PDF文件上传等功能代码如下:
  1. <script setup lang=''ts''>
  2. import { ref } from ''vue''
  3. import { useRoute } from ''vue-router''
  4. import { router } from ''@/router''
  5. import { useScroll } from ''./hooks/useScroll''
  6. import VuePdfApp from "vue3-pdf-app"
  7. import "vue3-pdf-app/dist/icons/main.css"
  8. import { encode } from ''gpt-tokenizer''
  9. const { scrollRef, scrollToBottom } = useScroll()
  10. // Conversation and PDF preview panel toggle control
  11. let showTab = ref<string>("nav-tab-chat")
  12. let tabWidth = ref<string>("")
  13. // vue3-pdf-app UI configuration
  14. let pdfFile = ref<string>("")
  15. const config = ref<{}>({
  16. sidebar: true,
  17. toolbar: {
  18. toolbarViewerLeft: {
  19. findbar: true,
  20. previous: true,
  21. next: true,
  22. pageNumber: false,
  23. },
  24. toolbarViewerRight: {
  25. presentationMode: true,
  26. openFile: false,
  27. print: false,
  28. download: false,
  29. viewBookmark: false,
  30. },
  31. toolbarViewerMiddle: {
  32. zoomOut: true,
  33. zoomIn: true,
  34. scaleSelectContainer: true,
  35. }
  36. },
  37. })
  38. // Message input box
  39. const prompt = ref<string>('''')
  40. // Loading state and button state
  41. const buttonDisabled = ref<boolean>(false)
  42. // Get uuid from URL params
  43. const route = useRoute()
  44. let { uuid } = route.params as { uuid: string }
  45. interface Conversation {
  46. title: string;
  47. uuid: string;
  48. isEdit: boolean;
  49. createDate: string;
  50. lastChatContent: string;
  51. active: boolean;
  52. }
  53. interface Message {
  54. send: {
  55. model: string;
  56. messages: {
  57. role: string;
  58. content: string;
  59. fileName: any;
  60. fileSize: number;
  61. }[];
  62. temperature: number;
  63. };
  64. loading: boolean;
  65. receive?: {
  66. model: string;
  67. choices: {
  68. message?: {
  69. content: string;
  70. };
  71. delta: {
  72. content: string;
  73. };
  74. }[];
  75. };
  76. }
  77. // Conversation list and message list
  78. var conversationList = ref<Conversation[]>([])
  79. var messageList = ref<Message[]>([]);
  80. let conversations = window.localStorage.getItem("chatStore")
  81. if(conversations){
  82. conversationList.value = JSON.parse(conversations)
  83. }
  84. // Check if new conversation
  85. if (!uuid || uuid === ''0'') {
  86. uuid = Date.now().toString()
  87. // Initialize empty conversation
  88. if(!conversations){
  89. conversationList.value.push({
  90. title: ''New Chat'',
  91. uuid: uuid,
  92. isEdit: false,
  93. createDate: new Date().toLocaleString(),
  94. lastChatContent: ''Hello I am ChatGPT3.5...'',
  95. active: true
  96. })
  97. }else{
  98. // If has history, get last conversation
  99. let lastConversation = conversationList.value[conversationList.value.length-1]
  100. uuid = lastConversation.uuid
  101. let messages = window.localStorage.getItem(uuid)
  102. if(messages) {
  103. messageList.value = JSON.parse(messages)
  104. }
  105. router.push({ name: ''Chat'', params: { uuid } })
  106. }
  107. }else{
  108. // Load current conversation messages
  109. let messages = window.localStorage.getItem(uuid)
  110. if(messages) {
  111. messageList.value = JSON.parse(messages)
  112. }
  113. conversationList.value.forEach((item, index) => {
  114. if(item.uuid == uuid){
  115. item.active = true
  116. }else{
  117. item.active = false
  118. }
  119. })
  120. scrollToBottom()
  121. }
  122. // Set active conversation
  123. function handleAdd() {
  124. // Reset the message record of the new conversation
  125. messageList.value = []
  126. // Reset the active status of the conversation list
  127. conversationList.value.forEach((item, index) => {
  128. item.active = false
  129. })
  130. // Initialize an empty conversation
  131. uuid = Date.now().toString()
  132. conversationList.value.unshift({
  133. title: "New Chat",
  134. uuid: uuid,
  135. isEdit: false,
  136. createDate: new Date().toLocaleString(),
  137. lastChatContent: ''Hello I am ChatGPT3.5...'',
  138. active: true
  139. })
  140. // Save the conversation to local storage
  141. window.localStorage.setItem("chatStore", JSON.stringify(conversationList.value))
  142. }
  143. // Menu toggle
  144. function handleMenu(){
  145. let rootbody = document.getElementById("rootbody")
  146. if (rootbody) {
  147. if(rootbody.classList.value==""){
  148. rootbody.classList.value="open-sidebar-menu"
  149. }else{
  150. rootbody.classList.value=""
  151. }
  152. }
  153. }
  154. // Switch conversation
  155. function handleSwitch(selectedUuid: string) {
  156. uuid = selectedUuid
  157. // Reset message record of the new conversation
  158. let messages = window.localStorage.getItem(selectedUuid)
  159. if(messages){
  160. messageList.value = JSON.parse(messages)
  161. }else{
  162. messageList.value = []
  163. }
  164. // Reset active status of the conversation list
  165. conversationList.value.forEach((item, index) => {
  166. if(item.uuid == selectedUuid){
  167. item.active = true
  168. }else{
  169. item.active = false
  170. }
  171. })
  172. router.push({ name: ''Chat'', params: { uuid } })
  173. }
  174. // File upload related
  175. var fileName = ref()
  176. var fileSize = ref<number>(0)
  177. var formattedFileSize = ref<string>(''0B'')
  178. var fileUploadCard = ref<boolean>(false)
  179. var fileContent = ref()
  180. // Handle file upload
  181. function handleUpload(e: Event) {
  182. const target = e.target as HTMLInputElement;
  183. if(target.files && target.files[0].size >= 5 * 1024 * 1024){
  184. alert(''Maximum file size limit is 5MB'')
  185. return
  186. }else if (!target.files || target.files.length === 0) {
  187. alert(''Please select a file'')
  188. return
  189. }
  190. // Set file upload style
  191. fileName.value = target.files[0].name
  192. fileSize.value = target.files[0].size
  193. formatFileSize()
  194. // Preview PDF
  195. showTab.value = ''nav-tab-doc''
  196. tabWidth.value = ''width: 60%''
  197. pdfFile.value = URL.createObjectURL(target.files[0])
  198. // Upload file and extract content
  199. const formData = new FormData()
  200. formData.append(''doc'', target.files[0])
  201. fetch(import.meta.env.VITE_API_UPLOAD, {
  202. method: ''POST'',
  203. body: formData,
  204. })
  205. .then(response => response.text())
  206. .catch(error => console.error(''Error:'', error))
  207. .then(function (docContent) {
  208. if (typeof docContent !== ''string'') {
  209. alert("Failed to extract file content")
  210. return
  211. }
  212. const tokens = encode(docContent)
  213. if(tokens.length > 4096){
  214. alert("Exceeded maximum token limit of 4096")
  215. fileName.value = ''''
  216. fileSize.value = 0
  217. formattedFileSize.value = ''0B''
  218. }else{
  219. // Set the extracted content
  220. fileContent.value = docContent
  221. // Show file upload card
  222. fileUploadCard.value = true
  223. }
  224. })
  225. }
  226. function handleBackChat(){
  227. showTab.value = ''nav-tab-chat''
  228. tabWidth.value = ''''
  229. }
  230. function handleBackDoc(){
  231. showTab.value = ''nav-tab-doc''
  232. tabWidth.value = ''width: 40%''
  233. }
  234. // Format file size in Bytes, KB, MB, GB
  235. function formatFileSize() {
  236. if (fileSize.value < 1024) {
  237. formattedFileSize.value = fileSize.value + ''B'';
  238. } else if (fileSize.value < (1024*1024)) {
  239. var temp = fileSize.value / 1024
  240. formattedFileSize.value = temp.toFixed(2) + ''KB''
  241. } else if (fileSize.value < (1024*1024*1024)) {
  242. var temp = fileSize.value / (1024*1024)
  243. formattedFileSize.value = temp.toFixed(2) + ''MB''
  244. } else {
  245. var temp = fileSize.value / (1024*1024*1024);
  246. formattedFileSize.value = temp.toFixed(2) + ''GB''
  247. }
  248. }
  249. // Submit message
  250. function handleSubmit() {
  251. onConversation()
  252. }
  253. // Stream request to ChatGPT3.5
  254. async function onConversation() {
  255. let message = prompt.value
  256. if (!message || message.trim() === '''')
  257. return
  258. // Clear input box and disable button
  259. prompt.value = ''''
  260. buttonDisabled.value = true
  261. fileUploadCard.value = false
  262. // Send message (for local display, not directly sent to GPT)
  263. messageList.value.push({
  264. send: {
  265. model: "gpt-3.5-turbo-1106",
  266. messages: [
  267. {
  268. role: "user",
  269. content: message,
  270. fileName: fileName.value,
  271. fileSize: fileSize.value,
  272. },
  273. ],
  274. temperature: 0.7,
  275. },
  276. loading: true,
  277. });
  278. scrollToBottom()
  279. // Stream request to ChatGPT3.5
  280. try {
  281. if(fileContent.value){
  282. message += '', Uploaded file content: '' + fileContent.value
  283. }
  284. let data = {
  285. "model": "gpt-3.5-turbo-1106",
  286. "messages": [{"role": "user", "content": message }],
  287. "temperature": 0.7,
  288. "stream": true
  289. }
  290. let headers = {
  291. ''Content-Type'': ''application/json'',
  292. ''Authorization'': ''Bearer '' + import.meta.env.VITE_API_KEY,
  293. }
  294. // Send request
  295. let response = await fetch(import.meta.env.VITE_APP_URL, {
  296. method: ''POST'',
  297. headers: headers,
  298. body: JSON.stringify(data)
  299. })
  300. // Reset file upload related states immediately after sending to ChatGPT
  301. fileName.value = ''''
  302. fileSize.value = 0
  303. formattedFileSize.value = ''0B''
  304. if (!response.ok) {
  305. throw new Error(''Network response was not ok'')
  306. }
  307. // Read the data returned from the stream
  308. const reader = response.body?.getReader();
  309. const textDecoder = new TextDecoder()
  310. let result = true
  311. while (reader && result) {
  312. // Get a chunk
  313. const { done, value } = await reader.read()
  314. if (done) {
  315. console.log(''Stream ended'')
  316. result = false
  317. // Restore button state
  318. buttonDisabled.value = false
  319. fileContent.value = ''''
  320. // Save current messages
  321. window.localStorage.setItem(uuid, JSON.stringify(messageList.value))
  322. window.localStorage.setItem("chatStore", JSON.stringify(conversationList.value))
  323. break
  324. }
  325. // Convert chunk string to array
  326. let chunkText = textDecoder.decode(value)
  327. chunkText = chunkText.replace(/data:/g, '''')
  328. let results = chunkText.split(''\n\n'').filter(Boolean)
  329. // Iterate through the array and process multiple chunks
  330. for (let i = 0; i < results.length; i++) {
  331. var chunk = results[i]
  332. if (chunk.indexOf(''DONE'') == -1) {
  333. var chunkData = JSON.parse(chunk)
  334. if (chunkData.choices[0].delta.content) {
  335. if (!messageList.value[messageList.value.length - 1].receive) {
  336. // If it is the first result, set the state directly
  337. messageList.value[messageList.value.length - 1].receive = chunkData
  338. messageList.value[messageList.value.length - 1].loading = false
  339. } else {
  340. const lastMessage = messageList.value[messageList.value.length - 1]?.receive;
  341. if (lastMessage && lastMessage.choices[0].delta.content) {
  342. lastMessage.choices[0].delta.content += chunkData.choices[0].delta.content;
  343. }
  344. }
  345. scrollToBottom()
  346. }
  347. }
  348. }
  349. }
  350. } catch (e) {
  351. console.log(e)
  352. }
  353. }
  354. function handleDele(selectedUuid: string){
  355. // Reset the active state of the conversation list
  356. conversationList.value.forEach((item, index) => {
  357. if(item.uuid == selectedUuid){
  358. conversationList.value.splice(index,1)
  359. // Save the conversation to local storage
  360. window.localStorage.setItem("chatStore", JSON.stringify(conversationList.value))
  361. return false
  362. }
  363. })
  364. // Reset the message records of the new conversation
  365. if(uuid == selectedUuid){
  366. let messages = window.localStorage.getItem(selectedUuid)
  367. if(messages){
  368. window.localStorage.removeItem(selectedUuid)
  369. messageList.value = []
  370. }
  371. }
  372. }
  373. </script>
  374. <template>
  375. <div id="layout" >
  376. <!-- Sidebar -->
  377. <div >
  378. <a href="#" title="ChatGPT-UI" >
  379. <svg viewBox="0 0 128 128" width="24" height="24" data-v-c0161dce=""><path fill="#42b883" d="M78.8,10L64,35.4L49.2,10H0l64,110l64-110C128,10,78.8,10,78.8,10z" data-v-c0161dce=""></path><path fill="#35495e" d="M78.8,10L64,35.4L49.2,10H25.6L64,76l38.4-66H78.8z" data-v-c0161dce=""></path></svg>
  380. </a>
  381. <div role="tablist" aria-orientation="vertical">
  382. <a data-toggle="pill" href="#" role="tab">
  383. <i ></i> <!-- Chat -->
  384. </a>
  385. <a data-toggle="pill" href="#" role="tab">
  386. <i ></i> <!-- Layers -->
  387. </a>
  388. <a href="#">
  389. <i ></i> <!-- Light/Dark Mode -->
  390. <input type="checkbox">
  391. </a>
  392. <a href="#" role="tab">
  393. <i ></i> <!-- Settings -->
  394. </a>
  395. </div>
  396. <button type="submit" @click="handleMenu">
  397. <i ></i> <!-- Menu -->
  398. </button>
  399. </div>
  400. <!-- Sidebar -->
  401. <div :>
  402. <div >
  403. <!-- Chat Records -->
  404. <div id="nav-tab-chat" role="tabpanel" v-if="showTab === ''nav-tab-chat''">
  405. <div >
  406. <h3 >ChatGPT-UI</h3>
  407. <div>
  408. <button type="button" @click="handleAdd">New Chat</button></div>
  409. </div>
  410. <ul >
  411. <li >
  412. <span>RECENT CHATS</span>
  413. </li>
  414. <li v-for="(item, index) in conversationList" : @click="handleSwitch(item.uuid)">
  415. <div >
  416. <button type="button" ><i ></i></button>
  417. <button type="button" @click="handleDele(item.uuid)"><i ></i></button>
  418. </div>
  419. <a href="#" >
  420. <div >
  421. <div >
  422. <div >
  423. <span ></span>
  424. <img : src="../assets/chatgpt.jpg" alt="avatar"></div>
  425. <div >
  426. <div >
  427. <h6 >{{ item.title }}</h6>
  428. <p >{{ item.createDate }}</p></div>
  429. <div >{{ item.lastChatContent }}</div></div>
  430. </div>
  431. </div>
  432. </a>
  433. </li>
  434. </ul>
  435. </div>
  436. <!-- end Chat Records -->
  437. <!-- PDF Preview -->
  438. <div id="nav-tab-doc" role="tabpanel" v-if="showTab === ''nav-tab-doc''">
  439. <div >
  440. <h3 >ChatGPT-PDF</h3>
  441. <div>
  442. <button type="button" @click="handleBackChat">Back Chat</button></div>
  443. </div>
  444. <ul >
  445. <li >
  446. <span>PREVIEW</span>
  447. </li>
  448. <li>
  449. <vue-pdf-app :config="config" :pdf="pdfFile"></vue-pdf-app>
  450. </li>
  451. </ul>
  452. </div>
  453. <!-- end PDF Preview -->
  454. </div>
  455. </div>
  456. <div >
  457. <div >
  458. <!-- Chat Box Header -->
  459. <div >
  460. <div >
  461. <div >
  462. <div >
  463. <div >
  464. <div >
  465. <span ></span>
  466. <img src="../assets/chatgpt.jpg" alt="avatar"></div>
  467. <div >
  468. <div >
  469. <h6 >ChatGPT 3.5</h6></div>
  470. <div >Powered By OpenAI</div></div>
  471. </div>
  472. </div>
  473. </div>
  474. </div>
  475. </div>
  476. <!-- end Chat Box Header -->
  477. <div id="scrollRef" ref="scrollRef">
  478. <div >
  479. <ul v-for="(item, index) of messageList">
  480. <!-- Right Message -->
  481. <li >
  482. <div >
  483. <span ></span>
  484. <div >
  485. <div >
  486. {{ item.send.messages[0].content }}
  487. <div v-show="item.send.messages[0].fileName" @click="handleBackDoc">
  488. <div >
  489. <div >
  490. <div >
  491. <i ></i>
  492. </div>
  493. </div>
  494. <div >
  495. <h6 >{{ item.send.messages[0].fileName }}</h6>
  496. <span >{{ item.send.messages[0].fileSize }}</span>
  497. </div>
  498. </div>
  499. </div>
  500. </div>
  501. </div>
  502. </div>
  503. </li>
  504. <!-- end Right Message -->
  505. <!-- Left Message -->
  506. <li v-if="item.receive">
  507. <div >
  508. <img src="../assets/chatgpt.jpg" alt="avatar"></div>
  509. <div >
  510. <span >{{ item.receive.model }}</span>
  511. <div >
  512. <div >
  513. <v-md-preview :text="item.receive.choices[0].message?item.receive.choices[0].message.content:item.receive.choices[0].delta.content"></v-md-preview>
  514. </div>
  515. </div>
  516. </div>
  517. </li>
  518. <!-- end Left Message -->
  519. <!-- Loading Message -->
  520. <li v-if="item.loading">
  521. <div >
  522. <img src="../assets/chatgpt.jpg" alt="avatar"></div>
  523. <div >
  524. <div >
  525. <div >
  526. <div >
  527. <span ></span>
  528. <span ></span>
  529. <span ></span>
  530. </div>
  531. </div>
  532. </div>
  533. </div>
  534. </li>
  535. <!-- end Loading Message -->
  536. </ul>
  537. </div>
  538. </div>
  539. <!-- Message Input Box -->
  540. <div >
  541. <div >
  542. <div >
  543. <div >
  544. <form @submit.prevent="handleSubmit">
  545. <div >
  546. <input type="text" v-model="prompt" placeholder="Type your message...">
  547. <div v-show="fileUploadCard" @click="handleBackDoc">
  548. <div >
  549. <div >
  550. <div >
  551. <i ></i>
  552. </div>
  553. </div>
  554. <div >
  555. <h6 >{{ fileName }}</h6>
  556. <span >{{ fileSize }}</span>
  557. </div>
  558. </div>
  559. </div>
  560. <div >
  561. <span >
  562. <input type="file" accept="application/pdf" id="fileInput" ref="file" @change="handleUpload" >
  563. <button data-toggle="tooltip" @click="($refs.file as HTMLInputElement).click()" title="" type="button" data-original-title="Attachment">
  564. <i ></i>
  565. </button>
  566. </span>
  567. </div>
  568. <div >
  569. <span >
  570. <button type="submit" :disabled="buttonDisabled" @click="handleSubmit">
  571. <i ></i>
  572. </button>
  573. </span>
  574. </div>
  575. </div>
  576. </form>
  577. </div>
  578. </div>
  579. </div>
  580. </div>
  581. <!-- end Message Input Box -->
  582. </div>
  583. </div>
  584. <!-- Empty Page -->
  585. <div >
  586. <div >
  587. <div >
  588. <div >
  589. <div >
  590. <img src="../assets/user.png" alt="">
  591. <span ></span>
  592. <span ></span>
  593. </div>
  594. <h5 >Hey, Robert!</h5>
  595. <p>Please select a chat to start messaging.</p>
  596. </div>
  597. </div>
  598. </div>
  599. </div>
  600. <!-- end Empty Page -->
  601. </div>
  602. </template>
  • PDF会话、PDF预览、文件上传和附件信息等功能,功能代码如下:

New Image

  1. // vue3-pdf-app 组件配置,预览PDF界面配置
  2. let pdfFile = ref<string>("")
  3. const config = ref<{}>({
  4. sidebar: true,
  5. toolbar: { //工具条配置
  6. toolbarViewerLeft: {
  7. findbar: true, //查找功能
  8. previous: true,//上一页
  9. next: true,//下一页
  10. pageNumber: false, //页号
  11. },
  12. toolbarViewerRight: { // 工具条右侧
  13. presentationMode: true,
  14. openFile: false,
  15. print: false,
  16. download: false,
  17. viewBookmark: false,
  18. },
  19. toolbarViewerMiddle: { //工具条中间位置
  20. zoomOut: true,
  21. zoomIn: true,
  22. scaleSelectContainer: true,
  23. }
  24. },
  25. })
  1. <ul class="chat-list">
  2. <li class="header d-flex justify-content-between ps-3 pe-3 mb-1">
  3. <span>PREVIEW</span>
  4. </li>
  5. <li>
  6. // PDF预览组件
  7. <vue-pdf-app style="height: 100vh;" :config="config" :pdf="pdfFile"></vue-pdf-app>
  8. </li>
  9. </ul>

环境要求

开发版本: Node 18.15.0 + Vue 3

项目配置

ChatGPT UI的默认配置存储在“.env”文件中。您将需要覆盖一些值以使ChatGPT UI在本地运行。

  1. VITE_APP_URL = 填写OpenAI的API地址或第三方封装的API,格式示例:https://api.openai.com/v1/chat/completions
  2. VITE_API_KEY= 填写OpenAI的ApiKey, 格式示例: sk-FihjnhGKO14eYLmPpV1234BlbkFJUq1lS0RNenkDsjgGLopx
  3. VITE_API_UPLOAD = 填写解析pdf文件的API地址,格式示例: http://domain.com/upload/pdf

项目初始化

npm install

运行开发环境

npm run dev

访问项目

http://localhost:1003

构建生产环境

npm run build

下载地址

https://gitee.com/supertinys_ryan/chatgpt-ui

https://github.com/uniconnector/chatgpt-ui