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

# 向量数据库集成

> 为 HeavenBase 工作区配置向量存储后端 (Backend)。

<Note>
  向量 (Vector) 字段通过与 SQL 与搜索字段相同的工作区存储规划器路由。
</Note>

## 1. 支持的向量后端 (Backend)

HeavenBase 包含以下向量后端 (Backend) 实现：

* `lance`
* `milvus`
* `pgvector`
* `chroma`
* `pinecone`
* `inmem`

测试使用 `inmem`，本地文件向量存储使用 `lance`，本地或远程 Milvus 服务使用 `milvus`，向量与 Postgres 行共存时使用 `pgvector`。

<br />

## 2. 预设 (Preset)

内置 `debug` 工作区预设 (Preset) 对行使用 SQLite，对向量与搜索使用内存后端 (Backend)：

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import heavenbase as hb

workspace = hb.HeavenBase("demo", preset="debug")
```

`local-lts` 预设 (Preset) 面向 Postgres 行、LanceDB 向量与 Elasticsearch 搜索的本地技术栈：

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
workspace = hb.HeavenBase("agent-shop", preset="local-lts")
```

Lance 路径在配置的 URI 中使用工作区 id，因此每个工作区获得隔离的向量目录。

<br />

## 3. 显式后端 (Backend) 配置

当测试或应用应避免全局预设 (Preset) 时，可直接传入后端 (Backend) 配置：

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
workspace = hb.HeavenBase(
    "vector-demo",
    backends={
        "main": {"type": "sqlite", "database": ":memory:"},
        "vec": {"type": "inmem", "name": "vec"},
    },
)
```

然后在普通实体中定义向量 (Vector) 字段：

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Issue(hb.Entity):
    title = hb.field(hb.ShortText)
    emb = hb.field(hb.Vector[3])
```

查询 (Query) 使用相同的 JSON 与 Python DSL 面：

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
workspace.query(Issue).near(Issue.emb, [1.0, 0.0, 0.0], top_k=5).execute()
```

<br />

## 进一步探索

<Tip>
  **相关资源：**

  * [后端 (Backend)](/zh/features/backends) — 后端能力与存储族
  * [路由 (Routing)](/zh/features/routing) — 字段放置如何规划
  * [查询 (Query)](/zh/features/query) — JSON 与 Python 查询 (Query) 面
  * [数据库集成](/zh/integrations/databases) — SQL 族提供商 (Provider) 配置
</Tip>

<br />
