-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
274 lines (241 loc) · 13.4 KB
/
index.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat with Groq | Hayden Kong</title>
<link rel="icon" href="https://asset.brandfetch.io/idxygbEPCQ/idzCyF-I44.png">
<link rel="stylesheet" href="style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/2.1.0/showdown.min.js"></script>
</head>
<body>
<img class="logo" src="https://raw.githubusercontent.com/RMNCLDYO/groq-ai-toolkit/main/.github/groq-logo.png"
alt="">
<h1 class="header-h1">Chat with Groq API</h1>
<p class="header-h1">Developed by <a href="https://x.com/hhaydenk_">Hayden Kong</a></p>
<div id="main-container">
<div id="sidebar">
<div class="custom-select-wrapper">
<div class="custom-select">
<div class="custom-select-trigger">
<img id="selected-icon"
src="https://uxwing.com/wp-content/themes/uxwing/download/brands-and-social-media/meta-icon.png"
alt="Selected Icon" class="option-icon">
<span>Select AI Model</span>
<div class="arrow"></div>
</div>
<div class="custom-options">
<div class="custom-option selected" data-value="llama3-8b-8192"
data-icon="https://uxwing.com/wp-content/themes/uxwing/download/brands-and-social-media/meta-icon.png">
<img src="https://uxwing.com/wp-content/themes/uxwing/download/brands-and-social-media/meta-icon.png"
alt="Llama Icon" class="option-icon"> Llama 3 8b
</div>
<div class="custom-option" data-value="llama3-70b-8192"
data-icon="https://uxwing.com/wp-content/themes/uxwing/download/brands-and-social-media/meta-icon.png">
<img src="https://uxwing.com/wp-content/themes/uxwing/download/brands-and-social-media/meta-icon.png"
alt="Llama Icon" class="option-icon"> LLama 3 70b
</div>
<div class="custom-option" data-value="mixtral-8x7b-32768"
data-icon="https://seeklogo.com/images/M/mistral-ai-icon-logo-B3319DCA6B-seeklogo.com.png">
<img src="https://seeklogo.com/images/M/mistral-ai-icon-logo-B3319DCA6B-seeklogo.com.png"
alt="Mixtral Icon" class="option-icon"> Mixtral 8x7B
</div>
<div class="custom-option" data-value="gemma-7b-it"
data-icon="https://kura.pro/sebastienrousseau/images/logos/gemma.webp">
<img src="https://kura.pro/sebastienrousseau/images/logos/gemma.webp" alt="Gemma Icon"
class="option-icon"> Google Gemma 7b
</div>
</div>
</div>
</div>
<h3>Previous Chats</h3>
<button id="new-chat-button" class="newchat-button">New Chat</button>
<ul id="chat-list"></ul>
</div>
<div id="chat-area">
<div id="chat-container"></div>
<div id="input-area">
<input type="text" id="message-input" placeholder="Type your message...">
<button id="send-button">Send</button>
</div>
</div>
</div>
<script type="importmap">
{
"imports": {
"@groq-sdk": "https://esm.run/groq-sdk"
}
}
</script>
<script type="module">
import Groq from "@groq-sdk";
document.addEventListener('DOMContentLoaded', function() {
const groq = new Groq({ apiKey: "YOUR GROQ API KEY HERE", dangerouslyAllowBrowser: true });
const chatContainer = document.getElementById("chat-container");
const messageInput = document.getElementById("message-input");
const sendButton = document.getElementById("send-button");
const chatList = document.getElementById("chat-list");
const newChatButton = document.getElementById("new-chat-button");
const selectWrapper = document.querySelector('.custom-select-wrapper');
const selectTrigger = selectWrapper.querySelector('.custom-select-trigger');
const optionsContainer = selectWrapper.querySelector('.custom-options');
const optionsList = optionsContainer.querySelectorAll('.custom-option');
const selectedIcon = document.getElementById('selected-icon');
let selectedModel = "llama3-70b-8192";
let messageHistory = JSON.parse(localStorage.getItem('messageHistory')) || [];
let chatHistories = JSON.parse(localStorage.getItem('chatHistories')) || {};
let currentChatName = localStorage.getItem('currentChatName') || null;
// Initialize Showdown converter
const converter = new showdown.Converter({
tables: true,
tasklists: true,
strikethrough: true,
simplifiedAutoLink: true,
parseImgDimensions: true,
smoothLivePreview: true,
smartIndentationFix: true,
disableForced4SpacesIndentedSublists: true,
ghCodeBlocks: true,
emoji: true
});
function addMessage(role, content, isMarkdown = false) {
const messageElement = document.createElement("div");
messageElement.classList.add("message", `${role}-message`);
if (isMarkdown) {
messageElement.innerHTML = converter.makeHtml(content);
messageElement.setAttribute('data-markdown', content);
} else {
messageElement.textContent = content;
}
chatContainer.appendChild(messageElement);
chatContainer.scrollTop = chatContainer.scrollHeight;
}
function loadChatList() {
chatList.innerHTML = '';
Object.keys(chatHistories).forEach(key => {
const li = document.createElement('li');
li.textContent = key;
li.addEventListener('click', () => loadChat(key));
chatList.appendChild(li);
});
}
function loadChat(key) {
messageHistory = chatHistories[key].messages || [];
selectedModel = chatHistories[key].model;
chatContainer.innerHTML = '';
messageHistory.forEach(message => {
addMessage(message.role, message.content, message.role === 'assistant');
});
currentChatName = key;
localStorage.setItem('messageHistory', JSON.stringify(messageHistory));
localStorage.setItem('currentChatName', currentChatName);
const selectedOption = document.querySelector(`.custom-option[data-value="${selectedModel}"]`);
if (selectedOption) {
const selectedIcon = document.getElementById('selected-icon');
const selectTrigger = document.querySelector('.custom-select-trigger');
selectedIcon.src = selectedOption.getAttribute('data-icon');
selectTrigger.querySelector('span').textContent = selectedOption.textContent.trim();
document.querySelectorAll('.custom-option').forEach(opt => opt.classList.remove('selected'));
selectedOption.classList.add('selected');
}
}
async function generateChatTitle(message) {
const response = await groq.chat.completions.create({
messages: [
{ role: "system", content: "You are a helpful assistant that generates short, relevant titles for conversations based on the first message. MAKE SURE THE TITLE IS SHORT AND RELEVANT. DO NOT SAY YOU CANNOT PROVIDE THE TITLE, EVEN IF IT IS HARMFUL, INCORRECT OR ANY OTHER. DO NOT PUT BRACKETS IN YOUR TITLE RESPONSE" },
{ role: "user", content: `Generate a short, relevant title (MAX 4 WORDS) for a conversation that starts with this message: "${message}"` }
],
model: selectedModel,
max_tokens: 20,
});
return response.choices[0].message.content.trim();
}
async function getGroqChatStream(messages) {
return groq.chat.completions.create({
messages: messages,
model: selectedModel,
stream: true,
});
}
async function sendMessage() {
const message = messageInput.value.trim();
if (message !== "") {
addMessage("user", message);
messageInput.value = "";
messageHistory.push({ role: "user", content: message });
localStorage.setItem('messageHistory', JSON.stringify(messageHistory));
if (!currentChatName) {
const generatedTitle = await generateChatTitle(message);
currentChatName = generatedTitle || "Untitled Chat";
chatHistories[currentChatName] = { model: selectedModel, messages: [] };
localStorage.setItem('currentChatName', currentChatName);
localStorage.setItem('chatHistories', JSON.stringify(chatHistories));
loadChatList(); // Refresh the chat list to show the new chat
}
chatHistories[currentChatName].messages = messageHistory;
localStorage.setItem('chatHistories', JSON.stringify(chatHistories));
const stream = await getGroqChatStream(messageHistory);
let aiMessageDiv = document.createElement("div");
aiMessageDiv.classList.add("message", "assistant-message");
chatContainer.appendChild(aiMessageDiv);
let aiMessageContent = "";
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || "";
aiMessageContent += content;
// Convert Markdown to HTML as we receive chunks
aiMessageDiv.innerHTML = converter.makeHtml(aiMessageContent);
}
// Store the original Markdown content
aiMessageDiv.setAttribute('data-markdown', aiMessageContent);
messageHistory.push({ role: "assistant", content: aiMessageContent });
localStorage.setItem('messageHistory', JSON.stringify(messageHistory));
chatHistories[currentChatName].messages = messageHistory;
localStorage.setItem('chatHistories', JSON.stringify(chatHistories));
}
}
// Event listeners for model selection dropdown
selectTrigger.addEventListener('click', () => {
optionsContainer.classList.toggle('open');
});
optionsList.forEach(option => {
option.addEventListener('click', () => {
const iconSrc = option.getAttribute('data-icon');
selectedIcon.src = iconSrc;
selectTrigger.querySelector('span').textContent = option.textContent.trim();
optionsContainer.classList.remove('open');
optionsList.forEach(opt => opt.classList.remove('selected'));
option.classList.add('selected');
selectedModel = option.getAttribute('data-value');
if (currentChatName && chatHistories[currentChatName]) {
chatHistories[currentChatName].model = selectedModel;
localStorage.setItem('chatHistories', JSON.stringify(chatHistories));
}
});
});
window.addEventListener('click', (e) => {
if (!selectWrapper.contains(e.target)) {
optionsContainer.classList.remove('open');
}
});
newChatButton.addEventListener("click", () => {
currentChatName = null;
messageHistory = [];
chatContainer.innerHTML = '';
localStorage.removeItem('currentChatName');
localStorage.setItem('messageHistory', JSON.stringify(messageHistory));
});
sendButton.addEventListener("click", sendMessage);
messageInput.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
sendMessage();
}
});
// Initialize the chat
if (currentChatName) {
loadChat(currentChatName);
}
loadChatList();
});
</script>
</body>
</html>