-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat[DatabricksConnector]: connector to connect to Databricks on Cloud (
#580)
- Loading branch information
1 parent
36c1b2d
commit a8e5afb
Showing
11 changed files
with
550 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Example of using PandasAI with a DataBricks""" | ||
|
||
from pandasai import SmartDataframe | ||
from pandasai.llm import OpenAI | ||
from pandasai.connectors import DatabricksConnector | ||
|
||
|
||
databricks_connector = DatabricksConnector( | ||
config={ | ||
"host": "adb-*****.azuredatabricks.net", | ||
"database": "default", | ||
"token": "dapidfd412321", | ||
"port": 443, | ||
"table": "loan_payments_data", | ||
"httpPath": "/sql/1.0/warehouses/213421312", | ||
"where": [ | ||
# this is optional and filters the data to | ||
# reduce the size of the dataframe | ||
["loan_status", "=", "PAIDOFF"], | ||
], | ||
} | ||
) | ||
|
||
llm = OpenAI("OPEN_API_KEY") | ||
df = SmartDataframe(databricks_connector, config={"llm": llm}) | ||
|
||
response = df.chat("How many people from the United states?") | ||
print(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
Databricks Connector to connects you to your Databricks SQL Warhouse on | ||
Azure, AWS and GCP | ||
""" | ||
|
||
from .base import BaseConnectorConfig, DatabricksConnectorConfig | ||
from sqlalchemy import create_engine | ||
from typing import Union | ||
from .sql import SQLConnector | ||
|
||
|
||
class DatabricksConnector(SQLConnector): | ||
""" | ||
Databricks connectors are used to connect to Databricks Data Cloud. | ||
""" | ||
|
||
def __init__(self, config: Union[DatabricksConnectorConfig, dict]): | ||
""" | ||
Initialize the Databricks connector with the given configuration. | ||
Args: | ||
config (ConnectorConfig): The configuration for the Databricks connector. | ||
""" | ||
config["dialect"] = "databricks" | ||
if isinstance(config, dict): | ||
env_vars = { | ||
"token": "DATABRICKS_TOKEN", | ||
"database": "DATABRICKS_DATABASE", | ||
"host": "DATABRICKS_HOST", | ||
"port": "DATABRICKS_PORT", | ||
"httpPath": "DATABRICKS_HTTP_PATH", | ||
} | ||
config = self._populate_config_from_env(config, env_vars) | ||
|
||
super().__init__(config) | ||
|
||
def _load_connector_config(self, config: Union[BaseConnectorConfig, dict]): | ||
return DatabricksConnectorConfig(**config) | ||
|
||
def _init_connection(self, config: DatabricksConnectorConfig): | ||
""" | ||
Initialize Database Connection | ||
Args: | ||
config (DatabricksConnectorConfig): Configurations to load database | ||
""" | ||
self._engine = create_engine( | ||
f"{config.dialect}://token:{config.token}@{config.host}:{config.port}?http_path={config.httpPath}" | ||
) | ||
|
||
self._connection = self._engine.connect() | ||
|
||
def __repr__(self): | ||
""" | ||
Return the string representation of the Databricks connector. | ||
Returns: | ||
str: The string representation of the Databricks connector. | ||
""" | ||
return ( | ||
f"<{self.__class__.__name__} dialect={self._config.dialect} " | ||
f"host={self._config.host} port={self._config.port} " | ||
f"database={self._config.database} httpPath={str(self._config.httpPath)}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.