vdb/README.md

138 lines
3.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# VDB - Vector Database Service
基于 ahserver + pymilvus 的向量数据库 HTTP 服务,支持 Milvus Lite 和 Milvus Standalone 两种模式。
## 特性
- 支持 Milvus Lite本地文件和 Milvus StandaloneDocker 部署,千万级)
- COSINE / IP / L2 三种距离度量
- HNSW / IVF_FLAT / IVF_PQ 索引类型可配
- 批量插入batch_insert和批量搜索batch_query
- 向量搜索 + 标量过滤组合查询
- 集合管理list/stats/drop
## 部署
```bash
cd /data/ymq/vdb
bash build.sh deploy # 启动
bash build.sh stop # 停止
bash build.sh status # 状态
```
### 切换到 Milvus Standalone千万级
编辑 `conf/config.json`
```json
{
"milvus_mode": "standalone",
"milvus_host": "127.0.0.1:19530"
}
```
部署 Milvus StandaloneDocker
```yaml
# docker-compose.yml
services:
etcd:
image: quay.io/coreos/etcd:v3.5.18
environment:
- ETCD_AUTO_COMPACTION_MODE=revision
- ETCD_AUTO_COMPACTION_RETENTION=1000
volumes: ["etcd:/etcd"]
command: etcd --advertise-client-urls=http://127.0.0.1:2379 --listen-client-urls=http://0.0.0.0:2379 --data-dir=/etcd
minio:
image: minio/minio:latest
environment:
MINIO_ACCESS_KEY: minioadmin
MINIO_SECRET_KEY: minioadmin
volumes: ["minio:/minio_data"]
command: minio server /minio_data --console-address ":9001"
milvus:
image: milvusdb/milvus:v2.4-latest
depends_on: [etcd, minio]
ports: ["19530:19530", "9091:9091"]
volumes: ["milvus:/var/lib/milvus"]
environment:
ETCD_ENDPOINTS: etcd:2379
MINIO_ADDRESS: minio:9000
```
## API 接口
### GET /v1/listcollections
列出所有集合。
### POST /v1/createcollection
创建集合。
```json
{
"colname": "entities",
"fields": [
{"name": "id", "type": "str", "max_length": 32, "is_primary": true},
{"name": "embedding", "type": "fvector", "dim": 1024}
],
"metric": "COSINE",
"index_type": "HNSW"
}
```
### POST /v1/collectionstats
获取集合统计信息(行数、字段、索引)。
### POST /v1/upsert
插入或更新(自动删除旧记录再插入,适合少量)。
### POST /v1/batchinsert
批量插入不逐条flush适合大批量入库
```json
{"colname": "entities", "data": [...], "flush": true}
```
### POST /v1/query
向量搜索 + 标量过滤 + 分页。
```json
{
"colname": "entities",
"vector": [0.1, 0.2, ...],
"expr": "type == \"animal\"",
"pagerows": 20,
"page": 1,
"metric": "COSINE"
}
```
### POST /v1/batchquery
批量向量搜索(多个查询向量同时搜索)。
### POST /v1/delete
按主键删除。
### POST /v1/dropcollection
删除整个集合。
## 字段类型
| 类型 | 说明 | 备注 |
|------|------|------|
| str | 字符串 | 需要 max_length |
| int | 32位整数 | |
| int64 | 64位整数 | |
| bool | 布尔值 | |
| float | 浮点数 | |
| fvector | 浮点向量 | 需要 dim |
| bvector | 二进制向量 | 需要 dim |
| json | JSON | 灵活结构 |
## 配置 (conf/config.json)
| 字段 | 默认值 | 说明 |
|------|--------|------|
| milvus_mode | lite | lite 或 standalone |
| milvus_db | $[workdir]$/db/milvus.db | lite模式文件路径 |
| milvus_host | 127.0.0.1:19530 | standalone模式地址 |
| milvus_metric | COSINE | 默认距离度量 |
| client_max_size | 100MB | 请求体大小上限 |