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.agents import create_sql_agent
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase
from databricks_langchain import ChatDatabricks
from databricks.sdk import WorkspaceClient
This exercise is part of the course
Databricks with the Python SDK
Exercise instructions
- Create a LangChain SQLDatabase from the
nyctaxischema 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
dband thellm. - Create a LangChain Databricks SQL agent using the
toolkitandllm.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a LangChain SQLDatabase from the "nyctaxi" schema in the "samples" databricks catalog
db = SQLDatabase.____(
catalog="samples",
schema="nyctaxi",
warehouse_id=warehouse_id)
# Create a Databricks LangChain LLM using the Databricks Meta Llama 3 Foundational Model
llm = ____(
endpoint="databricks-meta-llama-3-3-70b-instruct",
max_tokens=100)
# Create a LangChain SQL Database toolkit that uses the LangChain Databricks SQL db and the llm
toolkit = ____(db=db, llm=llm)
# Create a langchain databricks sql agent using the toolkit and llm created above
databricks_sql_agent = ____(llm=llm, toolkit=toolkit, verbose=True, handle_parsing_errors=True)