Skip to main content

Advanced Knowledge Graph Engine with semantic search and temporal tracking

Project description

Knowledge Graph Engine v2

Modern Neo4j-based knowledge graph engine with semantic search capabilities and intelligent relationship management.

๐ŸŽฏ Overview

A production-ready knowledge graph system built entirely on Neo4j for persistent graph storage and vector search. Combines graph database operations with semantic vector search to provide intelligent information storage, retrieval, and reasoning.

โœจ Key Features

  • ๐Ÿ—๏ธ Neo4j-Native Architecture: Complete Neo4j integration for both graph and vector operations
  • ๐Ÿ” Enhanced Semantic Search: Improved vector search with dynamic thresholds and contextual boosting
  • ๐Ÿค– LLM Integration: OpenAI/Ollama support for entity extraction and query processing
  • โš”๏ธ Conflict Resolution: Intelligent handling of contradicting information with temporal tracking
  • โฐ Temporal Tracking: Complete relationship history with date ranges and conflict resolution
  • ๐ŸŽฏ Smart Query Understanding: Context-aware search with semantic category matching
  • ๐Ÿ“Š Optimized Performance: Lower similarity thresholds (0.3) for better recall
  • ๐Ÿš€ Production Ready: ACID compliance, comprehensive error handling, modern architecture

๐Ÿ“ Project Structure

src/                                  # Main source directory
โ”œโ”€โ”€ kg_engine/                        # Knowledge Graph Engine
โ”‚   โ”œโ”€โ”€ core/                         # Core engine
โ”‚   โ”‚   โ””โ”€โ”€ engine.py                 # Main KG Engine
โ”‚   โ”œโ”€โ”€ models/                       # Data models
โ”‚   โ”‚   โ””โ”€โ”€ models.py                 # Graph data structures
โ”‚   โ”œโ”€โ”€ storage/                      # Storage components
โ”‚   โ”‚   โ”œโ”€โ”€ graph_db.py               # Neo4j graph operations
โ”‚   โ”‚   โ”œโ”€โ”€ neo4j_vector_store.py     # Vector storage
โ”‚   โ”‚   โ”œโ”€โ”€ vector_store.py           # Vector store interface
โ”‚   โ”‚   โ””โ”€โ”€ ...                       # Other storage components
โ”‚   โ”œโ”€โ”€ llm/                          # LLM integration
โ”‚   โ”‚   โ””โ”€โ”€ llm_interface.py          # OpenAI/Ollama interface
โ”‚   โ”œโ”€โ”€ config/                       # Configuration
โ”‚   โ”‚   โ”œโ”€โ”€ neo4j_config.py           # Neo4j settings
โ”‚   โ”‚   โ””โ”€โ”€ neo4j_schema.py           # Schema management
โ”‚   โ””โ”€โ”€ utils/                        # Utilities
โ”‚       โ”œโ”€โ”€ date_parser.py            # Date parsing
โ”‚       โ””โ”€โ”€ ...                       # Other utilities
โ”œโ”€โ”€ examples/                         # Usage examples
โ”‚   โ”œโ”€โ”€ examples.py                   # Basic examples
โ”‚   โ”œโ”€โ”€ bio_example.py                # Biographical demo
โ”‚   โ””โ”€โ”€ simple_bio_demo.py            # Simple demo
โ””โ”€โ”€ test_neo4j_integration.py         # Test suite

docs/                                 # Comprehensive documentation
โ”œโ”€โ”€ architecture/                     # System design
โ”œโ”€โ”€ user-guide/                       # Getting started
โ”œโ”€โ”€ api/                              # API reference
โ””โ”€โ”€ development/                      # Development guides

๐Ÿš€ Quick Start

Prerequisites

# Install Neo4j (required)
docker run --name neo4j -p7474:7474 -p7687:7687 -d \
    -e NEO4J_AUTH=neo4j/password \
    neo4j:latest

Installation

pip install -e .

Basic Usage

from src.kg_engine import KnowledgeGraphEngineV2, InputItem
from src.kg_engine.config import Neo4jConfig

# Initialize with Neo4j
engine = KnowledgeGraphEngineV2(
    api_key="your-openai-key",  # or "ollama" for local LLM
    neo4j_config=Neo4jConfig()
)

# Add knowledge
result = engine.process_input([
    InputItem(description="Alice works as a software engineer at Google"),
    InputItem(description="Bob lives in San Francisco")
])

# Search with natural language
response = engine.search("Who works at Google?")
print(response.answer)  # "Alice works as a software engineer at Google."

๐Ÿค– LLM Setup Options

Option 1: OpenAI (Recommended for Production)

export OPENAI_API_KEY="your-api-key"
engine = KnowledgeGraphEngineV2(
    api_key="your-openai-key",
    model="gpt-4.1-nano"  # Fast and cost-effective
)

Option 2: Local Ollama (Privacy & Cost-Free)

# Install Ollama
curl -fsSL https://ollama.ai/install.sh | sh

# Start server
ollama serve

# Pull a model
ollama pull llama3.2:3b  # Recommended: good balance of size/performance
engine = KnowledgeGraphEngineV2(
    api_key="ollama",
    base_url="http://localhost:11434/v1",
    model="llama3.2:3b"
)

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   LLM Interface โ”‚    โ”‚   Graph Database โ”‚    โ”‚  Vector Store   โ”‚
โ”‚                 โ”‚    โ”‚                  โ”‚    โ”‚                 โ”‚
โ”‚ โ€ข Entity Extractโ”‚    โ”‚ โ€ข Neo4j Native   โ”‚    โ”‚ โ€ข Neo4j Vectors โ”‚
โ”‚ โ€ข Query Parse   โ”‚    โ”‚ โ€ข Conflict Det.  โ”‚    โ”‚ โ€ข Semantic      โ”‚
โ”‚ โ€ข Answer Gen.   โ”‚    โ”‚ โ€ข Temporal Track โ”‚    โ”‚ โ€ข Search        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚                       โ”‚                       โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚ KG Engine v2        โ”‚
                    โ”‚                     โ”‚
                    โ”‚ โ€ข Process Input     โ”‚
                    โ”‚ โ€ข Smart Updates     โ”‚
                    โ”‚ โ€ข Hybrid Search     โ”‚
                    โ”‚ โ€ข Natural Language  โ”‚
                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“Š Advanced Features

Intelligent Conflict Resolution

# Initial information
engine.process_input([InputItem(description="Alice lives in Boston")])

# Update with conflicting information (automatically resolves)
engine.process_input([InputItem(description="Alice moved to Seattle in 2024")])

# System automatically:
# 1. Marks old relationship as obsolete
# 2. Adds new relationship as active
# 3. Maintains complete history

Enhanced Semantic Search

# Improved semantic understanding with contextual boosting
response = engine.search("Who works in technology?")
# โœ… Returns all tech workers including software engineers, developers

response = engine.search("Who was born in Europe?")
# โœ… Returns all European births: Berlin, Lyon, Barcelona, Paris

response = engine.search("What do people do for hobbies?")
# โœ… Returns all "enjoys" relationships with boosted relevance scores

Temporal Relationship Tracking

# Natural language dates
engine.process_input([
    InputItem(description="Project started", from_date="2 months ago"),
    InputItem(description="Alice joined", from_date="last week")
])

๐Ÿ“š Documentation

๐Ÿšฆ Running Examples

# Run basic examples
python src/examples/examples.py

# Run biographical knowledge graph demo  
python src/examples/simple_bio_demo.py

# Verify project structure
python verify_structure.py

Expected output:

โœ… Neo4j connection verified
๐Ÿš€ Knowledge Graph Engine v2 initialized
   - Vector store: kg_v2 (neo4j)
   - Graph database: Neo4j (persistent)
   
=== Example: Semantic Relationship Handling ===
1. Adding: John Smith teaches at MIT
   Result: 1 new edge(s) created
...

๐Ÿ” Search Capabilities

The Knowledge Graph Engine v2 features advanced semantic search with:

  • Dynamic Similarity Thresholds: Base threshold of 0.3 with context-specific adjustments
  • Semantic Category Matching: Understands relationships between concepts (e.g., "technology" โ†’ "software engineer")
  • Query-Specific Boosting: Different query types get tailored relevance scoring
  • Geographic Intelligence: Recognizes European cities and other geographic relationships
  • Contextual Understanding: Distinguishes between work, hobbies, locations, and other relationship types

Example Queries

# Technology and profession queries
"Who works in technology?" โ†’ Finds software engineers, developers, tech professionals
"Tell me about engineers" โ†’ Returns all engineering-related professions

# Geographic queries  
"Who was born in Europe?" โ†’ Finds Berlin, Lyon, Barcelona, Paris births
"Who lives in Paris?" โ†’ Returns all Paris residents

# Activity and interest queries
"What do people do for hobbies?" โ†’ Returns all "enjoys" relationships
"Tell me about photographers" โ†’ Finds people who enjoy or specialize in photography

# Entity-specific queries
"Tell me about Emma Johnson" โ†’ Returns all relationships for Emma

PyPI Release Steps:

  1. Update version (currently 2.1.1 in both files)

  2. Clean and build: rm -rf dist/ build/ python -m build

  3. Test locally: pip install dist/*.whl python -c "from kg_engine import KnowledgeGraphEngineV2; print('Import successful')"

  4. twine upload dist/* Upload to production PyPI twine upload dist/*

License

MIT License

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

kg_engine_v2-2.1.3.tar.gz (81.2 kB view details)

Uploaded Source

Built Distribution

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

kg_engine_v2-2.1.3-py3-none-any.whl (50.6 kB view details)

Uploaded Python 3

File details

Details for the file kg_engine_v2-2.1.3.tar.gz.

File metadata

  • Download URL: kg_engine_v2-2.1.3.tar.gz
  • Upload date:
  • Size: 81.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for kg_engine_v2-2.1.3.tar.gz
Algorithm Hash digest
SHA256 f084d2df30b8c038b45dd0256fb9c64cdaaab996adf4b1d6358b209629718983
MD5 2f3902b78e227c703c5a6510e2822c22
BLAKE2b-256 521ecc658ad4693e2196a714349e75a9172fd0587808d58fbab585c31c8832f1

See more details on using hashes here.

File details

Details for the file kg_engine_v2-2.1.3-py3-none-any.whl.

File metadata

  • Download URL: kg_engine_v2-2.1.3-py3-none-any.whl
  • Upload date:
  • Size: 50.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for kg_engine_v2-2.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4e887522add98cf6e3de5d3a963b75ba9025af8d80c4df911bf79c45c8beb7d8
MD5 8d6c01763d0c107a0b9925e04c825e56
BLAKE2b-256 a2797a9dbce67ec4a99a7f0fb2334400b6ded63e630faf6888681b07fcfb70d1

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