mirror of
https://github.com/hwchase17/langchain.git
synced 2026-02-21 06:33:41 +00:00
### Summary Add `DatabricksVectorSearch` and `DatabricksEmbeddings` classes to the `langchain-databricks` partner packages. Core functionality is unchanged, but the vector search class is largely refactored for readability and maintainability. This PR does not add integration tests yet. This will be added once the Databricks test workspace is ready. Tagging @efriis as POC ### Tracker [✅] Create a package and imgrate ChatDatabricks [✍️] Migrate DatabricksVectorSearch, DatabricksEmbeddings, and their docs ~[ ] Migrate UCFunctionToolkit and its doc~ [ ] Add provider document and update README.md [ ] Add integration tests and set up secrets (after moved to an external package) [ ] Add deprecation note to the community implementations. --------- Signed-off-by: B-Step62 <yuki.watanabe@databricks.com> Co-authored-by: Erick Friis <erick@langchain.dev>
270 lines
8.9 KiB
Plaintext
270 lines
8.9 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "raw",
|
|
"id": "afaf8039",
|
|
"metadata": {},
|
|
"source": [
|
|
"---\n",
|
|
"sidebar_label: Databricks\n",
|
|
"---"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9a3d6f34",
|
|
"metadata": {},
|
|
"source": [
|
|
"# DatabricksEmbeddings\n",
|
|
"\n",
|
|
"> [Databricks](https://www.databricks.com/) Lakehouse Platform unifies data, analytics, and AI on one platform.\n",
|
|
"\n",
|
|
"This notebook provides a quick overview for getting started with Databricks [embedding models](/docs/concepts/#embedding-models). For detailed documentation of all `DatabricksEmbeddings` features and configurations head to the [API reference](https://python.langchain.com/v0.2/api_reference/community/embeddings/langchain_community.embeddings.databricks.DatabricksEmbeddings.html).\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Overview\n",
|
|
"### Integration details\n",
|
|
"\n",
|
|
"| Class | Package |\n",
|
|
"| :--- | :--- |\n",
|
|
"| [DatabricksEmbeddings](https://api.python.langchain.com/en/latest/embeddings/langchain_databricks.embeddings.DatabricksEmbeddings.html) | [langchain-databricks](https://api.python.langchain.com/en/latest/databricks_api_reference.html) |\n",
|
|
"\n",
|
|
"### Supported Methods\n",
|
|
"\n",
|
|
"`DatabricksEmbeddings` supports all methods of `Embeddings` class including async APIs.\n",
|
|
"\n",
|
|
"\n",
|
|
"### Endpoint Requirement\n",
|
|
"\n",
|
|
"The serving endpoint `DatabricksEmbeddings` wraps must have OpenAI-compatible embedding input/output format ([reference](https://mlflow.org/docs/latest/llms/deployments/index.html#embeddings)). As long as the input format is compatible, `DatabricksEmbeddings` can be used for any endpoint type hosted on [Databricks Model Serving](https://docs.databricks.com/en/machine-learning/model-serving/index.html):\n",
|
|
"\n",
|
|
"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.\n",
|
|
"2. Custom Models - You can also deploy custom embedding models to a serving endpoint via MLflow with\n",
|
|
"your choice of framework such as LangChain, Pytorch, Transformers, etc.\n",
|
|
"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.\n",
|
|
"\n",
|
|
"\n",
|
|
"## Setup\n",
|
|
"\n",
|
|
"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.\n",
|
|
"\n",
|
|
"### Credentials (only if you are outside Databricks)\n",
|
|
"\n",
|
|
"If you are running LangChain app inside Databricks, you can skip this step.\n",
|
|
"\n",
|
|
"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](https://docs.databricks.com/en/dev-tools/auth/index.html#databricks-personal-access-tokens) for how to get an access token."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "36521c2a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import getpass\n",
|
|
"import os\n",
|
|
"\n",
|
|
"os.environ[\"DATABRICKS_HOST\"] = \"https://your-workspace.cloud.databricks.com\"\n",
|
|
"os.environ[\"DATABRICKS_TOKEN\"] = getpass.getpass(\"Enter your Databricks access token: \")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d9664366",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Installation\n",
|
|
"\n",
|
|
"The LangChain Databricks integration lives in the `langchain-databricks` package:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "64853226",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%pip install -qU langchain-databricks"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "45dd1724",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Instantiation"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "9ea7a09b",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain_databricks import DatabricksEmbeddings\n",
|
|
"\n",
|
|
"embeddings = DatabricksEmbeddings(\n",
|
|
" endpoint=\"databricks-bge-large-en\",\n",
|
|
" # Specify parameters for embedding queries and documents if needed\n",
|
|
" # query_params={...},\n",
|
|
" # document_params={...},\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "77d271b6",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Indexing and Retrieval\n",
|
|
"\n",
|
|
"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 under the [working with external knowledge tutorials](/docs/tutorials/#working-with-external-knowledge).\n",
|
|
"\n",
|
|
"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`."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "d817716b",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Create a vector store with a sample text\n",
|
|
"from langchain_core.vectorstores import InMemoryVectorStore\n",
|
|
"\n",
|
|
"text = \"LangChain is the framework for building context-aware reasoning applications\"\n",
|
|
"\n",
|
|
"vectorstore = InMemoryVectorStore.from_texts(\n",
|
|
" [text],\n",
|
|
" embedding=embeddings,\n",
|
|
")\n",
|
|
"\n",
|
|
"# Use the vectorstore as a retriever\n",
|
|
"retriever = vectorstore.as_retriever()\n",
|
|
"\n",
|
|
"# Retrieve the most similar text\n",
|
|
"retrieved_document = retriever.invoke(\"What is LangChain?\")\n",
|
|
"\n",
|
|
"# show the retrieved document's content\n",
|
|
"retrieved_document[0].page_content"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e02b9855",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Direct Usage\n",
|
|
"\n",
|
|
"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.\n",
|
|
"\n",
|
|
"You can directly call these methods to get embeddings for your own use cases.\n",
|
|
"\n",
|
|
"### Embed single texts\n",
|
|
"\n",
|
|
"You can embed single texts or documents with `embed_query`:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "0d2befcd",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"single_vector = embeddings.embed_query(text)\n",
|
|
"print(str(single_vector)[:100]) # Show the first 100 characters of the vector"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1b5a7d03",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Embed multiple texts\n",
|
|
"\n",
|
|
"You can embed multiple texts with `embed_documents`:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "2f4d6e97",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"text2 = (\n",
|
|
" \"LangGraph is a library for building stateful, multi-actor applications with LLMs\"\n",
|
|
")\n",
|
|
"two_vectors = embeddings.embed_documents([text, text2])\n",
|
|
"for vector in two_vectors:\n",
|
|
" print(str(vector)[:100]) # Show the first 100 characters of the vector"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "98785c12",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Async Usage\n",
|
|
"\n",
|
|
"You can also use `aembed_query` and `aembed_documents` for producing embeddings asynchronously:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "4c3bef91",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import asyncio\n",
|
|
"\n",
|
|
"\n",
|
|
"async def async_example():\n",
|
|
" single_vector = await embeddings.aembed_query(text)\n",
|
|
" print(str(single_vector)[:100]) # Show the first 100 characters of the vector\n",
|
|
"\n",
|
|
"\n",
|
|
"asyncio.run(async_example())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "0d053b64",
|
|
"metadata": {},
|
|
"source": [
|
|
"## API Reference\n",
|
|
"\n",
|
|
"For detailed documentation on `DatabricksEmbeddings` features and configuration options, please refer to the [API reference](https://python.langchain.com/v0.2/api_reference/community/embeddings/langchain_community.embeddings.databricks.DatabricksEmbeddings.html).\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.5"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|