Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Generate Google Doc summaries using PaLM API and Google Apps Script

Save for later
  • 8 min read
  • 13 Sep 2023

article-image

Introduction

In this article, we'll delve into the powerful synergy of the PaLM API and Google Apps Script, unveiling a seamless way to generate concise summaries for your Google Docs. Say goodbye to manual summarization and embrace efficiency as we guide you through the process of simplifying your document management tasks. Let's embark on this journey to streamline your Google Doc summaries and enhance your productivity.

Sample Google Doc

For this blog, we will be using a very simple Google Doc that contains a paragraph for which we want to generate a summary for. 

generate-google-doc-summaries-using-palm-api-and-google-apps-script-img-0

If you want to work with the Google Docs, click here. Once you make a copy of the Google Doc you have to go ahead and change the API key in the Google Apps Script code. 

Step1: Get the API key

Currently, PaLM API hasn’t been released for public use but to access it before everybody does, you can apply for the waitlist by clicking here. If you want to know more about the process of applying for MakerSuite and PaLM API, you can check the YouTube tutorial here.

Once you have access, to get the API key, we have to go to MakerSuite and go to the Get API key section. To get the API key, follow these steps:

1. Go to MakerSuite or click here.

2. On opening the MakerSuite you will see something like this

generate-google-doc-summaries-using-palm-api-and-google-apps-script-img-1

3. To get the API key go ahead and click on Get API key on the left side of the page.

generate-google-doc-summaries-using-palm-api-and-google-apps-script-img-2

4. On clicking Get API key, you will see something like this where you can create your API key.

generate-google-doc-summaries-using-palm-api-and-google-apps-script-img-3

5. To create the API key go ahead and click on Create API key in new project.

generate-google-doc-summaries-using-palm-api-and-google-apps-script-img-4

On clicking Create API Key, in a few seconds, you will be able to copy the API key.

Step 2: Write the Automation Script

While you are in the Google Docs, let’s open up the Script Editor to write some Google Apps Script. To open the Script Editor, follow these steps:

1. Click on Extensions and open the Script Editor.

generate-google-doc-summaries-using-palm-api-and-google-apps-script-img-5

2. This brings up the Script Editor as shown below.

generate-google-doc-summaries-using-palm-api-and-google-apps-script-img-6

We have reached the script editor lets code.

Now that we have the Google Doc setup and the API key ready, let’s go ahead and write our Google Apps Script code to get the summary for the paragraph in the Google Doc. 

function onOpen(){
 
 var ui = DocumentApp.getUi();
 ui.createMenu('Custom Menu')
     .addItem('Summarize Selected Paragraph', 'summarizeSelectedParagraph')
     .addToUi();
 
   }

We are going to start out by creating our own custom menu using which we can select the paragraph we want to summarize and run the code. 

To do that we are going to start out by opening a new function called onOpen(). On opening the function we are going to create a menu using the create.Menu() function, inside which we will be passing the name of the menu. After that, we assign some text to the name followed by the function you want to run when the menu is clicked. 

function DocSummary(paragraph){

 var apiKey = "your_api_key";
 var apiUrl = "https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText";

We start out by opening a new function BARD() inside which we will declare the API key that we just copied. After declaring the API key we go ahead and declare the API endpoint that is provided in the PaLM API documentation. You can check out the documentation by checking out the link given below.

We are going to be receiving the prompt from the Google Doc from the BARD function that we just created.

Generative Language API | PaLM API | Generative AI for Developers

The PaLM API allows developers to build generative AI applications using the PaLM model. Large Language Models (LLMs)…developers.generativeai.googl
 

var url = apiUrl + "?key=" + apiKey
 
 var headers = {

   "Content-Type": "application/json"
 }
 
 var prompt = {
 
   'text': "Please generate a short summary for :\n" + paragraph
 }

 var requestBody = {
   "prompt": prompt
 }

Here we create a new variable called url inside which we combine the API URL and the API key, resulting in a complete URL that includes the API key as a parameter. The headers specify the type of data that will be sent in the request which in this case is “application/json”.

Now we come to the most important part of the code which is declaring the prompt. For this blog, we will be asking Bard to summarize a paragraph followed by the paragraph present in the Google Doc. All of this will be stored in the prompt variable. 

Now that we have the prompt ready, we create an object that will contain this prompt that will be sent in the request to the API. 

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime
var options = {

 
   "method": "POST",
   "headers": headers,
   "payload": JSON.stringify(requestBody)

 
 }

Now that we have everything ready, its time to define the parameters for the HTTP request that will be sent to the PaLM API endpoint. We start out by declaring the method parameter which is set to POST which indicates that the request will be sending data to the API.

The headers parameter contains the header object that we declared a while back. Finally, the payload parameter is used to specify the data that will be sent in the request.

These options are now passed as an argument to the UrlFetchApp.fetch function which sends the request to the PaLM API endpoint, and returns the response that contains the AI generated text.

var response = UrlFetchApp.fetch(url,options);
 var data = JSON.parse(response.getContentText());
 return data.candidates[0].output;

 
}

