How to use GPT-3 in Google Apps Script
Tags: gpt3, tutorial, productivity
Published: 2023-01-24 16:43:14

# How to use GPT-3 in Google Apps Script
GPT-3 (Generative Pre-trained Transformer 3) is a state-of-the-art language generation model developed by OpenAI. It has been trained on a diverse range of internet text and can generate human-like text on a wide range of topics.

Why don't you use GPT-3 in Google Sheets? It's definitely useful if you could make it possible.

![Google Sheets and GPT-3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vjh3uvjlironx80jrykx.png)

I prepared two options to use GPT-3 with Google Sheets. You can follow the tutorial according to your purpose and skill set.

1. Clone the sample sheet which has already deployed scripts.
2. Create a new sheet and deploy scripts from your local development environment.

## Create your account at OpenAI
You need to create an account at the first.
https://openai.com/api/

Access to OpenAI API keys, create a new secret key and copy it to your clipboard.

![OpenAI API keys](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3k6qp7ssebtkb78pbkuu.png)

## 1. Clone the sample sheet which has already deployed scripts
You can clone [this sample sheet](https://docs.google.com/spreadsheets/d/1xYZbBp4TFuSxOfjsW0HJtDCS3UEI5nWYd2FfPxEoLnQ/edit#gid=0) if you want to use GPT-3 function immediately without deployment.

[📈 gas-gpt3-sample](https://docs.google.com/spreadsheets/d/1xYZbBp4TFuSxOfjsW0HJtDCS3UEI5nWYd2FfPxEoLnQ/edit#gid=0)

Note: You need to set the OpenAI API key into script properties even though you cloned the sample sheet.

### How to set the OpenAI API key into script properties

![Apps Script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77fzbgaexonw04h8qulq.png)

1. Open your Apps Script project.
2. At the left, click Project Settings The icon for project settings.
3. To add the first property, under Script Properties click Add script property.
4. To add second and subsequent properties, under Script Properties click Edit script properties > Add script property.
5. For Property, enter the key name. The key name is must be `OPENAI_API_KEY`.
6. For Value, enter the secret key you generated before.

![How to add script properties](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/del73wuy6tlz9k3h8l3s.png)

## 2. Create a new sheet and deploy scripts from your local development environment
Creating a repository from [this template](https://github.com/Kazuki-tam/gas-gpt-starter) and cloning the repository.

[📖 gas-gpt-starter](https://github.com/Kazuki-tam/gas-gpt-starter)

You need to install Deno if you want to deploy and develop in your local environment.

Deno v1.29.4 or higher

[🦕 How to install Deno](https://deno.land/manual@v1.29.4/getting_started/installation)

Check out how to use and follow the documentaion.

[📖 gas-gpt-starter: How to use](https://github.com/Kazuki-tam/gas-gpt-starter#how-to-use)

## 3. GPT-3 function
1. Authorize this project's script by execution
2. Use `GPT3()` in your Google Workspace

```
GPT3(prompt, maxTokens, model, temperature)

// Example 1 on Google Sheets
=GPT3("Hello, world!")

// Example 2 on Google Sheets
=GPT3(A1, 200)

// Example 3 on Google Sheets
=GPT3(A1, 300, "text-babbage-001", 0.5)
```

### Parameters
1. prompt: The prompt to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
2. maxTokens: The maximum number of tokens to generate in the completion.
3. model: ID of the model to use.
4. temperature: What sampling temperature to use. Higher values means the model will take more risks.

## Conclusion
The combination between GPT-3 and Google Sheets is really useful. I hope this article will help you improve your productivity.