warehouse_id: The warehouse ID in the Databricks SQL.
cluster_id: The cluster ID in the Databricks Runtime. If running in a Databricks notebook and both ‘warehouse_id’ and ‘cluster_id’ are None, it uses the ID of the cluster the notebook is attached to.
engine_args: The arguments to be used when connecting Databricks.
**kwargs: Additional keyword arguments for the SQLDatabase.from_uri method.
Examples#
# Connecting to Databricks with SQLDatabase wrapper
from langchain import SQLDatabase
db = SQLDatabase.from_databricks(catalog='samples', schema='nyctaxi')
# Creating a OpenAI Chat LLM wrapper
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(temperature=0, model_name="gpt-4")
SQL Chain example#
This example demonstrates the use of the SQL Chain for answering a question over a Databricks database.
from langchain import SQLDatabaseChain
db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)
db_chain.run("What is the average duration of taxi rides that start between midnight and 6am?")
> Entering new SQLDatabaseChain chain...
What is the average duration of taxi rides that start between midnight and 6am?
SQLQuery:SELECT AVG(UNIX_TIMESTAMP(tpep_dropoff_datetime) - UNIX_TIMESTAMP(tpep_pickup_datetime)) as avg_duration
FROM trips
WHERE HOUR(tpep_pickup_datetime) >= 0 AND HOUR(tpep_pickup_datetime) < 6
SQLResult: [(987.8122786304605,)]
Answer:The average duration of taxi rides that start between midnight and 6am is 987.81 seconds.
> Finished chain.
'The average duration of taxi rides that start between midnight and 6am is 987.81 seconds.'
rtdocs_stable/api.python.langchain.com/en/stable/integrations/databricks/databricks.html