Skip to content

Latest commit

 

History

History
201 lines (157 loc) · 7.95 KB

README.md

File metadata and controls

201 lines (157 loc) · 7.95 KB

VEC 2.0 SDK

Client libraries for the binary protocol implemented by vec and vec-cpu. VEC 2.0 is not wire-compatible with 1.x; see MIGRATION.md and the byte-exact PROTOCOL-2.0.md.

Connect locally

The bundled SDKs connect by database name, not host and port:

OS Endpoint selected by the SDK
Windows \\.\pipe\vec_<name>
Linux /tmp/vec_<name>.sock
from vec_client import VecClient
db = VecClient("tools")                 # connects immediately
index = db.push(vector)
hits = db.query(query, shape=0)         # index + distance only
record = db.get(index)                  # returns a list of VecRecord
const VecClient = require('./vec_client');
const db = new VecClient('tools');
await db.connect();
const index = await db.push(vector);
const hits = await db.query(query, { shape: 0 });
#include "vec_client.h"
VecClient db;
db.connect("tools");
int index = db.push(vector, dim);

The server supports TCP and router namespaces at the raw-protocol level, but the current SDK APIs are local/direct only.

Start an in-memory server

The SDK does not need a different constructor or connection option. Start the server with --memory, then connect using the same database name:

./vec --memory tools 1024
# or, without a GPU:
./vec-cpu --memory tools 1024

This creates a new empty writable database for that server process. It does not discover, read, create, or update .tensors, .meta, .hashes, or .data files. All records disappear when the server exits, and existing persistence files named tools_1024_f32.* remain untouched. The local pipe/socket and TCP protocols are unchanged, so every SDK operation works normally. SAVE returns its normal OK response but does not persist anything.

Choose one ownership boundary

The examples directory provides three complete examples for Python, Node.js/TypeScript, C++, and Delphi:

  1. Relational database: PUSH returns the foreign key; QUERY shape 0 returns only integer IDs and distances. The relational DB owns metadata.
  2. Filename labels: VEC stores filenames as labels; label-only QUERY returns IDs, distances, and filenames. File contents stay outside VEC.
  3. Sidecar data: PUSH stores vector + label + up-to-100KB bytes; data-only QUERY returns the actual payload with each nearest-neighbor hit.

These are alternatives, not layers that every application must adopt. In particular, the original vector-in/ID-out design remains the first example.

Record model

Every physical slot has an integer ID and vector, plus optional label and opaque data. IDs append monotonically and tombstones are not reused; UNDO can make the last ID reusable by removing that physical tail slot.

PUSH returns the ID. It accepts an optional label and optional inline data, but non-empty inline data requires a label. SET_DATA can later attach data by ID, including to a record that has no label. Data is limited to 102400 bytes.

PUSH rejects exact duplicates among alive records. The comparison is against the stored representation (fp32 on CPU/fp32 databases, converted fp16 bytes on an fp16 GPU database). UPDATE changes only the vector and does not duplicate-check; use LABEL and SET_DATA to change the other fields.

Shape mask

QUERY, QID, and GET use a one-byte shape mask:

Bit Returned field
0x01 vector
0x02 label
0x04 data

The SDK default is 0x07. Shape 0x00 intentionally returns only IDs (and distances for QUERY/QID), which is the lean path when an external database owns the metadata. QUERY/QID/GET currently ignore unknown shape bits; EXISTS rejects bits outside 0x07 and ignores 0x01 because the caller already has the vector.

L2 distance is squared Euclidean distance. Cosine distance is 1 - cosine. GPU QUERY/QID return at most 16 records. CPU defaults to 50 and the server option --topk=N selects 1..100.

Commands

Command Purpose
PUSH insert and return an integer ID
QUERY nearest neighbors for a supplied vector
QID nearest neighbors for a stored ID or label
EXISTS exact stored-representation lookup
GET fetch by ID, label, or ID batch
UPDATE replace a vector only
LABEL set/clear a label by ID
SET_DATA / GET_DATA set/clear/fetch an opaque blob by ID or label
DELETE tombstone and clear label/data
UNDO remove the last physical slot and its label/data
SAVE / INFO persistence (or memory-mode no-op) and metadata
CLUSTER DBSCAN with binary member/centroid output
DISTINCT farthest-point sampling
REPRESENT farthest-from-centroid member of each DBSCAN cluster

GET by label returns every alive match. Other label-targeted commands require a unique alive match and return ambiguous label otherwise. LABEL itself accepts any in-range slot, including a tombstoned one, although normal label resolution only considers alive slots.

Language naming follows each SDK: Python uses set_data, Node uses setData, and the C++ class exposes set_data. Consult the declarations in each file for ownership rules and exact signatures.

Frame envelope

request:  F0 <u16 ns_len> [ns] <u8 command> <u16 label_len> [label]
          <u32 body_len> [body]
response: <u8 status> <u32 body_len> [body]

All integers are little-endian. Status 0 is OK and 1 is ERR; error bodies are ASCII text. Vectors on the wire are always fp32.

The label header can encode up to 65535 bytes, but PUSH and LABEL writes enforce a 2048-byte application limit for a non-empty label. Such labels reject ASCII space, control bytes (0x00..0x1f, 0x7f), and : * ? " < > | ,. An empty header means no label on PUSH or clear on LABEL; it is not an invalid label. The server does not trim, normalize, remove a BOM, or validate UTF-8. String SDKs encode labels as UTF-8; the C++ protocol surface treats them as bytes.

INFO notes

crc_ok is a byte with these source-defined values:

  • 1: .tensors CRC trailer was present and matched.
  • 0: mismatch, or a new/not-yet-validated database.
  • 255: a legacy .tensors file had no CRC trailer, or the server is using --memory and therefore has no persisted tensor CRC.

mtime is backend-specific: the GPU server reports its last in-process write time (zero before the first mutation), while the CPU server reports the tensor file's modification time. In --memory mode, CPU mtime is zero and crc_ok is 255 because no tensor file is loaded or validated.

Persistence

.tensors  [dim][count][deleted][fmt][count one-byte alive flags][vectors][CRC32]
.meta     [count][per-slot label length + bytes]
.hashes   [count][count u64 hashes][CRC32]
.data     [count][count one-byte presence flags][packed length+blob records][CRC32]

.tensors and .meta retain the old layout. A missing, truncated, count-mismatched, or CRC-invalid .hashes file is rebuilt from .tensors. The .data writer adds a CRC trailer, but the current loader does not read or validate that trailer.

On a real save, .hashes is written; .meta and .data are written only when at least one current label/blob exists. Existing sidecar files are not removed when the last label/blob is cleared, so applications that clear all sidecar data should remove stale files while the server is stopped if that distinction matters.

SAVE's returned count/CRC are a state snapshot, not a durable-write acknowledgement: the server still returns OK if its internal save routine was a no-op or only logged an I/O/disk-space failure. In --memory mode, SAVE is always a no-op and returns the current count with CRC 0.

Files

File Language
vec_client.h C++ header-only client
vec_client.py Python 3 + NumPy
vec_client.js Node.js
vec_client.d.ts TypeScript declarations
vec_client.pas Delphi
DATASHEET.md compact endpoint table