> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Tableau 集成

> 使用 LangChain Python 与 Tableau 工具集成。

本指南提供了快速入门 [Tableau](https://help.tableau.com/current/api/vizql-data-service/en-us/index.html) 的概述。

### 概述

Tableau 的 VizQL 数据服务（简称 VDS）为开发者提供程序化访问其 Tableau 发布数据源的权限，允许他们扩展其业务语义以适应任何自定义工作负载或应用程序，包括 AI 智能体。simple\_datasource\_qa 工具将 VDS 添加到 LangChain 框架中。本笔记本将向您展示如何使用它来构建能够基于您的企业语义模型回答分析问题的智能体。

关注 [tableau-langchain](https://github.com/Tab-SE/tableau_langchain) 项目，了解更多即将推出的工具！

#### 设置

确保您正在运行并可以访问：

1. python 版本 3.12.2 或更高版本
2. 至少包含 1 个已发布数据源的 Tableau 云或服务器环境

通过安装和/或导入所需的软件包开始使用

```python theme={null}
# pip install langchain-openai
```

```python theme={null}
# pip install langgraph
```

```python theme={null}
# pip install langchain-tableau --upgrade
```

```text theme={null}
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 访问该数据源。有关更多信息，请参阅 [Tableau 权限和功能](https://help.tableau.com/current/server/en-us/permissions_capabilities.htm#data-sources)。

```python theme={null}
# 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 langchain.agents import create_agent
```

## 身份验证变量

您可以在本文档的多个案例中显式声明环境变量。但是，如果这些参数未提供，simple\_datasource\_qa 工具将尝试自动从环境变量中读取它们。

对于您选择的数据源，请确保在 Tableau 中更新了 VizqlDataApiAccess 权限，以允许 VDS API 通过 REST 访问该数据源。有关更多信息，请参阅 [Tableau 权限和功能](https://help.tableau.com/current/server/en-us/permissions_capabilities.htm#data-sources)。

```python theme={null}
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"  # enter 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
)
model_provider = "openai"  # the name of the model provider you are using for your Agent
# Add variables to control LLM models for the Agent and Tools
os.environ["OPENAI_API_KEY"]  # set your model API key as an environment variable
tooling_llm_model = "gpt-5.4-mini"
```

## 实例化

initialize\_simple\_datasource\_qa 初始化名为 [simple\_datasource\_qa](https://github.com/Tab-SE/tableau_langchain/blob/3ff9047414479cd55d797c18a78f834d57860761/pip_package/langchain_tableau/tools/simple_datasource_qa.py#L101) 的 LangGraph 工具，该工具可用于对 Tableau 数据源进行分析问题并给出答案。

此初始化函数：

1. 使用 Tableau 的连接应用程序框架进行基于 JWT 的身份验证，以连接 Tableau。所有必需的变量必须在运行时或作为环境变量定义。
2. 异步查询 datasource\_luid 变量中指定的目标数据源的字段元数据。
3. 基于目标数据源的元数据，将自然语言问题转换为 VDS 查询数据源请求所需的 json 格式查询有效负载。
4. 执行 POST 请求到 VDS。
5. 格式化并返回结构化响应。

```python theme={null}
# Initialize 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,
    model_provider=model_provider,
)

# 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 智能体构造类定义一个智能体，并使用与目标数据源相关的查询调用它。

```python theme={null}
from IPython.display import Markdown, display

model = ChatOpenAI(model="gpt-5.5", temperature=0)

tableauAgent = create_agent(model, tools)

# Run the agent
messages = tableauAgent.invoke(
    {
        "messages": [
            (
                "human",
                "what's going on with table sales?",
            )
        ]
    }
)
messages
# display(Markdown(messages['messages'][3].content)) #display a nicely formatted answer for successful generations
```

***

> 官方原文：[src/oss/python/integrations/tools/tableau.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/tools/tableau.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.