How to connect GPT-4 To Google Sheets using Google Apps Script

On March 14th, 2023, OpenAI announced GPT-4. And the world took notice! Connecting GPT-4 to Google Sheets has never been easier.

At the time of writing, youโ€™ll need to get on the API waitlist for GPT-4. I had to wait 3 days for my access.

Open AI's Waiting List for GPT-4 Email
Open AIโ€™s Waiting List for GPT-4 Email

I had been looking forward to this for 3 days because I wanted to connect GPT-4 to Google Sheets so I could quickly speed up a project Iโ€™ve been working on.

How to connect GPT-4 with Google Sheets using Google Apps Script

Hereโ€™s how you can integrate the GPT-4 API with Google Sheets using the gpt-4 model.

Step 1: Get An API Key

Once approved with API access to GPT-4, youโ€™ll need an API key from Open AI. When logged into your Open AI account, you create your API key here

Step 2: Enter Apps Scripts

From Google Sheets, click on Extensions in the main menu and select Apps Script.

Within the code editor of Apps Script, paste the following code and donโ€™t forget to add your own unique API key where it says โ€œYOUR API KEYโ€ on the first line. You might need to tweak the Max_Tokens & Temperature values to suit your own needs.

const SECRET_KEY = "YOUR API KEY";
const MAX_TOKENS = 6000;
const TEMPERATURE = 0.9;

function AI_GPT4(prompt, temperature = 0.4, model = "gpt-4") {
  const url = "https://api.openai.com/v1/chat/completions";
  const payload = {
    model: model,
    messages: [
      { role: "system", content: "You are a helpful assistant." },
      { role: "user", content: prompt },
    ],
    temperature: TEMPERATURE,
    max_tokens: MAX_TOKENS,
  };
  const options = {
    contentType: "application/json",
    headers: { Authorization: "Bearer " + SECRET_KEY },
    payload: JSON.stringify(payload),
    timeoutInSeconds: 60
  };
  const res = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());
  return res.choices[0].message.content.trim();
}

Once youโ€™ve put your API key into the quotation marks on the first line, click the Save icon and go back into Google Sheets.

NOTE: You may have to give Google certain permissions. I allowed whatever they were looking for to get it working. I can't provide a tutorial on this because it's nuanced and requires a valid Google account etc. Also, Google often change how they do things, so updating this guide would soon become a nightmare for me. Hopefully this part goes easy for you.

Step 3: Complete in Google Sheets

Go back into Google Sheets.

Showing you exactly what you can do with prompts and spreadsheet layout is beyond the scope of this basic guide. Iโ€™m sure youโ€™ll figure it out and have fun doing so.

But as a quick example, to show you that it really works, if you have a topic title in cell A1, you can place this formula below in any other cell and then let it do some magic!

=AI_GPT4("Create an introduction paragraph for this topic: "&A1&".")

Obviously, thatโ€™s just a basic example prompt to get the GPT-4 API working properly with Sheets.

Now, itโ€™s up to you to tweak to your own needs.

Youโ€™ll notice that the GPT-4 API isnโ€™t as fast as the GPT-3.5-turbo integration that I talked about in my article about adding ChatGPT API to Google Sheets.

With a little Google Sheet trickery, you can easily create an entire batch of AI-generated titles, meta descriptions, definitions, paragraphs of text, and if youโ€™re that way inclined, even entire articles.

GPT-4 is a game-changer, able to process both text and images as input, although itโ€™s still limited to text output. This smarty-pants of a model leaps ahead of ChatGPT in complex reasoning. However, early access to the waitlist is for text-only GPT-4. So, hang tight, folks; weโ€™re expecting more to come!

Subscribers of ChatGPT Plus will also get GPT-4 access on chat.openai.com with a usage cap. But beware, with great power comes a higher price tag!

As for the practical applications, GPT-4 boasts an impressive 32k token context, meaning it can handle full reports, short books, and even taxation codes! Itโ€™s showcased its prowess by transforming handwritten UI mock-ups into functional code and tackling complex tax questions.

But hold up! Does this mean weโ€™re entering a world of perfect AI-generated content? Not quite. Although GPT-4 is definitely more reliable than ChatGPT, itโ€™s still prone to โ€œhallucinatingโ€ facts and making reasoning errors. As always, trust, but verify!

Safety-wise, GPT-4 shares risks with previous models, like generating harmful advice or buggy code. However, OpenAI has made strides in aligning GPT-4 with their safety goals, limiting responses to sensitive topics or harmful behavior.

And what about confidence & calibration? Well, thereโ€™s still some work to be done, so donโ€™t go all-in just yet.

So, the big question: GPT-4 or ChatGPT? In a nutshell, GPT-4 is a massive leap forward, but whether you need it depends on your application. If youโ€™re after image inputs, a much larger context, or better factual accuracy, GPT-4 is your go-to. But if you can make do with a 4k token context and simple reasoning, ChatGPT might be the more cost-effective choice.

GPT-4 is a groundbreaking model with loads of potential, but itโ€™s essential to weigh the costs, limitations, and specific application requirements before choosing to use it. Happy AI-ing, everyone! ๐Ÿค–๐Ÿš€

Frequently Asked Questions About ChatGPT

How does GPT-4 compare to ChatGPT?

GPT-4 surpasses ChatGPT in terms of advanced reasoning, problem-solving, and performance on tests like the Uniform Bar Exam and Biology Olympiad. It is more accurate, creative, and capable than its predecessor.

How has safety been improved in GPT-4?

OpenAI has spent six months making GPT-4 safer and more aligned. It is 82% less likely to respond to requests for disallowed content and 40% more likely to produce factual responses than GPT-3.5. This was achieved through incorporating more human feedback, working with experts, and continuous improvement from real-world use.

How was GPT-4 trained?

GPT-4 was trained on Microsoft Azure AI supercomputers, leveraging more data and computation to create increasingly sophisticated language models.

What are some limitations of GPT-4?

GPT-4 has some known limitations, such as social biases, hallucinations, and adversarial prompts. OpenAI is actively working to address these issues and encourages transparency, user education, and AI literacy.

How can I access GPT-4?

GPT-4 is available on ChatGPT Plus and as an API for developers to build applications and services. You can try it on ChatGPT Plus or join the API waitlist.

Similar Posts