最近chatgpt聊天機(jī)器,的一款聊天機(jī)器人模型爆火,本篇文章用一百行代碼給大家制作一款簡(jiǎn)易的聊天機(jī)器人chatgpt聊天機(jī)器人chatgpt聊天機(jī)器人,話不多說(shuō),上圖上代碼。

在線體驗(yàn)地址: 代碼下載:一百行代碼實(shí)現(xiàn)簡(jiǎn)易版聊天機(jī)器人

HTML完整代碼

		
簡(jiǎn)易版Chat GPT

復(fù)制

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;
}

復(fù)制

JS

const responseObj = {
	你好: "你好,我是最強(qiáng)人工智能ChatGPT,我能回答你所有問(wèn)題,快來(lái)和我聊天吧!",
	五塊錢(qián)怎么花三天: "坐公交回去找媽媽",				
	你是小黑子嗎: "不,我不是小黑子。我是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 ?
		"聽(tīng)不太懂呢試試輸點(diǎn)別的" :
		responseObj[userInput];
};
const setScrollPosition = () => {
	if (chatBody.scrollHeight > 0) {
		chatBody.scrollTop = chatBody.scrollHeight;
	}
};

復(fù)制

免責(zé)聲明:本文系轉(zhuǎn)載,版權(quán)歸原作者所有;旨在傳遞信息,不代表本站的觀點(diǎn)和立場(chǎng)和對(duì)其真實(shí)性負(fù)責(zé)。如需轉(zhuǎn)載,請(qǐng)聯(lián)系原作者。如果來(lái)源標(biāo)注有誤或侵犯了您的合法權(quán)益或者其他問(wèn)題不想在本站發(fā)布,來(lái)信即刪。