Mohit0199 commited on
Commit
b64250b
Β·
verified Β·
1 Parent(s): 4f20b87

update the code. I want user will enter their api key and then all the list from open router models will be shown and user can choose any model they want and it model will be used for chatbot conversation

Browse files
Files changed (1) hide show
  1. index.html +74 -13
index.html CHANGED
@@ -72,14 +72,11 @@
72
  <div>
73
  <label class="block text-sm font-medium mb-1">Select Model</label>
74
  <select id="modelSelect" class="w-full bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500">
75
- <option value="openai/gpt-3.5-turbo">GPT-3.5 Turbo</option>
76
- <option value="openai/gpt-4">GPT-4</option>
77
- <option value="anthropic/claude-2">Claude 2</option>
78
- <option value="google/palm-2-chat-bison">PaLM 2 Chat</option>
79
- <option value="meta-llama/llama-2-70b-chat">Llama 2 (70B)</option>
80
  </select>
 
81
  </div>
82
- <div>
83
  <label class="block text-sm font-medium mb-1">Chat Theme</label>
84
  <div class="flex space-x-2">
85
  <button data-theme="indigo" class="theme-btn w-8 h-8 bg-indigo-600 rounded-full border-2 border-transparent focus:border-white"></button>
@@ -150,9 +147,8 @@
150
  const typingIndicator = document.getElementById('typingIndicator');
151
  const modelIndicator = document.getElementById('modelIndicator');
152
  const themeBtns = document.querySelectorAll('.theme-btn');
153
-
154
  // Load saved settings
155
- function loadSettings() {
156
  const savedApiKey = localStorage.getItem('chatRouterApiKey');
157
  const savedModel = localStorage.getItem('chatRouterModel');
158
  const savedTheme = localStorage.getItem('chatRouterTheme') || 'indigo';
@@ -160,6 +156,7 @@
160
  if (savedApiKey) {
161
  apiKeyInput.value = savedApiKey;
162
  sendBtn.disabled = false;
 
163
  }
164
  if (savedModel) {
165
  modelSelect.value = savedModel;
@@ -170,12 +167,69 @@
170
  document.body.className = `bg-gradient-to-br from-${savedTheme}-900 to-purple-900 text-white`;
171
  }
172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  // Save settings
174
- saveSettings.addEventListener('click', () => {
175
  const apiKey = apiKeyInput.value.trim();
176
  const model = modelSelect.value;
177
 
178
- if (apiKey) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  localStorage.setItem('chatRouterApiKey', apiKey);
180
  localStorage.setItem('chatRouterModel', model);
181
  sendBtn.disabled = false;
@@ -184,12 +238,19 @@
184
 
185
  // Add confirmation message to chat
186
  addMessage('system', 'Settings saved successfully. You can now start chatting!');
187
- } else {
188
- alert('Please enter your OpenRouter API key');
189
  }
190
  });
191
 
192
- // Theme selection
 
 
 
 
 
 
 
