> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# Databricks vector search 集成

> 使用 LangChain Python 与 Databricks Vector Search 向量存储进行集成。

[Databricks Vector Search](https://docs.databricks.com/en/generative-ai/vector-search.html) 是一个无服务器相似度搜索引擎，允许您将数据的向量表示（包括元数据）存储在向量数据库中。通过 Vector Search，您可以从 Unity Catalog 管理的 Delta 表中创建自动更新的向量搜索索引，并使用简单的 API 查询它们以返回最相似的向量。

本笔记本演示了如何在 Databricks Vector Search 中使用 LangChain。

## 设置

要访问 Databricks 模型，您需要创建一个 Databricks 账户，设置凭据（如果您在 Databricks 工作区之外），并安装所需的包。

### 凭据（仅当您不在 Databricks 内部时）

如果您在 Databricks 内部运行 LangChain 应用，可以跳过此步骤。

否则，您需要手动将 Databricks 工作区主机名和个人访问令牌分别设置为 `DATABRICKS_HOST` 和 `DATABRICKS_TOKEN` 环境变量。请参阅 [身份验证文档](https://docs.databricks.com/en/dev-tools/auth/index.html#databricks-personal-access-tokens) 了解如何获取访问令牌。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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: "
    )
```

### 安装

LangChain Databricks 集成位于 `databricks-langchain` 包中。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU databricks-langchain
```

### 创建向量搜索端点和索引（如果尚未创建）

在本节中，我们将使用客户端 SDK 创建 Databricks Vector Search 端点和索引。

如果您已经有了端点和索引，可以跳过本节，直接进入“实例化”部分。

首先，实例化 Databricks VectorSearch 客户端：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from databricks.vector_search.client import VectorSearchClient

client = VectorSearchClient()
```

接下来，我们将创建一个新的 VectorSearch 端点。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
endpoint_name = "<your-endpoint-name>"

client.create_endpoint(name=endpoint_name, endpoint_type="STANDARD")
```

最后，我们将创建一个可以在端点上查询的索引。Databricks Vector Search 中有两种类型的索引，`DatabricksVectorSearch` 类支持这两种用例。

* **Delta Sync Index** 会自动与源 Delta 表同步，随着 Delta 表底层数据的变化自动增量更新索引。

* **Direct Vector Access Index** 支持向量和元数据的直接读写。用户负责使用 REST API 或 Python SDK 更新此表。

此外，对于 delta-sync 索引，您可以选择使用 Databricks 托管的嵌入或自托管嵌入（通过 LangChain 嵌入类）。

以下代码创建一个 **直接访问** 索引。有关创建其他类型索引的说明，请参阅 [Databricks 文档](https://docs.databricks.com/en/generative-ai/create-query-vector-search.html)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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()
```

## 实例化

`DatabricksVectorSearch` 的实例化略有不同，取决于您的索引是使用 Databricks 托管的嵌入还是自托管嵌入，即您选择的 LangChain Embeddings 对象。

如果您使用的是带有 Databricks 托管嵌入的 delta-sync 索引：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from databricks_langchain import DatabricksVectorSearch

vector_store = DatabricksVectorSearch(
    endpoint=endpoint_name,
    index_name=index_name,
)
```

如果您使用的是直接访问索引或带有自托管嵌入的 delta-sync 索引，您还需要提供嵌入模型和源表中的文本列以用于嵌入：

<EmbeddingTabs />

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# | output: false
# | echo: false
from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
vector_store = DatabricksVectorSearch(
    endpoint=endpoint_name,
    index_name=index_name,
    embedding=embeddings,
    # The column name in the index that contains the text data to be embedded
    text_column="document_content",
)
```

## 管理向量存储

### 向向量存储添加项

注意：通过 `add_documents` 方法向向量存储添加项仅支持 **直接访问** 索引。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_core.documents import Document

document_1 = Document(page_content="foo", metadata={"source": "https://example.com"})

document_2 = Document(page_content="bar", metadata={"source": "https://example.com"})

document_3 = Document(page_content="baz", metadata={"source": "https://example.com"})

documents = [document_1, document_2, document_3]

vector_store.add_documents(documents=documents, ids=["1", "2", "3"])
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
['1', '2', '3']
```

### 从向量存储删除项

注意：通过 `delete` 方法从向量存储删除项仅支持 **直接访问** 索引。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
vector_store.delete(ids=["3"])
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
True
```

## 查询向量存储

一旦创建了向量存储并添加了相关文档，您很可能希望在运行链或代理期间对其进行查询。

### 直接查询

执行简单的相似度搜索可以如下所示：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
results = vector_store.similarity_search(
    query="thud", k=1, filter={"source": "https://example.com"}
)
for doc in results:
    print(f"* {doc.page_content} [{doc.metadata}]")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
* foo [{'id': '1'}]
```

注意：默认情况下，相似度搜索仅返回主键和文本列。如果您想检索与文档关联的自定义元数据，请在初始化向量存储时在 `columns` 参数中传递额外的列。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
vector_store = DatabricksVectorSearch(
    endpoint=endpoint_name,
    index_name=index_name,
    embedding=embeddings,
    text_column="text",
    columns=["source"],
)

results = vector_store.similarity_search(query="thud", k=1)
for doc in results:
    print(f"* {doc.page_content} [{doc.metadata}]")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
* foo [{'source': 'https://example.com', 'id': '1'}]
```

如果您想执行相似度搜索并接收相应的分数，可以运行：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
results = vector_store.similarity_search_with_score(
    query="thud", k=1, filter={"source": "https://example.com"}
)
for doc, score in results:
    print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
* [SIM=0.414035] foo [{'source': 'https://example.com', 'id': '1'}]
```

### 转换为检索器进行查询

您还可以将向量存储转换为检索器，以便在链中更轻松地使用。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
retriever = vector_store.as_retriever(search_type="mmr", search_kwargs={"k": 1})
retriever.invoke("thud")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[Document(metadata={'source': 'https://example.com', 'id': '1'}, page_content='foo')]
```

## 检索增强生成 (RAG) 用法

关于如何使用此向量存储进行检索增强生成 (RAG) 的指南，请参见以下部分：

* [教程](/oss/python/langchain/rag)
* [操作指南：使用 RAG 问答](https://python.langchain.com/docs/how_to/#qa-with-rag)
* [检索概念文档](https://python.langchain.com/docs/concepts/retrieval)

***

## API 参考

有关所有 `DatabricksVectorSearch` 功能和配置的详细文档，请访问 API 参考：[api-docs.databricks.com/python/databricks-ai-bridge/latest/databricks\_langchain.html#databricks\_langchain.DatabricksVectorSearch](https://api-docs.databricks.com/python/databricks-ai-bridge/latest/databricks_langchain.html#databricks_langchain.DatabricksVectorSearch)

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\vectorstores\databricks_vector_search.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
