Tutorial

customTools

Add custom tool to extend the Assistant

Set type to "module" in your package.json

Step 1: Install the library into your application

npm install @sassoftware/viya-assistantjs

Step 2: Create a simple nodejs application(assistant.js)

Here is the skeleton code This is a fully functional chat program


import * as readline from 'node:readline/promises';
import { stdin as input, stdout as output } from 'node:process';
import {setupAssistant, runAssistant} from '@sassoftware/viya-assistantjs';

// this import is to get token and host for Viya - created with sas-viya auth login|loginCode
// replace the next two lines to suit your environment
// see this link for the getToken function
import getToken from './getToken.js'; 
let {host, token} = getToken();

// Add another tool 

// Tool specification
let customTools: [
  {
    type: 'function',
    function: {
      name: "runSAS",
      description: "run the specified file. The file is a path to the sas program",
      parameters: {
        properties: {
          file: {
            type: "string",
            description: "this is the file to run",
          },
        },
        type: "object",
        required: ["file"],
      },
    }
  }
];

//handler for running sas code
const runSASFunctionSpec = {
  name: "runSAS",
  description: "run the specified file. The file is a path to the sas program. The p",
  parameters: {
    properties: {
      file: {
        type: "string",
        description: "this is the file to run",
      },
    },
    type: "object",
    required: ["file"],
  },
};

async function runSAS(params, userData, appControl) {
  let { file } = params;
  let appEnv = appControl.getViyaSession(source);
  let { store, session } = appEnv;
  let src;
  try {
    src = await fss.readFile(file, "utf8");
  } catch (err) {
    console.log(err);
    return "Error reading file " + file;
  }

  if (appEnv.source === "cas") {
    let r = await caslRun(store, session, src, {}, true);
    return JSON.stringify(r.results);
  } else {
    let computeSummary = await computeRun(store, session, src);
    let log = await computeResults(store, computeSummary, "log");
    return logAsArray(log);
  }
}
// setup configuration
let config = {
  provider: 'openai', 
  model: 'gpt-4-turbo-review', 
  credentials: {
    key: process.env.OPENAI_KEY, // for security get it from environment
    endPoint: null
  },
  assistantid: 'NEW', //let system create a new assistant
  assistantName: "SAS_ASSISTANT",

  threadid: 'NEW', // let system create a new thread

  
  domainTools: {tools: tools, functionList: functionList, instructions: '', replace: false},
  code:  true,
  retrievel: true

  viyaConfig: {
    logonPayload: {
      authType: 'server',
      host: host,  // viya url - https://myviyaserver.acme.com
      token: token,// viya token | null
      tokenType: 'bearer'// if token is specified
      },
    source: 'cas' 
  }  
}

// 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);
    } catch (err) {
      console.log(err);
    }
  }
}

Step 3: Run the sample app

node assistant

If source was set to 'cas' or 'compute' you can issue prompts that will use the builtin tools. (see the main doc for the default tools). By default you can ask for list of several Viya assets:

  • list of reports, libraries (caslib or librefs
    • list reports
    • list libs
  • for a given libref or caslib get list of tables
    • list tables in public
    • list tables in sashelp
  • fetch data from a specified lib and table
    • get data for public.cars. limit to 20