Skip to content
This repository was archived by the owner on May 27, 2025. It is now read-only.

Commit f70e224

Browse files
authored
Notebook cleanup and update (#36)
1 parent c53814c commit f70e224

File tree

6 files changed

+212
-217
lines changed

6 files changed

+212
-217
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
logs
66
logs/*
77

8-
# ignore example directory crated by HelloWorld.ipynb
8+
# ignore files created by the jupyter notebooks demos
99
example_files/
1010
files/
11+
prompts
12+
testdata
1113
.scripts/
1214

1315
# Byte-compiled / optimized / DLL files

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ For FAQ, access instructions, and our roadmap, please visit `aka.ms/graphrag`
1212

1313
### Deployment Guide
1414
To deploy the solution accelerator, see the [deployment guide](docs/DEPLOYMENT-GUIDE.md). This will result in a full deployment of graphrag as an API.
15-
Afterwards, check out the [Hello World](notebooks/HelloWorld.ipynb) notebook for a demonstration of various API calls.
15+
Afterwards, check out the [Quickstart](notebooks/1-Quickstart.ipynb) notebook for a demonstration of various API calls.
1616

1717
## Development Guide
1818
Interested in contributing? Check out the [development guide](docs/DEVELOPMENT-GUIDE.md).

docs/DEPLOYMENT-GUIDE.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The deployment process requires the following tools to be installed:
1515
* [kubectl](https://kubernetes.io/docs/tasks/tools) - k8s command line tool
1616
* [yq](https://github.com/mikefarah/yq?tab=readme-ov-file#install) >= v4.40.7 - yaml file parser
1717

18-
TIP: If you open this repository inside a devcontainer (i.e. VSCode Dev Containers or Codespaces), all required tools for deployment will already be available. Opening a devcontainer using VS Code requires <a href="https://docs.docker.com/engine/install/" target="_blank" >Docker to be installed</a>.
18+
TIP: If you open this repository inside a devcontainer (i.e. VSCode Dev Containers or Codespaces), all required tools for deployment will already be available. Opening a devcontainer using VS Code requires <a href="https://docs.docker.com/engine/install/" target="_blank" >Docker to be installed</a>.
1919

2020
The setup/deployment process has been mostly automated with a shell script and Bicep files (infrastructure as code). Azure CLI will deploy all necessary Azure resources using these Bicep files. The deployment is configurable using values defined in `infra/deploy.parameters.json`. To the utmost extent, we have provided default values but users are still expected to modify some values.
2121

@@ -25,7 +25,7 @@ You will need the following <a href="https://learn.microsoft.com/en-us/azure/rol
2525
| Permission | Scope |
2626
| :--- | ---: |
2727
Contributor | Subscription
28-
Role Based Access Control (RBAC) Administrator | Subscription
28+
Role Based Access Control (RBAC) Administrator | Subscription
2929

3030
#### Resource Provider
3131
The Azure subscription that you deploy this solution accelerator in will require the `Microsoft.OperationsManagement` resource provider to be registered.
@@ -99,4 +99,4 @@ bash deploy.sh -p deploy.parameters.json
9999
When deploying for the first time, it will take ~40-50 minutes to deploy. Subsequent runs of this command will be faster.
100100

101101
### 6. Use GraphRAG
102-
Once the deployment has finished, check out our [`Hello World`](../notebooks/HelloWorld.ipynb) notebook for a demonstration of how to use the GraphRAG API. To access the API documentation, visit `<APIM_gateway_url>/manpage/docs` in your browser. You can find the `APIM_gateway_url` by looking in the Azure Portal for the deployed APIM instance.
102+
Once the deployment has finished, check out our [`Quickstart`](../notebooks/1-Quickstart.ipynb) notebook for a demonstration of how to use the GraphRAG API. To access the API documentation, visit `<APIM_gateway_url>/manpage/docs` in your browser. You can find the `APIM_gateway_url` by looking in the Azure Portal for the deployed APIM instance.
Lines changed: 66 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"cell_type": "markdown",
1212
"metadata": {},
1313
"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"
1616
]
1717
},
1818
{
@@ -21,7 +21,7 @@
2121
"metadata": {},
2222
"outputs": [],
2323
"source": [
24-
"! pip install devtools pandas python-magic requests tqdm"
24+
"! pip install devtools python-magic requests tqdm"
2525
]
2626
},
2727
{
@@ -32,12 +32,10 @@
3232
"source": [
3333
"import getpass\n",
3434
"import json\n",
35-
"import sys\n",
3635
"import time\n",
3736
"from pathlib import Path\n",
3837
"\n",
3938
"import magic\n",
40-
"import pandas as pd\n",
4139
"import requests\n",
4240
"from devtools import pprint\n",
4341
"from tqdm import tqdm"
@@ -47,15 +45,17 @@
4745
"cell_type": "markdown",
4846
"metadata": {},
4947
"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."
5150
]
5251
},
5352
{
5453
"cell_type": "markdown",
5554
"metadata": {},
5655
"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."
5959
]
6060
},
6161
{
@@ -66,7 +66,15 @@
6666
"source": [
6767
"ocp_apim_subscription_key = getpass.getpass(\n",
6868
" \"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}"
7078
]
7179
},
7280
{
@@ -75,13 +83,7 @@
7583
"source": [
7684
"#### Setup directories and API endpoint\n",
7785
"\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)."
8587
]
8688
},
8789
{
@@ -91,18 +93,21 @@
9193
"outputs": [],
9294
"source": [
9395
"\"\"\"\n",
94-
"These parameters must be defined by the user:\n",
96+
"These parameters must be defined by the notebook user:\n",
9597
"\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",
100105
"\"\"\"\n",
101106
"\n",
102107
"file_directory = \"\"\n",
103108
"storage_name = \"\"\n",
104109
"index_name = \"\"\n",
105-
"apim_url = \"\""
110+
"endpoint = \"\""
106111
]
107112
},
108113
{
@@ -112,31 +117,17 @@
112117
"outputs": [],
113118
"source": [
114119
"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",
116121
")"
117122
]
118123
},
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-
},
135124
{
136125
"cell_type": "markdown",
137126
"metadata": {},
138127
"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."
140131
]
141132
},
142133
{
@@ -156,16 +147,16 @@
156147
" Upload files to a blob storage container.\n",
157148
"\n",
158149
" 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",
161152
" 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",
163154
" max_retries - the maximum number of times to retry uploading a batch of files if the API is busy.\n",
164155
"\n",
165156
" NOTE: Uploading files may sometimes fail if the blob container was recently deleted\n",
166157
" (i.e. a few seconds before. The solution \"in practice\" is to sleep a few seconds and try again.\n",
167158
" \"\"\"\n",
168-
" url = apim_url + \"/data\"\n",
159+
" url = endpoint + \"/data\"\n",
169160
"\n",
170161
" def upload_batch(\n",
171162
" files: list, storage_name: str, overwrite: bool, max_retries: int\n",
@@ -236,9 +227,9 @@
236227
"cell_type": "markdown",
237228
"metadata": {},
238229
"source": [
239-
"## Create an Index\n",
230+
"## Build an Index\n",
240231
"\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."
242233
]
243234
},
244235
{
@@ -252,13 +243,10 @@
252243
" index_name: str,\n",
253244
") -> requests.Response:\n",
254245
" \"\"\"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",
256247
" \"\"\"\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",
262250
" return requests.post(url, params=request, headers=headers)"
263251
]
264252
},
@@ -268,10 +256,7 @@
268256
"metadata": {},
269257
"outputs": [],
270258
"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",
275260
"print(response)\n",
276261
"if response.ok:\n",
277262
" print(response.text)\n",
@@ -283,9 +268,9 @@
283268
"cell_type": "markdown",
284269
"metadata": {},
285270
"source": [
286-
"### Check the status of an indexing job\n",
271+
"### Check status of an indexing job\n",
287272
"\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."
289274
]
290275
},
291276
{
@@ -295,18 +280,11 @@
295280
"outputs": [],
296281
"source": [
297282
"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",
309285
"\n",
286+
"\n",
287+
"response = index_status(index_name)\n",
310288
"pprint(response.json())"
311289
]
312290
},
@@ -316,7 +294,7 @@
316294
"source": [
317295
"## Query\n",
318296
"\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."
320298
]
321299
},
322300
{
@@ -325,7 +303,7 @@
325303
"metadata": {},
326304
"outputs": [],
327305
"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",
329307
"def parse_query_response(\n",
330308
" response: requests.Response, return_context_data: bool = False\n",
331309
") -> requests.Response | dict[list[dict]]:\n",
@@ -350,7 +328,7 @@
350328
"source": [
351329
"### Global Query \n",
352330
"\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."
354332
]
355333
},
356334
{
@@ -359,25 +337,19 @@
359337
"metadata": {},
360338
"outputs": [],
361339
"source": [
340+
"%%time\n",
341+
"\n",
342+
"\n",
362343
"def global_search(index_name: str | list[str], query: str) -> requests.Response:\n",
363344
" \"\"\"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",
365346
" 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",
377350
"global_response = global_search(\n",
378351
" index_name=index_name, query=\"Summarize the main topics of this data\"\n",
379352
")\n",
380-
"# print the result and save context data in a variable\n",
381353
"global_response_data = parse_query_response(global_response, return_context_data=True)\n",
382354
"global_response_data"
383355
]
@@ -397,25 +369,20 @@
397369
"metadata": {},
398370
"outputs": [],
399371
"source": [
372+
"%%time\n",
373+
"\n",
374+
"\n",
400375
"def local_search(index_name: str | list[str], query: str) -> requests.Response:\n",
401376
" \"\"\"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",
403378
" 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",
415383
"local_response = local_search(\n",
416384
" index_name=index_name, query=\"Who are the primary actors in these communities?\"\n",
417385
")\n",
418-
"# print the result and save context data in a variable\n",
419386
"local_response_data = parse_query_response(local_response, return_context_data=True)\n",
420387
"local_response_data"
421388
]
@@ -437,7 +404,7 @@
437404
"name": "python",
438405
"nbconvert_exporter": "python",
439406
"pygments_lexer": "ipython3",
440-
"version": "3.10.14"
407+
"version": "3.10.13"
441408
}
442409
},
443410
"nbformat": 4,

0 commit comments

Comments
 (0)