Here’s the code you can use:
const SECRET_KEY = "YOUR_API_KEY";
const MAX_TOKENS = 800;
function AI_ChatGPT(prompt, neighborCell, temperature = 0.9, model = "gpt-3.5-turbo") {
const url = "https://api.openai.com/v1/chat/completions";
const fullPrompt = `${prompt} ${neighborCell}`;
const payload = {
model: model,
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: fullPrompt },
],
temperature: temperature,
max_tokens: MAX_TOKENS,
};
const options = {
contentType: "application/json",
headers: { Authorization: "Bearer " + SECRET_KEY },
payload: JSON.stringify(payload),
};
const res = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());
return res.choices[0].message.content.trim();
}