Unlocking the Power of GPT-3 in Google Sheets
Written on
Chapter 1: Transforming Google Sheets with GPT-3
Discover how to elevate your spreadsheet skills by integrating OpenAI's GPT-3 API into Google Sheets. This powerful combination allows you to create a dynamic version of traditional spreadsheets with enhanced capabilities.
Utilizing OpenAI’s GPT-3 Model in Google Sheets
With a few lines of code in Google Apps Script, you can unlock the potential of AI-enhanced formulas within Google Sheets. This straightforward method will have you transforming your spreadsheets in no time.
Is it worth it?
There’s only one way to determine the value of this integration—give it a try! If you prefer to skip the reading, a copy of the full code is available at the end of the article. For those keen on AI insights, consider following along for more tips that can elevate your skills.
OpenAI: The Creators Behind ChatGPT
If you haven’t been following tech news, you might have missed OpenAI's significant advancements, including their GPT-3 models and the DALL·E 2 image generation service. This tutorial will guide you through leveraging the OpenAI GPT-3 API to enhance your Google Sheets experience.
Prerequisites for Connecting GPT-3 to Google Sheets
Before diving in, ensure you have the following:
- An OpenAI account
- A Google account to create your spreadsheet
- A willingness to copy and paste some code
Don't worry if coding intimidates you; the process is simple enough to set up a working prototype in just five minutes.
Connecting Google Sheets to the OpenAI GPT-3 API
First, create or open an existing Google Sheet.
Once your spreadsheet is ready, navigate to the Apps Scripts editor. If you haven't explored Google Apps Script before, it’s straightforward. You'll find your files on the left and relevant libraries below.
Replace the default code with the following:
const SECRET_KEY = 'ENTER YOUR SECRET KEY HERE';
const MAX_TOKENS = 200;
The SECRET_KEY is a unique identifier from OpenAI that allows you to connect to the GPT-3 model.
Locating Your OpenAI API Key
To find your API key, log into your OpenAI account, click on your username in the top right, and select "View API keys."
Copy this key and paste it into the placeholder in your Apps Script:
const SECRET_KEY = "<Pasted API Key>";
Utilizing the OpenAI DaVinci Model
For our requests, we will utilize the OpenAI DaVinci Model, which is the most advanced option available. Add the following line to your Apps Script:
const model = "text-davinci-003";
Your script should now look like this:
const SECRET_KEY = "<Pasted API Key>";
const MAX_TOKENS = 200;
const GPT_MODEL = "text-davinci-003";
Note on Costs: Be aware that using the API incurs costs, approximately 2 cents per thousand tokens (around 750 words). OpenAI provides $18 in free credits, which should be sufficient for initial experimentation.
Enhancing Google Sheets with GPT-3
Now comes the exciting part! Paste the following function into your Apps Script Editor:
function OpenAI(prompt, temperature = 0.7, model = GPT_MODEL) {
const payload = {
model: model,
prompt: 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].text.trim();
}
This function, OpenAI, will send requests to the GPT-3 API. You can customize the prompt, temperature, and model parameters.
An Example Using GPT-3 in Google Sheets
Let’s see this in action! Use your spreadsheet to generate tweets based on any prompt you like. For instance, you could input this prompt into a cell:
"Write a catchy tweet about why Google Sheets outperforms Excel."
Then, call the function like this:
Hit enter, and you should receive a creative result!
Feel free to share your results in the comments! The prompt I used was simple, but the potential for creativity is endless. Experiment with different prompts to see what works best for you.
Chapter 2: What Will You Create with GPT-3?
This tutorial serves as a fundamental introduction to using OpenAI's GPT-3 with Google Sheets. Let your creativity flow and build something innovative. This integration could inspire unique MicroSaaS projects.
If you're interested in creating an AI text editor for Google Docs, I've covered that topic as well. If you encounter any issues, don't hesitate to comment, and I’ll do my best to assist.
For those seeking the complete code:
const SECRET_KEY = "<Open AI API Key>";
const MAX_TOKENS = 200;
const gpt_model = "text-davinci-003";
function OpenAI(prompt, temperature = 0.7, model = gpt_model) {
const payload = {
model: model,
prompt: 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].text.trim();
}
Thanks for reading! For more insightful content, visit PlainEnglish.io. Sign up for our weekly newsletter and follow us on various social media platforms.