ahvn.klbase package

class ahvn.klbase.KLBase(storages=None, engines=None, name=None, *args, **kwargs)[源代码]

基类:ToolRegistry

参数:
__init__(storages=None, engines=None, name=None, *args, **kwargs)[源代码]

Initialization.

参数:
  • storages (Union[List[BaseKLStore], Dict[BaseKLStore]], optional) -- A list or dictionary of storage backends. Defaults to None.

  • engines (Union[List[BaseKLEngine], Dict[BaseKLEngine]], optional) -- A list or dictionary of engine backends. Defaults to None.

  • name (Optional[str], optional) -- The name of this KLBase instance. If None, defaults to "default".

  • *args -- Additional positional arguments.

  • **kwargs -- Additional keyword arguments.

add_storage(storage, name=None)[源代码]

Add a storage backend.

参数:
  • storage (BaseKLStore) -- The storage backend to add.

  • name (Optional[str], optional) -- The name to register the storage under. If None, uses storage.name. Defaults to None

del_storage(name)[源代码]

Delete a storage backend.

参数:

name (str) -- The name of the storage backend to delete.

add_engine(engine, name=None, desync=False)[源代码]

Add an engine backend.

参数:
  • engine (BaseKLEngine) -- The engine backend to add.

  • name (Optional[str], optional) -- The name to register the engine under. If None, uses engine.name. Defaults to None.

  • desync (bool, optional) -- Whether to desynchronize the engine upon addition. Defaults to False.

del_engine(name)[源代码]

Delete an engine backend.

参数:

name (str) -- The name of the engine backend to delete.

desync_engine(name)[源代码]

Desynchronize an engine backend.

参数:

name (str) -- The name of the engine backend to desynchronize.

resync_engine(name)[源代码]

Synchronize an engine backend.

参数:

name (str) -- The name of the engine backend to synchronize.

upsert(kl, storages=None, engines=None, **kwargs)[源代码]

Upsert a KL into all storages and engines.

参数:
  • kl (BaseUKF) -- The KL to upsert.

  • storages (List[str], optional) -- The list of storage names to upsert into. If None and engines is None/empty, upserts into all storages. Defaults to None. If None and engines is not None/empty, upserts into no storages.

  • engines (List[str], optional) -- The list of engine names to upsert into. If None and storages is None/empty, upserts into all engines. Defaults to None. If None and storages is not None/empty, upserts into no engines.

  • **kwargs -- Additional keyword arguments.

insert(kl, storages=None, engines=None, **kwargs)[源代码]

Insert a KL into all storages and engines.

参数:
  • kl (BaseUKF) -- The KL to insert.

  • storages (List[str], optional) -- The list of storage names to insert into. If None and engines is None/empty, inserts into all storages. Defaults to None. If None and engines is not None/empty, inserts into no storages.

  • engines (List[str], optional) -- The list of engine names to insert into. If None and storages is None/empty, inserts into all engines. Defaults to None. If None and storages is not None/empty, inserts into no engines.

  • **kwargs -- Additional keyword arguments.

batch_upsert(kls, storages=None, engines=None, progress=None, **kwargs)[源代码]

Batch upsert KLs into all storages and engines.

参数:
  • kls (List[BaseUKF]) -- The list of KLs to upsert.

  • storages (List[str], optional) -- The list of storage names to upsert into. If None and engines is None/empty, batch upserts into all storages. Defaults to None. If None and engines is not None/empty, batch upserts into no storages.

  • engines (List[str], optional) -- The list of engine names to upsert into. If None, batch upserts into all engines. Defaults to None. If None and storages is None/empty, batch upserts into all engines. If None and storages is not None/empty, batch upserts into no engines.

  • **kwargs -- Additional keyword arguments.

  • progress (Type[Progress])

batch_insert(kls, storages=None, engines=None, progress=None, **kwargs)[源代码]

Batch insert KLs into all storages and engines.

