In this post, we will show you how to integrate Google's Gemini AI Model into SAS. Google released the Gemini API for large language models.
There is an official package for Python to run Gemini AI Model inside Python. Unfortunately there is no official SAS procedure or function to use Gemini inside SAS. Don't worry, Now we also have solution in SAS.
The Gemini API is currently available for free. There can be charges to use the API in future.
To access the API, you need to create an API key by going to this link. Copy and save your API key for future reference.
Please note that there are some countries where Gemini 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 gets answers from the Google Gemini AI Model when users ask questions.
%let api_key= XXXXXXXXXXXXXXXXXXXXX; %let prompt = %str(%"sas code to calculate descriptive statistics%"); %let url = https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent; %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 '"contents": [{"parts":[{"text": '"&prompt.}]}]"; 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.content_parts; do row=1 to max(1,countw(text,'0A'x)); outvar=scan(text,row,'0A'x); output; end; drop text; 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.
/* Calculate descriptive statistics for a dataset */
/* Load the necessary libraries */
%let lib=work;
/* Calculate summary statistics */
proc summary data=&lib..data;
var variable1 variable2 variable3;
output out=summary(drop=_type_ _freq_) mean=mean std=std min=min max=max;
run;
/* Print the results */
proc print data=summary;
run;
Response of Gemini Model is not as accurate as it should 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.
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;
Share Share Tweet