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 create links batch

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

Overview

Concrete tool for creating temporal, semantic, and causal links between newly stored facts in the memory knowledge graph.

Description

The link creation module provides three functions that create edges in the memory_links table:

  • create_temporal_links_batch: links facts that occurred close in time. Delegates to `link_utils.create_temporal_links_batch_per_fact`.
  • create_semantic_links_batch: links facts with high embedding cosine similarity (top_k=5, threshold=0.7). Delegates to `link_utils.create_semantic_links_batch`.
  • create_causal_links_batch: links facts where the LLM identified explicit causal relations (causes, enables, prevents). Delegates to `link_utils.create_causal_links_batch`.

All three functions validate that unit_ids and facts/embeddings have matching lengths and return integer counts of links created.

Usage

Import these functions when you need to build the graph structure connecting stored facts. They are called by the retain orchestrator after facts are stored, embedded, and entities are resolved.

Code Reference

Source Location

  • Repository: MemoMind
  • File: engine/hindsight_api/engine/retain/link_creation.py
  • Lines: L15-99 (delegates to link_utils.py:L394, L521-547, L730)

Signature

async def create_temporal_links_batch(
    conn, bank_id: str, unit_ids: list[str]
) -> int

async def create_semantic_links_batch(
    conn, bank_id: str, unit_ids: list[str], embeddings: list[list[float]]
) -> int

async def create_causal_links_batch(
    conn, unit_ids: list[str], facts: list[ProcessedFact]
) -> int

Import

from hindsight_api.engine.retain.link_creation import (
    create_temporal_links_batch,
    create_semantic_links_batch,
    create_causal_links_batch,
)

I/O Contract

Inputs

Name Type Required Description
conn asyncpg.Connection Yes Database connection
bank_id str Yes Bank identifier (temporal, semantic)
unit_ids list[str] Yes Newly stored fact IDs
embeddings list[list[float]] Semantic only Embedding vectors (same length as unit_ids)
facts list[ProcessedFact] Causal only Facts with causal_relations metadata

Outputs

Name Type Description
link_count int Number of links created in memory_links table

Usage Examples

Creating All Link Types

import asyncio
from hindsight_api.engine.retain.link_creation import (
    create_temporal_links_batch,
    create_semantic_links_batch,
    create_causal_links_batch,
)

async def main():
    # After facts are stored with unit_ids, embeddings, and entities resolved
    temporal_count = await create_temporal_links_batch(conn, "default", unit_ids)
    print(f"Created {temporal_count} temporal links")

    semantic_count = await create_semantic_links_batch(conn, "default", unit_ids, embeddings)
    print(f"Created {semantic_count} semantic links")

    causal_count = await create_causal_links_batch(conn, unit_ids, processed_facts)
    print(f"Created {causal_count} causal links")

asyncio.run(main())

Related Pages

Implements Principle

Requires Environment

Page Connections

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