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.
DatabricksEmbeddings
supports all methods of Embeddings
class including async APIs.
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:
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"
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
Instantiation
from databricks_langchain import DatabricksEmbeddings
embeddings = DatabricksEmbeddings(
endpoint="databricks-bge-large-en",
)
Indexing and Retrieval
Embedding models are often used in retrieval-augmented generation (RAG) flows, both as part of indexing data as well as later retrieving it. For more detailed instructions, please see our RAG tutorials.
Below, see how to index and retrieve data using the embeddings
object we initialized above. In this example, we will index and retrieve a sample document in the InMemoryVectorStore
.
from langchain_core.vectorstores import InMemoryVectorStore
text = "LangChain is the framework for building context-aware reasoning applications"
vectorstore = InMemoryVectorStore.from_texts(
[text],
embedding=embeddings,
)
retriever = vectorstore.as_retriever()
retrieved_document = retriever.invoke("What is LangChain?")
retrieved_document[0].page_content
Direct Usage
Under the hood, the vectorstore and retriever implementations are calling embeddings.embed_documents(...)
and embeddings.embed_query(...)
to create embeddings for the text(s) used in from_texts
and retrieval invoke
operations, respectively.
You can directly call these methods to get embeddings for your own use cases.
Embed single textsYou can embed single texts or documents with embed_query
:
single_vector = embeddings.embed_query(text)
print(str(single_vector)[:100])
Embed multiple texts
You can embed multiple texts with embed_documents
:
text2 = (
"LangGraph is a library for building stateful, multi-actor applications with LLMs"
)
two_vectors = embeddings.embed_documents([text, text2])
for vector in two_vectors:
print(str(vector)[:100])
Async Usage
You can also use aembed_query
and aembed_documents
for producing embeddings asynchronously:
import asyncio
async def async_example():
single_vector = await embeddings.aembed_query(text)
print(str(single_vector)[:100])
asyncio.run(async_example())
API Reference
For detailed documentation on DatabricksEmbeddings
features and configuration options, please refer to the API reference.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4