In this case, we just have to pass the url and options variable inside the UrlFetchApp.fetch function. Now that we have sent a request to the PaLM API endpoint we get a response back. In order to get an exact response we are going to be parsing the data.

The getContentText() function is used to extract the text content from the response object. Since the response is in JSON format, we use the JSON.parse function to convert the JSON string into an object.

The parsed data is then passed to the final variable output, inside which we get the first response out of multiple other drafts that Bard generates for us. On getting the first response we just return the output. 

function summarizeSelectedParagraph(){

 var selection = DocumentApp.getActiveDocument().getSelection();
 var text = selection.getRangeElements()[0].getElement().getText();
 var summary = DocSummary(text);

 Now that we have the summary function ready and good to go, we will now go ahead and open the function that will be interacting with the Google Doc. We want the summary to be generated for the paragraph that the user selects. 

To do that we are going to get the selected text from the Google Doc using the getSelection() function. Once we get the selected text we go ahead and get the text using the .getText() function. To generate the summary using Google Bard we pass the text in the DocSummary() function. 

DocumentApp.getActiveDocument().getBody().appendParagraph("Summary");
 DocumentApp.getActiveDocument().getBody().appendParagraph(summary)
 
}

Now that we have the summary for the selected text, it's time to append the paragraph back into the Google Doc. To do that we are going to be using the appendParagraph() function inside which we will pass the summary variable. Just to divide the summary from the original paragraph we append an additional line that says “Summary”. 

Our code is complete and good to go.

Step 3: Check the output

It's time to check the output and see if the code is working according to what we expected. To do that go ahead and save your code and run the OnOpen() function. This will create the menu that we can select and generate the summary for the paragraph.

generate-google-doc-summaries-using-palm-api-and-google-apps-script-img-7

On running the code you should get an output like this in the Execution Log.

generate-google-doc-summaries-using-palm-api-and-google-apps-script-img-8

On running the onOpen() function the custom menu has been created in the Google Doc successfully.

generate-google-doc-summaries-using-palm-api-and-google-apps-script-img-9

To generate the summary in the Google Doc, follow the steps.

1. Select the paragraph you want to generate the summary for.

generate-google-doc-summaries-using-palm-api-and-google-apps-script-img-10

2. Once you select the paragraph go ahead and click on the custom menu and click on Summarise Selected paragraph.

generate-google-doc-summaries-using-palm-api-and-google-apps-script-img-11

3. On clicking the option, you will see that the code will generate a summary for the paragraph we selected.

generate-google-doc-summaries-using-palm-api-and-google-apps-script-img-12

Here you can see the summary for the paragraph has been generated in the Google Doc successfully.

Conclusion

In this blog, we walked through the process of how we can access the PaLM API to integrate Google Bard inside of a Google Doc using Google Apps Script. The integration of Google Bard and Google Apps Script empowers users to generate summaries of paragraphs in Google Docs effortlessly.

You can get the code from the GitHub link given below. 

Google-Apps-Script/BlogSummaryPaLM.js at master · aryanirani123/Google-Apps-Script

Collection of Google Apps Script Automation scripts written and compiled by Aryan Irani. …github.com

Author Bio

Aryan Irani is a Google Developer Expert for Google Workspace. He is a writer and content creator who has been working in the Google Workspace domain for three years. He has extensive experience in the area, having published 100 technical articles on Google Apps Script, Google Workspace Tools, and Google APIs.

Website