import { fetch } from 'wix-fetch'; // Import wix-fetch for making HTTP requests // Function to call the ChatGPT API export async function getChatGptResponse(prompt) { const apiKey = "YOUR_API_KEY"; // Replace with your actual API key // ChatGPT API endpoint const apiUrl = "https://api.openai.com/v1/chat/completions"; // Request payload const body = { model: "gpt-4", // Or "gpt-3.5-turbo" depending on your subscription messages: [ { role: "system", content: "You are a helpful assistant." }, { role: "user", content: prompt } ] }; try { // Make the API call const response = await fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}` }, body: JSON.stringify(body) }); // Parse and return the response const data = await response.json(); if (data && data.choices && data.choices.length > 0) { return data.choices[0].message.content; } else { throw new Error("Invalid response from ChatGPT API"); } } catch (error) { console.error("Error in getChatGptResponse:", error); return "Error occurred: " + error.message; } }