Skip to main content

GraphRAG-style Python SDK for GibRAM - Graph in-Buffer Retrieval & Associative Memory

Project description

GibRAM Python SDK v0.3.0

GraphRAG-style knowledge graph indexing with automatic entity extraction, relationship detection, and community discovery.

Installation

pip install gibram

Quick Start

from gibram import GibRAMIndexer

# Initialize indexer with OpenAI
indexer = GibRAMIndexer(
    session_id="my-project",
    llm_api_key="sk-...",  # or set OPENAI_API_KEY env
)

# Index documents (automatic chunking, extraction, embedding)
stats = indexer.index_documents([
    "Einstein was born in 1879 in Ulm, Germany.",
    "He developed the theory of relativity in 1905.",
    "Einstein received the Nobel Prize in Physics in 1921.",
])

print(f"Indexed {stats.entities_extracted} entities in {stats.indexing_time_seconds:.2f}s")

# Query knowledge graph
result = indexer.query("Einstein's achievements", top_k=5)

for entity in result.entities:
    print(f"{entity.title} ({entity.type}): {entity.description}")

Configuration

Environment Variables

export OPENAI_API_KEY="sk-..."

Initialization Parameters

indexer = GibRAMIndexer(
    # Required
    session_id="unique-project-id",
    
    # Server connection
    host="localhost",
    port=6161,
    
    # LLM configuration
    llm_provider="openai",           # Only OpenAI supported currently
    llm_api_key="sk-...",            # Auto-detect from OPENAI_API_KEY
    llm_model="gpt-4o",              # GPT-4o recommended
    
    # Embedding configuration
    embedding_provider="openai",
    embedding_model="text-embedding-3-small",
    embedding_dimensions=1536,       # Must match server config
    
    # Chunking configuration
    chunk_size=512,                  # Tokens per chunk
    chunk_overlap=50,                # Overlap between chunks
    
    # Community detection
    auto_detect_communities=True,    # Auto-run after indexing
    community_resolution=1.0,        # Leiden algorithm resolution
)

API Reference

GibRAMIndexer

Main class for indexing and querying.

index_documents(documents, batch_size=10, show_progress=True) -> IndexStats

Index documents into knowledge graph.

Arguments:

  • documents: List of strings or dicts {"id": ..., "text": ..., "metadata": ...}
  • batch_size: Batch size for LLM/API calls (default: 10)
  • show_progress: Show progress bar (default: True)

Returns: IndexStats with counts and timing

Pipeline:

  1. Chunk documents → TextUnits
  2. Extract entities & relationships (LLM)
  3. Generate embeddings
  4. Store in graph
  5. Link entities to text units
  6. Detect communities (if enabled)

Example:

stats = indexer.index_documents([
    {"id": "doc1", "text": "...", "metadata": {"source": "wiki"}},
    {"id": "doc2", "text": "..."},
])

query(query, mode="local", top_k=10, include_entities=True, include_text_units=True, include_communities=False) -> QueryResult

Query knowledge graph.

Arguments:

  • query: Natural language query
  • mode: Query mode (currently only supports "local")
  • top_k: Number of results (default: 10)
  • include_entities: Include entity results
  • include_text_units: Include text unit results
  • include_communities: Include community results

Returns: QueryResult with scored results

Example:

result = indexer.query("machine learning applications", top_k=5)

for entity in result.entities:
    print(f"{entity.title}: {entity.score:.3f}")

for text_unit in result.text_units:
    print(f"{text_unit.content[:100]}... (score: {text_unit.score:.3f})")

get_stats() -> IndexStats

Get current indexing statistics.

close()

Close connection to server.

Types

IndexStats

@dataclass
class IndexStats:
    documents_indexed: int = 0
    text_units_created: int = 0
    entities_extracted: int = 0
    relationships_extracted: int = 0
    communities_detected: int = 0
    indexing_time_seconds: float = 0.0

QueryResult

