跳至主要內容
Open In ColabOpen on GitHub

Tableau

本筆記本提供快速入門 Tableau 的概觀。

概觀

Tableau 的 VizQL Data Service(又稱 VDS)為開發人員提供對其 Tableau 發佈資料來源的程式化存取權,允許他們為任何自訂工作負載或應用程式(包括 AI 代理程式)擴展其業務語義。simple_datasource_qa 工具將 VDS 新增至 Langchain 框架。本筆記本向您展示如何使用它來建立代理程式,以回答基於您的企業語義模型的分析問題。

追蹤 tableau-langchain 專案,以取得更多即將推出的工具!

設定

請確保您正在執行並可存取

  1. python 版本 3.12.2 或更高版本
  2. 具有至少 1 個發佈資料來源的 Tableau Cloud 或 Server 環境

開始使用,安裝和/或匯入所需的套件

# %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)

請注意,您可能需要重新啟動核心才能使用更新的套件

憑證

您可以明確宣告您的環境變數,如本文檔中的幾個案例所示。但是,如果未提供這些參數,simple_datasource_qa 工具將嘗試自動從環境變數中讀取它們。

對於您選擇查詢的資料來源,請確保您已更新 Tableau 中的 VizqlDataApiAccess 權限,以允許 VDS API 透過 REST 存取該資料來源。更多資訊請參閱此處

# 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
API 參考文件:ChatOpenAI | create_react_agent

身份驗證變數

您可以明確宣告您的環境變數,如本食譜中的幾個案例所示。但是,如果未提供這些參數,simple_datasource_qa 工具將嘗試自動從環境變數中讀取它們。

對於您選擇的資料來源,請確保您已更新 Tableau 中的 VizqlDataApiAccess 權限,以允許 VDS API 透過 REST 存取該資料來源。更多資訊請參閱此處

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"

實例化

initialize_simple_datasource_qa 初始化名為 simple_datasource_qa 的 Langgraph 工具,該工具可用於 Tableau 資料來源上的分析問題和解答。

此初始化函數

  1. 使用 Tableau 的 connected-app 框架進行基於 JWT 的身份驗證,以驗證 Tableau。所有必需的變數都必須在執行階段或作為環境變數定義。
  2. 非同步查詢 datasource_luid 變數中指定的目標資料來源的欄位中繼資料。
  3. 基於目標資料來源的中繼資料,將自然語言問題轉換為進行 VDS query-datasource 請求所需的 json 格式查詢酬載。
  4. 對 VDS 執行 POST 請求。
  5. 格式化並以結構化回應傳回結果。
# 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]

調用 - Langgraph 範例

首先,我們將初始化我們選擇的 LLM。然後,我們使用 langgraph 代理程式建構函式類別定義一個代理程式,並使用與目標資料來源相關的查詢來調用它。

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

以下是根據查詢資料的銷售額和利潤最高的州/地區的結果

銷售額最高的州/地區

  1. 加利福尼亞州: $457,687.63
  2. 紐約州: $310,876.27
  3. 德克薩斯州: $170,188.05
  4. 華盛頓州: $138,641.27
  5. 賓夕法尼亞州: $116,511.91

利潤最高的州/地區

  1. 加利福尼亞州: $76,381.39
  2. 紐約州: $74,038.55
  3. 華盛頓州: $33,402.65
  4. 密西根州: $24,463.19
  5. 維吉尼亞州: $18,597.95

比較

  • 加利福尼亞州紐約州是唯一同時出現在兩個列表中的州/地區,表示它們是頂級銷售商,也產生了最高的利潤。
  • 德克薩斯州雖然銷售額排名第三,但並未躋身利潤前五名,這表明儘管銷售額很高,但獲利能力可能存在問題。

此分析表明,高銷售額並不總是與高利潤相關,正如德克薩斯州的情況所示。

鏈結

待辦事項。

API 參考文件

待辦事項。


此頁面是否對您有幫助?