|
11 | 11 | "cell_type": "markdown", |
12 | 12 | "metadata": {}, |
13 | 13 | "source": [ |
14 | | - "### Prerequisite installs to run the quickstart notebook\n", |
15 | | - "Install 3rd party packages that are not part of the Python Standard Library" |
| 14 | + "## Prerequisites\n", |
| 15 | + "Install 3rd party packages, not part of the Python Standard Library, to run the notebook" |
16 | 16 | ] |
17 | 17 | }, |
18 | 18 | { |
|
21 | 21 | "metadata": {}, |
22 | 22 | "outputs": [], |
23 | 23 | "source": [ |
24 | | - "! pip install devtools pandas python-magic requests tqdm" |
| 24 | + "! pip install devtools python-magic requests tqdm" |
25 | 25 | ] |
26 | 26 | }, |
27 | 27 | { |
|
32 | 32 | "source": [ |
33 | 33 | "import getpass\n", |
34 | 34 | "import json\n", |
35 | | - "import sys\n", |
36 | 35 | "import time\n", |
37 | 36 | "from pathlib import Path\n", |
38 | 37 | "\n", |
39 | 38 | "import magic\n", |
40 | | - "import pandas as pd\n", |
41 | 39 | "import requests\n", |
42 | 40 | "from devtools import pprint\n", |
43 | 41 | "from tqdm import tqdm" |
|
47 | 45 | "cell_type": "markdown", |
48 | 46 | "metadata": {}, |
49 | 47 | "source": [ |
50 | | - "### Configuration - API Key, file directions and API endpoints" |
| 48 | + "## (REQUIRED) User Configuration\n", |
| 49 | + "Set the API subscription key, API base endpoint, and some file directory names that will be referenced later in the notebook." |
51 | 50 | ] |
52 | 51 | }, |
53 | 52 | { |
54 | 53 | "cell_type": "markdown", |
55 | 54 | "metadata": {}, |
56 | 55 | "source": [ |
57 | | - "#### Get API Key for API Management Service\n", |
58 | | - "For authentication, the API requires a *subscription key* to be passed in the header of all requests. To find this key, visit the Azure Portal. The API subscription key will be located under `<my_resource_group> --> <API Management service> --> <APIs> --> <Subscriptions> --> <Built-in all-access subscription> Primary Key`." |
| 56 | + "#### API subscription key\n", |
| 57 | + "\n", |
| 58 | + "APIM supports multiple forms of authentication and access control (e.g. managed identity). For this notebook demonstration, we will use a **[subscription key](https://learn.microsoft.com/en-us/azure/api-management/api-management-subscriptions)**. To locate this key, visit the Azure Portal. The subscription key can be found under `<my_resource_group> --> <API Management service> --> <APIs> --> <Subscriptions> --> <Built-in all-access subscription> Primary Key`. For multiple API users, individual subscription keys can be generated." |
59 | 59 | ] |
60 | 60 | }, |
61 | 61 | { |
|
66 | 66 | "source": [ |
67 | 67 | "ocp_apim_subscription_key = getpass.getpass(\n", |
68 | 68 | " \"Enter the subscription key to the GraphRag APIM:\"\n", |
69 | | - ")" |
| 69 | + ")\n", |
| 70 | + "\n", |
| 71 | + "\"\"\"\n", |
| 72 | + "\"Ocp-Apim-Subscription-Key\": \n", |
| 73 | + " This is a custom HTTP header used by Azure API Management service (APIM) to \n", |
| 74 | + " authenticate API requests. The value for this key should be set to the subscription \n", |
| 75 | + " key provided by the Azure APIM instance in your GraphRAG resource group.\n", |
| 76 | + "\"\"\"\n", |
| 77 | + "headers = {\"Ocp-Apim-Subscription-Key\": ocp_apim_subscription_key}" |
70 | 78 | ] |
71 | 79 | }, |
72 | 80 | { |
|
75 | 83 | "source": [ |
76 | 84 | "#### Setup directories and API endpoint\n", |
77 | 85 | "\n", |
78 | | - "The following parameters are required to access and use the GraphRAG solution accelerator API:\n", |
79 | | - "* file_directory\n", |
80 | | - "* storage_name\n", |
81 | | - "* index_name\n", |
82 | | - "* endpoint\n", |
83 | | - "\n", |
84 | | - "For demonstration purposes, you may use the provided `get-wiki-articles.py` script to download a small set of wikipedia articles or provide your own data." |
| 86 | + "For demonstration purposes, please use the provided `get-wiki-articles.py` script to download a small set of wikipedia articles or provide your own data (graphrag requires txt files to be utf-8 encoded)." |
85 | 87 | ] |
86 | 88 | }, |
87 | 89 | { |
|
91 | 93 | "outputs": [], |
92 | 94 | "source": [ |
93 | 95 | "\"\"\"\n", |
94 | | - "These parameters must be defined by the user:\n", |
| 96 | + "These parameters must be defined by the notebook user:\n", |
95 | 97 | "\n", |
96 | | - "- file_directory: local directory where data files of interest are stored.\n", |
97 | | - "- storage_name: unique name for an Azure blob storage container where files will be uploaded.\n", |
98 | | - "- index_name: unique name for a single knowledge graph construction. Multiple indexes can be created from the same blob container of data.\n", |
99 | | - "- apim_url: the endpoint URL for GraphRAG service (this is the Gateway URL found in the APIM resource).\n", |
| 98 | + "- file_directory: a local directory of text files. The file structure should be flat,\n", |
| 99 | + " with no nested directories. (i.e. file_directory/file1.txt, file_directory/file2.txt, etc.)\n", |
| 100 | + "- storage_name: a unique name to identify a blob storage container in Azure where files\n", |
| 101 | + " from `file_directory` will be uploaded.\n", |
| 102 | + "- index_name: a unique name to identify a single graphrag knowledge graph index.\n", |
| 103 | + " Note: Multiple indexes may be created from the same `storage_name` blob storage container.\n", |
| 104 | + "- endpoint: the base/endpoint URL for the GraphRAG API (this is the Gateway URL found in the APIM resource).\n", |
100 | 105 | "\"\"\"\n", |
101 | 106 | "\n", |
102 | 107 | "file_directory = \"\"\n", |
103 | 108 | "storage_name = \"\"\n", |
104 | 109 | "index_name = \"\"\n", |
105 | | - "apim_url = \"\"" |
| 110 | + "endpoint = \"\"" |
106 | 111 | ] |
107 | 112 | }, |
108 | 113 | { |
|
112 | 117 | "outputs": [], |
113 | 118 | "source": [ |
114 | 119 | "assert (\n", |
115 | | - " file_directory != \"\" and storage_name != \"\" and index_name != \"\" and apim_url != \"\"\n", |
| 120 | + " file_directory != \"\" and storage_name != \"\" and index_name != \"\" and endpoint != \"\"\n", |
116 | 121 | ")" |
117 | 122 | ] |
118 | 123 | }, |
119 | | - { |
120 | | - "cell_type": "code", |
121 | | - "execution_count": null, |
122 | | - "metadata": {}, |
123 | | - "outputs": [], |
124 | | - "source": [ |
125 | | - "\"\"\"\n", |
126 | | - "\"Ocp-Apim-Subscription-Key\": \n", |
127 | | - " This is a custom HTTP header used by Azure API Management service (APIM) to \n", |
128 | | - " authenticate API requests. The value for this key should be set to the subscription \n", |
129 | | - " key provided by the Azure APIM instance in your GraphRAG resource group.\n", |
130 | | - "\"\"\"\n", |
131 | | - "\n", |
132 | | - "headers = {\"Ocp-Apim-Subscription-Key\": ocp_apim_subscription_key}" |
133 | | - ] |
134 | | - }, |
135 | 124 | { |
136 | 125 | "cell_type": "markdown", |
137 | 126 | "metadata": {}, |
138 | 127 | "source": [ |
139 | | - "## Upload Files to Storage Data" |
| 128 | + "## Upload Files\n", |
| 129 | + "\n", |
| 130 | + "For a demonstration of how to index data in graphrag, we first need to ingest a few files into graphrag." |
140 | 131 | ] |
141 | 132 | }, |
142 | 133 | { |
|
156 | 147 | " Upload files to a blob storage container.\n", |
157 | 148 | "\n", |
158 | 149 | " Args:\n", |
159 | | - " file_directory - a local directory of .txt files to upload. All files must be in utf-8 encoding.\n", |
160 | | - " storage_name - a unique name for the Azure storage container.\n", |
| 150 | + " file_directory - a local directory of .txt files to upload. All files must have utf-8 encoding.\n", |
| 151 | + " storage_name - a unique name for the Azure storage blob container.\n", |
161 | 152 | " batch_size - the number of files to upload in a single batch.\n", |
162 | | - " overwrite - whether or not to overwrite files if they already exist in the storage container.\n", |
| 153 | + " overwrite - whether or not to overwrite files if they already exist in the storage blob container.\n", |
163 | 154 | " max_retries - the maximum number of times to retry uploading a batch of files if the API is busy.\n", |
164 | 155 | "\n", |
165 | 156 | " NOTE: Uploading files may sometimes fail if the blob container was recently deleted\n", |
166 | 157 | " (i.e. a few seconds before. The solution \"in practice\" is to sleep a few seconds and try again.\n", |
167 | 158 | " \"\"\"\n", |
168 | | - " url = apim_url + \"/data\"\n", |
| 159 | + " url = endpoint + \"/data\"\n", |
169 | 160 | "\n", |
170 | 161 | " def upload_batch(\n", |
171 | 162 | " files: list, storage_name: str, overwrite: bool, max_retries: int\n", |
|
236 | 227 | "cell_type": "markdown", |
237 | 228 | "metadata": {}, |
238 | 229 | "source": [ |
239 | | - "## Create an Index\n", |
| 230 | + "## Build an Index\n", |
240 | 231 | "\n", |
241 | | - "After data files have been uploaded, it is now possible to construct a knowledge graph by creating a search index. If an entity configuration is not provided, a default entity configuration will be used that has been shown to generally work well." |
| 232 | + "After data files have been uploaded, we can construct a knowledge graph by building a search index." |
242 | 233 | ] |
243 | 234 | }, |
244 | 235 | { |
|
252 | 243 | " index_name: str,\n", |
253 | 244 | ") -> requests.Response:\n", |
254 | 245 | " \"\"\"Create a search index.\n", |
255 | | - " This function kicks off a job that builds a knowledge graph (KG) index from files located in a blob storage container.\n", |
| 246 | + " This function kicks off a job that builds a knowledge graph index from files located in a blob storage container.\n", |
256 | 247 | " \"\"\"\n", |
257 | | - " url = apim_url + \"/index\"\n", |
258 | | - " request = {\n", |
259 | | - " \"storage_name\": storage_name,\n", |
260 | | - " \"index_name\": index_name\n", |
261 | | - " }\n", |
| 248 | + " url = endpoint + \"/index\"\n", |
| 249 | + " request = {\"storage_name\": storage_name, \"index_name\": index_name}\n", |
262 | 250 | " return requests.post(url, params=request, headers=headers)" |
263 | 251 | ] |
264 | 252 | }, |
|
268 | 256 | "metadata": {}, |
269 | 257 | "outputs": [], |
270 | 258 | "source": [ |
271 | | - "response = build_index(\n", |
272 | | - " storage_name=storage_name,\n", |
273 | | - " index_name=index_name\n", |
274 | | - ")\n", |
| 259 | + "response = build_index(storage_name=storage_name, index_name=index_name)\n", |
275 | 260 | "print(response)\n", |
276 | 261 | "if response.ok:\n", |
277 | 262 | " print(response.text)\n", |
|
283 | 268 | "cell_type": "markdown", |
284 | 269 | "metadata": {}, |
285 | 270 | "source": [ |
286 | | - "### Check the status of an indexing job\n", |
| 271 | + "### Check status of an indexing job\n", |
287 | 272 | "\n", |
288 | | - "Please wait for your index to reach 100 percent complete before continuing on to the next section to run queries." |
| 273 | + "Please wait for your index to reach 100 percent completion before continuing on to the next section (running queries). You may rerun the next cell multiple times to monitor status. Note: the indexing speed of graphrag is directly correlated to the TPM quota of the Azure OpenAI model you are using." |
289 | 274 | ] |
290 | 275 | }, |
291 | 276 | { |
|
295 | 280 | "outputs": [], |
296 | 281 | "source": [ |
297 | 282 | "def index_status(index_name: str) -> requests.Response:\n", |
298 | | - " url = apim_url + f\"/index/status/{index_name}\"\n", |
299 | | - " return requests.get(url, headers=headers)" |
300 | | - ] |
301 | | - }, |
302 | | - { |
303 | | - "cell_type": "code", |
304 | | - "execution_count": null, |
305 | | - "metadata": {}, |
306 | | - "outputs": [], |
307 | | - "source": [ |
308 | | - "response = index_status(index_name)\n", |
| 283 | + " url = endpoint + f\"/index/status/{index_name}\"\n", |
| 284 | + " return requests.get(url, headers=headers)\n", |
309 | 285 | "\n", |
| 286 | + "\n", |
| 287 | + "response = index_status(index_name)\n", |
310 | 288 | "pprint(response.json())" |
311 | 289 | ] |
312 | 290 | }, |
|
316 | 294 | "source": [ |
317 | 295 | "## Query\n", |
318 | 296 | "\n", |
319 | | - "After an indexing job has completed, the knowledge graph is ready to query. Two types of queries (global and local) are currently supported. In addition, you can issue a query over a single index or multiple indexes." |
| 297 | + "Once an indexing job is complete, the knowledge graph is ready to query. Two types of queries (global and local) are currently supported. We encourage you to try both and experience the difference in responses. Note that query response time is also correlated to the TPM quota of the Azure OpenAI model you are using." |
320 | 298 | ] |
321 | 299 | }, |
322 | 300 | { |
|
325 | 303 | "metadata": {}, |
326 | 304 | "outputs": [], |
327 | 305 | "source": [ |
328 | | - "\"\"\"Needed helper function to parse out the clear result from the query response. \"\"\"\n", |
| 306 | + "# a helper function to parse out the result from a query response\n", |
329 | 307 | "def parse_query_response(\n", |
330 | 308 | " response: requests.Response, return_context_data: bool = False\n", |
331 | 309 | ") -> requests.Response | dict[list[dict]]:\n", |
|
350 | 328 | "source": [ |
351 | 329 | "### Global Query \n", |
352 | 330 | "\n", |
353 | | - "Global search queries are resource-intensive, but give good responses to questions that require an understanding of the dataset as a whole." |
| 331 | + "Global queries are resource-intensive, but provide good responses to questions that require an understanding of the dataset as a whole." |
354 | 332 | ] |
355 | 333 | }, |
356 | 334 | { |
|
359 | 337 | "metadata": {}, |
360 | 338 | "outputs": [], |
361 | 339 | "source": [ |
| 340 | + "%%time\n", |
| 341 | + "\n", |
| 342 | + "\n", |
362 | 343 | "def global_search(index_name: str | list[str], query: str) -> requests.Response:\n", |
363 | 344 | " \"\"\"Run a global query over the knowledge graph(s) associated with one or more indexes\"\"\"\n", |
364 | | - " url = apim_url + \"/query/global\"\n", |
| 345 | + " url = endpoint + \"/query/global\"\n", |
365 | 346 | " request = {\"index_name\": index_name, \"query\": query}\n", |
366 | | - " return requests.post(url, json=request, headers=headers)" |
367 | | - ] |
368 | | - }, |
369 | | - { |
370 | | - "cell_type": "code", |
371 | | - "execution_count": null, |
372 | | - "metadata": {}, |
373 | | - "outputs": [], |
374 | | - "source": [ |
375 | | - "%%time\n", |
376 | | - "# pass in a single index name as a string or to query across multiple indexes, set index_name=[myindex1, myindex2]\n", |
| 347 | + " return requests.post(url, json=request, headers=headers)\n", |
| 348 | + "\n", |
| 349 | + "\n", |
377 | 350 | "global_response = global_search(\n", |
378 | 351 | " index_name=index_name, query=\"Summarize the main topics of this data\"\n", |
379 | 352 | ")\n", |
380 | | - "# print the result and save context data in a variable\n", |
381 | 353 | "global_response_data = parse_query_response(global_response, return_context_data=True)\n", |
382 | 354 | "global_response_data" |
383 | 355 | ] |
|
397 | 369 | "metadata": {}, |
398 | 370 | "outputs": [], |
399 | 371 | "source": [ |
| 372 | + "%%time\n", |
| 373 | + "\n", |
| 374 | + "\n", |
400 | 375 | "def local_search(index_name: str | list[str], query: str) -> requests.Response:\n", |
401 | 376 | " \"\"\"Run a local query over the knowledge graph(s) associated with one or more indexes\"\"\"\n", |
402 | | - " url = apim_url + \"/query/local\"\n", |
| 377 | + " url = endpoint + \"/query/local\"\n", |
403 | 378 | " request = {\"index_name\": index_name, \"query\": query}\n", |
404 | | - " return requests.post(url, json=request, headers=headers)" |
405 | | - ] |
406 | | - }, |
407 | | - { |
408 | | - "cell_type": "code", |
409 | | - "execution_count": null, |
410 | | - "metadata": {}, |
411 | | - "outputs": [], |
412 | | - "source": [ |
413 | | - "%%time\n", |
414 | | - "# pass in a single index name as a string or to query across multiple indexes, set index_name=[myindex1, myindex2]\n", |
| 379 | + " return requests.post(url, json=request, headers=headers)\n", |
| 380 | + "\n", |
| 381 | + "\n", |
| 382 | + "# perform a local query\n", |
415 | 383 | "local_response = local_search(\n", |
416 | 384 | " index_name=index_name, query=\"Who are the primary actors in these communities?\"\n", |
417 | 385 | ")\n", |
418 | | - "# print the result and save context data in a variable\n", |
419 | 386 | "local_response_data = parse_query_response(local_response, return_context_data=True)\n", |
420 | 387 | "local_response_data" |
421 | 388 | ] |
|
437 | 404 | "name": "python", |
438 | 405 | "nbconvert_exporter": "python", |
439 | 406 | "pygments_lexer": "ipython3", |
440 | | - "version": "3.10.14" |
| 407 | + "version": "3.10.13" |
441 | 408 | } |
442 | 409 | }, |
443 | 410 | "nbformat": 4, |
|
0 commit comments