Query NYC Taxi Dataset using SQL Agent
Databricks exposes an open source samples catalog that contains several schemas. In this exercise, we will create a LangChain Databricks SQL agent to connect to the nyctaxi schema and then ask it a question that requires SQL to answer. Creating one of these agents empowers the user to perform data analysis and ask questions on data that require SQL, with no prior knowledge of SQL.
This exercise is part of the course
Databricks with the Python SDK
Exercise instructions
- Create LangChain SQLDatabase object from
nyctaxischema in Databrickssamplescatalog. - Create a custom LangChain LLM using the Databricks Meta Llama 3 model.
- Create a LangChain Databricks SQL Agent using the custom LLM.
- Query the Databricks SQL Agent.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create LangChain SQLDatabase object from `nyctaxi` schema in Databricks `samples` catalog
db = SQLDatabase.from_databricks(
____="samples",
____="nyctaxi",
warehouse_id=warehouse_id)
# Create a custom Langchaim LLM using the Databricks Meta llama 3 model
llm = ChatDatabricks(
____="databricks-meta-llama-3-3-70b-instruct",
max_tokens=100)
toolkit = SQLDatabaseToolkit(db=db, llm=meta_llm)
# Create a LangChain Databricks SQL Agent using the custom LLM created above
agent = create_sql_agent(llm=____, toolkit=toolkit, verbose=True)
# Query the Databricks SQL Agent
result = agent.____("what's the duration and distance of the longest trip?")
print(result)