<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Robot Assistance</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div id="chat-container">
<div id="chat-header">
<img src="https://i.imgur.com/TnUa864.png" alt="ChatGPT Profile Picture" />
<h3>Robot-assitance</h3>
</div>
<div id="chat-history">
<ul id="message-list"></ul>
</div>
<div id="chat-input">
<form>
<input
type="text"
id="user-message"
placeholder="Type your message here..."
/>
<button type="submit">Send</button>
</form>
</div>
<script src="script.js"></script>
</div>
</body>
</html>
$primary-color: #4caf50;
$secondary-color: #f5f5f5;
$border-color: #ccc;
$box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
#chat-container {
border: 1px solid $border-color;
border-radius: 5px;
box-shadow: $box-shadow;
width: 400px;
height: 500px;
margin: 0 auto;
display: flex;
flex-direction: column;
}
#chat-header {
background-color: $secondary-color;
padding: 10px;
border-radius: 5px 5px 0 0;
}
#chat-header h3 {
margin: 0;
}
#chat-history {
flex-grow: 1;
padding: 10px;
overflow-y: scroll;
}
#message-list {
list-style: none;
margin: 0;
padding: 0;
}
.user-message {
background-color: $primary-color;
color: #fff;
border-radius: 5px;
padding: 10px;
margin: 10px 0;
align-self: flex-end;
}
.bot-message {
background-color: $secondary-color;
color: #000;
border-radius: 5px;
padding: 10px;
margin: 10px 0;
align-self: flex-start;
}
#chat-input {
background-color: $secondary-color;
padding: 10px;
border-radius: 0 0 5px 5px;
}
#chat-input input[type="text"] {
border: none;
border-radius: 5px;
padding: 10px;
width: 80%;
&::placeholder {
color: #ccc;
}
}
#chat-input button {
border: none;
border-radius: 5px;
background-color: $primary-color;
color: #fff;
padding: 10px;
margin-left: 10px;
width: 20%;
cursor: pointer;
&:hover {
background-color: darken($primary-color, 10%);
}
}
#chat-header {
display: flex;
align-items: center;
}
#chat-header img {
border-radius: 50%;
width: 40px;
height: 40px;
margin-right: 10px;
}
View Compiled
async function getChatResponse(message) {
const prompt = `${message}\n`;
const maxTokens = 50;
const apiKey = "YOUR_API_KEY_HERE";
const apiUrl = "https://api.openai.com/v1/engines/davinci-codex/completions";
try {
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
prompt,
max_tokens: maxTokens,
}),
});
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}
const data = await response.json();
if (!data.choices || data.choices.length === 0) {
throw new Error("Response data is empty");
}
const completions = data.choices[0].text;
const chatResponse = completions.substring(
completions.indexOf(prompt) + prompt.length,
completions.lastIndexOf("\n")
);
return chatResponse;
} catch (error) {
console.error(error);
return "I'm sorry, I'm having trouble processing your message right now. Please try again later.";
}
}
const form = document.querySelector("form");
const messageList = document.querySelector("#message-list");
form.addEventListener("submit", (event) => {
event.preventDefault();
const input = document.querySelector("#user-message");
const message = input.value.trim();
// Add user message to chat window
const userMessageElement = document.createElement("li");
userMessageElement.classList.add("user-message");
userMessageElement.textContent = message;
messageList.appendChild(userMessageElement);
// Get response from ChatGPT
getChatResponse(message)
.then((response) => {
// Add ChatGPT response to chat window
const chatResponseElement = document.createElement("li");
chatResponseElement.classList.add("chat-response");
chatResponseElement.textContent = response;
messageList.appendChild(chatResponseElement);
// Scroll to bottom of chat window
messageList.scrollTop = messageList.scrollHeight;
// Clear input field
input.value = "";
})
.catch((error) => {
console.error(error);
alert(
"I'm sorry, I'm having trouble processing your message right now. Please try again later."
);
});
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.