forked from Archives/langchain
power bi api wrapper integration tests & bug fix (#4983)
# Powerbi API wrapper bug fix + integration tests - Bug fix by removing `TYPE_CHECKING` in in utilities/powerbi.py - Added integration test for power bi api in utilities/test_powerbi_api.py - Added integration test for power bi agent in agent/test_powerbi_agent.py - Edited .env.examples to help set up power bi related environment variables - Updated demo notebook with working code in docs../examples/powerbi.ipynb - AzureOpenAI -> ChatOpenAI Notes: Chat models (gpt3.5, gpt4) are much more capable than davinci at writing DAX queries, so that is important to getting the agent to work properly. Interestingly, gpt3.5-turbo needed the examples=DEFAULT_FEWSHOT_EXAMPLES to write consistent DAX queries, so gpt4 seems necessary as the smart llm. Fixes #4325 ## Before submitting Azure-core and Azure-identity are necessary dependencies check integration tests with the following: `pytest tests/integration_tests/utilities/test_powerbi_api.py` `pytest tests/integration_tests/agent/test_powerbi_agent.py` You will need a power bi account with a dataset id + table name in order to test. See .env.examples for details. ## Who can review? @hwchase17 @vowelparrot --------- Co-authored-by: aditya-pethe <adityapethe1@gmail.com>searx_updates
parent
e68dfa7062
commit
06e524416c
@ -0,0 +1,47 @@
|
||||
import pytest
|
||||
|
||||
from langchain.agents.agent_toolkits import PowerBIToolkit, create_pbi_agent
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
from langchain.utilities.powerbi import PowerBIDataset
|
||||
from langchain.utils import get_from_env
|
||||
|
||||
|
||||
def azure_installed() -> bool:
|
||||
try:
|
||||
from azure.core.credentials import TokenCredential # noqa: F401
|
||||
from azure.identity import DefaultAzureCredential # noqa: F401
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"azure not installed, skipping test {e}")
|
||||
return False
|
||||
|
||||
|
||||
@pytest.mark.skipif(not azure_installed(), reason="requires azure package")
|
||||
def test_daxquery() -> None:
|
||||
from azure.identity import DefaultAzureCredential
|
||||
|
||||
DATASET_ID = get_from_env("", "POWERBI_DATASET_ID")
|
||||
TABLE_NAME = get_from_env("", "POWERBI_TABLE_NAME")
|
||||
NUM_ROWS = get_from_env("", "POWERBI_NUMROWS")
|
||||
|
||||
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=[TABLE_NAME],
|
||||
credential=DefaultAzureCredential(),
|
||||
),
|
||||
llm=smart_llm,
|
||||
)
|
||||
|
||||
agent_executor = create_pbi_agent(llm=fast_llm, toolkit=toolkit, verbose=True)
|
||||
|
||||
output = agent_executor.run(f"How many rows are in the table, {TABLE_NAME}")
|
||||
assert NUM_ROWS in output
|
@ -0,0 +1,36 @@
|
||||
"""Integration test for POWERBI API Wrapper."""
|
||||
import pytest
|
||||
|
||||
from langchain.utilities.powerbi import PowerBIDataset
|
||||
from langchain.utils import get_from_env
|
||||
|
||||
|
||||
def azure_installed() -> bool:
|
||||
try:
|
||||
from azure.core.credentials import TokenCredential # noqa: F401
|
||||
from azure.identity import DefaultAzureCredential # noqa: F401
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"azure not installed, skipping test {e}")
|
||||
return False
|
||||
|
||||
|
||||
@pytest.mark.skipif(not azure_installed(), reason="requires azure package")
|
||||
def test_daxquery() -> None:
|
||||
from azure.identity import DefaultAzureCredential
|
||||
|
||||
DATASET_ID = get_from_env("", "POWERBI_DATASET_ID")
|
||||
TABLE_NAME = get_from_env("", "POWERBI_TABLE_NAME")
|
||||
NUM_ROWS = get_from_env("", "POWERBI_NUMROWS")
|
||||
|
||||
powerbi = PowerBIDataset(
|
||||
dataset_id=DATASET_ID,
|
||||
table_names=[TABLE_NAME],
|
||||
credential=DefaultAzureCredential(),
|
||||
)
|
||||
|
||||
output = powerbi.run(f'EVALUATE ROW("RowCount", COUNTROWS({TABLE_NAME}))')
|
||||
numrows = str(output["results"][0]["tables"][0]["rows"][0]["[RowCount]"])
|
||||
|
||||
assert NUM_ROWS == numrows
|
@ -0,0 +1,10 @@
|
||||
def test_power_bi_can_be_imported() -> None:
|
||||
"""Test that powerbi tools can be imported.
|
||||
|
||||
The goal of this test is to verify that langchain users will not get import errors
|
||||
when loading powerbi related code if they don't have optional dependencies
|
||||
installed.
|
||||
"""
|
||||
from langchain.tools.powerbi.tool import QueryPowerBITool # noqa
|
||||
from langchain.agents.agent_toolkits import PowerBIToolkit, create_pbi_agent # noqa
|
||||
from langchain.utilities.powerbi import PowerBIDataset # noqa
|
Loading…
Reference in New Issue