ahvn.klengine.mongo_engine module¶
MongoDB-based KL engine implementation.
- class ahvn.klengine.mongo_engine.MongoKLEngine(storage, inplace=True, include=None, exclude=None, filters=None, name=None, condition=None, encoder=None, embedder=False, *args, **kwargs)[源代码]¶
基类:
BaseKLEngineA MongoDB-based search KLEngine implementation that provides multiple search interfaces.
This class extends BaseKLEngine with specialized search methods: - MongoDB query language (MQL) search through _search_mql method - Vector similarity + filter hybrid search through _search_vector method (requires encoder/embedder) - Default search delegates to vector search
The engine supports two modes: - inplace=True: Search directly on storage MongoDB collection (storage must be MongoKLStore) - inplace=False: Create separate MongoDB collection for indexing with custom field selection
Vector Search Support: - Requires encoder and embedder to be configured - Uses MongoDB's $vectorSearch for efficient similarity search - Combines vector search with MQL filters for hybrid retrieval
- Search Methods:
_search_mql(mql, include, *args, **kwargs): Raw MQL query search. _search_vector(query, topk, fetchk, include, **kwargs): Execute hybrid vector + filter search. _search = _search_vector: Alias for _search_vector for default search behavior.
- Abstract Methods (inherited from BaseKLEngine):
_upsert(kl): Insert or update a KL in the engine. _remove(key): Remove a KL from the engine by its key (id). _clear(): Clear all KLs from the engine.
- 参数:
- __init__(storage, inplace=True, include=None, exclude=None, filters=None, name=None, condition=None, encoder=None, embedder=False, *args, **kwargs)[源代码]¶
Initialize the MongoKLEngine.
- 参数:
storage (
BaseKLStore) -- Attach MongoKLEngine to a KLStore. For inplace=True, must be a MongoKLStore instance. For inplace=False, can be any KLStore.inplace (
bool) -- If True, search directly on storage MongoDB collection; if False, create a separate MongoDB collection for indexing.include (if inplace=False) -- List of BaseUKF field names to include. If None, includes all fields. Default is None.
exclude (if inplace=False) -- List of BaseUKF field names to exclude. If None, excludes no fields. Default is None. Notice that exclude is applied after include, so if a field is in both, it will be excluded. It is recommended to use only one of include or exclude.
filters (
Optional[dict]) -- Global filters that will be applied to all searches.name (
Optional[str]) -- Name of the KLEngine instance. If None, defaults to "{storage.name}_mongo_idx" for non-inplace mode.condition (
Optional[Callable]) -- Optional upsert/insert condition to apply to the KLEngine. KLs that do not satisfy the condition will be ignored. If None, all KLs are accepted.encoder (
Optional[Callable]) -- Optional encoder function for converting queries/KLs to text. If None, a default provider will be used.embedder (
Optional[Callable]) -- Optional embedder function for converting encoded text to vectors. If False, vector search is not available. If None, a default embedder will be used.*args -- Additional positional arguments passed to MongoKLEngine.
**kwargs -- Additional keyword arguments passed to MongoKLEngine.
- batch_q_embed(encoded_queries)[源代码]¶
Batch generate embeddings from encoded query texts using q_embedder.