{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copyright (c) 2024 Microsoft Corporation.\n",
    "# Licensed under the MIT License."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import pandas as pd\n",
    "import tiktoken\n",
    "\n",
    "from graphrag.query.indexer_adapters import (\n",
    "    read_indexer_communities,\n",
    "    read_indexer_entities,\n",
    "    read_indexer_reports,\n",
    ")\n",
    "from graphrag.query.llm.oai.chat_openai import ChatOpenAI\n",
    "from graphrag.query.llm.oai.typing import OpenaiApiType\n",
    "from graphrag.query.structured_search.global_search.community_context import (\n",
    "    GlobalCommunityContext,\n",
    ")\n",
    "from graphrag.query.structured_search.global_search.search import GlobalSearch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Global Search example\n",
    "\n",
    "Global search method generates answers by searching over all AI-generated community reports in a map-reduce fashion. This is a resource-intensive method, but often gives good responses for questions that require an understanding of the dataset as a whole (e.g. What are the most significant values of the herbs mentioned in this notebook?)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### LLM setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "api_key = os.environ[\"GRAPHRAG_API_KEY\"]\n",
    "llm_model = os.environ[\"GRAPHRAG_LLM_MODEL\"]\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    api_key=api_key,\n",
    "    model=llm_model,\n",
    "    api_type=OpenaiApiType.OpenAI,  # OpenaiApiType.OpenAI or OpenaiApiType.AzureOpenAI\n",
    "    max_retries=20,\n",
    ")\n",
    "\n",
    "token_encoder = tiktoken.encoding_for_model(llm_model)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load community reports as context for global search\n",
    "\n",
    "- Load all community reports in the `create_final_community_reports` table from the ire-indexing engine, to be used as context data for global search.\n",
    "- Load entities from the `create_final_nodes` and `create_final_entities` tables from the ire-indexing engine, to be used for calculating community weights for context ranking. Note that this is optional (if no entities are provided, we will not calculate community weights and only use the rank attribute in the community reports table for context ranking)\n",
    "- Load all communities in the `create_final_communites` table from the ire-indexing engine, to be used to reconstruct the community graph hierarchy for dynamic community selection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# parquet files generated from indexing pipeline\n",
    "INPUT_DIR = \"./inputs/operation dulce\"\n",
    "COMMUNITY_TABLE = \"create_final_communities\"\n",
    "COMMUNITY_REPORT_TABLE = \"create_final_community_reports\"\n",
    "ENTITY_TABLE = \"create_final_nodes\"\n",
    "ENTITY_EMBEDDING_TABLE = \"create_final_entities\"\n",
    "\n",
    "# community level in the Leiden community hierarchy from which we will load the community reports\n",
    "# higher value means we use reports from more fine-grained communities (at the cost of higher computation cost)\n",
    "COMMUNITY_LEVEL = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total report count: 20\n",
      "Report count after filtering by community level 2: 17\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>community</th>\n",
       "      <th>full_content</th>\n",
       "      <th>level</th>\n",
       "      <th>rank</th>\n",
       "      <th>title</th>\n",
       "      <th>rank_explanation</th>\n",
       "      <th>summary</th>\n",
       "      <th>findings</th>\n",
       "      <th>full_content_json</th>\n",
       "      <th>id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td># Paranormal Military Squad at Dulce Base: Dec...</td>\n",
       "      <td>1</td>\n",
       "      <td>8.5</td>\n",
       "      <td>Paranormal Military Squad at Dulce Base: Decod...</td>\n",
       "      <td>The impact severity rating is high due to the ...</td>\n",
       "      <td>The Paranormal Military Squad, stationed at Du...</td>\n",
       "      <td>[{'explanation': 'Jordan is a central figure i...</td>\n",
       "      <td>{\\n    \"title\": \"Paranormal Military Squad at ...</td>\n",
       "      <td>1ba2d200-dd26-4693-affe-a5539d0a0e0d</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11</td>\n",
       "      <td># Dulce and Paranormal Military Squad Operatio...</td>\n",
       "      <td>1</td>\n",
       "      <td>8.5</td>\n",
       "      <td>Dulce and Paranormal Military Squad Operations</td>\n",
       "      <td>The impact severity rating is high due to the ...</td>\n",
       "      <td>The community centers around Dulce, a secretiv...</td>\n",
       "      <td>[{'explanation': 'Dulce is described as a top-...</td>\n",
       "      <td>{\\n    \"title\": \"Dulce and Paranormal Military...</td>\n",
       "      <td>a8a530b0-ae6b-44ea-b11c-9f70d138298d</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12</td>\n",
       "      <td># Paranormal Military Squad and Dulce Base Ope...</td>\n",
       "      <td>1</td>\n",
       "      <td>7.5</td>\n",
       "      <td>Paranormal Military Squad and Dulce Base Opera...</td>\n",
       "      <td>The impact severity rating is relatively high ...</td>\n",
       "      <td>The community centers around the Paranormal Mi...</td>\n",
       "      <td>[{'explanation': 'Taylor is a central figure w...</td>\n",
       "      <td>{\\n    \"title\": \"Paranormal Military Squad and...</td>\n",
       "      <td>0478975b-c805-4cc1-b746-82f3e689e2f3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13</td>\n",
       "      <td># Mission Dynamics and Leadership: Cruz and Wa...</td>\n",
       "      <td>1</td>\n",
       "      <td>7.5</td>\n",
       "      <td>Mission Dynamics and Leadership: Cruz and Wash...</td>\n",
       "      <td>The impact severity rating is relatively high ...</td>\n",
       "      <td>This report explores the intricate dynamics of...</td>\n",
       "      <td>[{'explanation': 'Cruz is a central figure in ...</td>\n",
       "      <td>{\\n    \"title\": \"Mission Dynamics and Leadersh...</td>\n",
       "      <td>b56f6e68-3951-4f07-8760-63700944a375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14</td>\n",
       "      <td># Dulce Base and Paranormal Military Squad: Br...</td>\n",
       "      <td>1</td>\n",
       "      <td>8.5</td>\n",
       "      <td>Dulce Base and Paranormal Military Squad: Brid...</td>\n",
       "      <td>The impact severity rating is high due to the ...</td>\n",
       "      <td>The community centers around the Dulce Base, a...</td>\n",
       "      <td>[{'explanation': 'Sam Rivera, a member of the ...</td>\n",
       "      <td>{\\n    \"title\": \"Dulce Base and Paranormal Mil...</td>\n",
       "      <td>736e7006-d050-4abb-a122-00febf3f540f</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  community                                       full_content  level  rank  \\\n",
       "0        10  # Paranormal Military Squad at Dulce Base: Dec...      1   8.5   \n",
       "1        11  # Dulce and Paranormal Military Squad Operatio...      1   8.5   \n",
       "2        12  # Paranormal Military Squad and Dulce Base Ope...      1   7.5   \n",
       "3        13  # Mission Dynamics and Leadership: Cruz and Wa...      1   7.5   \n",
       "4        14  # Dulce Base and Paranormal Military Squad: Br...      1   8.5   \n",
       "\n",
       "                                               title  \\\n",
       "0  Paranormal Military Squad at Dulce Base: Decod...   \n",
       "1     Dulce and Paranormal Military Squad Operations   \n",
       "2  Paranormal Military Squad and Dulce Base Opera...   \n",
       "3  Mission Dynamics and Leadership: Cruz and Wash...   \n",
       "4  Dulce Base and Paranormal Military Squad: Brid...   \n",
       "\n",
       "                                    rank_explanation  \\\n",
       "0  The impact severity rating is high due to the ...   \n",
       "1  The impact severity rating is high due to the ...   \n",
       "2  The impact severity rating is relatively high ...   \n",
       "3  The impact severity rating is relatively high ...   \n",
       "4  The impact severity rating is high due to the ...   \n",
       "\n",
       "                                             summary  \\\n",
       "0  The Paranormal Military Squad, stationed at Du...   \n",
       "1  The community centers around Dulce, a secretiv...   \n",
       "2  The community centers around the Paranormal Mi...   \n",
       "3  This report explores the intricate dynamics of...   \n",
       "4  The community centers around the Dulce Base, a...   \n",
       "\n",
       "                                            findings  \\\n",
       "0  [{'explanation': 'Jordan is a central figure i...   \n",
       "1  [{'explanation': 'Dulce is described as a top-...   \n",
       "2  [{'explanation': 'Taylor is a central figure w...   \n",
       "3  [{'explanation': 'Cruz is a central figure in ...   \n",
       "4  [{'explanation': 'Sam Rivera, a member of the ...   \n",
       "\n",
       "                                   full_content_json  \\\n",
       "0  {\\n    \"title\": \"Paranormal Military Squad at ...   \n",
       "1  {\\n    \"title\": \"Dulce and Paranormal Military...   \n",
       "2  {\\n    \"title\": \"Paranormal Military Squad and...   \n",
       "3  {\\n    \"title\": \"Mission Dynamics and Leadersh...   \n",
       "4  {\\n    \"title\": \"Dulce Base and Paranormal Mil...   \n",
       "\n",
       "                                     id  \n",
       "0  1ba2d200-dd26-4693-affe-a5539d0a0e0d  \n",
       "1  a8a530b0-ae6b-44ea-b11c-9f70d138298d  \n",
       "2  0478975b-c805-4cc1-b746-82f3e689e2f3  \n",
       "3  b56f6e68-3951-4f07-8760-63700944a375  \n",
       "4  736e7006-d050-4abb-a122-00febf3f540f  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "community_df = pd.read_parquet(f\"{INPUT_DIR}/{COMMUNITY_TABLE}.parquet\")\n",
    "entity_df = pd.read_parquet(f\"{INPUT_DIR}/{ENTITY_TABLE}.parquet\")\n",
    "report_df = pd.read_parquet(f\"{INPUT_DIR}/{COMMUNITY_REPORT_TABLE}.parquet\")\n",
    "entity_embedding_df = pd.read_parquet(f\"{INPUT_DIR}/{ENTITY_EMBEDDING_TABLE}.parquet\")\n",
    "\n",
    "communities = read_indexer_communities(community_df, entity_df, report_df)\n",
    "reports = read_indexer_reports(report_df, entity_df, COMMUNITY_LEVEL)\n",
    "entities = read_indexer_entities(entity_df, entity_embedding_df, COMMUNITY_LEVEL)\n",
    "\n",
    "print(f\"Total report count: {len(report_df)}\")\n",
    "print(\n",
    "    f\"Report count after filtering by community level {COMMUNITY_LEVEL}: {len(reports)}\"\n",
    ")\n",
    "\n",
    "report_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Build global context based on community reports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "context_builder = GlobalCommunityContext(\n",
    "    community_reports=reports,\n",
    "    communities=communities,\n",
    "    entities=entities,  # default to None if you don't want to use community weights for ranking\n",
    "    token_encoder=token_encoder,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Perform global search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "context_builder_params = {\n",
    "    \"use_community_summary\": False,  # False means using full community reports. True means using community short summaries.\n",
    "    \"shuffle_data\": True,\n",
    "    \"include_community_rank\": True,\n",
    "    \"min_community_rank\": 0,\n",
    "    \"community_rank_name\": \"rank\",\n",
    "    \"include_community_weight\": True,\n",
    "    \"community_weight_name\": \"occurrence weight\",\n",
    "    \"normalize_community_weight\": True,\n",
    "    \"max_tokens\": 12_000,  # change this based on the token limit you have on your model (if you are using a model with 8k limit, a good setting could be 5000)\n",
    "    \"context_name\": \"Reports\",\n",
    "}\n",
    "\n",
    "map_llm_params = {\n",
    "    \"max_tokens\": 1000,\n",
    "    \"temperature\": 0.0,\n",
    "    \"response_format\": {\"type\": \"json_object\"},\n",
    "}\n",
    "\n",
    "reduce_llm_params = {\n",
    "    \"max_tokens\": 2000,  # change this based on the token limit you have on your model (if you are using a model with 8k limit, a good setting could be 1000-1500)\n",
    "    \"temperature\": 0.0,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "search_engine = GlobalSearch(\n",
    "    llm=llm,\n",
    "    context_builder=context_builder,\n",
    "    token_encoder=token_encoder,\n",
    "    max_data_tokens=12_000,  # change this based on the token limit you have on your model (if you are using a model with 8k limit, a good setting could be 5000)\n",
    "    map_llm_params=map_llm_params,\n",
    "    reduce_llm_params=reduce_llm_params,\n",
    "    allow_general_knowledge=False,  # set this to True will add instruction to encourage the LLM to incorporate general knowledge in the response, which may increase hallucinations, but could be useful in some use cases.\n",
    "    json_mode=True,  # set this to False if your LLM model does not support JSON mode.\n",
    "    context_builder_params=context_builder_params,\n",
    "    concurrent_coroutines=32,\n",
    "    response_type=\"multiple paragraphs\",  # free form text describing the response type and format, can be anything, e.g. prioritized list, single paragraph, multiple paragraphs, multiple-page report\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "### Overview of Cosmic Vocalization\n",
      "\n",
      "Cosmic Vocalization is a phenomenon that has garnered significant attention from various individuals and groups. It is perceived as a cosmic event with potential implications for security and interstellar communication. The Paranormal Military Squad is actively engaged with Cosmic Vocalization, indicating its strategic importance in security measures [Data: Reports (6)].\n",
      "\n",
      "### Key Perspectives and Concerns\n",
      "\n",
      "1. **Strategic Engagement**: The Paranormal Military Squad's involvement suggests that Cosmic Vocalization is not only a subject of interest but also a matter of strategic importance. This engagement highlights the potential security implications of these cosmic phenomena [Data: Reports (6)].\n",
      "\n",
      "2. **Community Interest**: Within the community, Cosmic Vocalization is a focal point of interest. Alex Mercer, for instance, perceives it as part of an interstellar duet, which suggests a responsive and perhaps communicative approach to these cosmic events [Data: Reports (6)].\n",
      "\n",
      "3. **Potential Threats**: Concerns have been raised by individuals like Taylor Cruz, who fears that Cosmic Vocalization might be a homing tune. This perspective adds a layer of urgency and suggests that there may be potential threats associated with these cosmic sounds [Data: Reports (6)].\n",
      "\n",
      "### Metaphorical Interpretation\n",
      "\n",
      "The Universe is metaphorically treated as a concert hall by the Paranormal Military Squad, which suggests a broader perspective on how cosmic events are interpreted and responded to by human entities. This metaphorical view may influence how strategies and responses are formulated in relation to Cosmic Vocalization [Data: Reports (6)].\n",
      "\n",
      "In summary, Cosmic Vocalization is a complex phenomenon involving strategic, communicative, and potentially threatening elements. The involvement of the Paranormal Military Squad and the concerns raised by community members underscore its significance and the need for careful consideration of its implications.\n"
     ]
    }
   ],
   "source": [
    "result = await search_engine.asearch(\n",
    "    \"What is Cosmic Vocalization and who are involved in it?\"\n",
    ")\n",
    "\n",
    "print(result.response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>title</th>\n",
       "      <th>occurrence weight</th>\n",
       "      <th>content</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>15</td>\n",
       "      <td>Dulce Base and the Paranormal Military Squad: ...</td>\n",
       "      <td>1.00</td>\n",
       "      <td># Dulce Base and the Paranormal Military Squad...</td>\n",
       "      <td>9.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11</td>\n",
       "      <td>Dulce and Paranormal Military Squad Operations</td>\n",
       "      <td>0.30</td>\n",
       "      <td># Dulce and Paranormal Military Squad Operatio...</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10</td>\n",
       "      <td>Paranormal Military Squad at Dulce Base: Decod...</td>\n",
       "      <td>0.30</td>\n",
       "      <td># Paranormal Military Squad at Dulce Base: Dec...</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7</td>\n",
       "      <td>Operation: Dulce and the Paranormal Military S...</td>\n",
       "      <td>0.20</td>\n",
       "      <td># Operation: Dulce and the Paranormal Military...</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>8</td>\n",
       "      <td>Dr. Jordan Hayes and the Paranormal Military S...</td>\n",
       "      <td>0.18</td>\n",
       "      <td># Dr. Jordan Hayes and the Paranormal Military...</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>Earth's Interstellar Communication Initiative</td>\n",
       "      <td>0.16</td>\n",
       "      <td># Earth's Interstellar Communication Initiativ...</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>12</td>\n",
       "      <td>Paranormal Military Squad and Dulce Base Opera...</td>\n",
       "      <td>0.16</td>\n",
       "      <td># Paranormal Military Squad and Dulce Base Ope...</td>\n",
       "      <td>7.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>13</td>\n",
       "      <td>Mission Dynamics and Leadership: Cruz and Wash...</td>\n",
       "      <td>0.16</td>\n",
       "      <td># Mission Dynamics and Leadership: Cruz and Wa...</td>\n",
       "      <td>7.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>14</td>\n",
       "      <td>Dulce Base and Paranormal Military Squad: Brid...</td>\n",
       "      <td>0.12</td>\n",
       "      <td># Dulce Base and Paranormal Military Squad: Br...</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>16</td>\n",
       "      <td>Dulce Military Base and Alien Intelligence Com...</td>\n",
       "      <td>0.08</td>\n",
       "      <td># Dulce Military Base and Alien Intelligence C...</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>18</td>\n",
       "      <td>Paranormal Military Squad Team and Dulce Base'...</td>\n",
       "      <td>0.04</td>\n",
       "      <td># Paranormal Military Squad Team and Dulce Bas...</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>5</td>\n",
       "      <td>Alien Script and First Contact Operations</td>\n",
       "      <td>0.02</td>\n",
       "      <td># Alien Script and First Contact Operations\\n\\...</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>4</td>\n",
       "      <td>Dulce Facility and Control Room of Dulce: Extr...</td>\n",
       "      <td>0.02</td>\n",
       "      <td># Dulce Facility and Control Room of Dulce: Ex...</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>17</td>\n",
       "      <td>Dulce Team and Underground Command Center: Int...</td>\n",
       "      <td>0.02</td>\n",
       "      <td># Dulce Team and Underground Command Center: I...</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>19</td>\n",
       "      <td>Central Terminal and Viewing Monitors at Dulce...</td>\n",
       "      <td>0.02</td>\n",
       "      <td># Central Terminal and Viewing Monitors at Dul...</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>6</td>\n",
       "      <td>Cosmic Vocalization and Universe Interactions</td>\n",
       "      <td>0.02</td>\n",
       "      <td># Cosmic Vocalization and Universe Interaction...</td>\n",
       "      <td>7.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>9</td>\n",
       "      <td>Dulce Base Exploration by TEAM and MAINFRAME ROOM</td>\n",
       "      <td>0.02</td>\n",
       "      <td># Dulce Base Exploration by TEAM and MAINFRAME...</td>\n",
       "      <td>7.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    id                                              title  occurrence weight  \\\n",
       "0   15  Dulce Base and the Paranormal Military Squad: ...               1.00   \n",
       "1   11     Dulce and Paranormal Military Squad Operations               0.30   \n",
       "2   10  Paranormal Military Squad at Dulce Base: Decod...               0.30   \n",
       "3    7  Operation: Dulce and the Paranormal Military S...               0.20   \n",
       "4    8  Dr. Jordan Hayes and the Paranormal Military S...               0.18   \n",
       "5    1      Earth's Interstellar Communication Initiative               0.16   \n",
       "6   12  Paranormal Military Squad and Dulce Base Opera...               0.16   \n",
       "7   13  Mission Dynamics and Leadership: Cruz and Wash...               0.16   \n",
       "8   14  Dulce Base and Paranormal Military Squad: Brid...               0.12   \n",
       "9   16  Dulce Military Base and Alien Intelligence Com...               0.08   \n",
       "10  18  Paranormal Military Squad Team and Dulce Base'...               0.04   \n",
       "11   5          Alien Script and First Contact Operations               0.02   \n",
       "12   4  Dulce Facility and Control Room of Dulce: Extr...               0.02   \n",
       "13  17  Dulce Team and Underground Command Center: Int...               0.02   \n",
       "14  19  Central Terminal and Viewing Monitors at Dulce...               0.02   \n",
       "15   6      Cosmic Vocalization and Universe Interactions               0.02   \n",
       "16   9  Dulce Base Exploration by TEAM and MAINFRAME ROOM               0.02   \n",
       "\n",
       "                                              content  rank  \n",
       "0   # Dulce Base and the Paranormal Military Squad...   9.5  \n",
       "1   # Dulce and Paranormal Military Squad Operatio...   8.5  \n",
       "2   # Paranormal Military Squad at Dulce Base: Dec...   8.5  \n",
       "3   # Operation: Dulce and the Paranormal Military...   8.5  \n",
       "4   # Dr. Jordan Hayes and the Paranormal Military...   8.5  \n",
       "5   # Earth's Interstellar Communication Initiativ...   8.5  \n",
       "6   # Paranormal Military Squad and Dulce Base Ope...   7.5  \n",
       "7   # Mission Dynamics and Leadership: Cruz and Wa...   7.5  \n",
       "8   # Dulce Base and Paranormal Military Squad: Br...   8.5  \n",
       "9   # Dulce Military Base and Alien Intelligence C...   8.5  \n",
       "10  # Paranormal Military Squad Team and Dulce Bas...   8.5  \n",
       "11  # Alien Script and First Contact Operations\\n\\...   8.5  \n",
       "12  # Dulce Facility and Control Room of Dulce: Ex...   8.5  \n",
       "13  # Dulce Team and Underground Command Center: I...   8.5  \n",
       "14  # Central Terminal and Viewing Monitors at Dul...   8.5  \n",
       "15  # Cosmic Vocalization and Universe Interaction...   7.5  \n",
       "16  # Dulce Base Exploration by TEAM and MAINFRAME...   7.5  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# inspect the data used to build the context for the LLM responses\n",
    "result.context_data[\"reports\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LLM calls: 2. Prompt tokens: 11292. Output tokens: 606.\n"
     ]
    }
   ],
   "source": [
    "# inspect number of LLM calls and tokens\n",
    "print(\n",
    "    f\"LLM calls: {result.llm_calls}. Prompt tokens: {result.prompt_tokens}. Output tokens: {result.output_tokens}.\"\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "graphrag",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
