Google Sheets Script for chatGPT3.

You’ll need:

  1. your chatGPT3 API key
  2. the below script

Step 1:

Go to: https://beta.openai.com/account/api-keys

Create your API key and copy it.

Step 2:

Open Google sheets.

Go extensions.

Open the Script Editor (Apps Script).

A new window opens up.

Replace the script with the below code. Insert your API key in the script.

 * Generates text using OpenAI's GPT-3 model
 * @param {string} prompt The prompt to feed to the GPT-3 model
 * @param {string} cell The cell to append to the prompt
 * @param {number} [maxWords=10] The maximum number of words to generate
 * @return {string} The generated text
 * @customfunction
function runOpenAI(prompt, cell, maxWords) {
maxTokens = 10
if (maxWords){maxTokens = maxWords * 0.75}
model = "text-davinci-003"
prompt = prompt+cell+":",
temperature= 0
 // Set up the request body with the given parameters
 const requestBody = {
 "model": model,
 "prompt": prompt,
 "temperature": temperature,
 "max_tokens": maxTokens
 // Set up the request options with the required headers
 const requestOptions = {
 "method": "POST",
 "headers": {
 "Content-Type": "application/json",
 "Authorization": "Bearer "+API_KEY
 "payload": JSON.stringify(requestBody)
 // Send the request to the GPT-3 API endpoint for completions
 const response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", requestOptions);
 // Get the response body as a JSON object
 const responseBody = JSON.parse(response.getContentText());
 let answer= responseBody.choices[0].text
 // Return the generated text from the response
 return answer

Hit Save.

Hit Deploy

Close the window. Return to your Google sheet. You may need to allow execution of the script before the first run. Reload the Google sheet.

Step 3: Call the script

You can call the script using this command:


Example, where input is in A1 and formula, is in A2:

=runOpenAI("write a love letter for the person mentioned in the cell",A1,300)

Happy love letter writing!

Leave a Reply

Your email address will not be published. Required fields are marked *