参数:
  • kls (List[BaseUKF]) -- The list of KLs to insert.

  • storages (List[str], optional) -- The list of storage names to insert into. If None and engines is None/empty, batch inserts into all storages. Defaults to None. If None and engines is not None/empty, batch inserts into no storages.

  • engines (List[str], optional) -- The list of engine names to insert into. If None and storages is None/empty, batch inserts into all engines. Defaults to None. If None and storages is not None/empty, batch inserts into no engines.

  • **kwargs -- Additional keyword arguments.

  • progress (Type[Progress])

remove(key, storages=None, engines=None, **kwargs)[源代码]

Remove a KL from all storages and engines.

参数:
  • key (Union[int, str, BaseUKF]) -- The key or BaseUKF instance of the KL to remove.

  • storages (List[str], optional) -- The list of storage names to remove from. If None and engines is None/empty, removes from all storages. Defaults to None. If None and engines is not None/empty, removes from no storages.

  • engines (List[str], optional) -- The list of engine names to remove from. If None and storages is None/empty, removes from all engines. Defaults to None. If None and storages is not None/empty, removes from no engines.

  • **kwargs -- Additional keyword arguments.

batch_remove(keys, storages=None, engines=None, progress=None, **kwargs)[源代码]

Batch remove KLs from all storages and engines.

参数:
  • keys (List[Union[int, str, BaseUKF]]) -- The list of keys or BaseUKF instances of the KLs to remove.

  • storages (List[str], optional) -- The list of storage names to remove from. If None and engines is None/empty, batch removes from all storages. Defaults to None. If None and engines is not None/empty, batch removes from no storages.

  • engines (List[str], optional) -- The list of engine names to remove from. If None and storages is None/empty, batch removes from all engines. Defaults to None. If None and storages is not None/empty, batch removes from no engines.

  • **kwargs -- Additional keyword arguments.

  • progress (Type[Progress])

clear(storages=None, engines=None)[源代码]

Clear all KLs from all storages and engines.

参数:
  • storages (List[str], optional) -- The list of storage names to clear. If None and engines is None/empty, clears all storages. Defaults to None. If None and engines is not None/empty, clears no storages.

  • engines (List[str], optional) -- The list of engine names to clear. If None and storages is None/empty, clears all engines. Defaults to None. If None and storages is not None/empty, clears no engines.

set_default_engine(name)[源代码]

Set the default engine for searches.

参数:

name (str) -- The name of the engine to set as default.

search(engine=None, *args, **kwargs)[源代码]

Search for KLs using a specified engine.

参数:
  • engine (Optional[str]) -- The name of the engine to use for searching. If None, uses the default engine. If no default engine is set, raises a ValueError. Defaults to None.

  • *args -- Additional positional arguments for the engine's search method.

  • **kwargs -- Additional keyword arguments for the engine's search method.

返回:

A list of dictionary, each being one search result.
As a recommended convention, all engine searches should support at least these keys:
  • "id" (int): the identifier of the KL.

  • "kl" (BaseUKF): the KL instance, if the engine is recoverable.

Other keys can be added as needed, which are usually search-specific (e.g., vector search score).

返回类型:

Iterable[Dict[str, Any]]

List all available engine search methods.

返回:

A list of search method names, each a Tuple:

The first string is the engine to call. The second (optional) string is the engine's search mode.

返回类型:

List[Tuple[str, Optional[str]]]

sync(include_desynced=False, progress=None, **kwargs)[源代码]

Sync all engines (one-time operation).

参数:
  • include_desynced (bool) -- Whether to sync desynced engines as well. Default is False.

  • progress (Type[Progress]) -- Progress class for reporting. None for silent, TqdmProgress for terminal.

  • **kwargs -- Additional keyword arguments for the engine's sync method.

sync_desynced(progress=None, **kwargs)[源代码]

Sync all desynced engines (one-time operation).

参数:
  • progress (Type[Progress]) -- Progress class for reporting. None for silent, TqdmProgress for terminal.

  • **kwargs -- Additional keyword arguments for the engine's sync method.

flush()[源代码]

Flush all storages and engines.

close()[源代码]

Close all storages and engines.

Submodules