mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-05 22:53:30 +00:00
284 lines
10 KiB
Plaintext
284 lines
10 KiB
Plaintext
{
|
|
"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": 1,
|
|
"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": null,
|
|
"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\" # enter 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",
|
|
"model_provider = \"openai\" # the name of the model provider you are using for your Agent\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-mini\""
|
|
]
|
|
},
|
|
{
|
|
"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": 6,
|
|
"id": "72ee3eca",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Initialize 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",
|
|
" model_provider=model_provider,\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": null,
|
|
"id": "06a1d3f7-79a8-452e-b37e-9070d15445b0",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from IPython.display import Markdown, display\n",
|
|
"\n",
|
|
"model = ChatOpenAI(model=\"gpt-4o\", 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",
|
|
" \"what's going on with table sales?\",\n",
|
|
" )\n",
|
|
" ]\n",
|
|
" }\n",
|
|
")\n",
|
|
"messages\n",
|
|
"# display(Markdown(messages['messages'][3].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 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"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
|
|
}
|