DatabricksVectorSearch
Databricks Vector Search is a serverless similarity search engine that allows you to store a vector representation of your data, including metadata, in a vector database. With Vector Search, you can create auto-updating vector search indexes from Delta tables managed by Unity Catalog and query them with a simple API to return the most similar vectors.
This notebook shows how to use LangChain with Databricks Vector Search.
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-databricks-workspace"
if "DATABRICKS_TOKEN" not in os.environ:
os.environ["DATABRICKS_TOKEN"] = getpass.getpass(
"Enter your Databricks access token: "
)
Installation
The LangChain Databricks integration lives in the databricks-langchain
package.
%pip install -qU databricks-langchain
Create a Vector Search Endpoint and Index (if you haven't already)
In this section, we will create a Databricks Vector Search endpoint and an index using the client SDK.
If you already have an endpoint and an index, you can skip the section and go straight to "Instantiation" section.
First, instantiate the Databricks VectorSearch client:
from databricks.vector_search.client import VectorSearchClient
client = VectorSearchClient()
Next, we will create a new VectorSearch endpoint.
endpoint_name = "<your-endpoint-name>"
client.create_endpoint(name=endpoint_name, endpoint_type="STANDARD")
Lastly, we will create an index that can be queried on the endpoint. There are two types of indexes in Databricks Vector Search and the DatabricksVectorSearch
class support both use cases.
-
Delta Sync Index automatically syncs with a source Delta Table, automatically and incrementally updating the index as the underlying data in the Delta Table changes.
-
Direct Vector Access Index supports direct read and write of vectors and metadata. The user is responsible for updating this table using the REST API or the Python SDK.
Also for delta-sync index, you can choose to use Databricks-managed embeddings or self-managed embeddings (via LangChain embeddings classes).
The following code creates a direct-access index. Please refer to the Databricks documentation for the instruction to create the other type of indexes.
index_name = "<your-index-name>" # Format: "<catalog>.<schema>.<index-name>"
index = client.create_direct_access_index(
endpoint_name=endpoint_name,
index_name=index_name,
primary_key="id",
# Dimension of the embeddings. Please change according to the embedding model you are using.
embedding_dimension=3072,
# A column to store the embedding vectors for the text data
embedding_vector_column="text_vector",
schema={
"id": "string",
"text": "string",
"text_vector": "array<float>",
# Optional metadata columns
"source": "string",
},
)
index.describe()
Instantiation
The instantiation of DatabricksVectorSearch
is a bit different depending on whether your index uses Databricks-managed embeddings or self-managed embeddings i.e. LangChain Embeddings object of your choice.
If you are using a delta-sync index with Databricks-managed embeddings:
from databricks_langchain import DatabricksVectorSearch
vector_store = DatabricksVectorSearch(
endpoint=endpoint_name,
index_name=index_name,
)
If you are using a direct-access index or a delta-sync index with self-managed embeddings, you also need to provide the embedding model and text column in your source table to use for the embeddings: