Vectorstore Qdrant Backend

The Qdrant backend: self-hosted on Kubernetes, or Qdrant Cloud.

See https://qdrant.tech/. A vectorstore is one Qdrant collection, with one dense vector per chunk. Its text is in the payload’s page_content, and its metadata in metadata, as LangChain’s QdrantVectorStore stores them, so that its collections stay readable.

Every operation is one of smarter.apps.vectorstore.execution’s. The texts are embedded here, in the app. A Qdrant Cloud collection, at its ApiConnection’s url, is reached through the tool runner, in the tool worker (ADR-038); a self-hosted server, which Smarter runs in the cluster, in process.

class smarter.apps.vectorstore.backends.qdrant.QdrantBackend(vectorstore, embeddings=None, client=None, kubernetes=None)[source]

Bases: SmarterVectorstoreBackend

The Qdrant backend.

Parameters:
  • client (QdrantClient | None) – A QdrantClient, e.g. an in-memory one in tests. By default it connects to the self-hosted server, or to the managed service of the vectorstore’s ApiConnection.

  • kubernetes (QdrantKubernetes | None) – The QdrantKubernetes of a self-hosted server. Created if needed.

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

Check that Qdrant accepts the vectorstore’s credentials.

A self-hosted server’s api key is generated by Smarter, and the server doesn’t exist yet, so only Qdrant Cloud is asked.

Return type:

str

Returns:

What was found, for a person.

Raises:
  • VectorStoreBackendCredentialsError – If Qdrant Cloud refuses the api key.

  • VectorStoreBackendConnectionError – If Qdrant Cloud can’t be reached.

property client: QdrantClient

The Qdrant client of a self-hosted server, created when it is first used, or the one a test injected.

A self-hosted server is reached at its in-cluster endpoint, with its generated API key. A Qdrant Cloud collection has no client here: its operations go to the tool runner (operation()).

Raises:

VectorStoreBackendError – For a Qdrant Cloud vectorstore without an injected client.

create()[source]

Create the Qdrant collection, with a payload index on each chunk’s document id.

Return type:

None

create_snapshot(name)[source]

Take a snapshot of the collection, and wait for it.

Parameters:

name (str) – Unused: Qdrant names the snapshot.

Return type:

SnapshotInfo

Returns:

The snapshot’s name and size.

Raises:

VectorStoreBackendError – If Qdrant did not create it.

delete(ids)[source]

Delete chunks by id.

Ids that do not exist are ignored.

Parameters:

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

Return type:

None

delete_document(document_id)[source]

Delete every chunk of a document, whatever their number.

Return type:

None

delete_snapshot(name)[source]

Delete a snapshot of the collection, and wait for it.

Parameters:

name (str) – The snapshot’s name.

Return type:

None

destroy()[source]

Delete the collection, or, if self-hosted, the whole Qdrant server and its volume.

The collection goes with the server’s volume.

Return type:

None

drop()[source]

Delete the Qdrant collection, if it exists, and its data.

Return type:

None

exists()[source]

Whether the Qdrant collection exists.

Raises:
  • VectorStoreBackendCredentialsError – If Qdrant refuses the api key.

  • VectorStoreBackendConnectionError – If Qdrant is unreachable.

Return type:

bool

infrastructure_conditions()[source]

The conditions of a self-hosted Qdrant server: its volume, then its pod.

Qdrant Cloud has none.

Return type:

list[SAMVectorstoreCondition]

Returns:

The Volume and Server conditions, or an empty list.

property kubernetes: QdrantKubernetes

The Kubernetes resources of the self-hosted Qdrant server, created when it is first used.

logs(tail=200)[source]

Return the self-hosted Qdrant server’s recent logs.

Parameters:

tail (int) – How many lines of each pod.

Return type:

str | None

Returns:

The logs, or None for Qdrant Cloud.

managed_url_and_api_key()[source]

The url and the api key of a Qdrant Cloud vectorstore’s ApiConnection, read here, in the app.

Raises:

VectorStoreBackendConnectionError – If the vectorstore has no ApiConnection url.

Return type:

tuple[str, str]

operation(name, params=None, timeout=30)[source]

Run one of the collection’s operations: in process on a self-hosted server, or through the tool runner for Qdrant Cloud.

Parameters:
  • name (str) – The operation, e.g. exists.

  • params (dict[str, Any] | None) – Its parameters, as JSON.

  • timeout (float) – Seconds that it may take.

Returns:

Its result.

Raises:

VectorStoreBackendError – If it fails, classified as credentials, rejected or connection.

provision()[source]

Create, or update, the Kubernetes resources of a self-hosted Qdrant server, without waiting for it.

Nothing is done for Qdrant Cloud.

Raises:

VectorStoreBackendError – If a self-hosted vectorstore has no API key Secret.

Return type:

None

restore_snapshot(name)[source]

Recover the collection from a snapshot on the self-hosted server’s volume, and wait for it.

Parameters:

name (str) – The snapshot’s name.

Raises:

VectorStoreBackendError – For Qdrant Cloud, whose snapshots are restored in its console.

Return type:

None

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, embedded here.

Parameters:
  • query (str) – The query.

  • k (int) – How many results.

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

  • score_threshold (float | None) – The lowest score of a result, for similarity_score_threshold.

  • fetch_k (int | None) – How many candidates MMR re-ranks. By default, four times k, and at least 20.

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

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

Return type:

list[SearchResult]

Returns:

The results. MMR’s have no score.

stats()[source]

The collection’s exact vector count, status, segments count and indexed vectors count.

Return type:

dict[str, Any]

stop()[source]

Stop a self-hosted Qdrant server, keeping its volume and data.

Return type:

None

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

Embed, here, and insert, or replace, chunks, a batch at a time.

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

smarter.apps.vectorstore.backends.qdrant.qdrant_filter(metadata_filter)[source]

A Qdrant filter that matches chunks whose metadata has every value of metadata_filter.

Return type:

Filter | None