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.
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.
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
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.
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;
Here is the output produced by the SAS program above.
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`.
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.
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 }
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.;
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" } ] } ] }
Post a Comment