const apiKey = 'your-openai-api-key-here'; // Replace with your OpenAI API key async function getChatGPTResponse(prompt) { const url = 'https://api.openai.com/v1/chat/completions'; const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}` }; const body = { model: 'gpt-4', // Use 'gpt-4' or 'gpt-3.5-turbo' messages: [ { role: 'system', content: 'You are a helpful assistant.' }, { role: 'user', content: prompt } ], max_tokens: 100, // Adjust the token limit as needed temperature: 0.7 // Adjust for creativity }; try { const response = await fetch(url, { method: 'POST', headers: headers, body: JSON.stringify(body) }); if (!response.ok) { throw new Error(`Error: ${response.status} - ${response.statusText}`); } const data = await response.json(); console.log('ChatGPT Response:', data.choices[0].message.content); return data.choices[0].message.content; } catch (error) { console.error('API call failed:', error); } } // Example usage getChatGPTResponse('What is the capital of France?');