Skip to content

Instantly share code, notes, and snippets.

@ruphy
Last active November 2, 2023 23:41
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruphy/b51b57dc20e5793b58a34e5639eb9d78 to your computer and use it in GitHub Desktop.
Save ruphy/b51b57dc20e5793b58a34e5639eb9d78 to your computer and use it in GitHub Desktop.
GPT3() function for Google Sheets
// SPDX-License-Identifier: MIT
//
// This code will add a GPT3() function in your Google Sheets
// This code is originally inspired from https://twitter.com/fabianstelzer/status/1572926883179778050
// To use it, insert your API key below, open Google Sheets -> Extensions -> Apps Script -> Copy & Paste this -> Save
//
// Usage: =GPT3(prompt, max_tokens (default=15), model (default=davinci))
// Example usage: =GPT3("Once upon a time,", 1000, "davinci")
var API_KEY = "your-API-key";
var temperature = 0.7;
function _callAPI(prompt, maxtokens, engine) {
var data = {
'prompt': prompt,
'max_tokens': maxtokens,
'temperature': temperature,
'top_p': 1,
'frequency_penalty': 0,
'presence_penalty': 0,
};
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(data),
'headers': {
Authorization: 'Bearer ' + API_KEY,
},
};
response = UrlFetchApp.fetch(
'https://api.openai.com/v1/engines/'+engine+'/completions',
options,
);
return JSON.parse(response.getContentText())['choices'][0]['text']
}
function GPT3(promptinput, maxtokens=15, engine="text-davinci-003") {
var prompt = promptinput;
response = _callAPI(prompt, maxtokens, engine);
return(response);
}
//
// Copyright 2022 Riccardo Iaconelli
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment