Skip to main content
Databricks Lakehouse Platform unifies data, analytics, and AI on one platform.
This guide provides a quick overview for getting started with Databricks chat models.

Overview

ChatDatabricks class wraps a chat model endpoint hosted on Databricks Model Serving. This example notebook shows how to wrap your serving endpoint and use it as a chat model in your LangChain application.

Integration details

Model features

Supported methods

ChatDatabricks supports all methods of ChatModel including async APIs.

Endpoint requirement

The serving endpoint ChatDatabricks wraps must have OpenAI-compatible chat input/output format (reference). As long as the input format is compatible, ChatDatabricks can be used for any endpoint type hosted on Databricks Model Serving:
  1. Foundation Models - Curated list of state-of-the-art foundation models such as DRBX, Llama3, Mixtral-8x7B, and etc. These endpoint are ready to use in your Databricks workspace without any set up.
  2. Custom Models - You can also deploy custom models to a serving endpoint via MLflow with your choice of framework such as LangChain, Pytorch, Transformers, etc.
  3. External Models - Databricks endpoints can serve models that are hosted outside Databricks as a proxy, such as proprietary model service like OpenAI GPT4.

Setup

To access Databricks models you’ll need to create a Databricks account, set up credentials (only if you are outside Databricks workspace), and install required packages.

Credentials (only if you are outside databricks)

If you are running LangChain app inside Databricks, you can skip this step. Otherwise, you need manually set the Databricks workspace hostname and personal access token to DATABRICKS_HOST and DATABRICKS_TOKEN environment variables, respectively. See Authentication Documentation for how to get an access token.

Installation

The LangChain Databricks integration lives in the databricks-langchain package.
We first demonstrates how to query DBRX-instruct model hosted as Foundation Models endpoint with ChatDatabricks. For other type of endpoints, there are some difference in how to set up the endpoint itself, however, once the endpoint is ready, there is no difference in how to query it with ChatDatabricks. Please refer to the bottom of this notebook for the examples with other type of endpoints.

Instantiation

Invocation

Chaining

Similar to other chat models, ChatDatabricks can be used as a part of a complex chain.

Invocation (streaming)

Async invocation

Tool calling

ChatDatabricks supports OpenAI-compatible tool calling API that lets you describe tools and their arguments, and have the model return a JSON object with a tool to invoke and the inputs to that tool. tool-calling is extremely useful for building tool-using chains and agents, and for getting structured outputs from models more generally. With ChatDatabricks.bind_tools, we can easily pass in Pydantic classes, dict schemas, LangChain tools, or even functions as tools to the model. Under the hood these are converted to the OpenAI-compatible tool schemas, which looks like:
and passed in every model invocation.

Wrapping custom model endpoint

Prerequisites: Once the endpoint is ready, the usage pattern is identical to that of Foundation Models.

Wrapping external models

Prerequisite: Create Proxy Endpoint First, create a new Databricks serving endpoint that proxies requests to the target external model. The endpoint creation should be fairy quick for proxying external models. This requires registering your OpenAI API Key within the Databricks secret manager as follows:
For how to set up Databricks CLI and manage secrets, please refer to docs.databricks.com/en/security/secrets/secrets.html
Once the endpoint status has become “Ready”, you can query the endpoint in the same way as other types of endpoints.

Function calling on databricks

Databricks Function Calling is OpenAI-compatible and is only available during model serving as part of Foundation Model APIs. See Databricks function calling introduction for supported models.
See Databricks Unity Catalog about how to use UC functions in chains.

API reference

For detailed documentation of all ChatDatabricks features and configurations head to the API reference: api-docs.databricks.com/python/databricks-ai-bridge/latest/databricks_langchain.html#databricks_langchain.ChatDatabricks