-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgem.js
More file actions
43 lines (36 loc) · 1.3 KB
/
gem.js
File metadata and controls
43 lines (36 loc) · 1.3 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
const apiKey = 'AIzaSyDscn9OoRVqAAiTwnO16CshOwKHi-9JO8I';
async function callGeminiAPI(prompt) {
const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent';
const body = {
contents: [
{
parts: [{ text: prompt }]
}
]
};
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-goog-api-key': apiKey
},
body: JSON.stringify(body)
});
if (!response.ok) {
throw new Error(`Error: ${response.status} ${response.statusText}`);
}
const data = await response.json();
// Extract generated text from the response structure
return data.candidates && data.candidates[0].content.parts[0].text;
}
document.getElementById('generateBtn').addEventListener('click', async () => {
const prompt = document.getElementById('input').value;
const outputDiv = document.getElementById('output');
outputDiv.textContent = 'Loading...';
try {
const result = await callGeminiAPI(prompt);
outputDiv.textContent = result;
} catch (error) {
outputDiv.textContent = `Failed to generate content: ${error.message}`;
}
});