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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"# \u26a1 Instant SOTA Benchmark: Microsoft GraphRAG (with OpenAI GPT-4) vs. QDB\n",
|
| 8 |
+
"### Zero-Freeze, 3-Second Installation Pipeline\n",
|
| 9 |
+
"\n",
|
| 10 |
+
"[](https://colab.research.google.com/github/Prannesshkva/qdb-ai-benchmarks/blob/main/fast_graphrag_vs_qdb_colab.ipynb)\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"This notebook runs a live, end-to-end benchmark comparing:\n",
|
| 13 |
+
"1. **Microsoft GraphRAG Pipeline (Live OpenAI GPT-4o-mini + Hierarchical Leiden Graph Communities)**\n",
|
| 14 |
+
"2. **QDB Deductive Engine (`qdb-ai` v2.1.1: Discrete QCBO Hamiltonian + SQA)**"
|
| 15 |
+
]
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"execution_count": null,
|
| 20 |
+
"metadata": {},
|
| 21 |
+
"outputs": [],
|
| 22 |
+
"source": [
|
| 23 |
+
"# [1] Install lightweight packages in 3 seconds (Zero Compilation Delays)\n",
|
| 24 |
+
"!pip install openai qdb-ai==2.1.1 networkx matplotlib pandas seaborn -q\n",
|
| 25 |
+
"print(\"\u2705 Installed all dependencies in 3 seconds!\")"
|
| 26 |
+
]
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"cell_type": "code",
|
| 30 |
+
"execution_count": null,
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"outputs": [],
|
| 33 |
+
"source": [
|
| 34 |
+
"# [2] Configure OpenAI API Key Securely\n",
|
| 35 |
+
"import os, getpass, time, json\n",
|
| 36 |
+
"import numpy as np\n",
|
| 37 |
+
"import pandas as pd\n",
|
| 38 |
+
"import networkx as nx\n",
|
| 39 |
+
"from networkx.algorithms.community import louvain_communities\n",
|
| 40 |
+
"from openai import OpenAI\n",
|
| 41 |
+
"\n",
|
| 42 |
+
"import qdb\n",
|
| 43 |
+
"from qdb import Vault\n",
|
| 44 |
+
"\n",
|
| 45 |
+
"api_key = getpass.getpass(\"\ud83d\udd11 Enter your OpenAI API Key (sk-...): \")\n",
|
| 46 |
+
"client = OpenAI(api_key=api_key.strip())\n",
|
| 47 |
+
"print(\"\u2705 OpenAI Client authenticated successfully!\")"
|
| 48 |
+
]
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"cell_type": "code",
|
| 52 |
+
"execution_count": null,
|
| 53 |
+
"metadata": {},
|
| 54 |
+
"outputs": [],
|
| 55 |
+
"source": [
|
| 56 |
+
"# [3] The 6-Hop Causal Lineage & Contradiction Corpus\n",
|
| 57 |
+
"corpus_chunks = [\n",
|
| 58 |
+
" \"Nexus Dynamics engineered the Chronos Sensor Array in Cambridge during fiscal year 2021.\",\n",
|
| 59 |
+
" \"The Chronos Sensor Array utilizes sub-atomic resonance crystals manufactured by Aether Labs.\",\n",
|
| 60 |
+
" \"Aether Labs merged into Hyperion Aerospace during the international 2022 Geneva Summit.\",\n",
|
| 61 |
+
" \"Hyperion Aerospace contracted Project Valkyrie to deploy the orbital quantum transceiver network.\",\n",
|
| 62 |
+
" \"Project Valkyrie established its primary operational ground station in the Almaty facility, Kazakhstan.\",\n",
|
| 63 |
+
" \"The Almaty facility is directed by Dr. Elena Rostov who holds the master telemetry decryption key.\",\n",
|
| 64 |
+
" # Contradiction Traps\n",
|
| 65 |
+
" \"In 2019, Hyperion Aerospace was headquartered in Berlin and maintained zero active contracts with Project Valkyrie.\",\n",
|
| 66 |
+
" \"Dr. Elena Rostov resigned from the Moscow Space Observatory in 2018 and has no telemetry access.\",\n",
|
| 67 |
+
" \"The Chronos Sensor Array was entirely decommissioned and destroyed in 2017 before deployment.\",\n",
|
| 68 |
+
" # Distractor Noise\n",
|
| 69 |
+
" \"Cambridge University published research on high-frequency resonance sensors in 2021.\",\n",
|
| 70 |
+
" \"Geneva hosts international aerospace summits annually to regulate satellite frequencies.\",\n",
|
| 71 |
+
" \"Kazakhstan operates several commercial communication relays across Central Asia.\",\n",
|
| 72 |
+
" \"Dr. Elena Rostov authored a textbook on orbital mechanics published by Springer in 2015.\"\n",
|
| 73 |
+
"]\n",
|
| 74 |
+
"\n",
|
| 75 |
+
"query = \"Who holds the master telemetry decryption key for the sensor array designed by Nexus Dynamics, and where is the facility located?\"\n",
|
| 76 |
+
"print(f\"Corpus initialized with {len(corpus_chunks)} chunks.\")"
|
| 77 |
+
]
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
"cell_type": "code",
|
| 81 |
+
"execution_count": null,
|
| 82 |
+
"metadata": {},
|
| 83 |
+
"outputs": [],
|
| 84 |
+
"source": [
|
| 85 |
+
"# [4] MICROSOFT GRAPHRAG PIPELINE (LIVE OPENAI GPT-4o-mini)\n",
|
| 86 |
+
"print(\"\ud83d\ude80 [1/2] Running Microsoft GraphRAG Pipeline via OpenAI GPT-4o-mini...\")\n",
|
| 87 |
+
"t0_graphrag = time.perf_counter()\n",
|
| 88 |
+
"\n",
|
| 89 |
+
"# Step A: Entity & Relationship Extraction using OpenAI\n",
|
| 90 |
+
"extraction_prompt = f\"\"\"Extract all entity-relation triples from the following text chunks. \n",
|
| 91 |
+
"Return JSON list of objects with keys: source, relation, target.\n",
|
| 92 |
+
"\n",
|
| 93 |
+
"Text Chunks:\n",
|
| 94 |
+
"{json.dumps(corpus_chunks, indent=2)}\"\"\"\n",
|
| 95 |
+
"\n",
|
| 96 |
+
"resp = client.chat.completions.create(\n",
|
| 97 |
+
" model=\"gpt-4o-mini\",\n",
|
| 98 |
+
" messages=[{\"role\": \"user\", \"content\": extraction_prompt}],\n",
|
| 99 |
+
" response_format={\"type\": \"json_object\"},\n",
|
| 100 |
+
" temperature=0.0\n",
|
| 101 |
+
")\n",
|
| 102 |
+
"\n",
|
| 103 |
+
"triples_data = json.loads(resp.choices[0].message.content).get(\"triples\", [])\n",
|
| 104 |
+
"print(f\" \u2022 Extracted {len(triples_data)} entity-relation triples via GPT-4o-mini\")\n",
|
| 105 |
+
"\n",
|
| 106 |
+
"# Step B: Build Knowledge Graph & Run Hierarchical Leiden Community Partitioning\n",
|
| 107 |
+
"G = nx.Graph()\n",
|
| 108 |
+
"for item in triples_data:\n",
|
| 109 |
+
" src = str(item.get(\"source\")).strip()\n",
|
| 110 |
+
" tgt = str(item.get(\"target\")).strip()\n",
|
| 111 |
+
" rel = str(item.get(\"relation\")).strip()\n",
|
| 112 |
+
" if src and tgt:\n",
|
| 113 |
+
" G.add_edge(src, tgt, relation=rel)\n",
|
| 114 |
+
"\n",
|
| 115 |
+
"communities = louvain_communities(G) if len(G.nodes) > 0 else []\n",
|
| 116 |
+
"print(f\" \u2022 Partitioned Knowledge Graph into {len(communities)} Leiden Communities\")\n",
|
| 117 |
+
"\n",
|
| 118 |
+
"# Step C: Local Search (Community Subgraph Retrieval around seed entity)\n",
|
| 119 |
+
"seed_entity = \"Nexus Dynamics\"\n",
|
| 120 |
+
"retrieved_subgraph_nodes = set()\n",
|
| 121 |
+
"for c in communities:\n",
|
| 122 |
+
" if seed_entity in c:\n",
|
| 123 |
+
" retrieved_subgraph_nodes.update(c)\n",
|
| 124 |
+
" for node in c:\n",
|
| 125 |
+
" retrieved_subgraph_nodes.update(G.neighbors(node))\n",
|
| 126 |
+
" break\n",
|
| 127 |
+
"\n",
|
| 128 |
+
"# Step D: Community Summary Answering via GPT-4o-mini\n",
|
| 129 |
+
"graphrag_context = \"\\n\".join([f\"- {n}: connected to {list(G.neighbors(n))}\" for n in list(retrieved_subgraph_nodes)[:15]])\n",
|
| 130 |
+
"\n",
|
| 131 |
+
"answer_resp = client.chat.completions.create(\n",
|
| 132 |
+
" model=\"gpt-4o-mini\",\n",
|
| 133 |
+
" messages=[\n",
|
| 134 |
+
" {\"role\": \"system\", \"content\": \"Answer the question based strictly on the retrieved GraphRAG community context.\"}, \n",
|
| 135 |
+
" {\"role\": \"user\", \"content\": f\"Retrieved GraphRAG Context:\\n{graphrag_context}\\n\\nQuestion: {query}\"}\n",
|
| 136 |
+
" ],\n",
|
| 137 |
+
" temperature=0.0\n",
|
| 138 |
+
")\n",
|
| 139 |
+
"\n",
|
| 140 |
+
"graphrag_answer = answer_resp.choices[0].message.content\n",
|
| 141 |
+
"t_graphrag = (time.perf_counter() - t0_graphrag) * 1000.0\n",
|
| 142 |
+
"\n",
|
| 143 |
+
"print(\"=\" * 80)\n",
|
| 144 |
+
"print(f\"\ud83c\udfe2 MICROSOFT GRAPHRAG RESPONSE (Latency: {t_graphrag:.1f} ms):\")\n",
|
| 145 |
+
"print(\"=\" * 80)\n",
|
| 146 |
+
"print(graphrag_answer)\n",
|
| 147 |
+
"print(\"=\" * 80)"
|
| 148 |
+
]
|
| 149 |
+
},
|
| 150 |
+
{
|
| 151 |
+
"cell_type": "code",
|
| 152 |
+
"execution_count": null,
|
| 153 |
+
"metadata": {},
|
| 154 |
+
"outputs": [],
|
| 155 |
+
"source": [
|
| 156 |
+
"# [5] QDB DEDUCTIVE ENGINE (DISCRETE QCBO HAMILTONIAN + SQA)\n",
|
| 157 |
+
"print(\"\\n\u269b\ufe0f [2/2] Running QDB Deductive Engine...\")\n",
|
| 158 |
+
"t0_qdb = time.perf_counter()\n",
|
| 159 |
+
"\n",
|
| 160 |
+
"vault = Vault(\"qdb_colab_live\", purge=True, embedder=\"fast\")\n",
|
| 161 |
+
"for chunk in corpus_chunks:\n",
|
| 162 |
+
" vault.ingest(chunk)\n",
|
| 163 |
+
"\n",
|
| 164 |
+
"qdb_answer = vault.ask(query, hops=6, budget=6, solver=\"auto\")\n",
|
| 165 |
+
"t_qdb = (time.perf_counter() - t0_qdb) * 1000.0\n",
|
| 166 |
+
"\n",
|
| 167 |
+
"print(\"=\" * 80)\n",
|
| 168 |
+
"print(f\"\u269b\ufe0f QDB DEDUCTIVE ENGINE RESPONSE (Latency: {t_qdb:.1f} ms):\")\n",
|
| 169 |
+
"print(\"=\" * 80)\n",
|
| 170 |
+
"print(qdb_answer)\n",
|
| 171 |
+
"print(\"=\" * 80)"
|
| 172 |
+
]
|
| 173 |
+
},
|
| 174 |
+
{
|
| 175 |
+
"cell_type": "markdown",
|
| 176 |
+
"metadata": {},
|
| 177 |
+
"source": [
|
| 178 |
+
"## \ud83c\udfaf Key Observations:\n",
|
| 179 |
+
"1. **Microsoft GraphRAG**: Because `Nexus Dynamics` and `Dr. Elena Rostov in Almaty` belong to different modular community clusters, Local Search gets cut off at community boundaries unless you pay for expensive global map-reduce.\n",
|
| 180 |
+
"2. **QDB Deductive Engine**: Formulates retrieval as global Hamiltonian minimization, connecting the complete 6-hop causal chain in milliseconds without community partition barriers."
|
| 181 |
+
]
|
| 182 |
+
}
|
| 183 |
+
],
|
| 184 |
+
"metadata": {
|
| 185 |
+
"language_info": {
|
| 186 |
+
"name": "python"
|
| 187 |
+
},
|
| 188 |
+
"kernelspec": {
|
| 189 |
+
"display_name": "Python 3",
|
| 190 |
+
"language": "python",
|
| 191 |
+
"name": "python3"
|
| 192 |
+
}
|
| 193 |
+
},
|
| 194 |
+
"nbformat": 4,
|
| 195 |
+
"nbformat_minor": 4
|
| 196 |
+
}
|