html+css+js實(shí)現(xiàn)簡(jiǎn)易版ChatGPT聊天機(jī)器人
OpenAI的一款聊天機(jī)器人模型ChatGPT爆火,本篇文章用一百行html+css+js代碼給大家制作一款簡(jiǎn)易的聊天機(jī)器人。
<!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">簡(jiǎn)易版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 = {
你好: "你好,我是最強(qiáng)人工智能ChatGPT,我能回答你所有問題,快來和我聊天吧!",
五塊錢怎么花三天: "坐公交回去找媽媽",
你是小黑子嗎: "不,我不是小黑子。我是OpenAI的聊天機(jī)器人模型ChatGPT",
你為什么和我聊天: "只因你太美",
嘿: "嘿! 這是怎么回事",
今天幾號(hào): new Date().toDateString(),
幾點(diǎn)了: 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 ?
"聽不太懂呢試試輸點(diǎn)別的" :
responseObj[userInput];
};
const setScrollPosition = () => {
if (chatBody.scrollHeight > 0) {
chatBody.scrollTop = chatBody.scrollHeight;
}
};
</script>
</body>
</html>到此這篇關(guān)于html+css+js實(shí)現(xiàn)簡(jiǎn)易版ChatGPT聊天機(jī)器人的文章就介紹到這了,更多相關(guān)html+css+js實(shí)現(xiàn)聊天內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JavaScript基于ChatGPT?API實(shí)現(xiàn)劃詞翻譯瀏覽器腳本
- 兩天沒解決的問題chatgpt用了5秒搞定隱藏bug
- 一文解析ChatGPT?之?Fetch?請(qǐng)求
- 讓chatGPT教你如何使用taro創(chuàng)建mbox
- LangChain簡(jiǎn)化ChatGPT工程復(fù)雜度使用詳解
- ChatGPT用于OA聊天助手導(dǎo)致訪問量服務(wù)宕機(jī)
- 詳解微信小程序如何實(shí)現(xiàn)類似ChatGPT的流式傳輸
- ChatGPT前端編程秀之別拿編程語(yǔ)言不當(dāng)語(yǔ)言
- 詳解如何利用chatgpt保護(hù)您的js代碼
相關(guān)文章
js冒泡法和數(shù)組轉(zhuǎn)換成字符串示例代碼
將數(shù)組轉(zhuǎn)換成字符串的方法有很多,想必大家也不會(huì)陌生,下面為大家講解下js冒泡法的使用,感興趣的朋友可以參考下2013-08-08
js es6系列教程 - 新的類語(yǔ)法實(shí)戰(zhàn)選項(xiàng)卡(詳解)
下面小編就為大家?guī)硪黄猨s es6系列教程 - 新的類語(yǔ)法實(shí)戰(zhàn)選項(xiàng)卡(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
用Div仿showModalDialog模式菜單的效果的代碼
用Div仿showModalDialog模式菜單的效果的代碼...2007-03-03
Bootstrap 附加導(dǎo)航(Affix)插件實(shí)例詳解
附加導(dǎo)航(Affix)插件允許某個(gè) <div> 固定在頁(yè)面的某個(gè)位置。接下來通過本文給大家介紹Bootstrap 附加導(dǎo)航(Affix)插件實(shí)例詳解,感興趣的朋友一起看看吧2016-06-06
枚舉的實(shí)現(xiàn)求得1-1000所有出現(xiàn)1的數(shù)字并計(jì)算出現(xiàn)1的個(gè)數(shù)
求得1-1000所有出現(xiàn)1的數(shù)字,并計(jì)算出現(xiàn)1的個(gè)數(shù),以下是采用枚舉的實(shí)現(xiàn)方法,但是若從1-N就不管用了,因?yàn)镹不一定會(huì)是多少2013-09-09
js前端加密庫(kù)Crypto-js進(jìn)行MD5/SHA256/BASE64/AES加解密的方法與示例
js加密解密可以使用crypto-js它可以進(jìn)行MD5、SHA-1、SHA-256、Base64、AES、DES、等算法和加密,這是一個(gè)對(duì)稱加密的庫(kù),可以使用 AES、DES、但沒有rsa等非對(duì)稱加密的方法2023-12-12
JavaScript Accessor實(shí)現(xiàn)說明
關(guān)于Getter與Setter大家一定不會(huì)陌生,下面簡(jiǎn)單介紹幾種我所知道的在JavaScript中實(shí)現(xiàn)G/S的方法.2010-12-12

