Get startedGet started for free

Create LangChain SQL Agent Connected to Databricks

LangChain is a library to build and deploy LLM applications. It also allows you to create AI agents, connecting connect to data sources and running SQL queries.

In the following exercises, you will be interacting with schemas from the samples catalog. In this exercise, you will create a Databricks SQL agent and in subsequent exercises you will also query the agents that you create.

We import the following functions into the coding environment for you:

from langchain_community.agent_toolkits import create_sql_agent
from langchain_community.agent_toolkits import SQLDatabaseToolkit
from langchain_community.utilities import SQLDatabase
from databricks_langchain import ChatDatabricks
from databricks.sdk import WorkspaceClient

This exercise is part of the course

Databricks with the Python SDK

View Course

Exercise instructions

  • Create a LangChain SQLDatabase from the nyctaxi schema in the "samples" Databricks catalog.
  • Create a Databricks LangChain LLM using the Databricks Meta Llama 3 Foundational Model.
  • Create a LangChain SQL Database toolkit that uses the LangChain Databricks SQL db and the llm.
  • Create a LangChain Databricks SQL agent using the toolkit and llm.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Connect to the nyctaxi schema
db = SQLDatabase.____(
    catalog="samples", 
    schema="nyctaxi",
    warehouse_id=warehouse_id)

# Create the LangChain LLM
llm = ____(
    endpoint="databricks-meta-llama-3-3-70b-instruct",
    max_tokens=100)

# Build the SQL toolkit
toolkit = ____(db=db, llm=llm) 

# Create the SQL agent
databricks_sql_agent = ____(llm=llm, toolkit=toolkit, verbose=True, handle_parsing_errors=True)
Edit and Run Code