跳至主要內容

PowerBI 工具包

本筆記本展示了 Agent 如何與 Power BI Dataset 互動。 Agent 不僅可以回答有關資料集的一般問題,還可以從錯誤中恢復。

請注意,由於此 Agent 仍在積極開發中,因此所有答案可能不完全正確。 它針對 executequery 端點執行,該端點不允許刪除。

注意事項:

  • 它依賴於使用 azure.identity 套件進行身份驗證,可以使用 pip install azure-identity 安裝。 或者,您可以創建 Power BI 資料集,並將 Token 作為字串提供,而無需提供憑證。
  • 您還可以提供要模擬的使用者名稱,以便用於啟用 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")

範例:新增您自己的少量範例提示

# fictional example
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?")

此頁面是否對您有幫助?