Skip to main content

Databricks

Databricks Lakehouse Platform unifies data, analytics, and AI on one platform.

This notebook provides a quick overview for getting started with Databricks embedding models. For detailed documentation of all DatabricksEmbeddings features and configurations head to the API reference.

Overviewโ€‹

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

Supported Methodsโ€‹

DatabricksEmbeddings supports all methods of Embeddings class including async APIs.

Endpoint Requirementโ€‹

The serving endpoint DatabricksEmbeddings wraps must have OpenAI-compatible embedding input/output format (reference). As long as the input format is compatible, DatabricksEmbeddings 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 BAAI General Embedding (BGE). These endpoint are ready to use in your Databricks workspace without any set up.
  2. Custom Models - You can also deploy custom embedding 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 text-embedding-3.

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.

import getpass
import os

os.environ["DATABRICKS_HOST"] = "https://your-workspace.cloud.databricks.com"
os.environ["DATABRICKS_TOKEN"] = getpass.getpass("Enter your Databricks access token: ")

Installationโ€‹

The LangChain Databricks integration lives in the langchain-community package. Also, mlflow >= 2.9 is required to run the code in this notebook.

%pip install -qU langchain-community mlflow>=2.9.0

We first demonstrates how to query BGE model hosted as Foundation Models endpoint with DatabricksEmbeddings.

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.

Instantiationโ€‹

from langchain_community.embeddings import DatabricksEmbeddings

embeddings = DatabricksEmbeddings(
endpoint="databricks-bge-large-en",
# Specify parameters for embedding queries and documents if needed
# query_params={...},
# document_params={...},
)
API Reference:DatabricksEmbeddings

Embed single textโ€‹

embeddings.embed_query("hello")[:3]
[0.051055908203125, 0.007221221923828125, 0.003879547119140625]

Embed documentsโ€‹

documents = ["This is a dummy document.", "This is another dummy document."]
response = embeddings.embed_documents(documents)
print([e[:3] for e in response]) # Show first 3 elements of each embedding

Wrapping Other Types of Endpointsโ€‹

The example above uses an embedding model hosted as a Foundation Models API. To learn about how to use the other endpoint types, please refer to the documentation for ChatDatabricks. While the model type is different, required steps are the same.

API referenceโ€‹

For detailed documentation of all ChatDatabricks features and configurations head to the API reference: https://api.python.langchain.com/en/latest/embeddings/langchain_community.embeddings.databricks.DatabricksEmbeddings.html


Was this page helpful?


You can leave detailed feedback on GitHub.