Skip to main content

PowerBI数据集代理

This notebook showcases an agent designed to interact with a Power BI Dataset. The agent is designed to answer more general questions about a dataset, as well as recover from errors. 这个笔记本展示了一个与Power BI数据集交互的代理程序。该代理程序旨在回答关于数据集的更一般的问题,并能够从错误中恢复。

Note that, as this agent is in active development, all answers might not be correct. It runs against the executequery endpoint, which does not allow deletes. 请注意,由于该代理程序正在积极开发中,所有的答案可能不正确。它运行在executequery endpoint上,该endpoint不允许删除操作。

一些注意事项

  • 它依赖于与azure.identity包进行身份验证,可以使用pip install azure-identity进行安装。或者,您可以使用一个字符串作为令牌来创建powerbi数据集,而不提供凭据。
  • 您还可以提供一个用户名来模拟使用启用了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("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 prompts

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