@dataclass
class QueryResult:
    entities: List[ScoredEntity]
    text_units: List[ScoredTextUnit]
    communities: List[ScoredCommunity]
    execution_time_ms: float

ScoredEntity

@dataclass
class ScoredEntity:
    id: int
    title: str
    type: str
    description: str
    score: float  # Similarity score

Exceptions

All exceptions inherit from GibRAMError:

  • ConnectionError: Server connection failed
  • TimeoutError: Operation timed out
  • ProtocolError: Protocol encoding/decoding error
  • ServerError: Server returned error
  • NotFoundError: Resource not found
  • ValidationError: Input validation failed
  • ExtractionError: LLM extraction failed
  • EmbeddingError: Embedding generation failed
  • ConfigurationError: Invalid configuration

Advanced Usage

Custom Extractors

Implement BaseExtractor for custom entity/relationship extraction:

from gibram.extractors import BaseExtractor
from gibram.types import ExtractedEntity, ExtractedRelationship

class MyExtractor(BaseExtractor):
    def extract(self, text: str) -> tuple[list[ExtractedEntity], list[ExtractedRelationship]]:
        # Your custom logic
        entities = [...]
        relationships = [...]
        return entities, relationships

indexer = GibRAMIndexer(
    session_id="custom",
    extractor=MyExtractor(),
    embedder=...,  # Still need embedder
)

Custom Embedders

Implement BaseEmbedder for custom embeddings:

from gibram.embedders import BaseEmbedder

class MyEmbedder(BaseEmbedder):
    def embed(self, texts: list[str]) -> list[list[float]]:
        # Your custom logic
        return [[0.1, 0.2, ...], ...]
    
    def embed_single(self, text: str) -> list[float]:
        return self.embed([text])[0]

indexer = GibRAMIndexer(
    session_id="custom",
    embedder=MyEmbedder(),
)

Context Manager

Use context manager for automatic cleanup:

with GibRAMIndexer(session_id="project") as indexer:
    stats = indexer.index_documents(documents)
    result = indexer.query("some query")
    # Connection automatically closed

Requirements

  • Python 3.8+
  • GibRAM server running (Docker recommended)
  • OpenAI API key (for extraction & embeddings)

Server Setup

Start GibRAM server with Docker:

docker run -d \
  --name gibram-server \
  -p 6161:6161 \
  -e EMBEDDING_DIM=1536 \
  gibram:latest

License

MIT

Version

v0.3.0 - Current release v0.1.0 - Initial release with OpenAI extraction & embeddings

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

gibram-0.3.0.tar.gz (26.3 kB view details)

Uploaded Source

Built Distribution

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

gibram-0.3.0-py3-none-any.whl (28.3 kB view details)

Uploaded Python 3

File details

Details for the file gibram-0.3.0.tar.gz.

File metadata

  • Download URL: gibram-0.3.0.tar.gz
  • Upload date:
  • Size: 26.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for gibram-0.3.0.tar.gz
Algorithm Hash digest
SHA256 35403b1356438d55e30dfcf15718f9f9f4741e2bac2d77436b16b7b9294dd2ab
MD5 611d9862d1e60ab630ed308241885825
BLAKE2b-256 f3e81d9e676f85939ed423a7477ad7245156ca5e7e81f744b210b9e5161add36

See more details on using hashes here.

Provenance

The following attestation bundles were made for gibram-0.3.0.tar.gz:

Publisher: publish.yml on gibram-io/gibram

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

File details

Details for the file gibram-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: gibram-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 28.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for gibram-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec2d083961192912b71090b3e5820a43cd4b30fb1fab23bc25f9db0618d470e6
MD5 d9a6f866423c72c3eea74411f1f59d04
BLAKE2b-256 48d225f11769c06d005e75a5009f7a3852693f5496c8105584286cb34006a322

See more details on using hashes here.

Provenance

The following attestation bundles were made for gibram-0.3.0-py3-none-any.whl:

Publisher: publish.yml on gibram-io/gibram

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