-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api.html
More file actions
212 lines (189 loc) · 8.09 KB
/
test-api.html
File metadata and controls
212 lines (189 loc) · 8.09 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gemini API Test</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.test-section {
margin: 20px 0;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
}
input, button {
padding: 10px;
margin: 5px;
font-size: 16px;
}
input {
width: 400px;
}
button {
background: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background: #0056b3;
}
.result {
margin-top: 20px;
padding: 15px;
background: #f8f9fa;
border-radius: 4px;
white-space: pre-wrap;
}
.error {
background: #f8d7da;
color: #721c24;
}
.success {
background: #d4edda;
color: #155724;
}
</style>
</head>
<body>
<h1>🧠 Gemini API Test</h1>
<div class="test-section">
<h3>Test Your Gemini API Key</h3>
<p>Enter your Gemini API key to test if it's working correctly:</p>
<input type="password" id="apiKey" placeholder="Enter your Gemini API key">
<button onclick="testApiKey()">Test API Key</button>
<div id="testResult"></div>
</div>
<div class="test-section">
<h3>Test Hint Generation</h3>
<p>Test the hint generation with a sample DSA problem:</p>
<textarea id="problemStatement" rows="4" style="width: 100%; padding: 10px;">Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice.</textarea>
<button onclick="testHintGeneration()">Generate Hints</button>
<div id="hintResult"></div>
</div>
<script>
// Simple test functions
async function testApiKey() {
const apiKey = document.getElementById('apiKey').value.trim();
const resultDiv = document.getElementById('testResult');
if (!apiKey) {
resultDiv.innerHTML = '<div class="result error">Please enter an API key</div>';
return;
}
resultDiv.innerHTML = '<div class="result">Testing API key...</div>';
try {
const response = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${apiKey}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
contents: [{
parts: [{
text: "Hello, this is a test message."
}]
}],
generationConfig: {
maxOutputTokens: 10,
}
})
}
);
if (response.ok) {
const data = await response.json();
resultDiv.innerHTML = `<div class="result success">✅ API Key is valid! Response: ${JSON.stringify(data, null, 2)}</div>`;
} else {
const errorData = await response.json();
resultDiv.innerHTML = `<div class="result error">❌ API Error: ${JSON.stringify(errorData, null, 2)}</div>`;
}
} catch (error) {
resultDiv.innerHTML = `<div class="result error">❌ Error: ${error.message}</div>`;
}
}
async function testHintGeneration() {
const apiKey = document.getElementById('apiKey').value.trim();
const problemStatement = document.getElementById('problemStatement').value.trim();
const resultDiv = document.getElementById('hintResult');
if (!apiKey) {
resultDiv.innerHTML = '<div class="result error">Please enter an API key first</div>';
return;
}
if (!problemStatement) {
resultDiv.innerHTML = '<div class="result error">Please enter a problem statement</div>';
return;
}
resultDiv.innerHTML = '<div class="result">Generating hints...</div>';
try {
const prompt = `You are an expert DSA mentor helping students learn problem-solving skills.
Given this problem statement:
${problemStatement}
Provide exactly 3 progressive hints that guide the student step by step WITHOUT giving away the complete solution.
IMPORTANT: You MUST format your response exactly as shown below with the exact labels:
**Hint 1 (General Direction):**
- Give a high-level approach or strategy
- Mention what to think about first
- Keep it conceptual, not implementation-specific
**Hint 2 (Data Structure/Algorithm):**
- Suggest specific data structures or algorithms to consider
- Explain why they might be useful for this problem
- Still keep it at the idea level
**Hint 3 (Optimization/Edge Cases):**
- Mention potential optimizations
- Highlight important edge cases to consider
- Give a nudge toward the final approach
CRITICAL FORMATTING RULES:
- NEVER provide complete code or full solutions
- Keep hints concise (1-2 sentences each)
- Make hints progressively more specific
- Focus on teaching the thought process
- Use clear, encouraging language
YOUR RESPONSE MUST BE FORMATTED EXACTLY LIKE THIS (copy this format exactly):
Hint 1: [Write your first hint here - general direction and strategy]
Hint 2: [Write your second hint here - data structures and algorithms to consider]
Hint 3: [Write your third hint here - optimizations and edge cases]`;
const response = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${apiKey}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
contents: [{
parts: [{
text: prompt
}]
}],
generationConfig: {
temperature: 0.7,
topK: 40,
topP: 0.95,
maxOutputTokens: 800,
}
})
}
);
if (response.ok) {
const data = await response.json();
const responseText = data.candidates[0].content.parts[0].text;
resultDiv.innerHTML = `<div class="result success">✅ Hints generated successfully!<br><br>${responseText}</div>`;
} else {
const errorData = await response.json();
resultDiv.innerHTML = `<div class="result error">❌ API Error: ${JSON.stringify(errorData, null, 2)}</div>`;
}
} catch (error) {
resultDiv.innerHTML = `<div class="result error">❌ Error: ${error.message}</div>`;
}
}
</script>
</body>
</html>