"### Configuration - API Key, file directions and API endpoints"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Get API Key for API Management Service\n",
"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`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ocp_apim_subscription_key = getpass.getpass(\n",
" \"Enter the subscription key to the GraphRag APIM:\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Setup directories and API endpoint\n",
"\n",
"The following parameters are required to access and use the GraphRAG solution accelerator API:\n",
"* file_directory\n",
"* storage_name\n",
"* index_name\n",
"* endpoint\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"These parameters must be defined by the user:\n",
"\n",
"- file_directory: local directory where data files of interest are stored.\n",
"- storage_name: unique name for an Azure blob storage container where files will be uploaded.\n",
"- index_name: unique name for a single knowledge graph construction. Multiple indexes can be created from the same blob container of data.\n",
"- apim_url: the endpoint URL for GraphRAG service (this is the Gateway URL found in the APIM resource).\n",
"\"\"\"\n",
"\n",
"file_directory = \"\"\n",
"storage_name = \"\"\n",
"index_name = \"\"\n",
"apim_url = \"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert (\n",
" file_directory != \"\" and storage_name != \"\" and index_name != \"\" and apim_url != \"\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"\"Ocp-Apim-Subscription-Key\": \n",
" This is a custom HTTP header used by Azure API Management service (APIM) to \n",
" authenticate API requests. The value for this key should be set to the subscription \n",
" key provided by the Azure APIM instance in your GraphRAG resource group.\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def build_index(\n",
" storage_name: str,\n",
" index_name: str,\n",
") -> requests.Response:\n",
" \"\"\"Create a search index.\n",
" This function kicks off a job that builds a knowledge graph (KG) index from files located in a blob storage container.\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"Needed helper function to parse out the clear result from the query response. \"\"\"\n",
"Local search queries are best suited for narrow-focused questions that require an understanding of specific entities mentioned in the documents (e.g. What are the healing properties of chamomile?)"