openviking/storage/vectordb_adapters/README.md
本指南说明如何在 openviking/storage/vectordb_adapters 下新增一个第三方向量库后端,并接入 OpenViking 现有检索链路。
find/search 等无需改调用方式)。当前分层职责如下:
上层语义层(OpenViking 业务)
面向语义接口,不关心后端协议差异。
通用向量存储层(Store/Backend)
提供统一查询、写入、删除、计数能力。
Adapter 层(本目录)
负责把统一能力映射到具体后端实现(local/http/volcengine/vikingdb/thirdparty)。
新增后端时,主要只改第 3 层。
在开始前,请确认:
在目录下新增文件,例如:
openviking/storage/vectordb_adapters/thirdparty_adapter.py定义类:
ThirdPartyCollectionAdapter(CollectionAdapter)基类位于:
openviking/storage/vectordb_adapters/base.py你需要实现以下方法:
from_config(cls, config)
VectorDBBackendConfig 读取后端配置并构造 adapter。config.name or "context"。_load_existing_collection_if_needed(self)
_collection is None。_create_backend_collection(self, meta)
如后端有差异,可重写:
_sanitize_scalar_index_fields(...)_build_default_index_meta(...)若后端支持服务端全文检索(BM25 grep)并需要写入 content 全文字段,设置类属性:
USE_CONTENT_FIELD = True(默认为 False)默认 False 时,写入时会自动跳过 content 字段(该字段仅 VikingDB 系后端使用),新增后端无需额外代码。
目的:把后端特性差异收敛在 adapter 内。
编辑:
openviking/storage/vectordb_adapters/factory.py在 _ADAPTER_REGISTRY 增加映射,例如:
"thirdparty": ThirdPartyCollectionAdapter
这样 create_collection_adapter(config) 会自动路由到你的实现。
确保配置中可声明新 backend(如 backend: thirdparty)及其专属字段(endpoint/auth/region 等)。
原则:
create_collection 时使用配置中的 name 绑定 collection。对于没有提交到仓库,或者在第三方仓库的 Adapter,可以通过配置 backend 为完整的类路径来动态加载。
同时,可以使用 custom_params 字段传递自定义参数。
在 ov.conf 中添加如下配置:
{
"storage": {
"vectordb": {
"backend": "tests.storage.mock_backend.MockCollectionAdapter",
"name": "mock_test_collection",
"custom_params": {
"custom_param1": "val1",
"custom_param2": 123
}
}
}
}
注意:
backend: 填写 Adapter 类的完整 Python 路径(例如 my_project.adapters.MyAdapter)。custom_params: 这是一个字典,你可以放入任何自定义参数,Adapter 的 from_config 方法可以通过 config.custom_params 获取这些值。关键原则:
from __future__ import annotations
from typing import Any, Dict
from openviking.storage.vectordb_adapters.base import CollectionAdapter
class ThirdPartyCollectionAdapter(CollectionAdapter):
def __init__(self, *, endpoint: str, token: str, collection_name: str):
super().__init__(collection_name=collection_name)
self.mode = "thirdparty"
self._endpoint = endpoint
self._token = token
@classmethod
def from_config(cls, config: Any):
if not config.thirdparty or not config.thirdparty.endpoint:
raise ValueError("ThirdParty backend requires endpoint")
return cls(
endpoint=config.thirdparty.endpoint,
token=config.thirdparty.token,
collection_name=config.name or "context",
)
def _load_existing_collection_if_needed(self) -> None:
if self._collection is not None:
return
# TODO: 查询远端 collection 是否存在,存在则初始化 handle
# self._collection = ...
pass
def _create_backend_collection(self, meta: Dict[str, Any]):
# TODO: 调后端 create collection,并返回 collection handle
# return ...
raise NotImplementedError
至少覆盖以下场景:
当满足以下条件,即可视为接入完成:
backend=thirdparty 可正常初始化。find/search 检索链路。