How to Integrate Google Bard into SAS

In this post, we will show you how to integrate Google Bard into SAS using the PaLM API. You might know that Bard is a ChatBot from Google that is an alternative to ChatGPT. Google released the PaLM API for large language models which can be used to integrate Bard into different programming languages.

There is an official package for Python to run Bard inside Python. Unfortunately there is no official SAS procedure or function to use Bard inside SAS. Don't worry, Now we also have solution in SAS using the PaLM API.

Integration of Bard into SAS with PaLM API
What does Bard bring within SAS

You can ask Bard about anything, just like you would on the official Bard website. Bard can help you debug your SAS program, understand statistical concepts, or build or validate a predictive model.

Steps to integrate Bard into SAS
Step 1 : Get API Key

The PaLM API is currently available for free. The documentation states that there will be no pricing for the PaLM API until the end of this year. In the future, there will be a cost involved in using the PaLM API.

First you need to join the PaLM API waitlist by visiting this link : PaLM API Waitlist. You may get access in a few minutes or it may take a day or so. Google will notify you of your access via email.

Once you have access, you can create an API key to access the PaLM API by going to this link. Copy and save your API key for future reference.

Please note that there are some countries where PaLM API is not currently available. See the countries list here
Step 2 : Enter API Key and Prompt

These two macro variables api_key and prompt take user's inputs as API key and the question user wants to ask (i.e. search query). See the text highlighted in red below. Make sure secret API key NOT to be in quotes. Also don't remove percentage (%) and quotes in "prompt" macro variable.

SAS Program

The following SAS program calls Bard from SAS using the PaLM API.

%let api_key= XXXXXXXXXXXXXXXXXXXXX;
%let prompt = %str(%"sas code to calculate descriptive statistics%");
%let url = https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText;
%let url_query = &url.?key=&api_key.;
%put &url_query;

/* Body of the POST request */
filename in temp;
data _null_;
file in;
put;
put "{";
put  '"prompt": {"text": '"&prompt.},";
put  '"temperature":0.1, "maxOutputTokens":1024';
put "}";
run;

/* reference that file as IN= parm in PROC HTTP POST */
filename resp "%sysfunc(getoption(WORK))/echo.json";
proc http 
 method="POST"
 url= "&url_query."
 ct="application/json"
 in=in
 out=resp; 
run;
 
/* Tell SAS to parse the JSON response */
libname response JSON fileref=resp;

/* Format JSON in presentable format */
data outdata ;
  set response.candidates;
  do row=1 to max(1,countw(output,'0A'x));
     outvar=scan(output,row,'0A'x);
     output;
  end;
  drop output;
run;  
  
proc report data=outdata ;
column outvar;
define outvar / display "" style(column)=[cellwidth=6in fontsize=10pt asis=ON];
run;
Output

Here is the output produced by the SAS program above.

Run Bard inside SAS
Example

Let's ask Bard about explaining the SAS code. Specify your prompt in the "prompt" macro variable.

%let prompt = %str(%"explain this SAS code: proc stdize data= test out= result; var X1-X10; run;%");
Output:
The `proc stdize` procedure standardizes the values of a variable by subtracting the mean and dividing by the standard deviation. In this case, the procedure is being used to standardize the values of the variables `X1` through `X10` in the data set `test`. The output of the procedure is saved in the data set `result`.
Warning

Bard/PaLM API is not as accurate as it could be, especially when it comes to generating programs for enterprise software, such as SAS, compared to open source programming languages. It works well when you ask general questions like rewriting some text or generating ideas for travel etc.

Request Body: Check JSON

The following SAS program returns body portion of the HTTP POST request you would like to send.

data _null_;
infile in;
input;
put _infile_;
run;

See JSON in the LOG Window.

 {
 "prompt": {"text": "sas code to calculate descriptive statistics"},
 "temperature":0.1, "maxOutputTokens":1024
 }
How to check the status of HTTP Request

PROC HTTP returns the status of request in LOG Window. Status code 200 means it has been done successfully. 400 refers to Bad Request (some issue in your JSON).

NOTE: 200 OK
NOTE: 400 Bad Request

SAS has also introduced macro variables for checking the status of HTTP Request. You need to have SAS 9.4 for accessing them.

%put HTTP Status code = &SYS_PROCHTTP_STATUS_CODE. : &SYS_PROCHTTP_STATUS_PHRASE.; 
Print Raw Response to Log Window
By using jsonpp( ) function you can print raw JSON returned as response to log window.
data _null_;
 rc = jsonpp('resp','log');
run;
{
   "candidates": [
     {
       "output": "```sas\nproc means data=mydata;\n  var height weight;\n  output out=output_table mean=mean std=std;\nrun;\n```",
       "safetyRatings": [
         {
           "category": "HARM_CATEGORY_DEROGATORY",
           "probability": "NEGLIGIBLE"
         },
         {
           "category": "HARM_CATEGORY_TOXICITY",
           "probability": "NEGLIGIBLE"
         },
         {
           "category": "HARM_CATEGORY_VIOLENCE",
           "probability": "NEGLIGIBLE"
         },
         {
           "category": "HARM_CATEGORY_SEXUAL",
           "probability": "NEGLIGIBLE"
         },
         {
           "category": "HARM_CATEGORY_MEDICAL",
           "probability": "NEGLIGIBLE"
         },
         {
           "category": "HARM_CATEGORY_DANGEROUS",
           "probability": "NEGLIGIBLE"
         }
       ]
     }
   ]
}
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

0 Response to "How to Integrate Google Bard into SAS"

Post a Comment

Next → ← Prev
Looks like you are using an ad blocker!

To continue reading you need to turnoff adblocker and refresh the page. We rely on advertising to help fund our site. Please whitelist us if you enjoy our content.