Extend the capabilities of PaLM API with Google Apps Script
Welcome!
In this blog we will be taking a look at how we use the PaLM API and Google Apps Script. We will be passing basic prompts and getting the appropriate responses using Google Apps Script.
In one of my previous tutorial series, I talked about how we use Google Apps Script and the Open AI API to get the power of ChatGPT into your Google Workspace tools such as Google Sheets, Google Docs and more. We will doing something similar using Google Apps Script and the PaLM API.
You can check out the Open AI tutorials series by clicking here.
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 below.
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:
- Go to MakerSuite or click here.
- On opening the MakerSuite you will see something like this
3. To get the API key go ahead and click on Get API key on the left side of the page.
4. On clicking Get API key, you will see something like this where you can create your API key.
5. To create the API key go ahead and click on Create API key in new project.
On clicking Create API Key, in a few seconds you will be able to copy the API key.
Step2: Write the Automation Script
While you are in the Google Sheet, let’s open up the Script Editor to write some Google Apps Script. To open the Script Editor, follow these steps:
- Click on Extensions and open the Script Editor.
2. This brings up the Script Editor as shown below.
We have reached the script editor lets code.
Now that we have the API key ready and good to go, its time to write our Google Apps Script code to interact with it by passing prompts and getting a successful response.
function getPalmData() {
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 getPalmData() 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.
var url = apiUrl + "?key=" + apiKey;
var headers = {
"Content-Type": "application/json"
};
var prompt = {
"text": "Write a story about a magic backpack"
};
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”.
We know come to the most important part of the code that is declaring the prompt. For this blog we will be using a very simple prompt that is to generate a story about a magic backpack. In the future blogs we will be having more complex prompts.
Now that we have the prompt ready, we create an object that will contain this prompt that will sent in the request to the API.
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 PaLM API. We start out by declaring the method parameter which is set to post which indicates that 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());
Logger.log(data);
In this case we just have to pass the url and options variables inside the UrlFetchApp.fetch function. Now that we have sent a request to the PaLM API endpoint we get a response back. We are now going to parse the response.
The getContentText() function is used to extract the text content from the response object. Since the response in JSON format, we use the JSON.parse function to convert the JSON string into an object.
The parsed data is then stored in the data variable, which contains the response the AI model has generated for us based on the prompt. Now that we have the response from the AI model we just go ahead and print it using the Logger.log function.
Our code is complete and good to go.
Step3: Check the output
Its time 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 getPalmData() function.
On running the code you should get an output like this in the Execution Log.
Here you can see on successful execution we have got a Magic Backpack story back.
Conclusion
This is just the beginning of combing Google Apps Script with the PaLM API. In the future tutorials, we will be exploring what more we can do with the PaLM API and how we can combine it with Google Workspace tools such as Google Sheets, Google Docs and more. I hope you have understood how to use the PaLM API and Google Apps Script to pass prompts and get back a response. You can get the code from the GitHub link given below.
Feel free to reach out if you have any issues/feedback at aryanirani123@gmail.com.