PowerBI 数据集代理
这个文档展示了一个与 Power BI 数据集进行交互的代理。该代理旨在回答关于数据集的更一般性的问题,并能够从错误中恢复。
请注意,由于该代理正在积极开发中,所有答案可能不都是正确的。它运行在 executequery 端点上,该端点不允许删除操作。
一些说明
- 该代理依赖于使用 azure.identity 包进行身份验证,可以使用
pip install azure-identity
安装该包。或者,您可以使用令牌字符串创建 Power BI 数据集,而无需提供凭据。 - 您还可以提供一个要模拟使用的用户名,用于支持启用了 RLS 的数据集。
- 该工具包使用 LLM 从问题生成查询,代理使用 LLM 进行整体执行。
- 测试主要是使用
text-davinci-003
模型进行的,Codex 模型似乎表现不佳。
初始化
from langchain.agents.agent_toolkits import create_pbi_agent
from langchain.agents.agent_toolkits import PowerBIToolkit
from langchain.utilities.powerbi import PowerBIDataset
from langchain.chat_models import ChatOpenAI
from langchain.agents import AgentExecutor
from azure.identity import DefaultAzureCredential
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_executor.run("表1中有多少条记录?")
例子:运行查询
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 提示
#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?")