-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat.html
More file actions
182 lines (169 loc) · 5.71 KB
/
chat.html
File metadata and controls
182 lines (169 loc) · 5.71 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple ChatGPT Clone</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f0f0f0;
}
#app {
width: 100%;
max-width: 600px;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
padding: 20px;
}
#apiKeyForm, #chatInterface {
display: none;
}
#chatMessages {
height: 300px;
overflow-y: auto;
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
}
.message {
margin-bottom: 10px;
padding: 5px;
border-radius: 5px;
}
.user {
background-color: #e6f2ff;
text-align: right;
}
.assistant {
background-color: #f0f0f0;
}
input[type="text"], textarea {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
margin-right: 10px;
}
button:hover {
background-color: #45a049;
}
#removeApiKey {
background-color: #f44336;
}
#removeApiKey:hover {
background-color: #d32f2f;
}
</style>
</head>
<body>
<div id="app">
<div id="apiKeyForm">
<h2>Enter your OpenAI API Key</h2>
<input type="text" id="apiKey" placeholder="Enter your OpenAI API Key">
<button onclick="saveApiKey()">Save API Key</button>
</div>
<div id="chatInterface">
<h2>ChatGPT Clone</h2>
<div id="chatMessages"></div>
<textarea id="userInput" placeholder="Type your message here..."></textarea>
<button onclick="sendMessage()">Send</button>
<button id="removeApiKey" onclick="removeApiKey()">Remove API Key</button>
</div>
</div>
<script>
const apiKeyForm = document.getElementById('apiKeyForm');
const chatInterface = document.getElementById('chatInterface');
const chatMessages = document.getElementById('chatMessages');
const userInput = document.getElementById('userInput');
function saveApiKey() {
const apiKey = document.getElementById('apiKey').value;
if (apiKey) {
localStorage.setItem('openaiApiKey', apiKey);
showChatInterface();
} else {
alert('Please enter a valid API key');
}
}
function removeApiKey() {
localStorage.removeItem('openaiApiKey');
showApiKeyForm();
}
function showChatInterface() {
apiKeyForm.style.display = 'none';
chatInterface.style.display = 'block';
}
function showApiKeyForm() {
apiKeyForm.style.display = 'block';
chatInterface.style.display = 'none';
chatMessages.innerHTML = ''; // Clear chat messages
}
function addMessage(content, isUser) {
const messageDiv = document.createElement('div');
messageDiv.classList.add('message');
messageDiv.classList.add(isUser ? 'user' : 'assistant');
messageDiv.textContent = content;
chatMessages.appendChild(messageDiv);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
async function sendMessage() {
const message = userInput.value.trim();
if (!message) return;
addMessage(message, true);
userInput.value = '';
const apiKey = localStorage.getItem('openaiApiKey');
try {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: message }]
})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
const assistantReply = data.choices[0].message.content;
addMessage(assistantReply, false);
} catch (error) {
console.error('Error:', error);
addMessage('Error: Unable to get a response from the API.', false);
}
}
// Check if API key exists in localStorage
if (localStorage.getItem('openaiApiKey')) {
showChatInterface();
} else {
showApiKeyForm();
}
// Allow sending message with Enter key
userInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
});
</script>
</body>
</html>