BookmarkSubscribeRSS Feed

Scoring using Models Deployed into MAS over REST API: A Step-by-Step Guide

Started ‎06-05-2023 by
Modified ‎06-05-2023 by
Views 2,546

SAS Model Manager supports a variety of publishing destinations so users can deploy their models to the location that best fits their process needs. One such location is the SAS Micro Analytic Service (MAS). SAS Micro Analytic Service is a stateless, memory-resident, high-performance program execution service. The MAS destination is included with SAS Model Manager on SAS Viya. MAS is also included as a part of several other SAS Viya solutions, such as SAS Intelligent Decisioning.   

 

The MAS destination hosts the model on SAS Viya, which reduces the amount of configuration work required to score new data using the model. Additionally, the MAS destination exposes a REST API endpoint to the model, making it easy to incorporate the model into a wider process or application. MAS supports execution of SAS and Python models, but users will need to configure an Python environment to run Python models.

 

MAS is best suited for ad-hoc transactional scoring in near-real time. For example, MAS is a great fit for determining if a credit card purchase is fraudulent right after the card is swiped. MAS is also great for scoring data in an application right after the applicant clicks submit. But for data that needs to be scored in batch, MAS is not the ideal destination. For scoring data in batch, an in-database or SAS Cloud Analytic Service (CAS) destination would be better suited.

 

Deploying and scoring a model using MAS just takes a few steps. In this article, I will walk through the steps as well as provide some of my best-practices.

 

Step 1: Deploy a Model into MAS

 

We can deploy a SAS or Python model into MAS in just a few clicks from the SAS Model Manager user interface. First, select your model(s) and hit the Publish button. From the Publish Models dialogue, select the MAS destination. Give your published model a memorable name before clicking the Publish button. In just a few seconds, we are ready to start scoring data over REST API using our model!

 

SophiaRowland_0-1684413040869.png

 

Step 2: Prepare to call REST API

 

REST APIs are a standard for communication between applications. Given the popularity of REST APIs, users have a variety of ways to call REST APIs. REST APIs can be called using tools like Postman or Insomnia, via cURL, using the requests package in Python, and many other methods. This allows users to call REST APIS using the method that is easiest for their skill sets or best fits their process needs.

 

I personally prefer programming in Python, but the following examples can easily be adapted to other methods. To call APIs in Python, I need to import two packages: requests and json. 

 

import requests
import json

 

Regardless of the method you use to call the REST APIs, you will need three things: the URL of your SAS Viya environment, your username, and your password. The following block prompts a user to enter this information in Python:

 

#Package to hide passwords
import getpass  

host = input("Viya URL: ")
username = input("Username: ")
password = getpass.getpass("Password: ")

 

Step 3: Get access token

 

An access token allows us to make the REST API calls to SAS Viya. We can reuse our access token, but it will expire after a set time passes. To use your token longer, you can refresh your token or increase the timeout before the token expires.

 

This is a POST request to the /SASLogon/oauth/token endpoint with your username and password in the body of the request.

 

url = f"{host}/SASLogon/oauth/token" 

authBody = 'grant_type=password&username=%s&password=%s' %(username, password)

headersAuth={'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded'}

r =  requests.request('POST', url, data= authBody, headers=headersAuth, auth=('sas.ec', ''))

token = r.json()['access_token']

 

Alternatively, there are a few other approaches you can take to create your access token, as highlighted in developer advocate Joe Furbee’s blog and Simon Edward’s article.

 

Step 4: Gather model information

 

If this is your first time calling the model, then I recommend making the next two calls. These calls leverage the Micro Analytic Score API to access and leverage modules deployed into MAS. More information about this API can be found in its documentation. In my experience, these two steps can prevent unnecessary debugging later. The first call shows you everything currently deployed into MAS. This confirms that your model was successfully deployed into MAS. Additionally, if you can’t remember the name of the published model, this is also helpful for finding the published model name.

 

This is a GET request to the /microanalyticScore/modules/ endpoint with your authorization token within the header.

 

headers = {'Authorization': 'Bearer ' + token}

url =f"{host}/microanalyticScore/modules/" 

r = requests.request('GET', url, headers = headers, verify=False)

for key in r.json()['items']:
    print(key['name'].lower())

 

You can prompt the user to save the published model name in Python using the input() function just like we saved the URL and username:

 

name = input("Published model name: ")

 

The second call returns information about your published model, including the inputs it expects and the outputs it returns. Using this information, you can ensure that you are passing the right data to the model. This is a GET request to the /microanalyticScore/modules/published-model-name/steps endpoint where you replace published-model-name with the name of your published model. You can reuse the headers from the previous REST API call.

 

headers = {'Authorization': 'Bearer ' + token}

url =f"{host}/microanalyticScore/modules/{name}/steps" 

r = requests.request('GET', url, headers = headers, verify=False) 

r.json()

 

Step 5: Make REST API call

 

And now, we are ready to pass data to our published model! Using the information about the inputs we can format our data payload. In Python, our data payload will take the following form:

 

payload = '{"inputs": [("name": "numericVariable", "value": n), ("name": "characterVariable", "value": "char")]}'

 

After your data is in the correct format, we are ready to pass it to our deployed model. This next call is a POST request to the /microanalyticScore/modules/published-model-name/steps/score endpoint where you replace published-model-name with the name of your published model. Additionally, you will need to pass your data payload in the body of the request.

 

headers = {'Content-Type': 'application/vnd.sas.microanalytic.module.step.input+json', 'Authorization': 'Bearer ' + token}

url =f"{host}/microanalyticScore/modules/{name}/steps/score" 

r = requests.request('POST', url, data=payload, headers = headers, verify=False)

r.json()

 

If you are accessing a deployed decision rather than a model, take note that the endpoint for a published decision will be /microanalyticScore/modules/published-decision-name/steps/execute and that the input variable names may end with an underscore.

 

Next Steps

 

And now you are ready to score using models deployed into MAS over REST API! In upcoming article, I’ll dive into scoring using models deployed into CAS over REST API. But what other publishing destinations do you want to learn more about? Let me know in the comments below! Additionally, if you are a MAS power-user, what tips and best practices have you found most helpful? Let us know in that comments!

 

 

 

Comments

Dear Sophia,

thanks a lot for this great and helpful description.

 

We are actually about to make use of such a scenario, where we have a model published in MAS and call it via REST API from another system.

I'm wondering if it would be possible to save the payload (JSON with input data) of this scoring request somewhere (in a table, CASLIB, folder?!). 

Can I add that that step to the published model's score code?

 

Hello @TimStettner! I'm linking to our other conversation so any interested folks in the future can find the answer: https://communities.sas.com/t5/SAS-Viya/Save-scoring-input-data-from-MAS-REST-API-call/m-p/899313#M2... 

Version history
Last update:
‎06-05-2023 10:58 AM
Updated by:

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags