vd.util#
The backend registry, the connect() factory, and small shared utilities.
This module is intentionally thin. It owns three things:
the registry mapping a backend name (
"chroma","qdrant", …) to its adapterClientclass, via theregister_backend()decorator;connect(), the one entry point users call to get a client;backend-agnostic helpers: document-input normalization, search-result
egressfunctions, and vector math.
Everything descriptive about a backend (pip package, license, docs URLs,
deployment archetype, setup checks) lives in vd.providers and its
data file, not here.
Functions
|
Connect to a vector database backend and return its |
|
Cosine similarity of two vectors (1.0 identical, 0.0 orthogonal). |
|
Euclidean (L2) distance between two vectors. |
|
Return the adapter |
|
Egress: keep |
|
Egress: keep only the document id. |
|
Egress: keep |
Return the names of all backends with a registered (importable) adapter. |
|
|
Component-wise mean of a non-empty iterable of equal-length vectors. |
|
Normalize a flexible document input to a |
|
Class decorator: register an adapter |
|
Egress: keep only the text. |
- vd.util.connect(backend, *, embedder=None, **backend_kwargs)[source]#
Connect to a vector database backend and return its
Client.This is the single entry point of
vd. Switching vector databases is a one-argument change here.- Parameters:
backend (
str) – Backend name:"memory","chroma","qdrant","faiss","lancedb","sqlite_vec","duckdb","pgvector","pinecone", … Runvd.list_backends()for what is installed.embedder (
Optional[Callable[[str],list[float]]]) – Atext -> vectorfunction. Supply it only if you want the convenience of passing raw text tocollection[key] = "text"andcollection.search("query text").vdnever embeds on its own — with no embedder, passDocumentobjects with vectors and pre-computed query vectors.**backend_kwargs – Backend-specific connection options (
persist_directory,url,api_key,path, …). See each adapter’s docstring.
- Returns:
A connected client — a
Mappingof collection name to collection.- Return type:
Examples
>>> client = connect('memory') >>> client = connect('chroma', persist_directory='./db') >>> client = connect('qdrant', url='http://localhost:6333')
- vd.util.cosine_similarity(vec1, vec2)[source]#
Cosine similarity of two vectors (1.0 identical, 0.0 orthogonal).
- Return type:
Examples
>>> cosine_similarity([1.0, 0.0], [1.0, 0.0]) 1.0 >>> cosine_similarity([1.0, 0.0], [0.0, 1.0]) 0.0
- vd.util.euclidean_distance(vec1, vec2)[source]#
Euclidean (L2) distance between two vectors.
- Return type:
Examples
>>> euclidean_distance([1.0, 0.0], [1.0, 0.0]) 0.0
- vd.util.get_backend(name)[source]#
Return the adapter
Clientclass registered undername.- Raises:
BackendNotInstalledError – If
nameis a known backend whose client library is not installed.ValueError – If
nameis not a known backend at all.
- Return type:
- vd.util.list_backends()[source]#
Return the names of all backends with a registered (importable) adapter.
- vd.util.mean_vector(vectors)[source]#
Component-wise mean of a non-empty iterable of equal-length vectors.
Examples
>>> mean_vector([[0.0, 2.0], [2.0, 4.0]]) [1.0, 3.0]
- vd.util.normalize_document_input(doc_input, *, auto_id=True)[source]#
Normalize a flexible document input to a
Document.Accepted shapes: a
Document; astr(just text); a tuple(text, id),(text, metadata), or(text, id, metadata).- Parameters:
- Return type:
Examples
>>> normalize_document_input(("Hello", "doc1")).id 'doc1' >>> normalize_document_input(("Hello", {"k": "v"})).metadata {'k': 'v'} >>> normalize_document_input("Hello world").id.startswith('doc_') True