Skip to main content

Embedded local-first property graph with vector search

Project description

polypack (Python)

Python bindings for the polypack embedded property-graph and vector-search core. PolyGraph and GraphQuery are a native Python layer mirroring the TypeScript PolyGraph/GraphQuery API; vector indexing, query execution, and directory-backed persistence run in the Rust core (polypack._core, built from crates/polypack-python).

Install

pip install polypack-db

Requires Python 3.8+ and numpy. Prebuilt wheels are published from polypack-db on PyPI; the version tracks the TypeScript package and polypack-core crate in lockstep.

Quick start

import time
from polypack import PolyGraph

graph = PolyGraph()

graph.add_node({
    "id": "doc_1",
    "type": "document",
    "data": {"title": "Quantum Computing"},
    "vector": [0.95, 0.20, 0.10],
    "insertedAt": int(time.time() * 1000),
    "updatedAt": int(time.time() * 1000),
})

graph.add_edge("doc_1", "REFERENCES", "doc_2", ownership="owned")

# Search by similarity
graph.query() \
    .where_type("document") \
    .similar_to([0.90, 0.30, 0.10], 0.5, 5) \
    .to_list()

# Filter, traverse, aggregate
graph.query().where("title", "Quantum Computing").traverse("REFERENCES", 3).to_list()
graph.query().where_type("book").aggregate("price", "avg")

Persistence

PolyGraph.open(directory) attaches a directory-backed binary store (snapshot + write-ahead log, shared format with the TypeScript BinaryStoreAdapter) and loads any existing state. Use it as a context manager so the store is compacted and closed on exit:

with PolyGraph.open("./data") as graph:
    graph.add_node({...})
    graph.save()  # persists queued puts and deletions

Activation (adaptive memory)

Nodes can carry durable activation state that persists through the store. Reinforcement decay-corrects the prior state, adds a delta, and re-anchors the decay clock; reads decay lazily as a pure function of elapsed time:

from polypack import PolyGraph, ActivationEngine

graph = PolyGraph()
graph.add_node({"id": "a", "type": "doc", "data": {}, "insertedAt": 1, "updatedAt": 1})
graph.reinforce_node("a", 0.6, "user_read")      # durable, persisted
graph.get_activation("a")                         # ~0.6, decay-corrected
graph.top_activated(5)                            # working-memory primitive

engine = ActivationEngine(graph)
engine.bump_attention("a", 0.2)                  # local, never serialized
engine.spread(["a"], depth=2, decay=0.5)         # relational spreading activation
engine.pulse([1.0, 0.0, ...])                    # semantic region scoring (vector)
engine.absorb([1.0, 0.0, ...])                   # pulse + reinforce above threshold
engine.working_memory(5)                         # current "mental state"

graph.query().where_activated(0.4) and graph.query().order_by_activation() filter/order by current activation. merge_activation and decay_factor are exposed for parity with the TypeScript/Rust implementations.

save() writes the complete current graph and flushes any deletions recorded since the last save. close_store() — and exiting the with block, which calls it — saves any unsaved changes first, then compacts and closes; you don't need to call save() yourself before it, though doing so periodically (as above) still bounds how much a single close has to write and how much a crash before that could lose.

Vector indexes

ExactIndex and HnswIndex wrap the Rust vector core directly and can be used standalone, independent of PolyGraph:

from polypack import ExactIndex, HnswIndex

index = ExactIndex(distance="cosine")
index.add("doc_1", [0.95, 0.20, 0.10])
index.query([0.90, 0.30, 0.10], top_k=5, threshold=0.5)

approx = HnswIndex(m=16, ef_search=200)
approx.add_many([("doc_1", [0.95, 0.20, 0.10]), ("doc_2", [0.10, 0.90, 0.05])])

Errors

Native errors surface as PolypackError subclasses: PolypackValueError, PolypackDimensionError, PolypackClosedError, PolypackVersionError, PolypackCorruptDataError, and PolypackStorageError.

Development

See the top-level CONTRIBUTING.md for building the native extension with maturin and running the Python test suite.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

polypack_db-2.5.0-cp38-abi3-win_amd64.whl (484.2 kB view details)

Uploaded CPython 3.8+Windows x86-64

polypack_db-2.5.0-cp38-abi3-manylinux_2_34_x86_64.whl (634.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.34+ x86-64

polypack_db-2.5.0-cp38-abi3-macosx_11_0_arm64.whl (573.9 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file polypack_db-2.5.0-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: polypack_db-2.5.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 484.2 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for polypack_db-2.5.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c8c091ad2add98ad28ccd905028388c1594c717d441f406efba189dc23a705e2
MD5 9eea5c51ce21626a5362af239b0526a9
BLAKE2b-256 39edbc30dfd74bfa6b46faafbb52b8fba5a928ed8eeb3e44df34c86eaa8af232

See more details on using hashes here.

Provenance

The following attestation bundles were made for polypack_db-2.5.0-cp38-abi3-win_amd64.whl:

Publisher: release.yml on imattau/polypack

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polypack_db-2.5.0-cp38-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for polypack_db-2.5.0-cp38-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 f77c19257b76c3329dcd8979e9262e90f4e06f61e49892411a6233fe0d91197c
MD5 a5c836132975e93e7de2a57889e2dacb
BLAKE2b-256 ba5a1d71b565be6a8b65f621b3b9c5e65cf4a3e45ee2727c42990301326fa54d

See more details on using hashes here.

Provenance

The following attestation bundles were made for polypack_db-2.5.0-cp38-abi3-manylinux_2_34_x86_64.whl:

Publisher: release.yml on imattau/polypack

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polypack_db-2.5.0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polypack_db-2.5.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ccd8cc7d48c68cd08059b06f40b464b264acaadba86b2823c7e8f1c756e54d82
MD5 9c8aa304aab2293b162a9d9b980c3d0a
BLAKE2b-256 f2e5f34e5971abce237b52158ba7c6b1400d6fa6dbb5ba0159fac1ce7161e775

See more details on using hashes here.

Provenance

The following attestation bundles were made for polypack_db-2.5.0-cp38-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on imattau/polypack

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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