Vectorstore Backend Base Class

The interface of vector database backends.

A backend runs one vectorstore’s database operations: its lifecycle, its data, and its dumps. Lifecycle, status and scheduling are the job of VectorstoreService, which calls a backend.

smarter.apps.vectorstore.backends.base.DEFAULT_MMR_LAMBDA = 0.5

MMR’s balance of relevance, 1, and diversity, 0, unless a search names its own.

class smarter.apps.vectorstore.backends.base.SearchResult(id, text, metadata=dict(), score=None)[source]

Bases: object

A chunk that a search found.

__init__(id, text, metadata=<factory>, score=None)
id: str | None
metadata: dict[str, Any]
score: float | None = None
text: str
class smarter.apps.vectorstore.backends.base.SmarterVectorstoreBackend(vectorstore, embeddings=None)[source]

Bases: ABC

Abstract base class of the vector database backends.

Parameters:
  • vectorstore (VectorstoreMeta) – The vectorstore.

  • embeddings (Embeddings | None) – Its embeddings model. Required for upsert() and search().

__init__(vectorstore, embeddings=None)[source]
check_credentials()[source]

Check, quickly, that the database accepts the vectorstore’s credentials.

By default it asks whether the index exists, which a managed service answers only with a valid api key.

Return type:

str

Returns:

What was found, for a person.

Raises:
  • VectorStoreBackendCredentialsError – If the database refuses them.

  • VectorStoreBackendConnectionError – If the database can’t be reached, which may pass.

abstractmethod create()[source]

Create the index or collection.

Return type:

None

abstractmethod create_snapshot(name)[source]

Take a snapshot, or backup.

Return type:

SnapshotInfo

abstractmethod delete(ids)[source]

Delete chunks.

Ids that do not exist are ignored.

Return type:

None

abstractmethod delete_snapshot(name)[source]

Delete a snapshot, or backup.

Return type:

None

destroy()[source]

Delete the index or collection, and any infrastructure that Smarter runs.

Return type:

None

abstractmethod drop()[source]

Delete the index or collection, and its data.

Return type:

None

property embeddings: Embeddings

The embeddings model, which turns texts into vectors.

Raises:

VectorStoreBackendError – If the backend was created without one.

abstractmethod exists()[source]

Whether the index or collection exists.

Return type:

bool

property index_name: str

the vectorstore’s index_name, or its default.

Type:

The name of the index or collection

index_ready()[source]

Whether the index or collection, which exists, is ready to use, and why not.

By default it is as soon as it exists. Pinecone initializes a new index for a minute or two.

Return type:

tuple[bool, str]

infrastructure_conditions()[source]

The conditions of the infrastructure that Smarter runs, e.g. a self-hosted server’s volume and pod.

A managed service has none.

Return type:

list[SAMVectorstoreCondition]

Returns:

One condition per part of the infrastructure, in the order that they become ready.

infrastructure_ready()[source]

Whether the infrastructure that Smarter runs is ready, and why not.

A managed service always is.

Return type:

tuple[bool, str]

logs(tail=200)[source]

The database server’s logs, if Smarter runs it.

Return type:

str | None

provision()[source]

Create the database’s infrastructure, if Smarter runs it.

It returns without waiting for it.

Return type:

None

abstractmethod restore_snapshot(name)[source]

Replace the index or collection’s data with a snapshot’s.

Return type:

None

abstractmethod search(query, k=4, search_type='similarity', score_threshold=None, fetch_k=None, lambda_mult=None, metadata_filter=None)[source]

Find the chunks that are closest in meaning to a query.

Parameters:
  • search_type (str) – similarity, similarity_score_threshold, or mmr (maximal marginal relevance).

  • metadata_filter (dict[str, Any] | None) – chunks whose metadata has these values, e.g. {“source”: “faq”}.

Return type:

list[SearchResult]

abstractmethod stats()[source]

The database’s statistics.

It includes vector_count.

Return type:

dict[str, Any]

stop()[source]

Stop the infrastructure that Smarter runs, keeping its data.

Return type:

None

abstractmethod upsert(ids, texts, metadatas, batch_size=64)[source]

Embed and insert, or replace, chunks.

Parameters:
  • ids (list[str]) – The chunks’ ids.

  • texts (list[str]) – The chunks’ texts.

  • metadatas (list[dict[str, Any]]) – The chunks’ metadata.

  • batch_size (int) – How many chunks to embed and upsert at a time.

Return type:

None

class smarter.apps.vectorstore.backends.base.SnapshotInfo(name, size_bytes=None, ready=True)[source]

Bases: object

A Qdrant snapshot, or a Pinecone backup.

__init__(name, size_bytes=None, ready=True)
name: str
ready: bool = True
size_bytes: int | None = None
exception smarter.apps.vectorstore.backends.base.VectorStoreBackendConnectionError(message='')[source]

Bases: VectorStoreBackendError

The vector database cannot be reached.

exception smarter.apps.vectorstore.backends.base.VectorStoreBackendCredentialsError(message='')[source]

Bases: VectorStoreBackendRejectedError

The vector database refused the api key, or there is none to send.

exception smarter.apps.vectorstore.backends.base.VectorStoreBackendError(message='')[source]

Bases: SmarterException

A vector database operation failed.

exception smarter.apps.vectorstore.backends.base.VectorStoreBackendRejectedError(message='')[source]

Bases: VectorStoreBackendError

The vector database refused a request, and would refuse it again, e.g. an invalid dimension or a full quota.

A deploy that meets it fails at once, rather than retrying.

smarter.apps.vectorstore.backends.base.json_dict(value)[source]

An operation’s result that is a JSON object, as a dict; anything else, an empty one.

Return type:

dict[str, Any]

smarter.apps.vectorstore.backends.base.json_dicts(value)[source]

An operation’s result that is a list of JSON objects, e.g. a query’s matches; anything else, an empty list.

Return type:

list[dict[str, Any]]

smarter.apps.vectorstore.backends.base.json_int(value)[source]

An operation’s result that is an integer, e.g. a snapshot’s size; anything else, None.

Return type:

int | None

smarter.apps.vectorstore.backends.base.mmr_fetch_k(k, fetch_k)[source]

How many candidates MMR re-ranks: fetch_k, or four times k and at least 20.

Return type:

int

smarter.apps.vectorstore.backends.base.result_of(match, score)[source]

Return a database’s match as a search result.

Parameters:
Return type:

SearchResult

smarter.apps.vectorstore.backends.base.select_mmr(query_vector, matches, k, lambda_mult)[source]

Re-rank matches for diversity, with maximal marginal relevance, in the app.

Parameters:
  • query_vector (list[float]) – The query’s embedding.

  • matches (list[dict[str, Any]]) – The candidates, each with its vector.

  • k (int) – How many results.

  • lambda_mult (float | None) – MMR’s balance of relevance, 1, and diversity, 0. By default DEFAULT_MMR_LAMBDA.

Return type:

list[SearchResult]

Returns:

The results, without scores.