Vectorstore Pinecone Backend

The Pinecone backend: a managed service.

See https://www.pinecone.io/. A vectorstore is one serverless Pinecone index. Its API key is the Secret of the vectorstore’s ApiConnection. The backend embeds the chunks itself, here, in the app, and sends each operation, one of smarter.apps.vectorstore.execution’s, through the tool runner, which calls Pinecone from the tool worker (ADR-038). The chunks’ text is in the metadata’s text, where LangChain’s PineconeVectorStore stores it, so indexes that it wrote stay searchable. Dumps are Pinecone backups, which Pinecone keeps, and which require a paid Pinecone plan.

class smarter.apps.vectorstore.backends.pinecone.PineconeBackend(vectorstore, embeddings=None, client=None)[source]

Bases: SmarterVectorstoreBackend

The Pinecone backend.

Parameters:

client (Any) – A Pinecone client, e.g. a mock in tests, which the operations run on in this process. By default there is none, and each operation goes through the tool runner, with the API key of the vectorstore’s ApiConnection.

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

The API key of the vectorstore’s ApiConnection, read here, in the app.

Raises:

VectorStoreBackendCredentialsError – If the vectorstore has no ApiConnection with an API key.

Return type:

str

check_credentials()[source]

Check that Pinecone accepts the api key, by asking whether the index exists.

Return type:

str

Returns:

What was found, for a person.

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

  • VectorStoreBackendConnectionError – If Pinecone can’t be reached.

create()[source]

Create a serverless Pinecone index, in the cloud and region of the spec’s pinecone.

It is tagged with the platform’s name and the vectorstore’s id, and has deletion protection if the vectorstore has. It returns at once: Pinecone initializes the index for a minute or two, which index_ready() reports, rather than holding a request or a worker while it does.

Raises:

VectorStoreBackendError – If Pinecone did not create it, as pinecone_error() classifies it.

Return type:

None

create_snapshot(name)[source]

Back up the index, with Pinecone’s backups.

Parameters:

name (str) – The backup’s name.

Return type:

SnapshotInfo

Returns:

The backup’s id, size, and whether it is ready.

Raises:

VectorStoreBackendError – If Pinecone did not back it up.

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_snapshot(name)[source]

Delete a Pinecone backup.

Parameters:

name (str) – The backup’s id.

Return type:

None

deletion_protection()[source]

Pinecone’s deletion protection of the index: enabled if the vectorstore has it.

Return type:

str

drop()[source]

Delete the Pinecone index, if it exists, and its data.

It returns at once, while Pinecone deletes the index.

Return type:

None

exists()[source]

Whether the Pinecone index exists.

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

  • VectorStoreBackendConnectionError – If Pinecone is unreachable.

Return type:

bool

index_ready()[source]

Whether the index, which exists, is ready.

Pinecone takes a minute or two to initialize a new one.

Return type:

tuple[bool, str]

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

Run one of the index’s operations: through the tool runner, or in process on an injected client.

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.

restore_snapshot(name)[source]

Replace the index with a new one from the backup.

Pinecone requires that the index is deleted first, so this waits until it is.

Return type:

None

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

Search the index with the query’s embedding, computed here.

Scores are Pinecone’s raw scores for the index’s metric. MMR re-ranks fetch_k matches for diversity, and its results have no score.

Parameters:
  • query (str) – The text to search for.

  • k (int) – How many results to return.

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

  • score_threshold (float | None) – For similarity_score_threshold, the lowest score to keep. A result without a score is dropped.

  • fetch_k (int | None) – For mmr, how many matches to re-rank. Defaults to max(20, k * 4).

  • lambda_mult (float | None) – For mmr, from 0 (most diverse) to 1 (most relevant). Defaults to 0.5.

  • metadata_filter (dict[str, Any] | None) – A Pinecone metadata filter.

Return type:

list[SearchResult]

Returns:

The matching chunks, best first.

stats()[source]

The index’s vector count, dimension and fullness, from Pinecone.

Return type:

dict[str, Any]

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

Embed the texts, here, and upsert them in batches, with each text in its metadata’s text.

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.pinecone.pinecone_error(operation, error)[source]

Return the backend error of an exception from Pinecone, by whether retrying could help.

Parameters:
  • operation (str) – The operation that failed.

  • error (Exception) – Pinecone’s exception.

Return type:

VectorStoreBackendError

Returns:

A credentials error for a refused api key, a rejected error for another request that Pinecone would refuse again, and a connection error for anything else, e.g. a timeout or a 5xx, which may pass.