Skip to main content

Spreading activation retrieval for document collections

Project description

Pensive

Spreading activation retrieval for document collections. Sub-millisecond queries at 50M+ documents.

Pensive builds a sparse entity graph from your documents using regex-based extraction, then retrieves answers via spreading activation -- a biologically-inspired algorithm where query terms "light up" connected entities and the activation spreads to relevant answers.

Install

pip install pensive

Quickstart

from pensive import SpreadingActivation

sa = SpreadingActivation()
sa.build([
    {'id': '1', 'content': 'The P99 latency was 42ms on 2025-10-08', 'value': '42ms on 2025-10-08'},
    {'id': '2', 'content': 'GPU temp hit 82C during the training run', 'value': '82C during training'},
    {'id': '3', 'content': 'Meeting with Sarah Chen about Project Atlas budget', 'value': 'Atlas budget meeting'},
])

results = sa.query("What was the P99 latency?")
# [('42ms on 2025-10-08', 1.623), ...]

Parallel Build (large corpora)

# 14x faster at 1M docs using multiprocessing
sa = SpreadingActivation()
sa.build_parallel(documents, workers=8)

Ingestion from Data Exports

from pensive.ingestion import IngestPipeline
from pensive.ingestion.parsers.chatgpt import ChatGPTParser
from pensive.ingestion.parsers.facebook import FacebookParser

pipe = IngestPipeline()
pipe.ingest_all([
    ChatGPTParser("/path/to/chatgpt-export/"),
    FacebookParser("/path/to/facebook-export/"),
])

# Query
results = pipe.sa.query("What did we talk about last week?")

# Save/load
pipe.save_graph("my_graph.pkl")
pipe = IngestPipeline.load_graph("my_graph.pkl")

CLI

pensive build --chatgpt ~/chatgpt-export/ --facebook ~/fb-export/ -o graph.pkl
pensive query --graph graph.pkl "What was the deployment date?"
pensive stats --graph graph.pkl

Document Format

Each document is a dict with:

  • id (str): Unique identifier
  • content (str): Text to extract entities from
  • value (str): The answer/snippet to retrieve
  • query (str, optional): Additional text for entity extraction

Configuration

from pensive import SpreadingActivation, SpreadingConfig

sa = SpreadingActivation(config=SpreadingConfig(
    max_hops=2,        # Spreading depth (default: 4)
    max_active=200,    # Max active nodes per hop (default: 50)
    decay=0.6,         # Activation decay per hop (default: 0.6)
    threshold=0.15,    # Min activation to keep spreading (default: 0.15)
))

Contextual Disambiguation

Provide conversation context to disambiguate queries:

results = sa.query(
    "What was the temperature?",
    context=["GPU", "training run"]  # Disambiguates toward GPU temp, not weather
)

Scale Characteristics

Scale Query Latency Peak RSS Build Time (parallel)
1M docs ~1ms 13 GB ~12s
5M docs ~0.4ms 16 GB ~5 min
10M docs ~0.4ms 30 GB ~10 min
50M docs ~0.45ms 139 GB ~28 min

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypensive-0.1.1.tar.gz (38.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pypensive-0.1.1-py3-none-any.whl (45.1 kB view details)

Uploaded Python 3

File details

Details for the file pypensive-0.1.1.tar.gz.

File metadata

  • Download URL: pypensive-0.1.1.tar.gz
  • Upload date:
  • Size: 38.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for pypensive-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6e7db5c1180c567c02176567d8d7cca286fd35d71fe1b5542c0b4aaa137f9ced
MD5 884effddf3d816907ec8cfeb65ac1495
BLAKE2b-256 3ab71172430d607b979e78132625301e355c4040aa5b09b4593ff646ed2837e7

See more details on using hashes here.

File details

Details for the file pypensive-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pypensive-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 45.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for pypensive-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0d26cca26d019d213cf9923ef7860d82b1541f293bfd2b2a2ac0685900ee4c2b
MD5 67b8a48e6e9dbea49574b02f270c0a84
BLAKE2b-256 96158bf89df9c5ad3f469a098bbfe6fd1c7b319c641bfddefdc8c95331008c8f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page