Prannesshkva commited on
Commit
3da09cb
·
verified ·
1 Parent(s): dd73596

Upload official_microsoft_graphrag_colab.ipynb with huggingface_hub

Browse files
official_microsoft_graphrag_colab.ipynb ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# \ud83c\udfe2 Official Microsoft GraphRAG (MSR) on Google Colab\n",
8
+ "### End-to-End Pipeline: Entity Extraction \u2794 Leiden Community Partitioning \u2794 Local & Global Search\n",
9
+ "\n",
10
+ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Prannesshkva/qdb-ai-benchmarks/blob/main/official_microsoft_graphrag_colab.ipynb)\n",
11
+ "\n",
12
+ "This notebook runs the **official Microsoft Research `graphrag` CLI pipeline** against OpenAI's API, testing both:\n",
13
+ "1. **Official Microsoft GraphRAG (Local Search & Global Search)**\n",
14
+ "2. **QDB Deductive Engine (`qdb-ai`)** on the exact same multi-hop challenge."
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": null,
20
+ "metadata": {},
21
+ "outputs": [],
22
+ "source": [
23
+ "# [1] Install Official Microsoft GraphRAG and QDB\n",
24
+ "!pip install graphrag qdb-ai==2.1.1 pandas -q\n",
25
+ "print(\"\u2705 Microsoft GraphRAG & QDB installed successfully!\")"
26
+ ]
27
+ },
28
+ {
29
+ "cell_type": "code",
30
+ "execution_count": null,
31
+ "metadata": {},
32
+ "outputs": [],
33
+ "source": [
34
+ "# [2] Configure OpenAI API Key\n",
35
+ "import os, getpass\n",
36
+ "\n",
37
+ "# Enter your OpenAI API Key (input is masked for security)\n",
38
+ "api_key = getpass.getpass(\"\ud83d\udd11 Enter your OpenAI API Key (sk-...): \")\n",
39
+ "os.environ[\"OPENAI_API_KEY\"] = api_key.strip()\n",
40
+ "os.environ[\"GRAPHRAG_API_KEY\"] = api_key.strip()\n",
41
+ "\n",
42
+ "print(\"\u2705 OpenAI API Key loaded successfully!\")"
43
+ ]
44
+ },
45
+ {
46
+ "cell_type": "code",
47
+ "execution_count": null,
48
+ "metadata": {},
49
+ "outputs": [],
50
+ "source": [
51
+ "# [3] Initialize Microsoft GraphRAG Workspace & settings.yaml\n",
52
+ "import shutil\n",
53
+ "\n",
54
+ "project_dir = \"./graphrag_project\"\n",
55
+ "input_dir = f\"{project_dir}/input\"\n",
56
+ "\n",
57
+ "if os.path.exists(project_dir):\n",
58
+ " shutil.rmtree(project_dir)\n",
59
+ "os.makedirs(input_dir, exist_ok=True)\n",
60
+ "\n",
61
+ "# Write Microsoft GraphRAG settings.yaml\n",
62
+ "settings_yaml = f\"\"\"\n",
63
+ "encoding_model: cl100k_base\n",
64
+ "skip_workflows: []\n",
65
+ "\n",
66
+ "llm:\n",
67
+ " api_key: \"{os.environ['OPENAI_API_KEY']}\"\n",
68
+ " type: openai_chat\n",
69
+ " model: gpt-4o-mini\n",
70
+ " model_supports_json: true\n",
71
+ " max_tokens: 4000\n",
72
+ " temperature: 0.0\n",
73
+ "\n",
74
+ "embeddings:\n",
75
+ " llm:\n",
76
+ " api_key: \"{os.environ['OPENAI_API_KEY']}\"\n",
77
+ " type: openai_embedding\n",
78
+ " model: text-embedding-3-small\n",
79
+ "\n",
80
+ "chunks:\n",
81
+ " size: 300\n",
82
+ " overlap: 50\n",
83
+ " group_by_columns: [id]\n",
84
+ "\n",
85
+ "input:\n",
86
+ " type: file\n",
87
+ " file_type: text\n",
88
+ " base_dir: \"input\"\n",
89
+ " file_pattern: \".*\\\\.txt$\"\n",
90
+ "\n",
91
+ "entity_extraction:\n",
92
+ " max_gleanings: 1\n",
93
+ "\n",
94
+ "community_reports:\n",
95
+ " max_length: 2000\n",
96
+ " max_input_length: 8000\n",
97
+ "\n",
98
+ "snapshots:\n",
99
+ " graphml: false\n",
100
+ " embeddings: false\n",
101
+ " transient: false\n",
102
+ "\"\"\"\n",
103
+ "\n",
104
+ "with open(f\"{project_dir}/settings.yaml\", \"w\", encoding=\"utf-8\") as f:\n",
105
+ " f.write(settings_yaml.strip())\n",
106
+ "\n",
107
+ "print(\"\u2705 Microsoft GraphRAG settings.yaml initialized!\")"
108
+ ]
109
+ },
110
+ {
111
+ "cell_type": "code",
112
+ "execution_count": null,
113
+ "metadata": {},
114
+ "outputs": [],
115
+ "source": [
116
+ "# [4] Write Benchmark Knowledge Base to input/data.txt\n",
117
+ "corpus_text = \"\"\"Nexus Dynamics engineered the Chronos Sensor Array in Cambridge during fiscal year 2021.\n",
118
+ "The Chronos Sensor Array utilizes sub-atomic resonance crystals manufactured by Aether Labs.\n",
119
+ "Aether Labs merged into Hyperion Aerospace during the international 2022 Geneva Summit.\n",
120
+ "Hyperion Aerospace contracted Project Valkyrie to deploy the orbital quantum transceiver network.\n",
121
+ "Project Valkyrie established its primary operational ground station in the Almaty facility, Kazakhstan.\n",
122
+ "The Almaty facility is directed by Dr. Elena Rostov who holds the master telemetry decryption key.\n",
123
+ "\n",
124
+ "In 2019, Hyperion Aerospace was headquartered in Berlin and maintained zero active contracts with Project Valkyrie.\n",
125
+ "Dr. Elena Rostov resigned from the Moscow Space Observatory in 2018 and has no telemetry access.\n",
126
+ "The Chronos Sensor Array was entirely decommissioned and destroyed in 2017 before deployment.\n",
127
+ "\n",
128
+ "Cambridge University published research on high-frequency resonance sensors in 2021.\n",
129
+ "Geneva hosts international aerospace summits annually to regulate satellite frequencies.\n",
130
+ "Kazakhstan operates several commercial communication relays across Central Asia.\n",
131
+ "Dr. Elena Rostov authored a textbook on orbital mechanics published by Springer in 2015.\n",
132
+ "\"\"\"\n",
133
+ "\n",
134
+ "with open(f\"{input_dir}/data.txt\", \"w\", encoding=\"utf-8\") as f:\n",
135
+ " f.write(corpus_text)\n",
136
+ "\n",
137
+ "print(\"\u2705 Benchmark corpus written to ./graphrag_project/input/data.txt\")"
138
+ ]
139
+ },
140
+ {
141
+ "cell_type": "code",
142
+ "execution_count": null,
143
+ "metadata": {},
144
+ "outputs": [],
145
+ "source": [
146
+ "# [5] Execute Official Microsoft GraphRAG Indexing Pipeline\n",
147
+ "print(\"\ud83d\ude80 Starting Microsoft GraphRAG Indexing (Calling OpenAI API)...\")\n",
148
+ "!python -m graphrag.index --root ./graphrag_project\n",
149
+ "print(\"\ud83c\udf89 Indexing complete! Generated parquet knowledge graph tables in ./graphrag_project/output/\")"
150
+ ]
151
+ },
152
+ {
153
+ "cell_type": "code",
154
+ "execution_count": null,
155
+ "metadata": {},
156
+ "outputs": [],
157
+ "source": [
158
+ "# [6] Query Official Microsoft GraphRAG: LOCAL SEARCH (Community Subgraph)\n",
159
+ "query = \"Who holds the master telemetry decryption key for the sensor array designed by Nexus Dynamics, and where is the facility located?\"\n",
160
+ "\n",
161
+ "print(\"=\" * 80)\n",
162
+ "print(\"\ud83d\udd0d RUNNING OFFICIAL MICROSOFT GRAPHRAG (LOCAL SEARCH):\")\n",
163
+ "print(\"=\" * 80)\n",
164
+ "!python -m graphrag.query --root ./graphrag_project --method local --query \"Who holds the master telemetry decryption key for the sensor array designed by Nexus Dynamics, and where is the facility located?\""
165
+ ]
166
+ },
167
+ {
168
+ "cell_type": "code",
169
+ "execution_count": null,
170
+ "metadata": {},
171
+ "outputs": [],
172
+ "source": [
173
+ "# [7] Query Official Microsoft GraphRAG: GLOBAL SEARCH (Community Summaries)\n",
174
+ "print(\"=\" * 80)\n",
175
+ "print(\"\ud83c\udf10 RUNNING OFFICIAL MICROSOFT GRAPHRAG (GLOBAL SEARCH):\")\n",
176
+ "print(\"=\" * 80)\n",
177
+ "!python -m graphrag.query --root ./graphrag_project --method global --query \"Who holds the master telemetry decryption key for the sensor array designed by Nexus Dynamics, and where is the facility located?\""
178
+ ]
179
+ },
180
+ {
181
+ "cell_type": "code",
182
+ "execution_count": null,
183
+ "metadata": {},
184
+ "outputs": [],
185
+ "source": [
186
+ "# [8] Run QDB Deductive Engine on the Exact Same Challenge\n",
187
+ "from qdb import Vault\n",
188
+ "\n",
189
+ "vault = Vault(\"official_comparison_vault\", purge=True, embedder=\"fast\")\n",
190
+ "for line in corpus_text.strip().split(\"\\n\"):\n",
191
+ " if len(line.strip()) > 10:\n",
192
+ " vault.ingest(line.strip())\n",
193
+ "\n",
194
+ "print(\"=\" * 80)\n",
195
+ "print(\"\u269b\ufe0f RUNNING QDB DEDUCTIVE ENGINE (DISCRETE QCBO HAMILTONIAN + SQA):\")\n",
196
+ "print(\"=\" * 80)\n",
197
+ "qdb_answer = vault.ask(query, hops=6, budget=6, solver=\"auto\")\n",
198
+ "print(qdb_answer)"
199
+ ]
200
+ },
201
+ {
202
+ "cell_type": "markdown",
203
+ "metadata": {},
204
+ "source": [
205
+ "## \ud83d\udcca Summary Comparison:\n",
206
+ "- **Microsoft GraphRAG Local Search**: Evaluates 1-2 hop neighborhood around seed entities.\n",
207
+ "- **Microsoft GraphRAG Global Search**: Aggregates macro community summaries.\n",
208
+ "- **QDB Deductive Engine**: Minimizes the global QCBO Hamiltonian across all 6 hops simultaneously while filtering contradictory claims with $+50.0$ energy barriers."
209
+ ]
210
+ }
211
+ ],
212
+ "metadata": {
213
+ "language_info": {
214
+ "name": "python"
215
+ },
216
+ "kernelspec": {
217
+ "display_name": "Python 3",
218
+ "language": "python",
219
+ "name": "python3"
220
+ }
221
+ },
222
+ "nbformat": 4,
223
+ "nbformat_minor": 4
224
+ }