Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
OurBuilding Ambient Agents with LangGraph course is now available on LangChain Academy!
Open In ColabOpen on GitHub

DatabricksEmbeddings

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

This notebook provides a quick overview for getting started with Databricksembedding models. For detailed documentation of allDatabricksEmbeddings features and configurations head to theAPI reference.

Overview

Integration details

ClassPackage
DatabricksEmbeddingsdatabricks-langchain

Supported Methods

DatabricksEmbeddings supports all methods ofEmbeddings class including async APIs.

Endpoint Requirement

The serving endpointDatabricksEmbeddings 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 onDatabricks 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 withyour 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 toDATABRICKS_HOST andDATABRICKS_TOKEN environment variables, respectively. SeeAuthentication 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"notin os.environ:
os.environ["DATABRICKS_TOKEN"]= getpass.getpass(
"Enter your Databricks access token: "
)

Installation

The LangChain Databricks integration lives in thedatabricks-langchain package:

%pip install-qU databricks-langchain

Instantiation

from databricks_langchainimport DatabricksEmbeddings

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

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 ourRAG tutorials.

Below, see how to index and retrieve data using theembeddings object we initialized above. In this example, we will index and retrieve a sample document in theInMemoryVectorStore.

# Create a vector store with a sample text
from langchain_core.vectorstoresimport InMemoryVectorStore

text="LangChain is the framework for building context-aware reasoning applications"

vectorstore= InMemoryVectorStore.from_texts(
[text],
embedding=embeddings,
)

# Use the vectorstore as a retriever
retriever= vectorstore.as_retriever()

# Retrieve the most similar text
retrieved_document= retriever.invoke("What is LangChain?")

# show the retrieved document's content
retrieved_document[0].page_content
API Reference:InMemoryVectorStore

Direct Usage

Under the hood, the vectorstore and retriever implementations are callingembeddings.embed_documents(...) andembeddings.embed_query(...) to create embeddings for the text(s) used infrom_texts and retrievalinvoke operations, respectively.

You can directly call these methods to get embeddings for your own use cases.

Embed single texts

You can embed single texts or documents withembed_query:

single_vector= embeddings.embed_query(text)
print(str(single_vector)[:100])# Show the first 100 characters of the vector

Embed multiple texts

You can embed multiple texts withembed_documents:

text2=(
"LangGraph is a library for building stateful, multi-actor applications with LLMs"
)
two_vectors= embeddings.embed_documents([text, text2])
for vectorin two_vectors:
print(str(vector)[:100])# Show the first 100 characters of the vector

Async Usage

You can also useaembed_query andaembed_documents for producing embeddings asynchronously:

import asyncio


asyncdefasync_example():
single_vector=await embeddings.aembed_query(text)
print(str(single_vector)[:100])# Show the first 100 characters of the vector


asyncio.run(async_example())

API Reference

For detailed documentation onDatabricksEmbeddings features and configuration options, please refer to theAPI reference.

Related


[8]ページ先頭

©2009-2025 Movatter.jp