html+css+js實現(xiàn)簡易版ChatGPT聊天機器人
OpenAI的一款聊天機器人模型ChatGPT爆火,本篇文章用一百行html+css+js代碼給大家制作一款簡易的聊天機器人。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>test</title> <style type="text/css"> @import url("https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500;600&family=Poppins:wght@200;300&display=swap"); * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Poppins", sans-serif; } body { background: #4b5c66; } .container { --light-color: #fff; height: 580px; background: var(--light-color); bottom: 50px; right: 10px; box-shadow: 0px 0px 15px 0px black; } @media screen and (min-width:440px) { .container { position: fixed; } } .chat-header { height: 60px; display: flex; align-items: center; padding: 0px 30px; background-color: #0652c0; color: var(--light-color); font-size: 1.5rem; } .chat-header .logo { height: 35px; width: 35px; box-shadow: 0px 0px 10px 0px black; } .chat-header img { height: 100%; width: 100%; } .chat-header .title { padding-left: 10px; } .chat-body { height: 465px; display: flex; flex-direction: column; padding: 8px 10px; align-items: flex-end; overflow-y: auto; } .chat-input { height: 60px; display: flex; align-items: center; border-top: 1px solid #ccc; } .input-sec { flex: 9; } .send { flex: 1; padding-right: 4px; } #txtInput { line-height: 30px; padding: 8px 10px; border: none; outline: none; caret-color: black; font-size: 1rem; width: 100%; } .chatbot-message, .user-message { padding: 8px; background: #ccc; margin: 5px; width: max-content; border-radius: 10px 3px 10px 10px; } .chatbot-message { background: #0652c0; color: var(--light-color); align-self: flex-start; border-radius: 10px 10px 3px 10px; } </style> </head> <body> <div class="container"> <div class="chat-header"> <div class="logo"><img src="https://code.haiyong.site/wp-content/uploads/2022/10/logo.jpg" alt="cwt" /></div> <div class="title">簡易版Chat GPT</div> </div> <div class="chat-body"></div> <div class="chat-input"> <div class="input-sec"><input type="text" id="txtInput" placeholder="在這里寫" autofocus /></div> <div class="send"><img src="https://haiyong.site/img/svg/send.svg" alt="send" /></div> </div> </div> <script> const responseObj = { 你好: "你好,我是最強人工智能ChatGPT,我能回答你所有問題,快來和我聊天吧!", 五塊錢怎么花三天: "坐公交回去找媽媽", 你是小黑子嗎: "不,我不是小黑子。我是OpenAI的聊天機器人模型ChatGPT", 你為什么和我聊天: "只因你太美", 嘿: "嘿! 這是怎么回事", 今天幾號: new Date().toDateString(), 幾點了: new Date().toLocaleTimeString(), }; const chatBody = document.querySelector(".chat-body"); const txtInput = document.querySelector("#txtInput"); const send = document.querySelector(".send"); send.addEventListener("click", () => renderUserMessage()); txtInput.addEventListener("keyup", (event) => { if (event.keyCode === 13) { renderUserMessage(); } }); const renderUserMessage = () => { const userInput = txtInput.value; renderMessageEle(userInput, "user"); txtInput.value = ""; setTimeout(() => { renderChatbotResponse(userInput); setScrollPosition(); }, 600); }; const renderChatbotResponse = (userInput) => { const res = getChatbotResponse(userInput); renderMessageEle(res); }; const renderMessageEle = (txt, type) => { let className = "user-message"; if (type !== "user") { className = "chatbot-message"; } const messageEle = document.createElement("div"); const txtNode = document.createTextNode(txt); messageEle.classList.add(className); messageEle.append(txtNode); chatBody.append(messageEle); }; const getChatbotResponse = (userInput) => { return responseObj[userInput] == undefined ? "聽不太懂呢試試輸點別的" : responseObj[userInput]; }; const setScrollPosition = () => { if (chatBody.scrollHeight > 0) { chatBody.scrollTop = chatBody.scrollHeight; } }; </script> </body> </html>
到此這篇關于html+css+js實現(xiàn)簡易版ChatGPT聊天機器人的文章就介紹到這了,更多相關html+css+js實現(xiàn)聊天內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
js冒泡法和數(shù)組轉(zhuǎn)換成字符串示例代碼
將數(shù)組轉(zhuǎn)換成字符串的方法有很多,想必大家也不會陌生,下面為大家講解下js冒泡法的使用,感興趣的朋友可以參考下2013-08-08js es6系列教程 - 新的類語法實戰(zhàn)選項卡(詳解)
下面小編就為大家?guī)硪黄猨s es6系列教程 - 新的類語法實戰(zhàn)選項卡(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09用Div仿showModalDialog模式菜單的效果的代碼
用Div仿showModalDialog模式菜單的效果的代碼...2007-03-03枚舉的實現(xiàn)求得1-1000所有出現(xiàn)1的數(shù)字并計算出現(xiàn)1的個數(shù)
求得1-1000所有出現(xiàn)1的數(shù)字,并計算出現(xiàn)1的個數(shù),以下是采用枚舉的實現(xiàn)方法,但是若從1-N就不管用了,因為N不一定會是多少2013-09-09js前端加密庫Crypto-js進行MD5/SHA256/BASE64/AES加解密的方法與示例
js加密解密可以使用crypto-js它可以進行MD5、SHA-1、SHA-256、Base64、AES、DES、等算法和加密,這是一個對稱加密的庫,可以使用 AES、DES、但沒有rsa等非對稱加密的方法2023-12-12