-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
306 lines (246 loc) · 12.1 KB
/
script.js
File metadata and controls
306 lines (246 loc) · 12.1 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
class WishlistGenerator {
constructor() {
this.form = document.getElementById('wishlistForm');
this.loadingContainer = document.getElementById('loadingContainer');
this.wishlistContainer = document.getElementById('wishlistContainer');
this.errorContainer = document.getElementById('errorContainer');
this.wishlistItems = document.getElementById('wishlistItems');
this.regenerateBtn = document.getElementById('regenerateBtn');
this.retryBtn = document.getElementById('retryBtn');
this.errorText = document.getElementById('errorText');
// GitHub Models API configuration
this.apiEndpoint = 'https://models.inference.ai.azure.com/chat/completions';
this.modelName = 'gpt-4o-mini';
this.init();
}
init() {
this.form.addEventListener('submit', (e) => this.handleSubmit(e));
this.regenerateBtn.addEventListener('click', () => this.regenerateWishlist());
this.retryBtn.addEventListener('click', () => this.hideError());
}
async handleSubmit(e) {
e.preventDefault();
const apiKey = document.getElementById('apiKey').value;
const age = document.getElementById('age').value;
const interest = document.getElementById('interest').value;
if (!apiKey || !apiKey.trim()) {
this.showError('Please provide a GitHub Models API key!');
return;
}
if (!age || !interest.trim()) {
this.showError('Please fill in both age and interests!');
return;
}
await this.generateWishlist(apiKey, age, interest);
}
async generateWishlist(apiKey, age, interest) {
this.showLoading();
try {
const wishlist = await this.callGitHubModels(apiKey, age, interest);
this.displayWishlist(wishlist);
} catch (error) {
console.error('Error generating wishlist:', error);
let errorMessage = 'Sorry, I had trouble generating your wishlist. Please try again!';
if (error.message.includes('401') || error.message.includes('unauthorized')) {
errorMessage = 'Invalid API key. Please check your GitHub Models API key and try again.';
} else if (error.message.includes('429')) {
errorMessage = 'Rate limit exceeded. Please wait a moment and try again.';
} else if (error.message.includes('network') || error.message.includes('fetch')) {
errorMessage = 'Network error. Please check your internet connection and try again.';
}
this.showError(errorMessage);
}
}
async callGitHubModels(apiKey, age, interest) {
const prompt = `You are a helpful assistant that creates personalized wishlists. Create a wishlist of exactly 10 items for someone who is ${age} years old and interested in: ${interest}.
Each item should be:
- Age-appropriate and suitable for their interests
- Realistic and obtainable
- Have a clear name (2-6 words)
- Have a helpful description (1-2 sentences)
Please respond with a JSON array of exactly 10 objects, each with "name" and "description" fields. Make the items diverse and interesting, covering different price ranges and categories related to their interests.
Example format:
[
{
"name": "Premium Notebook Set",
"description": "High-quality leather-bound notebooks perfect for journaling and creative writing."
}
]
Only return the JSON array, no other text.`;
const requestBody = {
messages: [
{
role: "system",
content: "You are a helpful assistant that creates personalized wishlists based on age and interests. Always respond with valid JSON only."
},
{
role: "user",
content: prompt
}
],
model: this.modelName,
temperature: 0.8,
max_tokens: 2000,
top_p: 1
};
const response = await fetch(this.apiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (!data.choices || !data.choices[0] || !data.choices[0].message) {
throw new Error('Invalid response format from API');
}
const content = data.choices[0].message.content.trim();
try {
// Try to parse the JSON response
const wishlistItems = JSON.parse(content);
if (!Array.isArray(wishlistItems) || wishlistItems.length === 0) {
throw new Error('Invalid wishlist format');
}
// Ensure we have exactly 10 items and they have the correct format
const validItems = wishlistItems
.filter(item => item && item.name && item.description)
.slice(0, 10);
if (validItems.length < 5) {
throw new Error('Not enough valid items generated');
}
return validItems;
} catch (parseError) {
console.error('Failed to parse LLM response:', content);
// Fallback: try to extract items from text response
return this.parseTextResponse(content, age, interest);
}
}
parseTextResponse(content, age, interest) {
// Fallback parser for when LLM doesn't return valid JSON
const lines = content.split('\\n').filter(line => line.trim());
const items = [];
let currentItem = {};
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed) continue;
// Look for numbered items or items starting with - or *
if (/^\\d+\\.|^[-*]/.test(trimmed)) {
if (currentItem.name) {
items.push(currentItem);
}
currentItem = {
name: trimmed.replace(/^\\d+\\.|^[-*]\\s*/, '').replace(/[:.].*$/, '').trim(),
description: trimmed.replace(/^\\d+\\.|^[-*]\\s*[^:.]+([:.]\\s*)?/, '').trim() || 'A great item for your wishlist!'
};
} else if (currentItem.name && !currentItem.description) {
currentItem.description = trimmed;
}
}
if (currentItem.name) {
items.push(currentItem);
}
// If we still don't have enough items, generate some fallback items
if (items.length < 5) {
return this.generateFallbackWishlist(age, interest);
}
return items.slice(0, 10);
}
generateFallbackWishlist(age, interest) {
// Fallback wishlist generator when API fails
const interestLower = interest.toLowerCase();
const items = [];
// Base items that work for most people
const baseItems = [
{ name: "Premium Notebook Set", description: "High-quality notebooks perfect for writing, sketching, or planning." },
{ name: "Bluetooth Wireless Earbuds", description: "High-quality earbuds for music, podcasts, and calls on the go." },
{ name: "Cozy Reading Blanket", description: "Ultra-soft blanket perfect for relaxing and unwinding at home." },
{ name: "Artisan Tea or Coffee Collection", description: "Curated selection of premium beverages to enjoy during quiet moments." },
{ name: "Desktop Plant Set", description: "Low-maintenance plants that add life and color to any space." }
];
items.push(...baseItems);
// Interest-specific items
if (interestLower.includes('read') || interestLower.includes('book')) {
items.push({ name: "Book Subscription Box", description: "Monthly delivery of carefully selected books based on your preferences." });
}
if (interestLower.includes('music') || interestLower.includes('guitar') || interestLower.includes('piano')) {
items.push({ name: "Music Streaming Premium Subscription", description: "Access to millions of songs and exclusive content." });
}
if (interestLower.includes('cook') || interestLower.includes('baking') || interestLower.includes('food')) {
items.push({ name: "Professional Chef's Knife", description: "High-quality kitchen knife that makes cooking more enjoyable." });
}
if (interestLower.includes('art') || interestLower.includes('draw') || interestLower.includes('paint')) {
items.push({ name: "Professional Art Supply Set", description: "Premium art supplies including pencils, paints, and paper." });
}
if (interestLower.includes('fitness') || interestLower.includes('yoga') || interestLower.includes('exercise')) {
items.push({ name: "Yoga Mat & Accessories", description: "Complete yoga set with mat, blocks, and straps." });
}
// Age-appropriate additions
if (age < 18) {
items.push({ name: "Study Planner & Stationery", description: "Organized planner and premium pens for school success." });
} else if (age >= 18 && age <= 30) {
items.push({ name: "Coffee Subscription", description: "Monthly delivery of specialty coffee beans from around the world." });
} else {
items.push({ name: "Luxury Bath Set", description: "Premium bath products for ultimate relaxation and self-care." });
}
// Generic items to fill up to 10
const fillerItems = [
{ name: "Portable Phone Charger", description: "Reliable power bank to keep your devices charged anywhere." },
{ name: "Aromatherapy Candle Set", description: "Soy candles with calming scents for relaxation." },
{ name: "Gourmet Chocolate Box", description: "Assortment of artisanal chocolates for special moments." }
];
items.push(...fillerItems);
return items.slice(0, 10);
}
displayWishlist(items) {
this.hideAll();
this.wishlistItems.innerHTML = '';
items.forEach((item, index) => {
const itemElement = document.createElement('div');
itemElement.className = 'wishlist-item';
itemElement.innerHTML = `
<div class="item-name">${this.escapeHtml(item.name)}</div>
<div class="item-description">${this.escapeHtml(item.description)}</div>
`;
this.wishlistItems.appendChild(itemElement);
});
this.wishlistContainer.classList.remove('hidden');
}
escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
showLoading() {
this.hideAll();
this.loadingContainer.classList.remove('hidden');
}
showError(message) {
this.hideAll();
this.errorText.textContent = message;
this.errorContainer.classList.remove('hidden');
}
hideError() {
this.errorContainer.classList.add('hidden');
}
hideAll() {
this.loadingContainer.classList.add('hidden');
this.wishlistContainer.classList.add('hidden');
this.errorContainer.classList.add('hidden');
}
async regenerateWishlist() {
const apiKey = document.getElementById('apiKey').value;
const age = document.getElementById('age').value;
const interest = document.getElementById('interest').value;
if (apiKey && age && interest) {
await this.generateWishlist(apiKey, age, interest);
}
}
}
// Initialize the app when the page loads
document.addEventListener('DOMContentLoaded', () => {
new WishlistGenerator();
});