193
  themeBtns.forEach(btn => {
194
  btn.addEventListener('click', () => {
195
  const theme = btn.dataset.theme;
 
72
  <div>
73
  <label class="block text-sm font-medium mb-1">Select Model</label>
74
  <select id="modelSelect" class="w-full bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500">
75
+ <option value="" disabled selected>Enter API key to load models</option>
 
 
 
 
76
  </select>
77
+ <p class="text-xs text-gray-400 mt-1">Models will be loaded after entering API key</p>
78
  </div>
79
+ <div>
80
  <label class="block text-sm font-medium mb-1">Chat Theme</label>
81
  <div class="flex space-x-2">
82
  <button data-theme="indigo" class="theme-btn w-8 h-8 bg-indigo-600 rounded-full border-2 border-transparent focus:border-white"></button>
 
147
  const typingIndicator = document.getElementById('typingIndicator');
148
  const modelIndicator = document.getElementById('modelIndicator');
149
  const themeBtns = document.querySelectorAll('.theme-btn');
 
150
  // Load saved settings
151
+ async function loadSettings() {
152
  const savedApiKey = localStorage.getItem('chatRouterApiKey');
153
  const savedModel = localStorage.getItem('chatRouterModel');
154
  const savedTheme = localStorage.getItem('chatRouterTheme') || 'indigo';
 
156
  if (savedApiKey) {
157
  apiKeyInput.value = savedApiKey;
158
  sendBtn.disabled = false;
159
+ await fetchModels(savedApiKey); // Load models when API key exists
160
  }
161
  if (savedModel) {
162
  modelSelect.value = savedModel;
 
167
  document.body.className = `bg-gradient-to-br from-${savedTheme}-900 to-purple-900 text-white`;
168
  }
169
 
170
+ // Fetch available models from OpenRouter
171
+ async function fetchModels(apiKey) {
172
+ try {
173
+ const response = await fetch('https://openrouter.ai/api/v1/models', {
174
+ headers: {
175
+ 'Authorization': `Bearer ${apiKey}`,
176
+ 'HTTP-Referer': window.location.href,
177
+ 'X-Title': 'ChatRouter'
178
+ }
179
+ });
180
+
181
+ const data = await response.json();
182
+ if (data.error) {
183
+ addMessage('system', `Error loading models: ${data.error.message}`);
184
+ return;
185
+ }
186
+
187
+ // Clear existing options
188
+ modelSelect.innerHTML = '';
189
+
190
+ // Add default option
191
+ const defaultOption = document.createElement('option');
192
+ defaultOption.value = '';
193
+ defaultOption.textContent = 'Select a model';
194
+ defaultOption.disabled = true;
195
+ defaultOption.selected = true;
196
+ modelSelect.appendChild(defaultOption);
197
+
198
+ // Add chat models with pricing info
199
+ data.data
200
+ .filter(model => model.description.includes('chat'))
201
+ .sort((a, b) => b.top_provider.max_completion_tokens - a.top_provider.max_completion_tokens)
202
+ .forEach(model => {
203
+ const option = document.createElement('option');
204
+ option.value = model.id;
205
+ option.textContent = `${model.name} (${(model.pricing.completion / 1000).toFixed(4)}/token)`;
206
+ modelSelect.appendChild(option);
207
+ });
208
+
209
+ } catch (error) {
210
+ addMessage('system', `Error loading models: ${error.message}`);
211
+ }
212
+ }
213
  // Save settings
214
+ saveSettings.addEventListener('click', async () => {
215
  const apiKey = apiKeyInput.value.trim();
216
  const model = modelSelect.value;
217
 
218
+ if (!apiKey) {
219
+ alert('Please enter your OpenRouter API key');
220
+ return;
221
+ }
222
+
223
+ if (!model) {
224
+ alert('Please select a model');
225
+ return;
226
+ }
227
+
228
+ try {
229
+ // Verify API key by fetching models
230
+ await fetchModels(apiKey);
231
+
232
+ // If successful, save settings
233
  localStorage.setItem('chatRouterApiKey', apiKey);
234
  localStorage.setItem('chatRouterModel', model);
235
  sendBtn.disabled = false;
 
238
 
239
  // Add confirmation message to chat
240
  addMessage('system', 'Settings saved successfully. You can now start chatting!');
241
+ } catch (error) {
242
+ addMessage('system', `Error verifying API key: ${error.message}`);
243
  }
244
  });
245
 
246
+ // Load models when API key changes
247
+ apiKeyInput.addEventListener('blur', async () => {
248
+ const apiKey = apiKeyInput.value.trim();
249
+ if (apiKey) {
250
+ await fetchModels(apiKey);
251
+ }
252
+ });
253
+ // Theme selection
254
  themeBtns.forEach(btn => {
255
  btn.addEventListener('click', () => {
256
  const theme = btn.dataset.theme;