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