Python API¶
The reference for the completype package, generated from its typed stubs:
__init__.pyi,
completype.pyi
and _async.pyi.
Every name on this page is importable from completype.
Databases¶
connect
¶
connect(
url: str,
options: Mapping[str, str] | None = None,
cache_dir: str | PathLike[str] | None = None,
**build_options,
) -> Database
Opens the database at url: a local path, or an s3://, gs://, az:// or memory:// URL.
connect(url, ...) is Database(url, ...): it takes url, options, cache_dir and the build options
min_word_chars, max_edit_distance, fuzzy_prefix_chars, vector_bits, compact_keys and
build_threads. See Configuration.
Database
¶
Database(
url: str,
options: Mapping[str, str] | None = None,
cache_dir: str | PathLike[str] | None = None,
*,
tags: Mapping[str, str] | None = None,
min_word_chars: int = 3,
max_edit_distance: int = 2,
fuzzy_prefix_chars: int = 7,
vector_bits: int = 4,
compact_keys: bool = False,
build_threads: int = 1,
)
namespace
¶
namespace(name: str) -> DatabaseNamespace
Namespace name: 1 to 128 letters, digits, '.', '_' or '-'. Does no I/O.
index_names
¶
Indexes of the default namespace.
open_index
¶
open_index(
name: str,
version: int | None = None,
*,
max_score: float | None = None,
popularity_weight: float = 0.4,
short_query_chars: int = 3,
short_query_limit: int = 100,
short_query_cache_entries: int = 10000,
vector_threads: int = 1,
) -> Index
engine
¶
engine(
*,
sync_every: float | None = 5.0,
overfetch: int = 2,
popularity_weight: float = 0.4,
short_query_chars: int = 3,
short_query_limit: int = 100,
short_query_cache_entries: int = 10000,
vector_threads: int = 1,
) -> Engine
compact
¶
compact(
index: str,
*,
fanout: int = 4,
max_segments: int = 16,
max_hidden_fraction: float = 0.25,
until_done: bool = False,
) -> int | None
cleanup
¶
DatabaseNamespace
¶
One namespace of a Database: its transactions, indexes and compaction.
open_index
¶
open_index(
name: str,
version: int | None = None,
*,
max_score: float | None = None,
popularity_weight: float = 0.4,
short_query_chars: int = 3,
short_query_limit: int = 100,
short_query_cache_entries: int = 10000,
vector_threads: int = 1,
) -> Index
compact
¶
compact(
index: str,
*,
fanout: int = 4,
max_segments: int = 16,
max_hidden_fraction: float = 0.25,
until_done: bool = False,
) -> int | None
Transaction
¶
As a context manager, commits on success and discards its changes on an exception.
append
¶
append(
index: str,
documents: Documents,
deletes: Sequence[Id] = ...,
vectors: Vectors | None = None,
) -> None
sync
¶
Makes ids, or all of index but its hide markers, read as documents; needs Engine.begin().
prune
¶
prune(index: str, keep: Sequence[Id]) -> None
Deletes every document of index not in keep, except hide markers; needs Engine.begin().
NamespaceTransaction
¶
The index operations of a Transaction in one namespace.
append
¶
append(
index: str,
documents: Documents,
deletes: Sequence[Id] = ...,
vectors: Vectors | None = None,
) -> None
sync
¶
Makes ids, or all of index but its hide markers, read as documents; needs Engine.begin().
Lease
¶
Store
¶
Store(
url: str,
options: Mapping[str, str] | None = None,
cache_dir: str | PathLike[str] | None = None,
*,
tags: Mapping[str, str] | None = None,
)
Indexes and engines¶
Index
¶
Index(
segments: Sequence[Segment],
*,
max_score: float | None = None,
popularity_weight: float = 0.4,
short_query_chars: int = 3,
short_query_limit: int = 100,
short_query_cache_entries: int = 10000,
vector_threads: int = 1,
)
from_documents
staticmethod
¶
from_documents(
documents: Documents,
vectors: Vectors | None = None,
*,
max_score: float | None = None,
popularity_weight: float = 0.4,
min_word_chars: int = 3,
max_edit_distance: int = 2,
fuzzy_prefix_chars: int = 7,
vector_bits: int = 4,
build_threads: int = 1,
) -> Index
complete
¶
complete(
query: str,
limit: int = 10,
*,
contexts: Sequence[str] | None = None,
) -> list[Suggestion]
complete_aliases
¶
complete_aliases(
query: str,
limit: int = 10,
*,
contexts: Sequence[str] | None = None,
) -> list[AliasSuggestion]
vector_search
¶
vector_search(
vector: Vector,
limit: int = 10,
*,
contexts: Sequence[str] | None = None,
) -> list[Suggestion]
hybrid_search
¶
hybrid_search(
text: str,
vector: Vector,
limit: int = 10,
*,
fusion: FusionKind = "rrf",
rrf_k: float = 60.0,
semantic_weight: float = 0.5,
candidates: int | None = None,
contexts: Sequence[str] | None = None,
) -> list[HybridSuggestion]
ids
¶
The numeric ids of the live documents, sorted; a keyed document's is key_id(key).
Segment
¶
build
staticmethod
¶
build(
documents: Documents,
deletes: Sequence[Id] = ...,
vectors: Vectors | None = None,
*,
min_word_chars: int = 3,
max_edit_distance: int = 2,
fuzzy_prefix_chars: int = 7,
vector_bits: int = 4,
compact_keys: bool = False,
build_threads: int = 1,
path: str | PathLike[str] | None = None,
) -> Segment
SegmentWriter
¶
Engine
¶
sync_status
property
¶
The background sync's last run: synced_at and error; None before it has run.
sync
¶
Loads the database's latest version now; only for engines from Database.engine().
begin
¶
begin() -> Transaction
A strict transaction against the version this engine serves; sync and prune compare with it.
transact
¶
transact(
function: Callable[[Transaction], object],
attempts: int = 10,
) -> dict[str, Any] | None
Syncs, runs function with begin()'s transaction and commits, again on a stale index; None if nothing staged.
namespace
¶
namespace(name: str) -> Namespace
The current version of namespace name; NamespaceNotFoundError if there is none.
complete
¶
complete(
query: str,
layers: Sequence[str],
limit: int = 10,
*,
contexts: Sequence[str] | None = None,
ignore_missing_layers: bool = False,
layer_weights: Sequence[float] | None = None,
) -> list[Suggestion]
Namespace.complete in the default namespace.
complete_aliases
¶
complete_aliases(
query: str,
layers: Sequence[str],
limit: int = 10,
*,
contexts: Sequence[str] | None = None,
ignore_missing_layers: bool = False,
layer_weights: Sequence[float] | None = None,
) -> list[AliasSuggestion]
vector_search
¶
vector_search(
vector: Vector,
layers: Sequence[str],
limit: int = 10,
*,
contexts: Sequence[str] | None = None,
ignore_missing_layers: bool = False,
layer_weights: Sequence[float] | None = None,
) -> list[Suggestion]
hybrid_search
¶
hybrid_search(
text: str,
vector: Vector,
layers: Sequence[str],
limit: int = 10,
*,
fusion: FusionKind = "rrf",
rrf_k: float = 60.0,
semantic_weight: float = 0.5,
candidates: int | None = None,
contexts: Sequence[str] | None = None,
ignore_missing_layers: bool = False,
layer_weights: Sequence[float] | None = None,
) -> list[HybridSuggestion]
Namespace
¶
One version of one namespace; every search through it sees that version.
complete
¶
complete(
query: str,
layers: Sequence[str],
limit: int = 10,
*,
contexts: Sequence[str] | None = None,
ignore_missing_layers: bool = False,
layer_weights: Sequence[float] | None = None,
) -> list[Suggestion]
Later layers override earlier ones per document id; a missing layer raises LayerNotFoundError.
complete_aliases
¶
complete_aliases(
query: str,
layers: Sequence[str],
limit: int = 10,
*,
contexts: Sequence[str] | None = None,
ignore_missing_layers: bool = False,
layer_weights: Sequence[float] | None = None,
) -> list[AliasSuggestion]
vector_search
¶
vector_search(
vector: Vector,
layers: Sequence[str],
limit: int = 10,
*,
contexts: Sequence[str] | None = None,
ignore_missing_layers: bool = False,
layer_weights: Sequence[float] | None = None,
) -> list[Suggestion]
hybrid_search
¶
hybrid_search(
text: str,
vector: Vector,
layers: Sequence[str],
limit: int = 10,
*,
fusion: FusionKind = "rrf",
rrf_k: float = 60.0,
semantic_weight: float = 0.5,
candidates: int | None = None,
contexts: Sequence[str] | None = None,
ignore_missing_layers: bool = False,
layer_weights: Sequence[float] | None = None,
) -> list[HybridSuggestion]
Replica
¶
Many writers¶
ChangeSet
¶
NamespaceChanges
¶
Ingestor
¶
Ingestor(
database: Database,
owner: str,
*,
lease_ttl_seconds: float = 30.0,
max_change_sets: int = 1000,
compact: bool = True,
)
Documents¶
Document
¶
Document(
id: Id,
text: str,
popularity: float = 0.0,
*,
synonyms: Sequence[str] = ...,
abbreviations: Sequence[str] = ...,
contexts: Sequence[str] = ...,
vector: Vector | None = None,
fields: Mapping[str, str] = ...,
)
fields
property
¶
Stored with the document and returned with it, never searched.
DocumentDict
¶
Bases: TypedDict
Documents
module-attribute
¶
Documents = Iterable[DocumentDict | Document] | Any
Dicts, Documents, or a pandas, polars or pyarrow table with those columns.
Vector
module-attribute
¶
An embedding: a 1-D float32 array or a list of floats.
Vectors
module-attribute
¶
One embedding row per document: a float32 array of shape (n, dim), or lists.
Suggestions¶
Suggestion
¶
highlights
instance-attribute
¶
(start, end) character offsets of text that matched the query.
AliasSuggestion
¶
HybridSuggestion
¶
MatchKind
¶
Bases: StrEnum
How a suggestion matched its query. Members compare equal to their string values.
asyncio¶
AsyncDatabase
¶
AsyncDatabase(
url: str,
options: Mapping[str, str] | None = None,
cache_dir: str | PathLike[str] | None = None,
**build_options: Any,
)
Bases: _Lazy
A Database whose storage operations are awaitable: AsyncDatabase(url, ...) takes connect's arguments.
namespace
¶
namespace(name: str) -> AsyncDatabaseNamespace
Namespace name; storage opens on its first awaited call.
AsyncDatabaseNamespace
¶
AsyncDatabaseNamespace(database: AsyncDatabase, name: str)
A DatabaseNamespace whose storage operations are awaitable.
AsyncEngine
¶
AsyncEngine(engine: Engine)
An Engine whose sync() is awaitable; completions stay synchronous.
transact
async
¶
transact(
function: Callable[[Transaction], object],
attempts: int = 10,
) -> dict[str, Any] | None
As Engine.transact; function is synchronous and runs in a worker thread.
Errors¶
Every error derives from CompletypeError. NotFoundError is also a LookupError, InvalidInputError a
ValueError, and StorageError an OSError. OutcomeUnknownError is a StorageError. NamespaceNotFoundError and LayerNotFoundError are
NotFoundErrors that carry the missing name and the names that exist in available.
CompletypeError
¶
Bases: Exception
ConflictError
¶
Bases: CompletypeError
CorruptionError
¶
Bases: CompletypeError
NotFoundError
¶
Bases: CompletypeError, LookupError
NamespaceNotFoundError
¶
Bases: NotFoundError
LayerNotFoundError
¶
Bases: NotFoundError
InvalidInputError
¶
Bases: CompletypeError, ValueError
StorageError
¶
Bases: CompletypeError, OSError
OutcomeUnknownError
¶
Bases: StorageError
Collections¶
The collections layer.
Client
¶
Client(
url: str = "memory://",
*,
sync_every: float | None = 5.0,
cache_dir: str | PathLike[str] | None = None,
options: Mapping[str, str] | None = None,
tags: Mapping[str, str] | None = None,
min_word_chars: int = 3,
max_edit_distance: int = 2,
fuzzy_prefix_chars: int = 7,
vector_bits: int = 4,
compact_keys: bool = False,
build_threads: int = 1,
)
create_collection
¶
create_collection(
name: str,
*,
optimize: OptimizeKind = "auto",
fanout: int = 4,
max_segments: int = 16,
max_hidden_fraction: float = 0.25,
min_interval: float = 30.0,
) -> Collection
get_or_create_collection
¶
get_or_create_collection(
name: str,
*,
optimize: OptimizeKind = "auto",
fanout: int = 4,
max_segments: int = 16,
max_hidden_fraction: float = 0.25,
min_interval: float = 30.0,
) -> Collection
ClientNamespace
¶
The collections of one namespace.
create_collection
¶
create_collection(
name: str,
*,
optimize: OptimizeKind = "auto",
fanout: int = 4,
max_segments: int = 16,
max_hidden_fraction: float = 0.25,
min_interval: float = 30.0,
) -> Collection
get_or_create_collection
¶
get_or_create_collection(
name: str,
*,
optimize: OptimizeKind = "auto",
fanout: int = 4,
max_segments: int = 16,
max_hidden_fraction: float = 0.25,
min_interval: float = 30.0,
) -> Collection
Collection
¶
add
¶
Adds documents, replacing any with the same id; durable on return. Returns the new version.
complete
¶
complete(
query: str,
limit: int = 10,
*,
aliases: bool = False,
layers: Sequence[str] | None = None,
contexts: Sequence[str] | None = None,
vector: Vector | None = None,
ignore_missing_layers: bool = False,
layer_weights: Sequence[float] | None = None,
) -> list[Suggestion]
Completions, best first; Suggestion.layer is the collection each came from.
CollectionStats
¶
Bases: TypedDict
AsyncClient
¶
Bases: _Lazy
A Client whose storage calls are awaitable: AsyncClient(url, ...) takes Client's arguments.
namespace
¶
namespace(name: str) -> AsyncClientNamespace
The collections of namespace name; storage opens on its first awaited call.
get_or_create_collection
async
¶
get_or_create_collection(
name: str, **settings: Any
) -> AsyncCollection
AsyncCollection
¶
AsyncCollection(collection: Collection)
A Collection whose writes and storage calls are awaitable; complete stays synchronous.
AsyncClientNamespace
¶
AsyncClientNamespace(client: AsyncClient, name: str)
A ClientNamespace whose storage calls are awaitable.
get_or_create_collection
async
¶
get_or_create_collection(
name: str, **settings: Any
) -> AsyncCollection
Full-text search¶
The experimental full-text search module, imported as from completype import fts.
Index
¶
A full-text index in a directory, written by one Writer and searched through Reader snapshots.
open
staticmethod
¶
open(
path: str | PathLike[str],
*,
language: str | None = None,
keep_accents: bool | None = None,
stop_words: str | Sequence[str] | None = None,
reordering: Literal[
"never", "on_merge", "always"
] = "on_merge",
first_letter_typos: bool = False,
positions: bool | None = None,
) -> Index
Opens the index in path, creating it if missing. language, keep_accents and positions default
to what the index was built with; stop_words, a language or a list of words, are left out of queries.
positions=True keeps token positions so quoted phrases match as phrases.
Writer
¶
Adds and deletes documents until a commit makes them visible. As a context manager it commits on success, discards the changes on an exception, and releases the lock.
Reader
¶
A consistent view of the index at one commit.
search
¶
search(
query: str,
limit: int = 10,
*,
fields: Fields | None = None,
filter: Filter | None = None,
typos: bool = True,
stored: bool = True,
) -> list[Hit]
The best limit documents for query by BM25 within filter, in every text field unless fields
names some. A query without terms lists the documents filter allows.
get
¶
The stored payload of document id, or None if it has none or does not exist.
Hit
¶
Filter
¶
Which documents a search may return. Combine with &, | and ~.
keyword
staticmethod
¶
keyword(field: str, value: str) -> Filter
Documents whose keyword field field holds value.
keyword_in
staticmethod
¶
keyword_in(field: str, values: Sequence[str]) -> Filter
Documents whose keyword field field holds any of values.