Skip to main content
Open In ColabOpen on GitHub

Tableau

This notebook provides a quick overview for getting started with Tableau.

Overviewโ€‹

Tableau's VizQL Data Service (aka VDS) provides developers with programmatic access to their Tableau Published Data Sources, allowing them to extend their business semantics for any custom workload or application, including AI Agents. The simple_datasource_qa tool adds VDS to the Langchain framework. This notebook shows you how you can use it to build agents that answer analytical questions grounded on your enterprise semantic models.

Follow the tableau-langchain project for more tools coming soon!

Setupโ€‹

Make sure you are running and have access to:

  1. python version 3.12.2 or higher
  2. A Tableau Cloud or Server environment with at least 1 published data source

Get started by installing and/or importing the required packages

# %pip install langchain-openai
# %pip install langgraph
# %pip install langchain-tableau --upgrade
Requirement already satisfied: regex>=2022.1.18 in /Users/joe.constantino/.pyenv/versions/3.12.2/lib/python3.12/site-packages (from tiktoken<1,>=0.7->langchain-openai->langchain-tableau) (2024.11.6)
Requirement already satisfied: httpcore==1.* in /Users/joe.constantino/.pyenv/versions/3.12.2/lib/python3.12/site-packages (from httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph->langchain-tableau) (1.0.7)
Requirement already satisfied: h11<0.15,>=0.13 in /Users/joe.constantino/.pyenv/versions/3.12.2/lib/python3.12/site-packages (from httpcore==1.*->httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph->langchain-tableau) (0.14.0)

Note you may need to restart your kernal to use updated packages

Credentialsโ€‹

You can declare your environment variables explicitly, as shown in several cases in this doc. However, if these parameters are not provided, the simple_datasource_qa tool will attempt to automatically read them from environment variables.

For the Data Source that you choose to query, make sure you've updated the VizqlDataApiAccess permission in Tableau to allow the VDS API to access that Data Source via REST. More info here.

# langchain package imports
from langchain_openai import ChatOpenAI

# langchain_tableau and langgraph imports
from langchain_tableau.tools.simple_datasource_qa import initialize_simple_datasource_qa
from langgraph.prebuilt import create_react_agent

Authentication Variablesโ€‹

You can declare your environment variables explicitly, as shown in several cases in this cookbook. However, if these parameters are not provided, the simple_datasource_qa tool will attempt to automatically read them from environment variables.

For the Data Source that you choose, make sure you've updated the VizqlDataApiAccess permission in Tableau to allow the VDS API to access that Data Source via REST. More info here.

import os

from dotenv import load_dotenv

load_dotenv()

tableau_server = "https://stage-dataplane2.tableau.sfdc-shbmgi.svc.sfdcfc.net/" # replace with your Tableau server name
tableau_site = "vizqldataservicestage02" # replace with your Tableau site
tableau_jwt_client_id = os.getenv(
"TABLEAU_JWT_CLIENT_ID"
) # a JWT client ID (obtained through Tableau's admin UI)
tableau_jwt_secret_id = os.getenv(
"TABLEAU_JWT_SECRET_ID"
) # a JWT secret ID (obtained through Tableau's admin UI)
tableau_jwt_secret = os.getenv(
"TABLEAU_JWT_SECRET"
) # a JWT secret ID (obtained through Tableau's admin UI)
tableau_api_version = "3.21" # the current Tableau REST API Version
tableau_user = "joe.constantino@salesforce.com" # replace with the username querying the target Tableau Data Source

# For this cookbook we are connecting to the Superstore dataset that comes by default with every Tableau server
datasource_luid = (
"0965e61b-a072-43cf-994c-8c6cf526940d" # the target data source for this Tool
)

# Add variables to control LLM models for the Agent and Tools
os.environ["OPENAI_API_KEY"] # set an your model API key as an environment variable
tooling_llm_model = "gpt-4o"

Instantiationโ€‹

The initialize_simple_datasource_qa initializes the Langgraph tool called simple_datasource_qa, which can be used for analytical questions and answers on a Tableau Data Source.

This initializer function:

  1. Authenticates to Tableau using Tableau's connected-app framework for JWT-based authentication. All the required variables must be defined at runtime or as environment variables.
  2. Asynchronously queries for the field metadata of the target datasource specified in the datasource_luid variable.
  3. Grounds on the metadata of the target datasource to transform natural language questions into the json-formatted query payload required to make VDS query-datasource requests.
  4. Executes a POST request to VDS.
  5. Formats and returns the results in a structured response.
# Initalize simple_datasource_qa for querying Tableau Datasources through VDS
analyze_datasource = initialize_simple_datasource_qa(
domain=tableau_server,
site=tableau_site,
jwt_client_id=tableau_jwt_client_id,
jwt_secret_id=tableau_jwt_secret_id,
jwt_secret=tableau_jwt_secret,
tableau_api_version=tableau_api_version,
tableau_user=tableau_user,
datasource_luid=datasource_luid,
tooling_llm_model=tooling_llm_model,
)

# load the List of Tools to be used by the Agent. In this case we will just load our data source Q&A tool.
tools = [analyze_datasource]

Invocation - Langgraph Exampleโ€‹

First, we'll initlialize the LLM of our choice. Then, we define an agent using a langgraph agent constructor class and invoke it with a query related to the target data source.

from IPython.display import Markdown, display

model = ChatOpenAI(model="gpt-4o-mini", temperature=0)

tableauAgent = create_react_agent(model, tools)

# Run the agent
messages = tableauAgent.invoke(
{
"messages": [
(
"human",
"which states sell the most? Are those the same states with the most profits?",
)
]
}
)
messages
# display(Markdown(messages['messages'][4].content)) #display a nicely formatted answer for successful generations

Here are the results for the states with the highest sales and profits based on the data queried:

States with the Most Salesโ€‹

  1. California: $457,687.63
  2. New York: $310,876.27
  3. Texas: $170,188.05
  4. Washington: $138,641.27
  5. Pennsylvania: $116,511.91

States with the Most Profitโ€‹

  1. California: $76,381.39
  2. New York: $74,038.55
  3. Washington: $33,402.65
  4. Michigan: $24,463.19
  5. Virginia: $18,597.95

Comparisonโ€‹

  • California and New York are the only states that appear in both lists, indicating they are the top sellers and also generate the most profit.
  • Texas, while having the third highest sales, does not rank in the top five for profit, showing a potential issue with profitability despite high sales.

This analysis suggests that high sales do not always correlate with high profits, as seen with Texas.

Chainingโ€‹

TODO.

API referenceโ€‹

TODO.


Was this page helpful?