@sassoftware/viya-assistantjs v0.19.0
@sassoftware/viya-assistantjs - Build your own AI ASSISTANT for SAS Viya
Introduction to azure and openai Assistant API
There are many resources available. This site from Microsoft is a good place to start
Getting started with Azure OpenAI Assistants (Preview)
gpt models
The information here is a moving target. Check with the provider for the proper model and zone to use for Assistant API.
Models I am using:
- openai: gpt-4-turbo-preview
- azureai: gpt-4 1106 preview in zone East US 2
@sassoftware/viya-assistantjs
The document is available here @sassoftware/viya-assistantjs is a JavaScript library with the following key features
Write your first assistant in a few minutes
The tools supported OOB are:
- code interpreter -- from the providers openai and azureai
- retrieval - Only openai supports this the time of this writing
- SAS Viya related custom tools
- List available reports and libraries
- Fetch data from Viya with filters
Extend or replace the tools with your own
Getting Started
Creating a AI Assistant with defaults
A version of this is here
Step 0 - Create a nodejs project and install the following:
- @sassoftware/viya-assistantjs
Recommend that your set type to module in your package.json
Create your program
In your index.js add the following imports:
import * as readline from 'node:readline/promises';
import { stdin as input, stdout as output } from 'node:process';
import {setupAssistant, runAssistant} from '@sassoftware/viya-assistantjs';
Create the configuration object as shown below. Substitute your own values
let config = {
provider: 'openai'|'azureai', // Depending on who your account is with
model: 'gpt-4-turbo-review'| for azureai the model you created in the portal
credentials: {
key: <your key> // obtain from provider
endPoint: <set this to our aureai resource url if provider is azureai>
},
// leave the next 4 items as is - explained in the document
assistantid: '0', //leave it as is for now
assistantName: "SAS_ASSISTANT",
threadid: '-1', // Ignore this for now
domainTools: {tools: [], functionList: {}, instructions: '', replace: false},
// fill in the host and token to authenticate to Viya
// set the source to cas or compute.
// if you want to run the AI assistant without Viya set source to none
viyaConfig: {
logonPayload: {
authType: 'server',
host: host, // viya url - https://myviyaserver.acme.com
token: token,// viya token - obtained from sas-viya auth login|loginCode
tokenType: 'bearer'//
},
source: 'cas'
},
code: true,
retrieval: <Must be false for azureai>
}
> Add a function to handle the prompts
```javascript
chat(config)
.then (() => console.log('bye'))
.catch(err => console.log(err));
async function chat(config) {
//Setup assistant
let gptControl = 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(gptControl, prompt,promptInstructions);
console.log(response);
} catch (err) {
console.log(err);
}
}
}
Run the program
node index.js
If everthing was setup properly, your should get a prompt(>). Enter your prompts and get results.
A note on prompts
Here are some prompts to try:(enter exit to stop the chat)
add 1 + 1
who is the CEO of SAS Institute?
Warning: The actual api calls to Viya is quick, but the
total response time from azure or openai might be much longer.
list lib
list the tables in public
fetch data from cars. Limit the rows to 10
A fun prompt - try it Fetch data from cars where origin='Japan'
Extend Assistant to support running SAS Code]
We will extend the Assistant from the last section to run SAS programs.
To do this we have to fill in the domainTools in the configuration.
Step 1: Define the customTool
Key points
Give the tool a name. This will also be the name of the function that implements the tool.
The description is important - This is what helps gpt decide whether this tool can satisfy the request
The parameters are what system will extract from the prompt and send it to your function as a params object. In this example the value of the program will be extracted.
let tools = [
{
type: 'function',
function: {
name: "processSASProgram",
description: "process named SAS program. The program extension must be .sas or .casl",
parameters: {
properties: {
resource: {
type: "string",
description: "the name of the program to run",
},
},
type: "object",
required: ["resource"],
},
}
}
];
Step 2: Create the function to handle the request
// You need to add this import to the program
// import fs from 'fs/promises';
async function processSASProgram(params, appEnv) {
let { resource} = params;
let { store, session } = appEnv;
let src;
try {
src = await fs.readFile(resource, "utf8");
} catch (err) {
console.log(err);
return "Error reading program " + resource;
}
try {
if (appEnv.source === "cas") {
let r = await restaflib.caslRun(store, session, src, {}, true);
return JSON.stringify(r.results);
} else if (appEnv) {
let computeSummary = await computeRun(store, session, src);
let log = await restaflib.computeResults(store, computeSummary, "log");
return log;
} else {
return "Cannot run program without a session";
}
} catch (err) {
console.log(err);
return "Error running program " + program;
}
}
### Step 3: Create the domainTool object in configuration
```javascript
config.domainTools = {
tools: tools,
functionList: {processSASProgram: processSASProgram},
instructions: 'Additionally use this tool to run the specified .',
replace: false // use true if you want to get rid of previous tool definition;
};
Step 4
Run the program as you did befoee
Prompts
The sample program(datastep.casl) is a simple casl program
action datastep.runcode r= result/ single='YES' code = 'data casuser.a; x=1; run;';
send_response({casResults={result=result}});
Here is a sample prompt
process datastep.casl
If the run is successful you can query the table that was created.
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago