xtrace_sdk.x_vec¶
XTrace Vec for Python.
This package provides the XTrace Vec for Python, which allows you to augment your Python AI applications using a privacy-preserving vector database. The SDK provides a set of tools and libraries to help you integrate with the XTrace platform and equip your AI applications with the ability to store, retrieve, and manage memory as needed in a secure and efficient manner.
Submodules¶
Classes¶
Envelope encryption via AWS KMS. |
|
Protocol for objects that supply a 256-bit AES key and can wrap/unwrap it for storage. |
|
Derives a 256-bit AES key from a passphrase using scrypt. |
|
Encrypts and uploads document collections to XTrace. |
|
This class provides an interface to generate embeddings using different providers. |
|
A client wrapper for interacting with inference models via OpenAI API. |
|
Retrieves and decrypts chunks from XTrace using encrypted hamming distance search. |
|
Bundles a homomorphic encryption client and an AES key under a single key-provider-protected object. |
Package Contents¶
- class xtrace_sdk.x_vec.AWSKMSKeyProvider(kms_client, key_id)¶
Envelope encryption via AWS KMS.
On creation, generates a random 256-bit DEK and wraps it with KMS. On load, unwraps a previously wrapped DEK using KMS.
Requires
boto3at runtime. The KMS key must grant the callerkms:Encryptandkms:Decryptpermissions.- Parameters:
- _kms¶
- _key_id¶
- classmethod create(kms_client, key_id)¶
Generate a fresh DEK via KMS
GenerateDataKey.- Parameters:
- Return type:
- classmethod from_wrapped(wrapped, **kwargs)¶
Unwrap a previously stored EDEK using KMS
Decrypt.- Parameters:
wrapped (bytes) – The EDEK bytes returned by
wrap_key().kms_client – A
boto3KMS client (passed via kwargs).key_id – KMS key ID, ARN, or alias (passed via kwargs).
kwargs (Any)
- Return type:
- class xtrace_sdk.x_vec.KeyProvider¶
Bases:
ProtocolProtocol for objects that supply a 256-bit AES key and can wrap/unwrap it for storage.
- wrap_key()¶
Return an opaque blob that can be stored alongside ciphertext to recover the key later.
- Return type:
- classmethod from_wrapped(wrapped, **kwargs)¶
Reconstruct a provider from a blob previously returned by
wrap_key().- Parameters:
wrapped (bytes)
kwargs (Any)
- Return type:
- class xtrace_sdk.x_vec.PassphraseKeyProvider(passphrase, salt=None)¶
Derives a 256-bit AES key from a passphrase using scrypt.
This is the default key provider and preserves backwards-compatible behavior. The passphrase is never stored — only the derived key is kept in memory.
- _salt = b'xtrace-aes-gcm-v1'¶
- _key¶
- classmethod from_wrapped(wrapped, **kwargs)¶
Re-derive the key from the passphrase and stored salt.
- Parameters:
wrapped (bytes) – The salt bytes returned by
wrap_key().passphrase – The original passphrase (passed via kwargs).
kwargs (Any)
- Return type:
- class xtrace_sdk.x_vec.DataLoader(execution_context, integration)¶
Encrypts and uploads document collections to XTrace.
DataLoaderhandles the two encryption steps required before data reaches XTrace:AES encryption of chunk content (text → ciphertext bytes).
Homomorphic encryption of embedding vectors (float vector → encrypted index).
The encrypted data is then uploaded via
XTraceIntegration. Neither the plaintext chunk content nor the raw embedding vectors leave the client.- Parameters:
execution_context (xtrace_sdk.x_vec.utils.execution_context.ExecutionContext) – Initialised
ExecutionContextcontaining the AES key and homomorphic client.integration (xtrace_sdk.integrations.xtrace.XTraceIntegration) – Authenticated
XTraceIntegrationinstance.
- execution_context¶
- integration¶
- async dump_db(db, index, kb_id, concurrent=False)¶
Upload a pre-encrypted database to XTrace.
Typically called after
load_data_from_memory()orload_data_from_memory_batch()have produced an(index, encrypted_db)pair.- Parameters:
db (EncryptedDB) – Encrypted document collection produced by
load_data_from_memory().index (EncryptedIndex) – Encrypted embedding vectors produced by
load_data_from_memory().kb_id (str) – Destination knowledge-base ID.
concurrent (bool, optional) – Upload batches concurrently. Defaults to
False.
- Returns:
List of server responses, one per upload batch.
- Return type:
- async upsert_one(chunk, vector, kb_id)¶
Encrypt and upload a single chunk.
- async delete_chunks(chunk_ids, kb_id)¶
Delete chunks by ID.
- async update_chunks(chunk_updates, vectors, kb_id)¶
Re-encrypt updated chunks and upload them.
Each chunk in
chunk_updatesmust include achunk_idfield identifying the record to replace.
- async load_data_from_memory(chunks, vectors, disable_progress=False)¶
Encrypt a document collection one chunk at a time.
AES-encrypts each chunk’s
chunk_contentand homomorphically encrypts each float embedding vector into an encrypted index. Results are ready to pass todump_db().- Parameters:
chunks (DocumentCollection) – Document collection — each item must have a
chunk_contentstring field.vectors (list[list[float]]) – Float embedding vectors, one per chunk. Each entry may also be a coroutine (e.g. an unawaited
bin_embed()call) — it will be awaited automatically.disable_progress (bool, optional) – If
True, suppress the tqdm progress bar, defaults toFalse.
- Returns:
Tuple of
(index, encrypted_db)whereindexcontains the encrypted vectors andencrypted_dbcontains chunks with AES-encrypted content.- Return type:
tuple[EncryptedIndex, EncryptedDB]
- async load_data_from_memory_batch(chunks, vectors, disable_progress=False)¶
Encrypt a document collection using batch homomorphic encryption.
Faster than
load_data_from_memory()for large collections because all embedding vectors are passed to the homomorphic client in a single batch call instead of one at a time. AES encryption is still applied per chunk.- Parameters:
chunks (DocumentCollection) – Document collection — each item must have a
chunk_contentstring field.vectors (list[list[float]]) – Float embedding vectors, one per chunk. Each entry may also be a coroutine (e.g. an unawaited
bin_embed()call) — it will be awaited automatically.disable_progress (bool, optional) – Unused; kept for API compatibility with
load_data_from_memory().
- Returns:
Tuple of
(index, encrypted_db)whereindexcontains the encrypted vectors andencrypted_dbcontains chunks with AES-encrypted content.- Return type:
tuple[EncryptedIndex, EncryptedDB]
- class xtrace_sdk.x_vec.Embedding(provider, model_name, dim)¶
This class provides an interface to generate embeddings using different providers. Supported providers include “ollama”, “openai”, and “sentence_transformer”. It also includes methods to convert float embeddings to binary format.
- dim¶
- provider¶
- url¶
- model_name¶
- async embed(text)¶
Generates an embedding for the given text using the specified provider.
- Parameters:
text (str) – The input text to be embedded.
- Returns:
A numpy array representing the embedding of the input text.
- Return type:
np.ndarray
- Raises:
ValueError – If the embedding dimension does not match the expected dimension.
- static float_2_bin(float_array)¶
Convert a list of floats to a list of binary integers, naive implementation, preserves dimension
- class xtrace_sdk.x_vec.InferenceClient(inference_provider, model_name, api_key=None, base_url=None, prompt_template=None)¶
A client wrapper for interacting with inference models via OpenAI API.
- Parameters:
- client¶
- model_name¶
- prompt_template¶
- query(query, context=None, stream=False)¶
Query the inference model.
- class xtrace_sdk.x_vec.Retriever(execution_context, integration, parallel=False)¶
Retrieves and decrypts chunks from XTrace using encrypted hamming distance search.
- Parameters:
execution_context (xtrace_sdk.x_vec.utils.execution_context.ExecutionContext) – Holds AES + homomorphic clients and context ID.
integration (xtrace_sdk.integrations.xtrace.XTraceIntegration) – XTrace API integration instance.
parallel (bool) – If True, decode hamming distances in parallel using multiprocessing.
- execution_context¶
- integration¶
- parallel = False¶
- async nn_search_for_ids(query_vector, k=3, kb_id='', meta_filter=None, range_filter=None, include_scores=False)¶
Find the k nearest neighbors by encrypted hamming distance.
- Parameters:
query_vector (list[float]) – Float embedding vector to search with.
k (int) – Number of nearest neighbors to return.
kb_id (str) – Knowledge-base ID to search.
meta_filter (dict | None) – Optional metadata filter dict (MongoDB-style operators).
range_filter (list[int] | None) – Optional
[min, max]range to restrict which chunks are searched.include_scores (bool) – If True, also return the plain hamming distances.
- Returns:
List of chunk IDs, or (chunk_ids, scores) if include_scores=True.
- Return type:
- async retrieve_and_decrypt(chunk_ids, kb_id, projection=None)¶
Fetch chunks by ID and AES-decrypt their content.
- class xtrace_sdk.x_vec.ExecutionContext(homomorphic_client, key_provider, context_id=None)¶
Bundles a homomorphic encryption client and an AES key under a single key-provider-protected object.
An
ExecutionContextis the root secret for a XTrace deployment. It holds:A homomorphic client (
PaillierClientorPaillierLookupClient) whose secret key is used to decrypt Hamming distances returned by the XTrace server.An AES key supplied by a
KeyProvider, used to encrypt chunk content before upload.
The secret key is never transmitted in plaintext — it is AES-encrypted with the key provider’s key before any remote storage.
- Parameters:
homomorphic_client (HomomorphicClient) – An initialised
PaillierClientorPaillierLookupClient.key_provider (xtrace_sdk.x_vec.crypto.key_provider.KeyProvider) – A
KeyProviderthat supplies the AES encryption key.context_id (str | None) – Optional deterministic ID. If omitted, one is derived from a SHA-256 hash of the public key and configuration.
- homomorphic¶
- key_provider¶
- aes¶
- classmethod create(passphrase=None, homomorphic_client_type='paillier', embedding_length=512, key_len=1024, salt=None, path=None, key_provider=None)¶
Create a new execution context and optionally save it to disk.
Supply either
key_providerorpassphrase(with optionalsalt). If both are given,key_providertakes precedence.- Parameters:
passphrase (str | None) – Secret passphrase used to derive the AES encryption key and protect the homomorphic secret key at rest.
homomorphic_client_type (str) –
"paillier"or"paillier_lookup".embedding_length (int) – Dimension of the binary embedding vectors (must match the model).
key_len (int) – RSA modulus size in bits (minimum
1024).salt (bytes | None) – Optional salt bytes for passphrase-based key derivation.
path (str | None) – If provided, persist the context to this file path via
save_to_disk().key_provider (xtrace_sdk.x_vec.crypto.key_provider.KeyProvider | None) – Explicit
KeyProviderinstance (e.g.AWSKMSKeyProvider).
- Returns:
Initialised
ExecutionContext.- Raises:
ValueError – If
homomorphic_client_typeis not recognised orembedding_length >= key_len.- Return type:
- to_dict_enc()¶
Return a serialisable dict with the secret key AES-encrypted under the key provider.
- Return type:
- to_dict_plain()¶
Return a serialisable dict with the secret key in plaintext. Do not persist or transmit.
- Return type:
- hash()¶
Compute a deterministic SHA-256 fingerprint of this context’s cryptographic identity.
The
devicefield is excluded so that CPU and GPU contexts sharing the same keys compare as equal.- Returns:
Hex-encoded SHA-256 digest.
- Return type:
- serialize_exec_context()¶
Serialise the execution context to a JSON string suitable for storage or transmission.
The secret key is AES-encrypted under the key provider before inclusion.
- Returns:
JSON string representing the encrypted execution context.
- Return type:
- Raises:
ValueError – If the homomorphic client type is not supported.
- classmethod _from_serialized_exec_context(json_obj, passphrase=None, key_provider=None, context_id=None)¶
Reconstruct an
ExecutionContextfrom a previously serialised dict.Supply either
key_providerorpassphrase. For passphrase-based contexts the salt is read from the storedwrapped_keyfield automatically.- Parameters:
json_obj (dict) – Dict produced by
to_dict_enc().passphrase (str | None) – Passphrase for passphrase-based contexts.
key_provider (xtrace_sdk.x_vec.crypto.key_provider.KeyProvider | None) – Explicit
KeyProviderto use for decryption.context_id (str | None) – Optional context ID to attach; if
Noneone is recomputed.
- Returns:
Restored
ExecutionContext.- Raises:
ValueError – If the stored homomorphic client type is not supported.
- Return type:
- dump_tables()¶
Dump precomputed encryption tables (Paillier-Lookup only) for caching.
- Returns:
Dict containing
g_tableandnoise_table, or an empty dict if the underlying client does not support table export.- Return type:
- save_to_disk(path)¶
Persist the execution context to a local file.
The secret key is AES-encrypted before writing. The passphrase/key is not stored.
- Parameters:
path (str) – File path to write to.
- Return type:
None
- classmethod load_from_disk(passphrase=None, path='', key_provider=None)¶
Load an
ExecutionContextfrom a file previously saved withsave_to_disk().- Parameters:
passphrase (str | None) – Passphrase for passphrase-based contexts.
path (str) – File path to read from.
salt – Optional salt for passphrase-based key derivation.
key_provider (xtrace_sdk.x_vec.crypto.key_provider.KeyProvider | None) – Explicit
KeyProvider(e.g.AWSKMSKeyProvider).
- Returns:
Restored
ExecutionContext.- Return type:
- async save_to_remote(integration)¶
Upload the execution context to XTrace remote storage.
The secret key is AES-encrypted under the key provider before upload — XTrace never sees the plaintext secret key or the passphrase.
- Parameters:
integration (xtrace_sdk.integrations.xtrace.XTraceIntegration) – Authenticated
XTraceIntegrationinstance.- Returns:
The
context_idassigned by the server.- Return type:
- classmethod load_from_remote(passphrase=None, context_id='', integration=None, key_provider=None)¶
- Async:
- Parameters:
passphrase (str | None)
context_id (str)
integration (XTraceIntegration | None)
key_provider (xtrace_sdk.x_vec.crypto.key_provider.KeyProvider | None)
- Return type:
Fetch and decrypt an
ExecutionContextfrom XTrace remote storage.- Parameters:
passphrase (str | None) – Passphrase for passphrase-based contexts.
context_id (str) – ID returned when the context was originally saved.
integration (XTraceIntegration | None) – Authenticated
XTraceIntegrationinstance.salt – Optional salt for passphrase-based key derivation.
key_provider (xtrace_sdk.x_vec.crypto.key_provider.KeyProvider | None) – Explicit
KeyProvider(e.g.AWSKMSKeyProvider).
- Returns:
Restored
ExecutionContext.- Return type: