Skip to content
fahammohmdPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

KGB: Knowledge Graph Builder

Python 3.10+ FastAPI Neo4j Docker License: MIT

An advanced, locally-hosted ecosystem for transforming unstructured data into interactive, semantically-deduplicated knowledge graphs. KGB leverages the power of local LLMs (via Ollama) and Graph Databases (Neo4j) to provide a private, high-precision knowledge discovery platform.


πŸš€ System Architecture

KGB is built on a modern, asynchronous stack designed for high throughput and local privacy.

  • Core Engine (FastAPI): Orchestrates the extraction pipeline, managing concurrency and state.
  • Graph Intelligence (Neo4j): Stores complex network data with idempotent Cypher operations.
  • Metadata Layer (SQLite + SQLAlchemy): Tracks project settings, extraction history, and audit trails.
  • LLM Integration (Ollama): Executes downloaded local models for entity extraction and embedding generation. Cloud model tags are rejected.
  • Visualization (vis-network): A physics-based frontend for real-time graph exploration.

High-Level Data Flow

Document (PDF/TXT) β†’ Asynchronous Chunking β†’ Two-Pass LLM Extraction β†’ Semantic Deduplication β†’ Idempotent Neo4j Writes β†’ Real-time UI Update


🧠 Advanced Features

1. Two-Pass AI Extraction Pipeline

Unlike naive extraction, KGB uses a multi-stage process to minimize hallucinations and maximize link density:

  • Pass 1 (Entity Identification): The LLM identifies unique entities based on a project-specific schema (e.g., Person, Org, Technology).
  • Pass 2 (Relationship Mapping): A second targeted prompt maps relationships only between the entities discovered in Pass 1, ensuring high-fidelity edges.

2. Semantic Deduplication & Resolution

KGB solves the "entity explosion" problem by merging similar concepts:

  • Vector Embeddings: Generates nomic-embed-text vectors for every entity name.
  • Hybrid Similarity: Combines Cosine Similarity (semantic) with Jaccard Similarity (token overlap) to intelligently cluster and merge nodes (e.g., "Apple" and "Apple Inc.").
  • Cumulative Knowledge: Descriptions from merged entities are appended, preserving contextual richness across different source documents.

3. Live Graph Streaming

Utilizing Server-Sent Events (SSE) and asynchronous generators, the UI updates in real-time as each text chunk is processed. You can watch the graph materialize as the AI reads the document.


🐳 Docker Deployment (Recommended)

The fastest and most secure way to run KGB. A single command brings up the whole stack β€” API, Neo4j, and Ollama β€” with a hardened configuration.

1. Configure

cp .env.docker.example .env.docker
# Set a strong Neo4j password, e.g.:  openssl rand -base64 24
$EDITOR .env.docker

.env.docker is git-ignored β€” secrets never enter the image, the repo, or your shell history.

2. Launch

docker-compose --env-file .env.docker up -d --build

The stack is ready when all services report healthy:

docker-compose --env-file .env.docker ps

The first launch also pulls your configured LLM and embedding models into the Ollama container. Track progress with:

docker-compose --env-file .env.docker logs -f ollama-init

Then open http://localhost:8000.

3. Operate

# Stop the stack           (data persists in named volumes)
docker-compose --env-file .env.docker down

# Stop and wipe all data   (⚠️ destroys graphs, projects, and downloaded models)
docker-compose --env-file .env.docker down -v

Security Model

The deployment is hardened end-to-end:

Layer Hardening
Network isolation Neo4j and Ollama run on an internal network with no published ports; only the API is exposed, bound to 127.0.0.1 by default. Neo4j has no internet egress at all.
Container privileges Every container runs non-root, drops all Linux capabilities, and sets no-new-privileges.
Filesystem The API container has a read-only root filesystem with a writable /tmp tmpfs (noexec) and a dedicated volume for SQLite.
Supply chain Base images are version/digest-pinned; the app image is a minimal multi-stage build with no package manager, shell utilities, or debug tools.
Secrets Credentials are injected at runtime from the git-ignored .env.docker; ${NEO4J_PASSWORD} is mandatory β€” the stack refuses to start without it.
Application CORS is restricted to explicit origins (CORS_ORIGINS), responses carry a restrictive Content Security Policy and other security headers, Cypher identifiers (labels/relationship types) are sanitized against injection, uploads and extraction text are size-capped, and CSV exports are escaped against formula injection.
Availability Healthchecks gate startup ordering; CPU/memory limits cap every container.

Exposing KGB beyond your machine: set APP_BIND=0.0.0.0 in .env.docker only behind a TLS-terminating reverse proxy with access control (e.g., Caddy/nginx + auth), and add your public origin to CORS_ORIGINS.

Browsing Neo4j from your workstation: don't publish its ports β€” tunnel instead: ssh -L 7474:localhost:7474 -L 7687:localhost:7687 <host>, then open http://localhost:7474 (bolt://localhost:7687).


πŸ›  Manual Installation (without Docker)

Prerequisites

1. Clone & Environment

git clone https://github.com/your-repo/KGB.git
cd KGB
python -m venv venv
source venv/bin/activate  # venv\Scripts\activate on Windows
pip install -r requirements.txt

2. Configuration (.env)

# Neo4j
NEO4J_URI=bolt://localhost:7687
NEO4J_USER=neo4j
NEO4J_PASSWORD=your_secure_password

# Ollama
OLLAMA_BASE_URL=http://localhost:11434
OLLAMA_MODEL=llama3.2:3b
OLLAMA_EMBEDDING_MODEL=nomic-embed-text
OLLAMA_NO_CLOUD=1

# Security (optional) β€” comma-separated origins allowed to call the API.
# Defaults to http://localhost:8000,http://127.0.0.1:8000
# CORS_ORIGINS=http://localhost:8000

3. Pull AI Models

ollama pull llama3.2:3b
ollama pull nomic-embed-text

OLLAMA_NO_CLOUD must be applied to the Ollama server process, not only the KGB process. For a shell-managed server, start or restart it with:

OLLAMA_NO_CLOUD=1 ollama serve

For the macOS app, Linux systemd service, or Windows app, configure the server environment as described in Ollama's documentation and restart Ollama. The Docker deployment sets this variable on the Ollama server automatically. KGB also refuses model tags ending in -cloud as a second guardrail.


πŸƒ Running the Application

  1. Initialize Databases:
    python migrate_db.py
  2. Start the Server:
    python -m app.main
  3. Explore: Navigate to http://localhost:8000.

πŸ“‚ Project Structure

β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ routers/          # API Gateway (Project, Extraction, Graph endpoints)
β”‚   β”œβ”€β”€ services/         # Business Logic (Neo4j, Ollama, Entity Resolver)
β”‚   β”œβ”€β”€ utils/            # Utilities (PDF Parsing, Text Chunking)
β”‚   β”œβ”€β”€ models.py         # SQLAlchemy ORM Models
β”‚   └── schemas.py        # Pydantic Data Validation
β”œβ”€β”€ frontend/             # Single-Page Application (Vanilla JS)
β”œβ”€β”€ data/                 # SQLite Persistence
β”œβ”€β”€ Dockerfile            # Hardened multi-stage container image
β”œβ”€β”€ docker-compose.yml    # Secured full-stack deployment (API + Neo4j + Ollama)
β”œβ”€β”€ .env.docker.example   # Template for Docker deployment secrets/config
β”œβ”€β”€ LICENSE               # MIT License
└── repair_project.py     # System Recovery Utility

πŸ›‘ Security & Privacy

KGB is designed for local execution when it is paired with a downloaded local Ollama model and an Ollama server started with OLLAMA_NO_CLOUD=1.

  • KGB rejects Ollama model tags ending in -cloud.
  • The Docker deployment disables Ollama cloud features on the server.
  • Manual installations must configure OLLAMA_NO_CLOUD=1 on the Ollama server itself and restart it; setting it only for KGB does not reconfigure an already-running Ollama process.
  • Keep the API bound to 127.0.0.1. There is currently no user authentication, so do not expose it directly to a LAN or the internet.
  • Do not treat the current prototype as approved for confidential or regulated documents without an independent review of your complete deployment.
  • Treat all AI-generated entities and relationships as unverified leads. Check them against the source documents before relying on them. KGB is not investment advice or a production-grade research database.
  • The Docker deployment adds defense-in-depth: internal-only networks for Neo4j and Ollama, non-root containers with dropped capabilities, a read-only API filesystem, and loopback-only port binding. See the Docker security model.

βš–οΈ License

Distributed under the MIT License. You are free to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of this software β€” attribution required, no warranty provided.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages