-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
204 lines (178 loc) · 6.3 KB
/
Copy pathindex.html
File metadata and controls
204 lines (178 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>
<html>
<head>
<title>Chatbot</title>
<style>
.bot, .user {
background: linear-gradient(120deg, #b39ddb, #7e57c2);
padding: 10px;
border-radius: 10px;
margin-bottom: 10px;
color: white;
}
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #333;
}
h1 {
text-align: center;
background: linear-gradient(120deg, #7e57c2, #673ab7);
color: white;
margin: 0;
padding: 20px;
}
#typing-indicator {
color: white;
}
#chat-history {
height: 60vh; /* Adjusted height for better visibility on smaller screens */
overflow-y: scroll;
padding: 10px;
}
#chat-controls {
display: flex;
flex-wrap: wrap; /* Allow controls to wrap on smaller screens */
justify-content: space-between;
align-items: center;
padding: 10px;
}
#user-input {
width: 100%; /* Full width on smaller screens */
margin-bottom: 10px; /* Add margin for better spacing */
padding: 10px;
border-radius: 5px;
order: 1; /* Set the order to 1 to position it above the buttons */
}
button {
width: 48%; /* Adjusted button width for better layout on smaller screens */
margin-bottom: 10px; /* Add margin for better spacing */
padding: 10px;
background: linear-gradient(120deg, #7e57c2, #673ab7);
color: white;
border-radius: 5px;
order: 2; /* Set the order to 2 */
}
#api-key-input {
width: 48%; /* Adjusted width for better layout */
margin-bottom: 10px; /* Add margin for better spacing */
padding: 10px;
border-radius: 5px;
order: 3; /* Set the order to 3 */
}
#send-button,
#clear-chat-button {
width: 100%; /* Full width on smaller screens */
margin-bottom: 10px; /* Add margin for better spacing */
padding: 10px;
background: linear-gradient(120deg, #7e57c2, #673ab7);
color: white;
border-radius: 5px;
order: 4; /* Set the order to 4 */
box-sizing: border-box; /* Include padding and border in the element's total width and height */
}
@media (max-width: 600px) {
#user-input,
#api-key-input {
width: 100%; /* Full width on smaller screens */
}
button {
width: 48%; /* Adjusted button width for better layout on smaller screens */
}
#send-button,
#clear-chat-button {
width: 100%; /* Full width on smaller screens */
}
}
button:hover, #send-button:hover, #clear-chat-button:hover {
opacity: 0.7;
transition: opacity 0.5s;
background: linear-gradient(120deg, #673ab7, #7e57c2);
}
footer {
background: linear-gradient(120deg, #7e57c2, #673ab7);
color: white;
text-align: center;
padding: 20px;
position: absolute;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<h1>Chatbot</h1>
<div id="chat-history"></div>
<div id="chat-controls">
<div id="typing-indicator"></div>
<input type="text" id="user-input" placeholder="Type your message here..." onkeydown="if (event.keyCode === 13) sendMessage()">
<button onclick="sendMessage()">Send</button>
<button onclick="clearChatHistory()">Clear chat</button>
<input type="text" id="api-key-input" placeholder="Enter API Key">
<button onclick="setApiKey()">Set API Key</button>
</div>
<script>
let openaiApiKey = ""; // Initialize API key variable
let chatHistory = [];
window.onload = () => {
chatHistory.push({ sender: "bot", message: "Hello, how can I help you?" });
updateChatHistory();
}
function setApiKey() {
openaiApiKey = document.getElementById("api-key-input").value;
alert("API Key set successfully!");
}
async function sendMessage() {
if (!openaiApiKey) {
alert("Please set the API Key first!");
return;
}
const userInput = document.getElementById("user-input").value;
chatHistory.push({ sender: "user", message: userInput });
document.getElementById("user-input").value = "";
const typingIndicator = document.getElementById("typing-indicator");
typingIndicator.innerText = "Bot is thinking...";
const userMessages = chatHistory
.filter((chat) => chat.sender === "user")
.map((chat) => chat.message);
const response = await fetch("https://api.openai.com/v1/engines/text-davinci-003/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${openaiApiKey}`,
},
body: JSON.stringify({
prompt: userMessages.join("\n"),
max_tokens: 2000,
temperature: 0.5,
}),
});
const data = await response.json();
const botResponse = data.choices[0].text.trim();
chatHistory.push({ sender: "bot", message: botResponse });
typingIndicator.innerText = "";
if (!botResponse) {
chatHistory.push({ sender: "bot", message: "I'm sorry, I didn't understand your question." });
}
updateChatHistory();
}
function updateChatHistory() {
const chatHistoryDiv = document.getElementById("chat-history");
chatHistoryDiv.innerHTML = "";
for (const chat of chatHistory) {
const chatDiv = document.createElement("div");
chatDiv.innerText = `${chat.sender}: ${chat.message}`;
chatDiv.classList.add(chat.sender);
chatHistoryDiv.appendChild(chatDiv);
}
chatHistoryDiv.scrollTop = chatHistoryDiv.scrollHeight;
}
function clearChatHistory() {
chatHistory = [];
chatHistory.push({ sender: "bot", message: "Hello, how can I help you?" });
updateChatHistory();
}
</script>
</body>
</html>