openviking/storage/vectordb/service/README_FASTAPI.md
重构后的 VikingDB Collection Server,使用 FastAPI 替代 Flask。
pip install fastapi uvicorn pydantic
cd openviking/storage/vectordb/service
python server_fastapi.py
cd openviking/storage/vectordb/service
uvicorn server_fastapi:app --host 0.0.0.0 --port 5000 --reload
VIKINGDB_PERSIST_PATH: 数据持久化路径,默认为 ./vikingdb_data/
示例:
export VIKINGDB_PERSIST_PATH="./my_data_path/"
python server_fastapi.py
FastAPI 自动生成交互式 API 文档:
POST /CreateVikingdbCollection - 创建 CollectionPOST /UpdateVikingdbCollection - 更新 CollectionGET /GetVikingdbCollection - 获取 Collection 信息GET /ListVikingdbCollection - 列出所有 CollectionsPOST /DeleteVikingdbCollection - 删除 CollectionPOST /api/vikingdb/data/upsert - 写入/更新数据GET /api/vikingdb/data/fetch_in_collection - 获取数据POST /api/vikingdb/data/delete - 删除数据POST /CreateVikingdbIndex - 创建索引POST /UpdateVikingdbIndex - 更新索引GET /GetVikingdbIndex - 获取索引信息GET /ListVikingdbIndex - 列出所有索引POST /DeleteVikingdbIndex - 删除索引POST /api/vikingdb/data/search/vector - 向量搜索POST /api/vikingdb/data/search/id - 通过 ID 搜索POST /api/vikingdb/data/search/multi_modal - 多模态搜索POST /api/vikingdb/data/search/scalar - 标量字段搜索POST /api/vikingdb/data/search/random - 随机搜索POST /api/vikingdb/data/search/keywords - 关键词搜索GET / - 根端点GET /health - 健康检查端点API 端点路径和请求/响应格式与原 Flask 版本完全兼容,可以无缝切换。
使用 curl 测试:
# 创建 Collection
curl -X POST "http://localhost:5000/CreateVikingdbCollection" \
-H "Content-Type: application/json" \
-d '{
"CollectionName": "test_collection",
"ProjectName": "default",
"Description": "Test collection",
"Fields": "[{\"FieldName\":\"id\",\"FieldType\":\"int64\",\"IsPrimaryKey\":true},{\"FieldName\":\"text\",\"FieldType\":\"string\"}]"
}'
# 获取健康状态
curl "http://localhost:5000/health"
使用 Python requests:
import requests
import json
# 创建 Collection
response = requests.post(
"http://localhost:5000/CreateVikingdbCollection",
json={
"CollectionName": "test_collection",
"ProjectName": "default",
"Description": "Test collection",
"Fields": json.dumps([
{
"FieldName": "id",
"FieldType": "int64",
"IsPrimaryKey": True
},
{
"FieldName": "text",
"FieldType": "string"
}
])
}
)
print(response.json())