BookmarkSubscribeRSS Feed

GPT-4 Assisted Data Management in SAS Viya: A Custom LangChain Agent Approach

Started ‎01-29-2024 by
Modified ‎02-09-2024 by
Views 685

This post explores an alternative approach to data management in SAS Viya using a custom LangChain agent. This agent enables users to engage in a conversational interface with SAS Viya, bypassing the need for complex programming. By leveraging the power of large language models (LLMs) and SASPY, the agent offers a variety of data management tasks that include fetching table information, retrieving column details, and manipulating tables. What's more, the agent's built-in memory feature allows for the context-aware handling of user queries and the reusability of responses for subsequent questions. This approach opens the doors to an era of enhanced user interaction and automation in data management tasks, making it a valuable tool for data engineers and analysts alike.


Earlier, I explored employing LangChain with structured data:



I was excited by the possibilities. I was equally disappointed that there was no LangChain agent for SAS Viya.


Therefore, I wrote one. The post explains the result.


The Big Picture


Watch the following video. The code executed is a Python program that defines a custom agent for interacting with SAS Viya. It leverages the LangChain Python library, Azure OpenAI, and SASPY to interact with SAS libraries and perform various operations such as fetching table information, getting column information, and appending tables.


The Finer Picture


The custom agent is primarily designed to interact with a Large Language Model (LLM), specifically, the GPT-4 model deployed on Azure OpenAI. It utilizes a set of custom defined tools to retrieve and manipulate data in SAS libraries. These tools use SASPY to connect to a SAS Viya instance, start a session and execute SAS code. It also features a memory component to remember previous interactions, which aids in maintaining context across a series of questions or commands.


Key Components


The program consists of several key components:


  1. LLM Setup: The LLM is set up using AzureChatOpenAI from the langchain_openai Python module. The environment variables for Azure OpenAI are set, and the GPT-4 model is selected for deployment.
  2. Tool Definitions: A series of functions (tools) are defined for various tasks, such as fetching table and column info, or appending tables in SAS. These tools are then bound to the LLM. The LLM has to use them. The SAS Viya tools were created using the approach described on the LangChain Custom Agent page. I am using the getting word length tool as a control function, to sepaarate SAS from non-SAS tools.
  3. Chat Prompt Creation: A ChatPromptTemplate is created, which sets up the structure of the conversation, including the system prompts, user inputs, agent's scratchpad, and chat history.
  4. Agent Creation: An agent is created that takes user input and chat history, applies the prompt template, executes the LLM with the tools, and parses the output.
  5. Agent Execution: The agent is executed on a series of questions or commands, and the responses are stored in the chat history to maintain context.


How The Components Work Together


The agent operates by taking a user question or command, applying the prompt template, and executing the LLM with the relevant tools. The output is parsed and returned to the user. The chat history maintains a record of previous interactions, which allows the agent to refer back to earlier responses, ensuring continuity and context across multiple user inputs.


The Importance of the Approach


This approach is critical for creating intelligent, interactive agents that can handle complex tasks and maintain context across multiple interactions. By leveraging LLMs, the agent can understand and respond to a wide range of queries or commands. The use of specific tools enables the agent to interact with SAS libraries, making it a useful tool for data engineers and analysts working with SAS.


What the Program Brings to the Table


This program demonstrates how to create a powerful, interactive custom agent for SAS Viya capable of performing complex tasks and maintaining context across multiple interactions. It showcases the potential of LLMs in creating intelligent agents and the use of SASPY in interacting with SAS Viya. SASPY is just an example. The same approach could be employed with SWAT, SASCTL, or plain SAS Viya REST API.


Conclusions and Next Steps


The implementation of the custom SAS Viya agent in LangChain is successful, demonstrating the potential of using LLMs and specific tools to create intelligent, interactive agents. The next steps could involve expanding the range of tools available to the agent and optimizing the agent's memory component to handle more complex interactions.


My Own Opinion About the Approach


Personally, I believe this approach represents an exciting advancement in the field of data management. By using AI and natural language processing to make SAS Viya more accessible to non-technical users, making it easier to use, we're opening up new possibilities for data analysis and manipulation. I'm excited to see where this technology takes us in the future.


In a next post, I will describe in more detail the Python program and the different prerequisites. 


What To Read Next


Want to understand what it takes to build a custom agent? Read the post How to Create Your Custom LangChain Agent for SAS Viya.



Thanks to @AjmalFarzam  for teaching me SASPY in 15 minutes with his Submit workloads to SAS Viya from Python.


Thank you for your time reading this post. If you liked the post, give it a thumbs up! Please comment and tell us what you think about having conversations with your data. If you wish to get more information, please write me an email.



Version history
Last update:
‎02-09-2024 01:03 AM
Updated by:


Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.


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