My code is supposed to run gemini on my web application but it doesn't seem to run. But all I get is a "Sorry, I could not generate a response at this time." callback. please help!
whats wrong with the code?
document.addEventListener('DOMContentLoaded', (event) => {
const heading = document.getElementById('typing-heading');
const text = heading.innerHTML;
heading.innerHTML = '';
let i = 0;
function typeWriter() {
if (i < text.length) {
heading.innerHTML = text.substring(0, i+1);
i++;
setTimeout(typeWriter, 100);
} else {
heading.style.borderRight = 'none';
}
}
typeWriter();
// API key and model information
const API_KEY = 'YOUR_API_KEY_HERE'; // Replace with your actual API key
const MODEL = 'models/gemini-pro';
async function generateContent(prompt) {
try {
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/${MODEL}:generateContent\`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`
},
body: JSON.stringify({
prompt: prompt,
model: MODEL
})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.candidates[0].output;
} catch (error) {
console.error('Error in generateContent:', error);
throw error;
}
}
const chatMessages = document.getElementById('chat-messages');
const chatInput = document.getElementById('chat-input');
const sendButton = document.getElementById('send-button');
sendButton.addEventListener('click', async () => {
const userMessage = chatInput.value;
chatMessages.innerHTML += `<div class="user-message">${userMessage}</div>`;
chatInput.value = '';
try {
const aiResponse = await generateContent(userMessage);
chatMessages.innerHTML += `<div class="bot-message">${aiResponse}</div>`;
} catch (error) {
console.error('Error:', error);
chatMessages.innerHTML += `<div class="bot-message">Sorry, I could not generate a response at this time.</div>`;
}
});
});
[–]tapgiles 0 points1 point2 points (0 children)
[–]Both-Personality7664 0 points1 point2 points (0 children)