Reference
srvdb.create
Create a new database with its index and options.
Create a new database. Its dimension, index and options are fixed here and recorded on disk, so later you reopen it with only the path.
srvdb.create(path: str, dimension: int, index: str = "hnsw", *, exist_ok: bool = False, **options) -> dbParameters
Section titled “Parameters”-
path : str Directory for the database files, used exactly as given. It must not exist, or must be an empty directory. Missing parent directories are created.
-
dimension : int Vector dimension, 1 to 4096 (128 to 4096 for
sq8andpqwithtraining). Every vector added and every query must have it. -
index : str, default
"hnsw"One of"hnsw","flat","ivf","sq8","pq","rabitq"or"auto"(case-insensitive). See Choosing an index mode. -
exist_ok : bool, default
FalseIfTrueand a database already exists atpathwith the same index, dimension and options, open it instead of raising, likeos.makedirs(exist_ok=True). If its settings differ, raiseValueError. -
options : keyword arguments Each index accepts only its own options:
Index Options (default) hnswm(16),ef_construction(200),ef_search(50)ivfnlist(100),nprobe(10), plusm,ef_construction,ef_searchfor the partition graphssq8,pqtraining: sample vectors the quantizer is trained on; without it, the first batch added (PQ needs at least 256)rabitqbits(1, 2 or 4; default 4),rerank(40, 20 or 10 for 1, 2 or 4 bits; 0 disables),seedautomemory_mb: a resident-memory budget in MiB, 1 to 1048576 (none). See Memory budgetflatnone
Returns
Section titled “Returns”The open database object (db). Use it in a with block to save and close it on exit; see the database object.
Raises
Section titled “Raises”-
ValueError Unknown
index; an option that does not apply to the index (the message lists the valid ones); a value out of range (for examplemoutside 2 to 256,bitsnot 1, 2 or 4);dimensionoutside 1 to 4096; training vectors of another dimension; or, withexist_ok=True, an existing database with other settings. Nothing is written. -
FileExistsError
pathis a file, already holds a database, or is a non-empty directory. -
BlockingIOError The database is open elsewhere.
-
OSError The files cannot be created.
-
RuntimeError An internal fault occurred.
- The database is built in a hidden sibling directory and renamed into place only when complete. If
createfails, nothing is left atpathand you can simply retry. - The settings are durable when
createreturns: a crash right after still leaves a database thatsrvdb.openreopens with the same index. - Search-time knobs can be changed later:
db.set_ef_search,db.configure_ivf,db.set_rabitq_rerank. - One database object holds an exclusive lock on the directory until it is closed.
Examples
Section titled “Examples”import srvdb
with srvdb.create("./vectors", dimension=128) as db: db.add(["a"], [[1.0] + [0.0] * 127], ["{}"])print(srvdb.open("./vectors").count())# 1import srvdb
db = srvdb.create("./graph", dimension=128, index="hnsw", m=32, ef_search=100)print(db)# SrvDB(dimension=128, mode='hnsw', vectors=0)db.close()db = srvdb.create("./graph", dimension=128, index="hnsw", exist_ok=True, m=32)print(db.count())# 0import srvdb
try: srvdb.create("./codes", dimension=128, index="rabitq", m=16)except ValueError as e: print(e)# invalid input: option 'm' does not apply to index 'rabitq'; valid options: bits, rerank, seedSee also
Section titled “See also”srvdb.open: Open an existing database by path.srvdb.delete: Delete a database from disk.- The database object: What
createreturns.
Was this page helpful? Tell us in Discussions.