-
Notifications
You must be signed in to change notification settings - Fork 0
/
aichat.html
183 lines (168 loc) · 5.27 KB
/
aichat.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat with AI</title>
<style>
* { border-radius: 5px; }
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
margin: 0;
height: 100vh;
justify-content: center;
}
#chat-box { width: 80%; max-width: 600px; }
#msgs {
border: 1px solid #ccc;
padding: 10px;
height: 300px;
overflow-y: auto;
margin-bottom: 10px;
position: relative;
}
#msgs p {
margin: 5px 0;
position: relative;
}
#msgs p:hover .delete-btn { display: block; }
#input-area { display: flex; }
#input-area input {
flex: 1;
padding: 10px;
font-size: 16px;
border: 1.5px solid;
}
#input-area button {
padding: 10px;
font-size: 16px;
margin-left: 10px;
}
.msg {
margin-bottom: 10px;
}
.msg.system { color: #888; }
.msg.user { color: #000; background-color: #e1e1e1; padding: 10px; }
.msg.assistant { color: #000; }
.btns {
background-color: rgb(215, 215, 215);
border-radius: 5px;
padding: 10px;
font-size: 16px;
cursor: pointer;
border: none;
}
#send-btn { background-color: rgb(0, 208, 255); }
.btns:hover { background-color: rgb(192, 192, 192); }
#send-btn:hover { background-color: rgb(0, 181, 222); }
.delete-btn {
display: none;
position: absolute;
top: 50%;
right: 10px;
transform: translateY(-50%);
background-color: rgba(255, 0, 0, 0.374);
color: white;
border: none;
width: 20px;
height: 20px;
cursor: pointer;
font-size: 14px;
line-height: 20px;
text-align: center;
}
hr { border: 0; border-top: 1px solid #ccc; margin: 10px 0; }
</style>
</head>
<body>
<div id="chat-box">
<div id="msgs"></div>
<div id="input-area">
<input type="text" id="txt-input" placeholder="Type your message..." required>
<button class="btns" id="send-btn">Send</button>
<button class="btns" id="clear-btn">Clear Chat</button>
<button class="btns" id="set-prompt-btn">Set System Prompt</button>
</div>
</div>
<script>
const url = 'https://purple-dream-a721.cosingscratcher.workers.dev/';
const msgContainer = document.getElementById('msgs');
const txtInput = document.getElementById('txt-input');
const sendBtn = document.getElementById('send-btn');
const clearBtn = document.getElementById('clear-btn');
const setPromptBtn = document.getElementById('set-prompt-btn');
let promptText = 'You will answer short, simple and friendly';
let chatData = JSON.parse(localStorage.getItem('chatData')) || [{ role: 'system', content: promptText }];
function saveData() {
localStorage.setItem('chatData', JSON.stringify(chatData));
}
function addMsg(content, role) {
const msgElement = document.createElement('p');
msgElement.classList.add('msg', role);
msgElement.textContent = content;
const separator = document.createElement('hr');
if (role !== 'system') {
const deleteBtn = document.createElement('button');
deleteBtn.textContent = 'x';
deleteBtn.classList.add('delete-btn');
deleteBtn.onclick = () => {
msgContainer.removeChild(msgElement);
msgContainer.removeChild(separator);
chatData = chatData.filter(msg => msg.content !== content);
saveData();
};
msgElement.appendChild(deleteBtn);
}
msgContainer.append(msgElement, separator);
msgContainer.scrollTop = msgContainer.scrollHeight;
}
async function sendMsg(msg) {
chatData.push({ role: 'user', content: msg });
addMsg(msg, 'user');
saveData();
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ messages: chatData })
});
const { response: { response: assistantReply } } = await response.json();
chatData.push({ role: 'assistant', content: assistantReply });
addMsg(assistantReply, 'assistant');
saveData();
}
function clearMsgs() {
msgContainer.innerHTML = '';
chatData = [{ role: 'system', content: promptText }];
addMsg(promptText, 'system');
saveData();
}
function updatePrompt() {
const newPrompt = prompt('Enter the new system prompt:');
if (newPrompt) {
promptText = newPrompt;
chatData[0] = { role: 'system', content: promptText };
addMsg(promptText, 'system');
saveData();
}
}
sendBtn.addEventListener('click', async () => {
const msg = txtInput.value;
txtInput.value = '';
if (msg) await sendMsg(msg);
});
txtInput.addEventListener('keydown', async event => {
if (event.key === 'Enter') {
const msg = txtInput.value;
txtInput.value = '';
if (msg) await sendMsg(msg);
}
});
clearBtn.addEventListener('click', clearMsgs);
setPromptBtn.addEventListener('click', updatePrompt);
chatData.forEach(msg => addMsg(msg.content, msg.role));
</script>
</body>
</html>