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

# Powerbi 工具包集成

> 使用 LangChain Python 与 Powerbi 工具包集成。

本笔记本展示了一个代理与 `Power BI 数据集` 交互的过程。该代理能够回答关于数据集的更一般性问题，并能从错误中恢复。

请注意，由于此代理正在积极开发中，所有答案可能并不完全正确。它通过 [executequery 端点](https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/execute-queries) 运行，该端点不允许删除操作。

### 注意事项

* 它依赖于 `azure.identity` 包进行身份验证，该包可通过 `pip install azure-identity` 安装。或者，您也可以使用字符串形式的令牌创建 powerbi 数据集，而无需提供凭据。
* 您还可以提供一个要模拟的用户名，用于启用了行级安全性 (RLS) 的数据集。
* 该工具包使用 LLM 根据问题创建查询，代理则使用 LLM 进行整体执行。
* 测试主要使用 `gpt-3.5-turbo-instruct` 模型进行，Codex 模型似乎表现不佳。

## 初始化

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from azure.identity import DefaultAzureCredential
from langchain_community.agent_toolkits import PowerBIToolkit, create_pbi_agent
from langchain_community.utilities.powerbi import PowerBIDataset
from langchain_openai import ChatOpenAI
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
fast_llm = ChatOpenAI(
    temperature=0.5, max_tokens=1000, model_name="gpt-3.5-turbo", verbose=True
)
smart_llm = ChatOpenAI(temperature=0, max_tokens=100, model_name="gpt-4", verbose=True)

toolkit = PowerBIToolkit(
    powerbi=PowerBIDataset(
        dataset_id="<dataset_id>",
        table_names=["table1", "table2"],
        credential=DefaultAzureCredential(),
    ),
    llm=smart_llm,
)

agent_executor = create_pbi_agent(
    llm=fast_llm,
    toolkit=toolkit,
    verbose=True,
)
```

## 示例：描述表

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
agent_executor.run("Describe table1")
```

## 示例：对表进行简单查询

在此示例中，代理实际上弄清楚了获取表行数的正确查询。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
agent_executor.run("How many records are in table1?")
```

## 示例：运行查询

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
agent_executor.run("How many records are there by dimension1 in table2?")
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
agent_executor.run("What unique values are there for dimensions2 in table2")
```

## 示例：添加您自己的少样本提示

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 虚构示例
few_shots = """
Question: How many rows are in the table revenue?
DAX: EVALUATE ROW("Number of rows", COUNTROWS(revenue_details))
----
Question: How many rows are in the table revenue where year is not empty?
DAX: EVALUATE ROW("Number of rows", COUNTROWS(FILTER(revenue_details, revenue_details[year] <> "")))
----
Question: What was the average of value in revenue in dollars?
DAX: EVALUATE ROW("Average", AVERAGE(revenue_details[dollar_value]))
----
"""
toolkit = PowerBIToolkit(
    powerbi=PowerBIDataset(
        dataset_id="<dataset_id>",
        table_names=["table1", "table2"],
        credential=DefaultAzureCredential(),
    ),
    llm=smart_llm,
    examples=few_shots,
)
agent_executor = create_pbi_agent(
    llm=fast_llm,
    toolkit=toolkit,
    verbose=True,
)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
agent_executor.run("What was the maximum of value in revenue in dollars in 2022?")
```

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/tools/powerbi.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
