Tableau docs for new datasource qa tool (#30125)

- **Description: a notebook showing langchain and langraph agents using
the new langchain_tableau tool
- **Twitter handle: @joe_constantin0

---------

Co-authored-by: Joe Constantino <joe@constantino.com>
Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
joeconstantino 2025-03-06 06:58:56 -08:00 committed by GitHub
parent 52b0570bec
commit 022ff9eead
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 334 additions and 0 deletions

View File

@ -0,0 +1,15 @@
# Tableau
[Tableau](https://www.tableau.com/) is an analytics platform that enables anyone to
see and understand data.
## Installation and Setup
```bash
pip install langchain-tableau
```
## Tools
See detail on available tools [here](/docs/integrations/tools/tableau).

View File

@ -0,0 +1,315 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "1f302499-eb05-4296-8716-950babc0f10e",
"metadata": {},
"source": [
"# Tableau\n",
"\n",
"This notebook provides a quick overview for getting started with [Tableau](https://help.tableau.com/current/api/vizql-data-service/en-us/index.html). "
]
},
{
"cell_type": "markdown",
"id": "4d57b913-819e-4676-9f6e-3afe0a80030e",
"metadata": {},
"source": [
"### Overview\n",
"\n",
"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. \n",
"\n",
"Follow the [tableau-langchain](https://github.com/Tab-SE/tableau_langchain) project for more tools coming soon!"
]
},
{
"cell_type": "markdown",
"id": "311bce64",
"metadata": {},
"source": [
"#### Setup\n",
"Make sure you are running and have access to:\n",
"1. python version 3.12.2 or higher\n",
"2. A Tableau Cloud or Server environment with at least 1 published data source\n",
"\n",
"Get started by installing and/or importing the required packages"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9b178e95-ffae-4f04-ad77-1fdc2ab05edf",
"metadata": {},
"outputs": [],
"source": [
"# %pip install langchain-openai"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8605e87a-2253-4c89-992a-ecdbec955ef6",
"metadata": {},
"outputs": [],
"source": [
"# %pip install langgraph"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c13dca76",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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)\r\n",
"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)\r\n",
"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)\r\n"
]
}
],
"source": [
"# %pip install langchain-tableau --upgrade"
]
},
{
"cell_type": "markdown",
"id": "bbaa05f4",
"metadata": {},
"source": [
"Note you may need to restart your kernal to use updated packages"
]
},
{
"cell_type": "markdown",
"id": "80473fcc",
"metadata": {},
"source": [
"### Credentials\n",
"\n",
"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.\n",
"\n",
"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](https://help.tableau.com/current/server/en-us/permissions_capabilities.htm#data-sources\n",
"). "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "310d21b3",
"metadata": {},
"outputs": [],
"source": [
"# langchain package imports\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"# langchain_tableau and langgraph imports\n",
"from langchain_tableau.tools.simple_datasource_qa import initialize_simple_datasource_qa\n",
"from langgraph.prebuilt import create_react_agent"
]
},
{
"cell_type": "markdown",
"id": "596d6718-f2e1-44bb-b614-65447862661c",
"metadata": {},
"source": [
"## Authentication Variables\n",
"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.\n",
"\n",
"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](https://help.tableau.com/current/server/en-us/permissions_capabilities.htm#data-sources\n",
"). "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ccfb4159-34ac-4816-a8f0-795c5442c0b2",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"from dotenv import load_dotenv\n",
"\n",
"load_dotenv()\n",
"\n",
"tableau_server = \"https://stage-dataplane2.tableau.sfdc-shbmgi.svc.sfdcfc.net/\" # replace with your Tableau server name\n",
"tableau_site = \"vizqldataservicestage02\" # replace with your Tableau site\n",
"tableau_jwt_client_id = os.getenv(\n",
" \"TABLEAU_JWT_CLIENT_ID\"\n",
") # a JWT client ID (obtained through Tableau's admin UI)\n",
"tableau_jwt_secret_id = os.getenv(\n",
" \"TABLEAU_JWT_SECRET_ID\"\n",
") # a JWT secret ID (obtained through Tableau's admin UI)\n",
"tableau_jwt_secret = os.getenv(\n",
" \"TABLEAU_JWT_SECRET\"\n",
") # a JWT secret ID (obtained through Tableau's admin UI)\n",
"tableau_api_version = \"3.21\" # the current Tableau REST API Version\n",
"tableau_user = \"joe.constantino@salesforce.com\" # replace with the username querying the target Tableau Data Source\n",
"\n",
"# For this cookbook we are connecting to the Superstore dataset that comes by default with every Tableau server\n",
"datasource_luid = (\n",
" \"0965e61b-a072-43cf-994c-8c6cf526940d\" # the target data source for this Tool\n",
")\n",
"\n",
"# Add variables to control LLM models for the Agent and Tools\n",
"os.environ[\"OPENAI_API_KEY\"] # set an your model API key as an environment variable\n",
"tooling_llm_model = \"gpt-4o\""
]
},
{
"cell_type": "markdown",
"id": "64d08107",
"metadata": {},
"source": [
"## Instantiation\n",
"The initialize_simple_datasource_qa initializes the Langgraph tool called [simple_datasource_qa](https://github.com/Tab-SE/tableau_langchain/blob/3ff9047414479cd55d797c18a78f834d57860761/pip_package/langchain_tableau/tools/simple_datasource_qa.py#L101), which can be used for analytical questions and answers on a Tableau Data Source.\n",
"\n",
"This initializer function:\n",
"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.\n",
"2. Asynchronously queries for the field metadata of the target datasource specified in the datasource_luid variable.\n",
"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.\n",
"4. Executes a POST request to VDS.\n",
"5. Formats and returns the results in a structured response."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72ee3eca",
"metadata": {},
"outputs": [],
"source": [
"# Initalize simple_datasource_qa for querying Tableau Datasources through VDS\n",
"analyze_datasource = initialize_simple_datasource_qa(\n",
" domain=tableau_server,\n",
" site=tableau_site,\n",
" jwt_client_id=tableau_jwt_client_id,\n",
" jwt_secret_id=tableau_jwt_secret_id,\n",
" jwt_secret=tableau_jwt_secret,\n",
" tableau_api_version=tableau_api_version,\n",
" tableau_user=tableau_user,\n",
" datasource_luid=datasource_luid,\n",
" tooling_llm_model=tooling_llm_model,\n",
")\n",
"\n",
"# load the List of Tools to be used by the Agent. In this case we will just load our data source Q&A tool.\n",
"tools = [analyze_datasource]"
]
},
{
"cell_type": "markdown",
"id": "0ac5daa0-4336-48d0-9c26-20bf2c252bad",
"metadata": {},
"source": [
"## Invocation - Langgraph Example\n",
"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. "
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "06a1d3f7-79a8-452e-b37e-9070d15445b0",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Here are the results for the states with the highest sales and profits based on the data queried:\n",
"\n",
"### States with the Most Sales\n",
"1. **California**: $457,687.63\n",
"2. **New York**: $310,876.27\n",
"3. **Texas**: $170,188.05\n",
"4. **Washington**: $138,641.27\n",
"5. **Pennsylvania**: $116,511.91\n",
"\n",
"### States with the Most Profit\n",
"1. **California**: $76,381.39\n",
"2. **New York**: $74,038.55\n",
"3. **Washington**: $33,402.65\n",
"4. **Michigan**: $24,463.19\n",
"5. **Virginia**: $18,597.95\n",
"\n",
"### Comparison\n",
"- **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.\n",
"- **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.\n",
"\n",
"This analysis suggests that high sales do not always correlate with high profits, as seen with Texas."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import Markdown, display\n",
"\n",
"model = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
"\n",
"tableauAgent = create_react_agent(model, tools)\n",
"\n",
"# Run the agent\n",
"messages = tableauAgent.invoke(\n",
" {\n",
" \"messages\": [\n",
" (\n",
" \"human\",\n",
" \"which states sell the most? Are those the same states with the most profits?\",\n",
" )\n",
" ]\n",
" }\n",
")\n",
"messages\n",
"# display(Markdown(messages['messages'][4].content)) #display a nicely formatted answer for successful generations"
]
},
{
"cell_type": "markdown",
"id": "e6b20093",
"metadata": {},
"source": [
"## Chaining\n",
"\n",
"TODO."
]
},
{
"cell_type": "markdown",
"id": "12ab3d7b",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"TODO."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (package_test_env)",
"language": "python",
"name": "package_test_env"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@ -474,3 +474,7 @@ packages:
name_title: Taiga
path: .
repo: Shikenso-Analytics/langchain-taiga
- name: langchain-tableau
name_title: Tableau
path: .
repo: Tab-SE/tableau_langchain