Skip to main content
本 notebook 展示了一个与 Power BI Dataset 交互的 Agent。该 Agent 能够回答关于数据集的通用问题,并能从错误中恢复。 注意,由于此 Agent 仍在积极开发中,并非所有答案都是正确的。它运行于 executequery 端点,该端点不允许删除操作。

注意事项

  • 它依赖于 azure.identity 包进行身份验证,可通过 pip install azure-identity 安装。或者,你也可以通过字符串形式的 token 创建 powerbi 数据集,无需提供凭据。
  • 你也可以提供用户名以模拟用户,适用于启用了 RLS 的数据集。
  • 工具包使用 LLM 根据问题生成查询,Agent 使用 LLM 进行整体执行。
  • 测试主要使用 gpt-3.5-turbo-instruct 模型完成,codex 模型表现不佳。

初始化

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
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,
)

示例:描述一张表

agent_executor.run("Describe table1")

示例:对表执行简单查询

在本示例中,Agent 实际上找出了正确的查询来获取表的行数。
agent_executor.run("How many records are in table1?")

示例:执行查询

agent_executor.run("How many records are there by dimension1 in table2?")
agent_executor.run("What unique values are there for dimensions2 in table2")

示例:添加自定义 few-shot 提示词

# 虚构示例
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,
)
agent_executor.run("What was the maximum of value in revenue in dollars in 2022?")