Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:24kchengYe MemoMind process entities batch

From Leeroopedia
Knowledge Sources
Domains NLP, Knowledge_Graph
Last Updated 2026-10-28 00:00 GMT

Overview

Concrete tool for extracting, merging, and resolving entity mentions from stored facts into canonical entities using the EntityResolver with spaCy NER and fuzzy matching.

Description

The process_entities_batch function processes entities for all facts in a retain batch. It:

  1. Extracts entity mentions from fact texts (LLM-extracted entities are already in ProcessedFact)
  2. Merges LLM-extracted entities with user-provided entities (case-insensitive deduplication)
  3. Delegates to link_utils.extract_entities_batch_optimized which uses the EntityResolver to resolve each mention to a canonical entity ID
  4. Returns EntityLink objects ready for batch insertion into the unit_entities table

The EntityResolver supports two lookup strategies:

  • full: loads all bank entities into memory, matches in Python (fast for small banks)
  • trigram: uses pg_trgm GIN index to fetch only similar candidates per entity name (scalable for large banks)

After resolution, entity mention counts and co-occurrence statistics are tracked for graph traversal.

Usage

Import this function when you need to link stored facts to canonical entities in the knowledge graph. It is called by the retain orchestrator after facts are stored and unit IDs are assigned.

Code Reference

Source Location

  • Repository: MemoMind
  • File: engine/hindsight_api/engine/retain/entity_processing.py
  • Lines: L15-97 (delegates to link_utils.py:L143 and entity_resolver.py:L156-180)

Signature

async def process_entities_batch(
    entity_resolver,
    conn,
    bank_id: str,
    unit_ids: list[str],
    facts: list[ProcessedFact],
    log_buffer: list[str] = None,
    user_entities_per_content: dict[int, list[dict]] = None,
    entity_labels: list | None = None,
) -> list[EntityLink]:

Import

from hindsight_api.engine.retain.entity_processing import process_entities_batch

I/O Contract

Inputs

Name Type Required Description
entity_resolver EntityResolver Yes EntityResolver instance with spaCy NER and fuzzy matching
conn asyncpg.Connection Yes Database connection
bank_id str Yes Bank identifier
unit_ids list[str] Yes Stored fact unit IDs (same length as facts)
facts list[ProcessedFact] Yes Processed facts with entities
user_entities_per_content dict[int, list[dict]] No User-provided entities keyed by content_index
entity_labels list No Controlled vocabulary labels

Outputs

Name Type Description
entity_links Entity links ready for batch insertion into unit_entities table

Usage Examples

Entity Resolution After Fact Storage

import asyncio
from hindsight_api.engine.retain.entity_processing import process_entities_batch, insert_entity_links_batch
from hindsight_api.engine.entity_resolver import EntityResolver

async def main():
    entity_resolver = EntityResolver(pool, entity_lookup="trigram")

    # After facts are stored and unit_ids are assigned
    entity_links = await process_entities_batch(
        entity_resolver=entity_resolver,
        conn=conn,
        bank_id="default",
        unit_ids=unit_ids,
        facts=processed_facts,
        user_entities_per_content={0: [{"text": "Alice Smith", "type": "PERSON"}]},
    )

    # Insert entity links into database
    await insert_entity_links_batch(conn, entity_links)
    print(f"Created {len(entity_links)} entity links")

asyncio.run(main())

Related Pages

Implements Principle

Requires Environment

Uses Heuristics

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment