Created
March 26, 2023 19:12
-
-
Save morisy/88a542b2584ca3347fc6002362f6886c to your computer and use it in GitHub Desktop.
Google Sheets Open AI Apps Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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