Skip to content

Instantly share code, notes, and snippets.

@morisy
Created March 26, 2023 19:12
Show Gist options
  • Save morisy/88a542b2584ca3347fc6002362f6886c to your computer and use it in GitHub Desktop.
Save morisy/88a542b2584ca3347fc6002362f6886c to your computer and use it in GitHub Desktop.
Google Sheets Open AI Apps Script
const SECRET_KEY = "OPEN AI API KEY GO HERE";
const MAX_TOKENS = 800;
const TEMPERATURE = 0.9;
function GPT3(prompt, temperature = 0.4, model = "gpt-3.5-turbo") {
const url = "https://api.openai.com/v1/chat/completions";
const payload = {
model: model,
messages: [
{ role: "system", content: "Instruct the AI what you want it to do with the input here, such as 'Summarize the text as a haiku' or 'extract the first address you see or otherwise respond none.'" },
{ role: "user", content: prompt },
],
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment