| 1 | { |
| 2 | "nbformat": 4, |
| 3 | "nbformat_minor": 0, |
| 4 | "metadata": { |
| 5 | "colab": { |
| 6 | "provenance": [] |
| 7 | }, |
| 8 | "kernelspec": { |
| 9 | "name": "python3", |
| 10 | "display_name": "Python 3" |
| 11 | }, |
| 12 | "language_info": { |
| 13 | "name": "python" |
| 14 | } |
| 15 | }, |
| 16 | "cells": [ |
| 17 | { |
| 18 | "attachments": {}, |
| 19 | "cell_type": "markdown", |
| 20 | "id": "82f90261", |
| 21 | "metadata": {}, |
| 22 | "source": [ |
| 23 | "# Getting Started with GraphRAG-SDK\n", |
| 24 | "\n", |
| 25 | "This notebook provides an introduction to using FalkorDB's GraphRAG software development kit.", |
| 26 | "It guides users through the setup process, dataset loading, and running basic examples.", |
| 27 | "Ideal for new users, this notebook helps familiarize with core functionalities and workflows", |
| 28 | "in GraphRAG for developing graph-based applications and analyses." |
| 29 | ] |
| 30 | }, |
| 31 | { |
| 32 | "cell_type": "markdown", |
| 33 | "source": [ |
| 34 | "Install dependencies" |
| 35 | ], |
| 36 | "metadata": { |
| 37 | "id": "Hry9wXv7Cret" |
| 38 | } |
| 39 | }, |
| 40 | { |
| 41 | "cell_type": "code", |
| 42 | "source": [ |
| 43 | "! python --version" |
| 44 | ], |
| 45 | "metadata": { |
| 46 | "colab": { |
| 47 | "base_uri": "https://localhost:8080/" |
| 48 | }, |
| 49 | "id": "C8JKk7ghLH08", |
| 50 | "outputId": "042e13c8-613f-4998-8795-eec11dec1b26" |
| 51 | }, |
| 52 | "execution_count": null, |
| 53 | "outputs": [ |
| 54 | { |
| 55 | "output_type": "stream", |
| 56 | "name": "stdout", |
| 57 | "text": [ |
| 58 | "Python 3.10.12\n" |
| 59 | ] |
| 60 | } |
| 61 | ] |
| 62 | }, |
| 63 | { |
| 64 | "cell_type": "code", |
| 65 | "execution_count": null, |
| 66 | "metadata": { |
| 67 | "colab": { |
| 68 | "base_uri": "https://localhost:8080/" |
| 69 | }, |
| 70 | "id": "EiVD4zpi8BIx", |
| 71 | "outputId": "ffe6a228-7955-42bd-96be-431acbbda374", |
| 72 | "collapsed": true |
| 73 | }, |
| 74 | "outputs": [ |
| 75 | { |
| 76 | "output_type": "stream", |
| 77 | "name": "stdout", |
| 78 | "text": [ |
| 79 | "Requirement already satisfied: FalkorDB in /usr/local/lib/python3.10/dist-packages (1.0.5)\n", |
| 80 | "Requirement already satisfied: redis<6.0.0,>=5.0.1 in /usr/local/lib/python3.10/dist-packages (from FalkorDB) (5.0.7)\n", |
| 81 | "Requirement already satisfied: async-timeout>=4.0.3 in /usr/local/lib/python3.10/dist-packages (from redis<6.0.0,>=5.0.1->FalkorDB) (4.0.3)\n", |
| 82 | "Collecting graphrag_sdk==0.1.3b0\n", |
| 83 | " Downloading graphrag_sdk-0.1.3b0.tar.gz (20 kB)\n", |
| 84 | " Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n", |
| 85 | " Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n", |
| 86 | " Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", |
| 87 | "Collecting bs4<0.0.3,>=0.0.2 (from graphrag_sdk==0.1.3b0)\n", |
| 88 | " Downloading bs4-0.0.2-py2.py3-none-any.whl (1.2 kB)\n", |
| 89 | "Requirement already satisfied: falkordb<2.0.0,>=1.0.4 in /usr/local/lib/python3.10/dist-packages (from graphrag_sdk==0.1.3b0) (1.0.5)\n", |
| 90 | "Collecting openai<2.0.0,>=1.30.3 (from graphrag_sdk==0.1.3b0)\n", |
| 91 | " Downloading openai-1.35.7-py3-none-any.whl (327 kB)\n", |
| 92 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m327.5/327.5 kB\u001b[0m \u001b[31m3.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", |
| 93 | "\u001b[?25hRequirement already satisfied: sqlalchemy<3.0.0,>=2.0.30 in /usr/local/lib/python3.10/dist-packages (from graphrag_sdk==0.1.3b0) (2.0.31)\n", |
| 94 | "Requirement already satisfied: typing-extensions<5.0.0,>=4.12.1 in /usr/local/lib/python3.10/dist-packages (from graphrag_sdk==0.1.3b0) (4.12.2)\n", |
| 95 | "Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.10/dist-packages (from bs4<0.0.3,>=0.0.2->graphrag_sdk==0.1.3b0) (4.12.3)\n", |
| 96 | "Requirement already satisfied: redis<6.0.0,>=5.0.1 in /usr/local/lib/python3.10/dist-packages (from falkordb<2.0.0,>=1.0.4->graphrag_sdk==0.1.3b0) (5.0.7)\n", |
| 97 | "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (3.7.1)\n", |
| 98 | "Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (1.7.0)\n", |
| 99 | "Collecting httpx<1,>=0.23.0 (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0)\n", |
| 100 | " Downloading httpx-0.27.0-py3-none-any.whl (75 kB)\n", |
| 101 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m8.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", |
| 102 | "\u001b[?25hRequirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (2.7.4)\n", |
| 103 | "Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (1.3.1)\n", |
| 104 | "Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (4.66.4)\n", |
| 105 | "Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.10/dist-packages (from sqlalchemy<3.0.0,>=2.0.30->graphrag_sdk==0.1.3b0) (3.0.3)\n", |
| 106 | "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (3.7)\n", |
| 107 | "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (1.2.1)\n", |
| 108 | "Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (2024.6.2)\n", |
| 109 | "Collecting httpcore==1.* (from httpx<1,>=0.23.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0)\n", |
| 110 | " Downloading httpcore-1.0.5-py3-none-any.whl (77 kB)\n", |
| 111 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m10.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", |
| 112 | "\u001b[?25hCollecting h11<0.15,>=0.13 (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0)\n", |
| 113 | " Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n", |
| 114 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m7.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", |
| 115 | "\u001b[?25hRequirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (0.7.0)\n", |
| 116 | "Requirement already satisfied: pydantic-core==2.18.4 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (2.18.4)\n", |
| 117 | "Requirement already satisfied: async-timeout>=4.0.3 in /usr/local/lib/python3.10/dist-packages (from redis<6.0.0,>=5.0.1->falkordb<2.0.0,>=1.0.4->graphrag_sdk==0.1.3b0) (4.0.3)\n", |
| 118 | "Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.10/dist-packages (from beautifulsoup4->bs4<0.0.3,>=0.0.2->graphrag_sdk==0.1.3b0) (2.5)\n", |
| 119 | "Building wheels for collected packages: graphrag_sdk\n", |
| 120 | " Building wheel for graphrag_sdk (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", |
| 121 | " Created wheel for graphrag_sdk: filename=graphrag_sdk-0.1.3b0-py3-none-any.whl size=26462 sha256=69ae6d4408a40b6cc4119e670a7a82af70225c0f9d3c412426aad4c798b40732\n", |
| 122 | " Stored in directory: /root/.cache/pip/wheels/b5/e7/44/c5997105d448c1b73d27b9b2ae440afafc1617e495f9c7fb0e\n", |
| 123 | "Successfully built graphrag_sdk\n", |
| 124 | "Installing collected packages: h11, httpcore, bs4, httpx, openai, graphrag_sdk\n", |
| 125 | "Successfully installed bs4-0.0.2 graphrag_sdk-0.1.3b0 h11-0.14.0 httpcore-1.0.5 httpx-0.27.0 openai-1.35.7\n" |
| 126 | ] |
| 127 | } |
| 128 | ], |
| 129 | "source": [ |
| 130 | "# prompt: pip install FalkorDB & GraphRAG-SDK\n", |
| 131 | "\n", |
| 132 | "!pip install FalkorDB\n", |
| 133 | "!pip install graphrag_sdk==0.1.3b0" |
| 134 | ] |
| 135 | }, |
| 136 | { |
| 137 | "cell_type": "markdown", |
| 138 | "source": [ |
| 139 | "Setup dependencies:\n", |
| 140 | "\n", |
| 141 | "1. OpenAI KEY\n", |
| 142 | "\n", |
| 143 | "2. FalkorDB connection details\n", |
| 144 | "\n", |
| 145 | "Create a FalkorDB instance at: http://cloud.falkordb.com/\n", |
| 146 | "\n", |
| 147 | "And update the following fields accordingly: host, port, username, password" |
| 148 | ], |
| 149 | "metadata": { |
| 150 | "id": "cHTwoEtWcwgX" |
| 151 | } |
| 152 | }, |
| 153 | { |
| 154 | "cell_type": "code", |
| 155 | "source": [ |
| 156 | "# set OpenAI key\n", |
| 157 | "import os\n", |
| 158 | "os.environ[\"OPENAI_API_KEY\"] = \"YOUR-OPENAI-KEY\"\n", |
| 159 | "\n", |
| 160 | "# Define connection details for FalkorDB\n", |
| 161 | "host = \"FALKORDB_HOST\"\n", |
| 162 | "port = \"FALKORDB_PORT\"\n", |
| 163 | "username = 'FALKORDB_USERNAME'\n", |
| 164 | "password = 'FALKORDB_PASSWORD'" |
| 165 | ], |
| 166 | "metadata": { |
| 167 | "id": "YmAoBzUL_kfA" |
| 168 | }, |
| 169 | "execution_count": null, |
| 170 | "outputs": [] |
| 171 | }, |
| 172 | { |
| 173 | "cell_type": "markdown", |
| 174 | "source": [ |
| 175 | "Auto detect knowledge graph ontology" |
| 176 | ], |
| 177 | "metadata": { |
| 178 | "id": "Ebb2RrhOBUii" |
| 179 | } |
| 180 | }, |
| 181 | { |
| 182 | "cell_type": "code", |
| 183 | "source": [ |
| 184 | "from graphrag_sdk import Source\n", |
| 185 | "from graphrag_sdk.schema import Schema\n", |
| 186 | "\n", |
| 187 | "# Auto detect schema based on a single URL\n", |
| 188 | "source = Source(\"https://www.rottentomatoes.com/m/matrix\")\n", |
| 189 | "s = Schema.auto_detect([source])\n", |
| 190 | "\n", |
| 191 | "# Print schema\n", |
| 192 | "print(\"Auto detected schema:\")\n", |
| 193 | "print(s.to_JSON())" |
| 194 | ], |
| 195 | "metadata": { |
| 196 | "colab": { |
| 197 | "base_uri": "https://localhost:8080/" |
| 198 | }, |
| 199 | "id": "b0Yo-wqMBEhl", |
| 200 | "outputId": "38a2554c-4af5-4926-c5ee-758b7224fa62", |
| 201 | "collapsed": true |
| 202 | }, |
| 203 | "execution_count": null, |
| 204 | "outputs": [ |
| 205 | { |
| 206 | "output_type": "stream", |
| 207 | "name": "stdout", |
| 208 | "text": [ |
| 209 | "Auto detected schema:\n", |
| 210 | "{\n", |
| 211 | " \"entities\": [\n", |
| 212 | " {\n", |
| 213 | " \"name\": \"Reviews\",\n", |
| 214 | " \"attributes\": [\n", |
| 215 | " {\n", |
| 216 | " \"name\": \"review_date\",\n", |
| 217 | " \"type\": \"str\",\n", |
| 218 | " \"desc\": \"Reviews's review_date\",\n", |
| 219 | " \"unique\": true,\n", |
| 220 | " \"mandatory\": true\n", |
| 221 | " },\n", |
| 222 | " {\n", |
| 223 | " \"name\": \"movie_title\",\n", |
| 224 | " \"type\": \"str\",\n", |
| 225 | " \"desc\": \"Reviews's movie_title\",\n", |
| 226 | " \"unique\": false,\n", |
| 227 | " \"mandatory\": false\n", |
| 228 | " },\n", |
| 229 | " {\n", |
| 230 | " \"name\": \"reviewer\",\n", |
| 231 | " \"type\": \"str\",\n", |
| 232 | " \"desc\": \"Reviews's reviewer\",\n", |
| 233 | " \"unique\": false,\n", |
| 234 | " \"mandatory\": false\n", |
| 235 | " },\n", |
| 236 | " {\n", |
| 237 | " \"name\": \"rating\",\n", |
| 238 | " \"type\": \"str\",\n", |
| 239 | " \"desc\": \"Reviews's rating\",\n", |
| 240 | " \"unique\": false,\n", |
| 241 | " \"mandatory\": false\n", |
| 242 | " }\n", |
| 243 | " ]\n", |
| 244 | " },\n", |
| 245 | " {\n", |
| 246 | " \"name\": \"Cast\",\n", |
| 247 | " \"attributes\": [\n", |
| 248 | " {\n", |
| 249 | " \"name\": \"movie_title\",\n", |
| 250 | " \"type\": \"str\",\n", |
| 251 | " \"desc\": \"Cast's movie_title\",\n", |
| 252 | " \"unique\": false,\n", |
| 253 | " \"mandatory\": false\n", |
| 254 | " },\n", |
| 255 | " {\n", |
| 256 | " \"name\": \"character_name\",\n", |
| 257 | " \"type\": \"str\",\n", |
| 258 | " \"desc\": \"Cast's character_name\",\n", |
| 259 | " \"unique\": true,\n", |
| 260 | " \"mandatory\": true\n", |
| 261 | " },\n", |
| 262 | " {\n", |
| 263 | " \"name\": \"actor_name\",\n", |
| 264 | " \"type\": \"str\",\n", |
| 265 | " \"desc\": \"Cast's actor_name\",\n", |
| 266 | " \"unique\": false,\n", |
| 267 | " \"mandatory\": false\n", |
| 268 | " }\n", |
| 269 | " ]\n", |
| 270 | " },\n", |
| 271 | " {\n", |
| 272 | " \"name\": \"Ratings\",\n", |
| 273 | " \"attributes\": [\n", |
| 274 | " {\n", |
| 275 | " \"name\": \"title\",\n", |
| 276 | " \"type\": \"str\",\n", |
| 277 | " \"desc\": \"Ratings's title\",\n", |
| 278 | " \"unique\": true,\n", |
| 279 | " \"mandatory\": true\n", |
| 280 | " },\n", |
| 281 | " {\n", |
| 282 | " \"name\": \"tomatometer_rating\",\n", |
| 283 | " \"type\": \"str\",\n", |
| 284 | " \"desc\": \"Ratings's tomatometer_rating\",\n", |
| 285 | " \"unique\": false,\n", |
| 286 | " \"mandatory\": false\n", |
| 287 | " },\n", |
| 288 | " {\n", |
| 289 | " \"name\": \"audience_score\",\n", |
| 290 | " \"type\": \"str\",\n", |
| 291 | " \"desc\": \"Ratings's audience_score\",\n", |
| 292 | " \"unique\": false,\n", |
| 293 | " \"mandatory\": false\n", |
| 294 | " },\n", |
| 295 | " {\n", |
| 296 | " \"name\": \"num_reviews\",\n", |
| 297 | " \"type\": \"str\",\n", |
| 298 | " \"desc\": \"Ratings's num_reviews\",\n", |
| 299 | " \"unique\": false,\n", |
| 300 | " \"mandatory\": false\n", |
| 301 | " }\n", |
| 302 | " ]\n", |
| 303 | " },\n", |
| 304 | " {\n", |
| 305 | " \"name\": \"Movies\",\n", |
| 306 | " \"attributes\": [\n", |
| 307 | " {\n", |
| 308 | " \"name\": \"title\",\n", |
| 309 | " \"type\": \"str\",\n", |
| 310 | " \"desc\": \"Movies's title\",\n", |
| 311 | " \"unique\": true,\n", |
| 312 | " \"mandatory\": true\n", |
| 313 | " },\n", |
| 314 | " {\n", |
| 315 | " \"name\": \"genre\",\n", |
| 316 | " \"type\": \"str\",\n", |
| 317 | " \"desc\": \"Movies's genre\",\n", |
| 318 | " \"unique\": false,\n", |
| 319 | " \"mandatory\": false\n", |
| 320 | " },\n", |
| 321 | " {\n", |
| 322 | " \"name\": \"release_date\",\n", |
| 323 | " \"type\": \"str\",\n", |
| 324 | " \"desc\": \"Movies's release_date\",\n", |
| 325 | " \"unique\": false,\n", |
| 326 | " \"mandatory\": false\n", |
| 327 | " },\n", |
| 328 | " {\n", |
| 329 | " \"name\": \"duration\",\n", |
| 330 | " \"type\": \"str\",\n", |
| 331 | " \"desc\": \"Movies's duration\",\n", |
| 332 | " \"unique\": false,\n", |
| 333 | " \"mandatory\": false\n", |
| 334 | " }\n", |
| 335 | " ]\n", |
| 336 | " },\n", |
| 337 | " {\n", |
| 338 | " \"name\": \"Directors\",\n", |
| 339 | " \"attributes\": [\n", |
| 340 | " {\n", |
| 341 | " \"name\": \"director_name\",\n", |
| 342 | " \"type\": \"str\",\n", |
| 343 | " \"desc\": \"Directors's director_name\",\n", |
| 344 | " \"unique\": true,\n", |
| 345 | " \"mandatory\": true\n", |
| 346 | " },\n", |
| 347 | " {\n", |
| 348 | " \"name\": \"movie_title\",\n", |
| 349 | " \"type\": \"str\",\n", |
| 350 | " \"desc\": \"Directors's movie_title\",\n", |
| 351 | " \"unique\": false,\n", |
| 352 | " \"mandatory\": false\n", |
| 353 | " }\n", |
| 354 | " ]\n", |
| 355 | " }\n", |
| 356 | " ],\n", |
| 357 | " \"relations\": []\n", |
| 358 | "}\n" |
| 359 | ] |
| 360 | } |
| 361 | ] |
| 362 | }, |
| 363 | { |
| 364 | "cell_type": "markdown", |
| 365 | "source": [ |
| 366 | "Manually define knowledge graph ontology" |
| 367 | ], |
| 368 | "metadata": { |
| 369 | "id": "ovQHYWzCB_rC" |
| 370 | } |
| 371 | }, |
| 372 | { |
| 373 | "cell_type": "code", |
| 374 | "source": [ |
| 375 | "# Manually define schema\n", |
| 376 | "s = Schema()\n", |
| 377 | "\n", |
| 378 | "# Entities:\n", |
| 379 | "# 1. Actor\n", |
| 380 | "# 2. Director\n", |
| 381 | "# 3. Movie\n", |
| 382 | "\n", |
| 383 | "actor = s.add_entity('Actor')\n", |
| 384 | "actor.add_attribute('Name', str, unique=True, mandatory=True)\n", |
| 385 | "\n", |
| 386 | "director = s.add_entity('Director')\n", |
| 387 | "director.add_attribute('Name', str, unique=True, mandatory=True)\n", |
| 388 | "\n", |
| 389 | "movie = s.add_entity('Movie')\n", |
| 390 | "movie.add_attribute('Title', str, unique=True, mandatory=True)\n", |
| 391 | "movie.add_attribute('ReleaseYear', int)\n", |
| 392 | "movie.add_attribute('Rating', int)\n", |
| 393 | "\n", |
| 394 | "# Relations:\n", |
| 395 | "# 1. (Actor)-[ACTED]->(Movie)\n", |
| 396 | "# 2. (Director)-[DIRECTED]->(Movie)\n", |
| 397 | "\n", |
| 398 | "s.add_relation(\"ACTED\", actor, movie)\n", |
| 399 | "s.add_relation(\"DIRECTED\", director, movie)\n", |
| 400 | "\n", |
| 401 | "# Print schema\n", |
| 402 | "print(\"Manually defined schema:\")\n", |
| 403 | "print(s.to_JSON())" |
| 404 | ], |
| 405 | "metadata": { |
| 406 | "colab": { |
| 407 | "base_uri": "https://localhost:8080/" |
| 408 | }, |
| 409 | "id": "dDY7hReqCDpZ", |
| 410 | "outputId": "72376333-723d-49ea-8f56-fe266b1d2253", |
| 411 | "collapsed": true |
| 412 | }, |
| 413 | "execution_count": null, |
| 414 | "outputs": [ |
| 415 | { |
| 416 | "output_type": "stream", |
| 417 | "name": "stdout", |
| 418 | "text": [ |
| 419 | "Manually defined schema:\n", |
| 420 | "{\n", |
| 421 | " \"entities\": [\n", |
| 422 | " {\n", |
| 423 | " \"name\": \"Movie\",\n", |
| 424 | " \"attributes\": [\n", |
| 425 | " {\n", |
| 426 | " \"name\": \"ReleaseYear\",\n", |
| 427 | " \"type\": \"int\",\n", |
| 428 | " \"desc\": \"Movie's ReleaseYear\",\n", |
| 429 | " \"unique\": false,\n", |
| 430 | " \"mandatory\": false\n", |
| 431 | " },\n", |
| 432 | " {\n", |
| 433 | " \"name\": \"Title\",\n", |
| 434 | " \"type\": \"str\",\n", |
| 435 | " \"desc\": \"Movie's Title\",\n", |
| 436 | " \"unique\": true,\n", |
| 437 | " \"mandatory\": true\n", |
| 438 | " },\n", |
| 439 | " {\n", |
| 440 | " \"name\": \"Rating\",\n", |
| 441 | " \"type\": \"int\",\n", |
| 442 | " \"desc\": \"Movie's Rating\",\n", |
| 443 | " \"unique\": false,\n", |
| 444 | " \"mandatory\": false\n", |
| 445 | " }\n", |
| 446 | " ]\n", |
| 447 | " },\n", |
| 448 | " {\n", |
| 449 | " \"name\": \"Actor\",\n", |
| 450 | " \"attributes\": [\n", |
| 451 | " {\n", |
| 452 | " \"name\": \"Name\",\n", |
| 453 | " \"type\": \"str\",\n", |
| 454 | " \"desc\": \"Actor's Name\",\n", |
| 455 | " \"unique\": true,\n", |
| 456 | " \"mandatory\": true\n", |
| 457 | " }\n", |
| 458 | " ]\n", |
| 459 | " },\n", |
| 460 | " {\n", |
| 461 | " \"name\": \"Director\",\n", |
| 462 | " \"attributes\": [\n", |
| 463 | " {\n", |
| 464 | " \"name\": \"Name\",\n", |
| 465 | " \"type\": \"str\",\n", |
| 466 | " \"desc\": \"Director's Name\",\n", |
| 467 | " \"unique\": true,\n", |
| 468 | " \"mandatory\": true\n", |
| 469 | " }\n", |
| 470 | " ]\n", |
| 471 | " }\n", |
| 472 | " ],\n", |
| 473 | " \"relations\": [\n", |
| 474 | " {\n", |
| 475 | " \"name\": \"ACTED\",\n", |
| 476 | " \"src\": \"Actor\",\n", |
| 477 | " \"dest\": \"Movie\"\n", |
| 478 | " },\n", |
| 479 | " {\n", |
| 480 | " \"name\": \"DIRECTED\",\n", |
| 481 | " \"src\": \"Director\",\n", |
| 482 | " \"dest\": \"Movie\"\n", |
| 483 | " }\n", |
| 484 | " ]\n", |
| 485 | "}\n" |
| 486 | ] |
| 487 | } |
| 488 | ] |
| 489 | }, |
| 490 | { |
| 491 | "cell_type": "markdown", |
| 492 | "source": [ |
| 493 | "Construct knowledge graph\n", |
| 494 | "From a number of sources:\n", |
| 495 | "1. https://www.rottentomatoes.com/m/matrix\n", |
| 496 | "2. https://www.rottentomatoes.com/m/matrix_reloaded\n", |
| 497 | "3. https://www.rottentomatoes.com/m/the_matrix_resurrections" |
| 498 | ], |
| 499 | "metadata": { |
| 500 | "id": "biEmU00dYcov" |
| 501 | } |
| 502 | }, |
| 503 | { |
| 504 | "cell_type": "code", |
| 505 | "source": [ |
| 506 | "from graphrag_sdk import KnowledgeGraph\n", |
| 507 | "# Create Knowledge Graph\n", |
| 508 | "g = KnowledgeGraph(\"rottentomatoes\", host=host, port=port, username=username,\n", |
| 509 | " password=password, schema=s, model=\"gpt-3.5-turbo-0125\")\n", |
| 510 | "\n", |
| 511 | "# Ingest\n", |
| 512 | "# Define sources from which knowledge will be created\n", |
| 513 | "sources = [\n", |
| 514 | " Source(\"https://www.rottentomatoes.com/m/matrix\"),\n", |
| 515 | " Source(\"https://www.rottentomatoes.com/m/matrix_reloaded\"),\n", |
| 516 | " Source(\"https://www.rottentomatoes.com/m/the_matrix_resurrections\")\n", |
| 517 | "]\n", |
| 518 | "\n", |
| 519 | "g.process_sources(sources)" |
| 520 | ], |
| 521 | "metadata": { |
| 522 | "id": "lIZB4-pUYfD6" |
| 523 | }, |
| 524 | "execution_count": null, |
| 525 | "outputs": [] |
| 526 | }, |
| 527 | { |
| 528 | "cell_type": "markdown", |
| 529 | "source": [ |
| 530 | "Predefined questions" |
| 531 | ], |
| 532 | "metadata": { |
| 533 | "id": "qUNmYow0a5Xe" |
| 534 | } |
| 535 | }, |
| 536 | { |
| 537 | "cell_type": "code", |
| 538 | "source": [ |
| 539 | "# Query\n", |
| 540 | "msgs = []\n", |
| 541 | "questions = [\n", |
| 542 | " \"List a few actors\",\n", |
| 543 | " \"Provide a break down of the number of movies each actored played in\",\n", |
| 544 | " \"Which movie has the heighest Rating?\"\n", |
| 545 | "]\n", |
| 546 | "\n", |
| 547 | "for q in questions:\n", |
| 548 | " print(f\"Question: {q}\")\n", |
| 549 | " ans, msgs = g.ask(q, history=msgs)\n", |
| 550 | " print(f\"Answer: {ans}\\n\")" |
| 551 | ], |
| 552 | "metadata": { |
| 553 | "colab": { |
| 554 | "base_uri": "https://localhost:8080/" |
| 555 | }, |
| 556 | "id": "02XZHFdOY421", |
| 557 | "outputId": "4f31e139-2626-4930-ed06-34a2b9613973", |
| 558 | "collapsed": true |
| 559 | }, |
| 560 | "execution_count": null, |
| 561 | "outputs": [ |
| 562 | { |
| 563 | "output_type": "stream", |
| 564 | "name": "stdout", |
| 565 | "text": [ |
| 566 | "Question: List a few actors\n", |
| 567 | "Answer: Here are a few actors:\n", |
| 568 | "1. Keanu Reeves\n", |
| 569 | "2. Carrie-Anne Moss\n", |
| 570 | "3. Yahya Abdul-Mateen II\n", |
| 571 | "4. Jonathan Groff\n", |
| 572 | "5. Jessica Henwick\n", |
| 573 | "\n", |
| 574 | "Question: Provide a break down of the number of movies each actored played in\n", |
| 575 | "Answer: Here is the breakdown of the number of movies each actor played in:\n", |
| 576 | "- Keanu Reeves: 2 movies\n", |
| 577 | "- Jessica Henwick: 1 movie\n", |
| 578 | "- Carrie-Anne Moss: 2 movies\n", |
| 579 | "- Laurence Fishburne: 1 movie\n", |
| 580 | "- Jonathan Groff: 1 movie\n", |
| 581 | "- Yahya Abdul-Mateen II: 1 movie\n", |
| 582 | "\n", |
| 583 | "Question: Which movie has the heighest Rating?\n", |
| 584 | "Answer: The movie with the highest rating is \"The Matrix\" with a rating of 83.\n", |
| 585 | "\n" |
| 586 | ] |
| 587 | } |
| 588 | ] |
| 589 | }, |
| 590 | { |
| 591 | "cell_type": "markdown", |
| 592 | "source": [ |
| 593 | "Q&A Session\n", |
| 594 | "Type exit to quit." |
| 595 | ], |
| 596 | "metadata": { |
| 597 | "id": "S0H5vAX2azhy" |
| 598 | } |
| 599 | }, |
| 600 | { |
| 601 | "cell_type": "code", |
| 602 | "source": [ |
| 603 | "while True:\n", |
| 604 | " q = input(\"Question: \")\n", |
| 605 | " if q == \"exit\":\n", |
| 606 | " break\n", |
| 607 | "\n", |
| 608 | " ans, msgs = g.ask(q, history=msgs)\n", |
| 609 | " print(f\"Answer: {ans}\\n\")" |
| 610 | ], |
| 611 | "metadata": { |
| 612 | "colab": { |
| 613 | "base_uri": "https://localhost:8080/" |
| 614 | }, |
| 615 | "id": "TCTSZGi5ae8n", |
| 616 | "outputId": "044fba36-bcf4-4d25-a0dc-b79727faf4b5", |
| 617 | "collapsed": true |
| 618 | }, |
| 619 | "execution_count": null, |
| 620 | "outputs": [ |
| 621 | { |
| 622 | "name": "stdout", |
| 623 | "output_type": "stream", |
| 624 | "text": [ |
| 625 | "Question: Which movies are in the dataset ?\n", |
| 626 | "Answer: The movies in the dataset are:\n", |
| 627 | "1. The Matrix\n", |
| 628 | "2. The Matrix Resurrections\n", |
| 629 | "3. The Matrix Reloaded\n", |
| 630 | "\n", |
| 631 | "Question: Which movie has the largest cast ?\n", |
| 632 | "Answer: The movie with the largest cast is \"The Matrix Resurrections\" with a cast size of 5.\n", |
| 633 | "\n", |
| 634 | "Question: exit\n" |
| 635 | ] |
| 636 | } |
| 637 | ] |
| 638 | } |
| 639 | ] |
| 640 | } |