Skip to content
function getChatGPTResponse() {
const query = document.getElementById('user-query').value;
const apiKey = 'sk-...mTAC'; // Replace with your actual API key
fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: 'gpt-4',
messages: [{ role: 'user', content: query }]
})
})
.then(response => response.json())
.then(data = > {
const message = data.choices[0]?.message?.content || 'No response';
document.getElementById('chatgpt-response').innerHTML = `${message}
`;
})
.catch(error => {
console.error('Error:', error);
document.getElementById('chatgpt-response').innerHTML = 'Something went wrong. Please try again later.
';
});
}