怎么使用html+css+js实现简易版ChatGPT聊天机器人

前端开发   发布日期:2023年07月12日   浏览次数:740

本篇内容介绍了“怎么使用html+css+js实现简易版ChatGPT聊天机器人”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>test</title>
  7. <style type="text/css">
  8. @import url("https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500;600&family=Poppins:wght@200;300&display=swap");
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. font-family: "Poppins", sans-serif;
  14. }
  15. body {
  16. background: #4b5c66;
  17. }
  18. .container {
  19. --light-color: #fff;
  20. height: 580px;
  21. background: var(--light-color);
  22. bottom: 50px;
  23. right: 10px;
  24. box-shadow: 0px 0px 15px 0px black;
  25. }
  26. @media screen and (min-width:440px) {
  27. .container {
  28. position: fixed;
  29. }
  30. }
  31. .chat-header {
  32. height: 60px;
  33. display: flex;
  34. align-items: center;
  35. padding: 0px 30px;
  36. background-color: #0652c0;
  37. color: var(--light-color);
  38. font-size: 1.5rem;
  39. }
  40. .chat-header .logo {
  41. height: 35px;
  42. width: 35px;
  43. box-shadow: 0px 0px 10px 0px black;
  44. }
  45. .chat-header img {
  46. height: 100%;
  47. width: 100%;
  48. }
  49. .chat-header .title {
  50. padding-left: 10px;
  51. }
  52. .chat-body {
  53. height: 465px;
  54. display: flex;
  55. flex-direction: column;
  56. padding: 8px 10px;
  57. align-items: flex-end;
  58. overflow-y: auto;
  59. }
  60. .chat-input {
  61. height: 60px;
  62. display: flex;
  63. align-items: center;
  64. border-top: 1px solid #ccc;
  65. }
  66. .input-sec {
  67. flex: 9;
  68. }
  69. .send {
  70. flex: 1;
  71. padding-right: 4px;
  72. }
  73. #txtInput {
  74. line-height: 30px;
  75. padding: 8px 10px;
  76. border: none;
  77. outline: none;
  78. caret-color: black;
  79. font-size: 1rem;
  80. width: 100%;
  81. }
  82. .chatbot-message,
  83. .user-message {
  84. padding: 8px;
  85. background: #ccc;
  86. margin: 5px;
  87. width: max-content;
  88. border-radius: 10px 3px 10px 10px;
  89. }
  90. .chatbot-message {
  91. background: #0652c0;
  92. color: var(--light-color);
  93. align-self: flex-start;
  94. border-radius: 10px 10px 3px 10px;
  95. }
  96. </style>
  97. </head>
  98. <body>
  99. <div class="container">
  100. <div class="chat-header">
  101. <div class="logo"><img src="https://cache.yisu.com/upload/information/20230225/112/12222.jpg" alt="cwt" /></div>
  102. <div class="title">简易版Chat GPT</div>
  103. </div>
  104. <div class="chat-body"></div>
  105. <div class="chat-input">
  106. <div class="input-sec"><input type="text" id="txtInput" placeholder="在这里写" autofocus /></div>
  107. <div class="send"><img src="https://haiyong.site/img/svg/send.svg" alt="send" /></div>
  108. </div>
  109. </div>
  110. <script>
  111. const responseObj = {
  112. 你好: "你好,我是最强人工智能ChatGPT,我能回答你所有问题,快来和我聊天吧!",
  113. 五块钱怎么花三天: "坐公交回去找妈妈",
  114. 你是小黑子吗: "不,我不是小黑子。我是OpenAI的聊天机器人模型ChatGPT",
  115. 你为什么和我聊天: "只因你太美",
  116. 嘿: "嘿! 这是怎么回事",
  117. 今天几号: new Date().toDateString(),
  118. 几点了: new Date().toLocaleTimeString(),
  119. };
  120. const chatBody = document.querySelector(".chat-body");
  121. const txtInput = document.querySelector("#txtInput");
  122. const send = document.querySelector(".send");
  123. send.addEventListener("click", () => renderUserMessage());
  124. txtInput.addEventListener("keyup", (event) => {
  125. if (event.keyCode === 13) {
  126. renderUserMessage();
  127. }
  128. });
  129. const renderUserMessage = () => {
  130. const userInput = txtInput.value;
  131. renderMessageEle(userInput, "user");
  132. txtInput.value = "";
  133. setTimeout(() => {
  134. renderChatbotResponse(userInput);
  135. setScrollPosition();
  136. }, 600);
  137. };
  138. const renderChatbotResponse = (userInput) => {
  139. const res = getChatbotResponse(userInput);
  140. renderMessageEle(res);
  141. };
  142. const renderMessageEle = (txt, type) => {
  143. let className = "user-message";
  144. if (type !== "user") {
  145. className = "chatbot-message";
  146. }
  147. const messageEle = document.createElement("div");
  148. const txtNode = document.createTextNode(txt);
  149. messageEle.classList.add(className);
  150. messageEle.append(txtNode);
  151. chatBody.append(messageEle);
  152. };
  153. const getChatbotResponse = (userInput) => {
  154. return responseObj[userInput] == undefined ?
  155. "听不太懂呢试试输点别的" :
  156. responseObj[userInput];
  157. };
  158. const setScrollPosition = () => {
  159. if (chatBody.scrollHeight > 0) {
  160. chatBody.scrollTop = chatBody.scrollHeight;
  161. }
  162. };
  163. </script>
  164. </body>
  165. </html>

以上就是怎么使用html+css+js实现简易版ChatGPT聊天机器人的详细内容,更多关于怎么使用html+css+js实现简易版ChatGPT聊天机器人的资料请关注九品源码其它相关文章!