02-24-2025
kumardeva
SAS Employee
Member since
05-15-2015
- 21 Posts
- 2 Likes Given
- 0 Solutions
- 2 Likes Received
-
Latest posts by kumardeva
Subject Views Posted 1126 05-01-2024 01:50 PM 3339 05-02-2022 08:29 AM 4111 04-28-2021 01:56 PM 9756 03-12-2021 11:15 AM 3836 11-12-2020 02:57 PM 6239 10-28-2020 05:39 PM 4727 07-27-2020 10:45 AM 1474 07-09-2020 08:14 AM 5837 06-25-2020 04:07 PM 4912 06-05-2020 03:39 PM -
Activity Feed for kumardeva
- Tagged Creating an AI Assistant for SAS Viya in 5 steps (@sassoftware/viya-assistantjs) - Part I on SAS Communities Library. 05-03-2024 08:30 AM
- Tagged Creating an AI Assistant for SAS Viya in 5 steps (@sassoftware/viya-assistantjs) - Part I on SAS Communities Library. 05-03-2024 08:30 AM
- Tagged Creating an AI Assistant for SAS Viya in 5 steps (@sassoftware/viya-assistantjs) - Part I on SAS Communities Library. 05-03-2024 08:30 AM
- Tagged Creating an AI Assistant for SAS Viya in 5 steps (@sassoftware/viya-assistantjs) - Part I on SAS Communities Library. 05-03-2024 08:30 AM
- Tagged Creating an AI Assistant for SAS Viya in 5 steps (@sassoftware/viya-assistantjs) - Part I on SAS Communities Library. 05-03-2024 08:30 AM
- Tagged Creating an AI Assistant for SAS Viya in 5 steps (@sassoftware/viya-assistantjs) - Part I on SAS Communities Library. 05-03-2024 08:30 AM
- Tagged Creating an AI Assistant for SAS Viya in 5 steps (@sassoftware/viya-assistantjs) - Part I on SAS Communities Library. 05-03-2024 08:30 AM
- Tagged Creating an AI Assistant for SAS Viya in 5 steps (@sassoftware/viya-assistantjs) - Part I on SAS Communities Library. 05-03-2024 08:30 AM
- Tagged Creating an AI Assistant for SAS Viya in 5 steps (@sassoftware/viya-assistantjs) - Part I on SAS Communities Library. 05-03-2024 08:30 AM
- Posted Creating an AI Assistant for SAS Viya in 5 steps (@sassoftware/viya-assistantjs) - Part I on SAS Communities Library. 05-01-2024 01:50 PM
- Posted Jump start your SAS Viya Application on SAS Communities Library. 05-02-2022 08:29 AM
- Liked Using the SAS Container Runtime for publishing SAS models to Kubernetes on the Azure cloud for HansEdert. 10-28-2021 07:52 PM
- Posted k8s !== A 4-letter word: Rapid Deployment of your SAS Viya Applications in Kubernetes on SAS Communities Library. 04-28-2021 01:56 PM
- Posted Jump Start a REST API server for your SAS Viya programs on SAS Communities Library. 03-12-2021 11:15 AM
- Liked MLPA web application - accelerating the model lifecycle through APIs for peciro. 01-15-2021 04:13 PM
- Posted Creating applications with sas--viya-quickstart - a short video on SAS Communities Library. 11-12-2020 02:57 PM
- Posted Lazy Programmer's Guide to SAS Viya Application Development Part III: viya-app-quickstart on SAS Communities Library. 10-28-2020 05:39 PM
- Posted Lazy Programmer's Guide to SAS Viya Application Development Part II: create-react-restaf-viya-app on SAS Communities Library. 07-27-2020 10:45 AM
- Posted Bullet Proof your Application - Checking validity of input table on SAS Communities Library. 07-09-2020 08:14 AM
- Posted Lazy Programmer’s Guide to Developing SAS Viya Applications on SAS Communities Library. 06-25-2020 04:07 PM
-
Posts I Liked
Subject Likes Author Latest Post 4 7 -
My Liked Posts
Subject Likes Posted 1 04-24-2019 02:49 PM 1 04-11-2019 10:46 PM -
My Library Contributions
Subject Likes Author Latest Post 2 0 0 1 1
05-01-2024
01:50 PM
2 Likes
Introduction
Since you opened this article, you must take the next step and visit the SAS Innovate 2024 site to learn about SAS approach to providing a world class AI and Analytic experience for SAS users.
Like most application developers in the world, I was curious to learn about LLM and how one can put the LLM to work for specific domains.
My first attempt was to build a copilot for the drag and drop app builder (with react components) I was working on. While I did get an initial prototype working, I was left with the uneasy feeling that I was using a hammer when the job called for a screwdriver - I could build an entire app using drag and drop and minimal coding, so why build a layer with LLM? I shelved the project with a note to build @sassoftware/restaf library copilot for building REST API based Viya apps.
Around this time, OpenAI released the first version of their AI Assistant API for writing AI Assistants. The description from OpenAI is:
Create an Assistant by defining its custom instructions and picking a model. If helpful, add files and enable tools like Code Interpreter, File Search, and Function calling.
Create a Thread when a user starts a conversation.
Add Messages to the Thread as the user asks questions.
Run the Assistant on the Thread to generate a response by calling the model and the tools.
There are a few more steps involved, but I was able to build assistants using this. The tools mentioned in step 1 allow developers to integrate their backend with the assistant. For Viya app developers this meant using the published REST API to access SAS Viya. In my case, I used the @sassoftware/restaf libraries to make the calls to SAS Viya, but one could use any http library to make the calls.
Recently OpenAI released V2 beta that added support for vector store. The additional operations allow the developer to add to the vector store which could be used by the files_search tool. Please note that Azureai Assistant does not support vector store at this time.
As with any project I work on, I always try to create reusable code (the lazy programmer approach!). This project is no different. I created a library @sassoftware/viya-assistantjs to simplify writing assistants. This library is available under Apache-2 license. Details on the Api and usage is here.
Use this library to build an Assistant in 5 steps
Decide on the tools you need for your domain and develop them.
The library comes with a default set of tools as an example and to jump start your journey.
The source code for these tools are available in the repository.
Additional examples are here.
Call the setupAssistant with configuration to initialize the assistant.
Pass in information to logon to Viya. The assistant will manage both CAS and SAS sessions.
See this link for details
Pass in information for creating and managing the assistant.
Accept prompt from user and call the runAssistant with the prompt.
The OpenAI assistant maintains a thread of the conversation
Process the response
Repeat steps 3 and 4.
The library has a uploadFile method for uploading documents to the vector store associated with the assistant. This method can also be used in the tools.
Key point of the samples tools
The purpose of the sample tools is to show how easy it is to invoke SAS Viya from the tools and use the results in your response. While the mechanism to call Viya to resolve user prompts is necessary, it is not sufficient. One must have a well defined domain that is of interest to the user. The tools must be designed to access data and programs that are relevant to the domain. In SAS this usually is a collection of SAS programs and data that can resolve user questions about the domain.
Build an Assistant Step-by-Step
Import the necessary modules. Note the two entries imported from @sassoftware/viya-assistantjs
// Step 0: Import the necessary modules
import * as readline from "node:readline/promises";
import fss from "fs/promises";
import { stdin as input, stdout as output } from "node:process";
import getToken from "./getToken.js";
import { setupAssistant, runAssistant } from "@sassoftware/viya-assistantjs";
let { host, token } = getToken();
Set up the tools.
const tools = [
{
type: "function",
function: {
name: "listTables",
description: `for a given library for either sas or cas source, get the list of available tables.
(ex: list tables in cas library samples, list tables in sas library sashelp)`,
parameters: {
properties: {
library: {
type: "string",
description: "A SAS library like casuser, sashelp, samples",
},
start: {
type: "integer",
description: "Start at lookup at this index. Default is 0.",
},
limit: {
type: "integer",
description:
"Return only this many tables. If not specified, then return 10 tables.",
},
source: {
type: "string",
description: "The source of the data. cas or sas",
enum: ["cas", "sas"],
},
},
type: "object",
required: ["library"],
},
},
},
];
Set up the function for the tool.
async function listTables(params, userData, appControl) {
let { library, source, start, limit } = params;
// get session information
let appEnv = await appControl.getViyaSession(source);
// setup filters
let p = {
qs: {
limit: limit == null ? 10 : limit,
start: start == null ? 0 : start,
},
};
// use restafedit library to get the list of tables.
// Note you can make use your favorite library to make this call. see documentation
let r = await appEnv.restafedit.getTableList(library, appEnv, p);
// return the retrieved list
return JSON.stringify(r);
}
Write the chat program.
// run a chat session
chat(config)
.then((r) => console.log("done"))
.catch((err) => console.log(err));
async function chat(config) {
//Setup assistant
let appControl = await setupAssistant(config);
// create readline interface and chat with user
const rl = readline.createInterface({ input, output });
// process user input in a loop
while (true) {
let prompt = await rl.question(">");
// exit session
if (prompt.toLowerCase() === "exit" || prompt.toLowerCase() === "quit") {
rl.close();
break;
}
// let assistant process the prompt
let promptInstructions = " ";
try {
// run prompt
let response = await runAssistant(appControl, prompt, promptInstructions);
console.log(response[0].content);
} catch (err) {
console.log(err);
}
}
}
Here is a short video showing the assistant in action.
Below is a recording of an assistant using the builtin tools.
Conclusion
I hope you find this article useful. Feel free to clone the library and modify it to suit your needs. Since the library is based on an SDK that is in beta, I expect to enhance the library and publish updates as the OpenAI assistant evolves and I learn more.
All comments are welcomed.
... View more
- Find more articles tagged with:
- AI Assistant
- Azure AI
- LLM
- OpenAI
- restaf
- restafedit
- restaflib
- SAS Viya
- viya-assistantjs
Labels:
05-02-2022
08:29 AM
This article details a starter app, as described in the viyaapp GitHub repository, to setup your SAS Viya Web Application with minimal effort. Note: viyaapp is a branch of the restaf-uidemos repository. Upgrading from the sample application to your own application is as simple as replacing the index.html file and setting some configuration values. Additionally, creating a docker container for your application is a snap, as you'll see documented in this posting.
SAS Viya Server Setup
You need to do this for all applications that will access SAS Viya. Typically these are set up by a SAS Viya Administrator.
Create a clientid and clientsecret. This document uses these values:
flow: authorization_code
clientid: viyaapp
clientsecret: secret
redirect: <https://localhost:5002/viyaapp>
Also set the following using SAS Environment configurations:
sas.commons.web.security.cors: set AllowedUrl to https://localhost:5002
sas.commons.web.security.csrf: set to .*
sas.commons.web.security.cookies: set sameSite to None
Technical Details
The starter application uses @sassoftware/viya-appserverjs as its app server. The primary purpose of the app server is to handle authentication and serve up the application to the browser.
As you will notice, the application has quite a few configurations. All these configurations are read by the application server to setup the application. Visit this wiki pages for details.
Install
Clone the repository and install the dependencies
git clone https://github.com/sassoftware/restaf-uidemos -b viyaapp viyaapp
npm install
cd viyaapp
Configuration
Edit the .env file in the cloned repository and set the value of VIYA_SERVER to your SAS Viya server url.
VIYA_SERVER=https://Your-viya-server-url
CLIENTID=clientapp
CLIENTSECRET=secret
APPNAME=viyaapp
APPHOST=localhost
APPPORT=5002
# If APPENTRY is not specified, it defaults to index.html
APPENTRY=index.html
Run the application
npm start
Go to your browser and enter this to access the application
https://localhost:5002/viyaapp
You should get the SAS Logon dialog. Logon on with you SAS Viya userid and password. This will open the application.
Default Application
The application is simple on functionality. It lists the available files on the SAS server. Press the button to display the file list.
The default application is in public/index.html. The full html is shown below with embedded notes.
<html>
<head>
<meta charset="UTF-8" >
<! the script below returns useful information from server in a javascript object name LOGONPAYLOAD -->
<!-- The most useful element is LOGONPAYLOAD.host , the url of the Viya server -->
<!-- This helps avoid hard codung the Viya host url in the html -->
<script src="/viyaapp/appenv" > </script>
<-- http package to make calls to Viya -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.26.1/axios.min.js">></script>
<script>
function setup() {
console.log(LOGONPAYLOAD); /* for learning purposes */
}
function runit() {
document.getElementById('output').innerHTML = '...running';
let config = {
url : `${LOGONPAYLOAD.host}/files/files`,
method: 'GET',
withCredentials: true /* This is required. This allows the browser communicate the credentials to the server */
}
axios(config)
.then (r => {
console.log(r.data.items);
let items = r.data.items.map( item => { return item.id;})
document.getElementById('output').innerHTML = JSON.stringify(items, null, 4);
})
.catch(err => {
document.getElementById('output').innerHTML = JSON.stringify(err, null, 4);
})
}
</script>
<body onload="setup()">
<h1 id="head">Hi</h1>
<h2> Custom call to Viya Server</h2>
<div>
<button onclick="runit()">
Press to make a call to file service
</button>
<br />
<br />
</div>
<div>
<pre id="output"></pre>
</div>
</body>
</html>
Points of interest
Replace index.html to point to your application's main entry point. If it is not index.html, then edit .env file and set the APPENTRY value to your entry point.
Note that there is no code in the application for logging. This is handled for you by the app server included in this starter app.
There are no references to authorization token. In your http call payload, set the withCredentials to true as shown in the example above. This lets the browser do the necessary bookkeeping to authorize the calls to SAS Viya.
The /viyaapp/appenv end point is optional. It sets a JS variable LOGONPAYLOAD with the following structure. The host field is the most useful one. This is used in the application listed above.
"authType": "server",
"redirect": null, /* advanced feature */
"host" : "your viya server url",
"clientID": "your client id",
"appName" : "The value of the APPNAME field in the .env file",
"keepAlive": null, /* advanced feature to keep the session alive for longer periods */
"ns" : null /* for future use
Bonus Points
Change the value of APPENTRY in the .env file to the value below. Restart the application and visit the same site for a more interesting starter app.
APPENTRY=indexrestaf
Running in Docker
To run your application in docker, edit the docker-compose file. Set the value of VIYA_SERVER to the appropriate value.
version: "3.8"
services:
viyaapp:
build: .
restart: always
ports:
- 5002:8080
environment:
- VIYA_SERVER=https://<your viya url>
- CLIENTID=clientapp
- CLIENTSECRET=secret
- APPNAME=viyaapp
Note: The docker-compose command uses the Dockerfile in the same directory to create the container. The application is running on port 8080 in the container and the port is mapped to 5002 in the docker-compose file.
To run the application in docker run the following command.
docker compose up
and access the application as before.
https://localhost:5002/viyaapp
Conclusion
You just built a simple web application for SAS Viya with minimal effort. To make it your own, replace the contents of the public directory with your assets and modify the values in the configuration files.
... View more
Labels:
04-28-2021
01:56 PM
So you have a bunch of applications for SAS Viya that you want to migrate to the cloud and deploy them alongside SAS Viya - just like all the other cool kids.
If you are just starting on this migration you will be confronted by this very long (and incomplete list) of things to learn – ingress, deployment, pods, loadBalancer, clusterIP, nodePort, secrets, secret Generator, configMap, configMap Generator, patches, operators, Kustomize, Helm charts and many more. DO NOT PANIC. I have published a set of simple configuration files (https://lnkd.in/dEaAihv) that will get you up and running in no time. With a few key strokes you can deploy your application in the same cluster as SAS Viya. The figure below shows the resulting deployment.
Hope you find this useful. Cheers… Deva #kubernetes #sasprogramming #sassoftware #sasviya
... View more
03-12-2021
11:15 AM
4 Likes
Preamble
Hi fellow SAS Viya Programmers - Do you want to enable your users to access your SAS programs using REST API? Do you want to deliver your programs in a container? Then read on!
Introduction
This article presents an easy way for SAS programmers to create a REST API server to front their SAS Viya Programs - including data steps, procedures, CAS Actions or CASL programs.
Full documentation for the create-viya-api tool is located on GitHub. Additionally, please follow this link if you'd like to see the details of the underlying server code. The entire project is based on restaf - An Easy Button To Access SAS Viya Using SAS Viya REST APIs.
Below is a short introductory video demonstrating the functionality.
Key Features
Define your API end points using JavaScript
Authentication
Accepts a valid SAS Viya token
If called from an authenticated browser session it will get a token from SAS Viya
By design it does not support client credentials(userid, password) - since bullets 1 and 2 are the more common use cases.
In your handler (where you process the request), you have access to a context object with the following:
The current valid token
All the standard http artifacts - path, query, payload, headers etc
Access the REST end points via swagger, web applications and any scripting environment
Deploy the server in a Docker container
The starter example has the following end points to jump start your development. These cover some basic scenarios.
CAS actions
CASL statements
Compute service with the user-supplied code
Specific program saved in the "server". In this example, the code is in a file on the API server, but in a real deployment this code will be in some repository
Next Steps
Please try it out for yourself. And be sure to send your feedback - good or bad.
SAS Documentation
Please refer to the SAS Support site for full documentation.
... View more
Labels:
11-12-2020
02:57 PM
Hello Fellow SAS Viya Programmers: Earlier I had published a note on the viya-app-quickstart template for create-react-app. If you are anything like me you are probably waiting for the movie version - so here is a short video to get you going... Hope you give it a try... Cheers... Deva
... View more
Labels:
10-28-2020
05:39 PM
1 Like
One of the common patterns in SAS Viya Web Applications is shown in the 3 images below.
The application starts with a landing page and a banner.
The user selects an application to run from the hamburger menu in the banner.
The user is routed to the selected menu
The viya-app-quickstart template for create-react-app is designed to simplify the development of such applications. Once the local development environment has been setup, you can add new applications as shown in the diagram below:
Please see viya-app-quickstart for instructions on how to build these applications using this template.
Cheers... Deva
... View more
Labels:
07-27-2020
10:45 AM
2 Likes
The create-react-app tool is a very popular CLI to jump start the development of react-based applications. I created the create-react-restaf-viya-app as an extension of create-react-app. The main motivation for my new tool is to help restaf users build react-based Viya applications. restaf is a framework for building applications with SAS REST APIs, supported with SAS Viya and is composed of the following components:
restaf
restaflib
restaf-server
Benefits
The key benefits of using the create-react-restaf-viya-app CLI to jump start your SAS Viya Applications are:
You retain all the benefits of using create-react-app, which are well documented.
You are automatically authenticated to the Viya Server.
You can start making API calls right away during development just as you would in production.
Note that this cli does not restrict you to using restaf - you are free to use your favorite library to access SAS Viya REST APIs.
A note on making API calls
The recommended way to make the REST API calls is using restaf and restaflib. If for some reason (the author can think of few-to-none) you prefer to hand-code the REST API calls you can do that too. Remember that the session is authenticated using authorization_code flow and you'll need to code your calls accordingly.
Creating your React application
Issue the command below to create the default application.
npx create-react-restaf-viya-app react-appname --webapp webapp-name --title webapp-title --script scriptTags-file
Example:
npx create-react-restaf-viya-app mycoolapp
Only the react-appname is required. The optional parameters are:
webapp – this is the user-friendly application name; defaults to viyademo
title – the text for the title tag in index.html; defaults to SAS/Viya Application
script – a file which has some HTML script tags to be inserted into index.html; default is a comment line file
i – specify additional modules to install (ex: --i "@material-ui/core prettyjson")
Using the generated application
Once the app is created, you can start your development.
Key assumption
The application authenticates using authorization_code flow.
Configuration
Set the following in the .env file:
CLIENTID – the default value is viyademo
CLIENTSECRET – the default value is secret
VIYA_SERVER – no defaults.You must specify this (e.g., http://myviya.com)
Some defaults
The app server runs on localhost:5000/viyademo
The clientid redirect is http://localhost:5000/viyademo
The create-react-app server runs on its standard port(3000)
For more detailed configuration options, see https://github.com/sassoftware/restaf-server/wiki
Application development
Modify App.js to suit your needs.
Install any additional packages your app might need.
Running in development mode
Run this command to have HMR enabled.
cd to-the-app-directory
yarn dev
The flow of the application on the yarn dev command is show below.
Step 1 : The user invokes the application at http://localhost:5000/viyademo.
Step 2: The restaf-server authenticates with the Viya Server. The user is prompted for credentials.
Step 3a: The status of the initialization is displayed in the browser.
Step 3b: restaf-server runs the create-react-app’s npm start script, which launches your react application in a second tab of your browser.
At this point you can view your application, edit your code and see the updated application. The session is authenticated -so you can make REST API calls to SAS Viya using restaf or any other library. Refer to the flow below for more details.
Running in Application mode
Run the commands shown below. Note the create-react-app dev server will not start in this mode.
cd to-the-app-directory
yarn build
yarn app
React Context - AppContext
By default a react context named AppContext is created. To access the data code something like this:
import React,{useContext} from 'react';
import {AppContext} from '../providers';
let appContext = useContext(AppContext);
let {store, appOptions} = appContext;
let {appenv, logonPayload} = appOptions;
The store is the restaf store object you use to make the API calls. The logonPayload has the following form:
{
authType: 'server'
host : "your Viya hostname"
}
The host value is useful if you are making use of Visual Analytics SDK and/or if you are coding the REST API calls without restaf.
Importing restaf and restaflib in your application
These two libraries are part of the installed dependencies. To access them in your react components do these as follows:
let restaf = require('@sassoftware/restaf/dist/restaf.js');
let restaflib = require('@sassoftware/restaflib/dist/restaflib.js');
or
import * as restaf from '@sassoftware/restaf/dist/restaf.js';
import * as restaflib from '@sassoftware/restaf/dist/restaflib.js';
In all probability you will not refer directly to restaf in your code. Instead, use the store object in the AppContext (see above). This value is set as part of the application startup.
Finally
The create-react-restaf-viya-app CLI tool helps jump start your application development. Feel free to clone the repository and make enhancements.
... View more
Labels:
07-09-2020
08:14 AM
1 Like
In many applications the users specifies the the input table. In CAS, the program can fail for one of the following reasons (among others)
table exists but the table is not loaded
table does not exist
Caslib is incorrect
A good program should check the input and give users a friendly message if there is a problem. The program should not make users crawl through logs to find out what happened. Below is a simple CASL function checkAndLoadTable that I use in my applications and libraries like @sassoftware/restaflib to make sure that the table is loaded and is ready to go. If necessary, the function will load the table.
The code is available in the attached file.
Typical usage
rc = checkAndLoadTable(caslib, name);
if ( rc ne true) then do;
... handle error...
end;
else do;
... real work ...
end;
Test program with sample output
For your convenience here is a simple test program and sample results.
function test(testName, caslib, name) ;
print '-----------------------------------------------------';
print testName;
print '-----------------------------------------------------';
print 'Input caslib.name is: ' caslib '.' name;
result = checkAndLoadTable(caslib, name);
print 'Returned value: ' result;
if (result eq true) then do;
action table.recordCount r=result/
table = {caslib=caslib, name=name};
print 'Record Count: ' result.recordCount[1,1];
end;
end;
Below is a sample run and output.
test('Loaded Table','systemData', 'postgres');
test('Table not pre-loaded','casuser', 'iris');
test('Bad caslib', 'x', 'iris');
test('Bad table', 'casuser', 'x');
and the sample output is
-----------------------------------------------------
Loaded Table
-----------------------------------------------------
Input caslib.name is: systemData.postgres
Returned value: TRUE
Record Count: 4015
-----------------------------------------------------
Table not pre-loaded
-----------------------------------------------------
Input caslib.name is: casuser.iris
Returned value: TRUE
Record Count: 150
-----------------------------------------------------
Bad caslib
-----------------------------------------------------
Input caslib.name is: x.iris
Returned value: -1
-----------------------------------------------------
Bad table
-----------------------------------------------------
Input caslib.name is: casuser.x
Returned value: FALSE
test('Promoted Table','systemData', 'postgres');
test('Table not loaded','casuser', 'iris');
test('Bad caslib', 'x', 'iris');
test('Bad table', 'casuser', 'x');
Finally
This function can be used inside PROC CAS or in any CASL program running on the CAS server. If your application is used in spurts the checkAndLoadTable function is a good way to load the tables on demand. Remember that defensive programming saves hours of debugging for the user. Cheers. Deva
... View more
Labels:
06-25-2020
04:07 PM
2 Likes
Lazy Programmer’s Guide to Developing SAS Viya Applications
Philipp Lenssen writes in an article titled “Why Good Programmers Are Lazy and Dumb”
Lazy because only lazy programmers will want to write the kind of tools that might replace them in the end. Lazy, because only a lazy programmer will avoid writing monotonous, repetitive code – thus avoiding redundancy, the enemy of software maintenance and flexible refactoring. Mostly, the tools and processes that come out of this endeavor fired by laziness will speed up the production. This makes a lazy programmer a smart and efficient programmer.
From a SAS Viya Application developer’s perspective
One of the great strengths of SAS Viya is the large collection of REST APIs that allow customers to integrate SAS Viya capabilities into their applications. Using these APIs, the user’s applications access the full complement of SAS Viya capabilities - data preparation, analysis and reporting.
Just about every SAS Viya application will call a few key services to do the heavy lifting on the Viya server. The figure below shows these typical patterns.
How many times does one have to write the same code to call different REST APIs when those are all similar, but for the payload? How many times does one to have to write code to “reduce” the JSON response to be useful in presentations?
You too can become a smart lazy programmer by using the techniques discussed in this article titled Lazy Programmer’s Guide to Developing SAS Viya Applications.
Avoid writing code that "writes itself", spend more time solving real business problems and use the extra time doing things you always wanted to do - like watching the world go by.
... View more
Labels:
06-05-2020
03:39 PM
2 Likes
In my previous article I discussed a couple useful tools for deploying SAS Viya Applications. This article discusses the restaf-server app server, designed for rapid development and deployment of SAS Viya applications. Using a couple of configurations files, you can deploy an application with standard SAS Viya authentication and access to all SAS Viya services using REST APIs.
The key features of this app server are:
authentication
serve up static content
extendable with custom routes
TLS support
restaf-server uses hapijs to do all the heavy lifting and takes advantage of its great configurability.
The complete restaf-server documentation and code are available in GitHub.
Basic Steps
build your web application(s)
obtain a ClientID and Clientsecret
configure the server using a combination of
env files
Docker file
environment variables
create appenv.js for application specific configurations
start the server with a simple command (see below for an example)
npx @sassoftware/restaf-server --env=your-env-env-file --docker=your-Dockerfile --appenv=your-appenv.js-file
Typical flow
Once the server is started the applications flows as shown below. The sequence of steps is numbered.
Quick start example
Let us assume your application directory structure is along the lines of:
appdir/
public/
index.html
override.env
Dockerfile
appenv.js
package.json
Step 1: Create your application
Create your web application using any framework. This article assumes that the artifacts of your application are in the public directory but that is purely a personal choice.
Step 2: Create clientid and clientsecret
Obtain the clientid and clientsecret from your system administrator. If you are the administrator see Managing clientids page on GitHub for instructions on how to create this.
Step 3.1: Create the env file
This file (along with Dockerfile) is used to configure the app server. Create a file with a .env extension. In this example override.env is the name of this file. Below is an example of override.env.
VIYA_SERVER=http://your-viya-server
# OAUTHFLOW - clientid - code|implicit
AUTHFLOW=code
# Get these from your administrator. Sample value shown below
CLIENTID=appc
CLIENTSECRET=secret
# Where the app server is running:
# Valid values:
# localhost
# http(s)://hostname
# http(s)://ip address
# When running in docker do not specify this.
APPHOST=localhost
# PORT for app server. Pick a port of your choice
APPPORT=8080
# APPNAME – A user friendly name for your application
# The appserver will start at {APPHOST}:{APPPORT}/{APPNAME}
APPNAME=viyaapp
# Location of assets
# All assets are located relative to APPLOC
APPLOC=./public
# Main entry of your app
# On successful authentication this entry will be displayed in the browser
APPENTRY=index.html
# The JS object is available as APPENV js variable in your app.
# Include the following script tag in your html
# <script src=”/{APPNAME}/appenv”></script>
APPENV=appenv.js
# TLS Support
# see documentation on TLS Support
Step 3.2: Create the Dockerfile
A typical Dockerfile for this example appears below. This is the Dockerfile you will use if your application is delivered via docker containers.
FROM node:12.16.1-alpine
LABEL maintainer="your-email"
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
ENV APPHOST=0.0.0.0
ENV SAMESITE=None,false
ENV KEEPALIVE=YES
Step 4: Create Application Specific configuration file
In a typical SAS Viya Application, this file configures items like default caslibs, tables, reports, etc., for the application. See more details and an example in the viya-optimization-apps GitHub repository.
Build a placeholder appenv.js file with this content:
let x= {hi: 'hi there'};
return x;
You then start the server with the following command:
npx @sassoftware/restaf-server --env=./override.env --docker=./Dockerfile --appenv=./appenv.js
In the current example, the server will start at http://localhost:8080/viyaapp (http:{APPHOST}:{APPPORT}/APPNAME).
When you visit this link, SAS prompts you for user id and password. On successful authentication the html listed for APPENTRY (index.html in this example) displays in an authenticated browser session.
From your html you can make API calls to SAS Viya. Below is sample JavaScript code to get the root links for the files service using axios (note the host is the url for the Viya server).
async function makeViyaCall () {
let config = {
url : host + '/files/',
method : 'GET',
withCredentials: true,
headers : {
'accept':'application/json,application/vnd.sas.api+json',
}
};
let r = await axios(config);
return r.data;
};
Finally
With a few building materials and configuration values you can deploy your application easily and quickly. Feel free to clone and adopt this code as you see fit. Comments and contributions are welcome.
Cheers…
... View more
Labels:
05-21-2020
06:31 PM
By report I am assuming you are referring to SAS VA reports or somethiing else?
... View more
05-18-2020
02:37 PM
1 Like
Over the last several months I have been involved in the development of applications using SAS Viya. I witnessed a common pattern when integrating the capabilities of CAS and Visual Analytics Reports in web applications using SAS REST APIs.
To deploy the application at a user site the following is the typical list of artifacts.
web applications
SAS and CASL programs to execute the analysis
application configuration
One or more CAS tables which are updated at run time based on user actions; usually user input results in running multiple actions and updating of the tables.
pre-defined reports used to visualize the results; the reports might be viewed using SAS Visual Analytics or embedded in the user’s web application using the VA SDK.
There are standard ways to deploy items 1 and 2. This article is focused on two interactive CLI’s (available as npm packages) making the deployment of 3 through 5 simple and repeatable.
@sassoftware/registerclient – use this package to create, delete and list the clientids for three OAuth flows – password, implicit and authorization_code.
@sassoftware/viyacaddy – a tool to assist with:
import or export SAS Viya VA Reports
import data stored as hdat, b7dat or csv
import SAS and ds2 code for execution by datastep and scoring actions
import astore and sasast files
To facilitate the CI/CD process the tools allow for "batch" mode processing in addition to the CLI.
@sassoftware/registerclient
The creation of the clientid is not magic and just requires a couple of API calls. There are several blogs and other documentation on developer.sas.com that discuss how to get these using curl, postman, etc. In particular, refer to Authentication to SAS Viya: a couple of approaches. I always felt that something so basic should be managed either with a UI or a CLI. This CLI attempts to achieve that goal.
The registerclient tool is part of the restaf GitHub repository and is accompanied by full documentation.
The sample code below launches the app, displays the help text, logs on to SAS, and registers a clientid myapp1.
$ npx @sassoftware/registerclient --env register.env
--------------------------------------
Welcome to @sassoftware/registerclient
Enter help to get a list of all the commands
Use logon command to start your SAS Viya session. User must be an admin.
>> help
Commands:
help [command...] Provides help for a given command.
exit Exits application.
logon Logon to Viya
list [all] List clients. Use all option to include system clientids
config <config> File containing the configuration for clientid registeration
new [options] <clientid> Add a new client with specified name
delete <clientid> Delete specified client
>> logon
Enter your userid> sasdemo
Enter your password> ********
Logon Successful
>> add myapp1 -t authorization_code -s mysecret -r http://localhost:8080/myapp
myapp1 has been added
>> list
>>>>>>>>>>>>>>User defined clientids
clientid : myapp1
grantTypes: authorization_code,refresh_token
redirect : http://localhost:8080/myapp
undefined clientids detected
>>exit
@sassoftware/viyacaddy
This module is useful for the import and export of selected contents in SAS Viya. It is designed to address the simple import/export use case discussed earlier in this article. To move large amounts of data and reports please refer to the SAS Viya documentation.
The viyacaddy tool is located in the restaf GitHub repository and is accompanied by full documentation.
The sample code below launches the app, displays the help text, lists current reports, and imports a report.
$ npx @sassoftware/viyacaddy --env register.env
Welcome to @sassoftware/viyacaddy
Enter help to get a list of all the commands
Use logon command to start your SAS Viya session
>> help
Commands:
help [command...] Provides help for a given command.
exit Exits application.
logon Logon to Viya
tables import [options] <dir> [files...] Import .sas, .ds2, .sashdat, .sasb7dat, .astore , .sasast, .csv into CAS Tables
reports list List all the VA reports
reports import [options] <dir> [files...] Import VA reports
reports export [options] [files...] Export VA reports
caslibs List all active caslibs
tables list <caslib> List tables in a specified caslib
>> logon
Enter your userid> sasdemo
Enter your password> ********
Logon Successful
>> reports list
[
'Application Activity',
'CAS Activity',
'Disk Space',
'Home Loan Default Demo',
'SAS Viya Inventory'
]
>> reports import C:\Users\sas\Documents\env testReport.json -f Public
Imports started
Import of C:\Users\sas\Documents\env/testReport.json as report testReport completed
>> reports list
[
'Application Activity',
'CAS Activity',
'Disk Space',
'Home Loan Default Demo',
'SAS Viya Inventory'
'testReport'
]
>>exit
Finally
I hope you find these to be good additions to your toolbox. Please feel free to clone the code and make improvements.
Cheers…
... View more
04-30-2019
11:31 AM
Hi EduxEdux:
Interesting approach to solving the problem . Kudos to you..
Just to complete the discussion I am including a simple example of using restaf to run a cas action(echo in this case) when using the DDC or the web content component in VA.
If you are satisfied with your solution can you mark the issue as solved?
Cheers..
Deva
... View more
04-26-2019
10:51 AM
You cannot create session with a GET – it should fail since you have to use POST method.
The XSRF protection occurs at the http server that fronts all incoming requests and not at the cas level.
Since I was quickly getting into deep waters when I can barely swim I got the final word from the developer who handles such esoteric things for SAS Viya:
CSRF is in play when using HTTP sessions. If you’re calling services with an access token and no HTTP session cookie, you won’t see CSRF. Web apps like VA run in the browser and use HTTP sessions. If the browser is calling a service with an HTTP session (and no token), and it is doing a POST/PUT/DELETE, it must pass a CSRF token along with the request. The CSRF tokens really only applies to an app running in the browser, using HTTP sessions instead of access tokens.
That pretty much exhausts my knowledge on this topic 🙂
Cheers... Deva
... View more
04-25-2019
11:01 AM
Can you post a snippet of what you are doing and where ( in proc cas? in casl on server? others?). I have inkling of what you are trying to do but not really sure... Cheers... Deva
... View more