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 extract facts from contents

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

Overview

Concrete tool for extracting structured facts from raw text content using LLM-based extraction, provided by the MemoMind retain pipeline.

Description

The extract_facts_from_contents function is the entry point of the fact extraction stage in the retain pipeline. It takes a list of RetainContent objects (raw text with context and metadata), sends each to an LLM for fact extraction in parallel, and returns typed ExtractedFactType objects with fact text, type, entities, temporal fields, and causal relations.

The function handles:

  • Parallel extraction: all content items are processed concurrently via `asyncio.gather`
  • Chunk tracking: tracks which facts came from which chunks for provenance
  • Temporal ordering: adds time offsets to facts to preserve ordering within and across contents
  • Label auto-tagging: injects entity label tags (key:value pairs) into fact tags for filtering
  • Batch API routing: delegates to a batch API mode when `config.retain_batch_enabled` is True

Usage

Import this function when you need to transform raw text content into structured, typed facts for storage in the memory knowledge graph. It is called by the retain orchestrator as the first stage of the retain pipeline.

Code Reference

Source Location

  • Repository: MemoMind
  • File: engine/hindsight_api/engine/retain/fact_extraction.py
  • Lines: L1892-2097

Signature

async def extract_facts_from_contents(
    contents: list[RetainContent],
    llm_config,
    agent_name: str,
    config,
    pool=None,
    operation_id: str | None = None,
    schema: str | None = None,
) -> tuple[list[ExtractedFactType], list[ChunkMetadata], TokenUsage]:

Import

from hindsight_api.engine.retain.fact_extraction import extract_facts_from_contents

I/O Contract

Inputs

Name Type Required Description
contents list[RetainContent] Yes Content items to extract facts from (content text, context, event_date, metadata, entities, tags)
llm_config LLMConfig Yes LLM configuration for extraction calls
agent_name str Yes Name of the agent for agent-related fact detection
config HindsightConfig Yes Resolved config for this bank
pool asyncpg.Pool No DB connection pool (passed to batch API mode)
operation_id str No Async operation ID for crash recovery
schema str No Database schema for multi-tenant support

Outputs

Name Type Description
extracted_facts list[ExtractedFactType] Typed fact objects with text, type, entities, temporal fields, causal relations
chunks_metadata list[ChunkMetadata] Chunk provenance (chunk text, fact count, content/chunk indices)
usage TokenUsage LLM token usage statistics

Usage Examples

Basic Fact Extraction

import asyncio
from hindsight_api.engine.retain.fact_extraction import extract_facts_from_contents
from hindsight_api.engine.retain.types import RetainContent

async def main():
    contents = [
        RetainContent(
            content="Alice told me she got a promotion at Acme Corp yesterday.",
            context="team-chat",
            event_date="2024-06-15T10:00:00Z",
            tags=["work", "alice"],
        ),
    ]

    facts, chunks, usage = await extract_facts_from_contents(
        contents=contents,
        llm_config=llm_config,
        agent_name="assistant",
        config=bank_config,
    )

    for fact in facts:
        print(f"[{fact.fact_type}] {fact.fact_text}")
        print(f"  Entities: {fact.entities}")
        print(f"  Occurred: {fact.occurred_start}")

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