| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "metadata": {}, |
| 6 | "source": [ |
| 7 | "# Using GraphRAG-SDK to Create a Multi-Agent-Orchestrator RAG System based on Knowledge Graph from Structured Data\n", |
| 8 | "\n", |
| 9 | "GraphRAG-SDK provides a powerful tool, enhanced by LLM technology, for building a Multi-Agent-Orchestrator RAG System. This example demonstrates how to create a knowledge graph-based multi-agent system from structured data and how to use the orchestrator to manage the agents and perform question answering." |
| 10 | ] |
| 11 | }, |
| 12 | { |
| 13 | "cell_type": "code", |
| 14 | "execution_count": null, |
| 15 | "metadata": {}, |
| 16 | "outputs": [], |
| 17 | "source": [ |
| 18 | "!pip install graphrag_sdk" |
| 19 | ] |
| 20 | }, |
| 21 | { |
| 22 | "cell_type": "code", |
| 23 | "execution_count": 1, |
| 24 | "metadata": {}, |
| 25 | "outputs": [ |
| 26 | { |
| 27 | "data": { |
| 28 | "text/plain": [ |
| 29 | "True" |
| 30 | ] |
| 31 | }, |
| 32 | "execution_count": 1, |
| 33 | "metadata": {}, |
| 34 | "output_type": "execute_result" |
| 35 | } |
| 36 | ], |
| 37 | "source": [ |
| 38 | "\n", |
| 39 | "from json import loads\n", |
| 40 | "from dotenv import load_dotenv\n", |
| 41 | "from graphrag_sdk.orchestrator import Orchestrator\n", |
| 42 | "from graphrag_sdk.agents.kg_agent import KGAgent\n", |
| 43 | "from graphrag_sdk.models.openai import OpenAiGenerativeModel\n", |
| 44 | "from graphrag_sdk import (\n", |
| 45 | " Ontology, Entity, Relation, Attribute, AttributeType, KnowledgeGraph, KnowledgeGraphModelConfig\n", |
| 46 | ")\n", |
| 47 | "\n", |
| 48 | "# Load environment variables\n", |
| 49 | "load_dotenv()\n", |
| 50 | "\n", |
| 51 | "# Configuration\n", |
| 52 | "# OPENAI_API_KEY = \"sk-\"# OpenAI API key" |
| 53 | ] |
| 54 | }, |
| 55 | { |
| 56 | "cell_type": "markdown", |
| 57 | "metadata": {}, |
| 58 | "source": [ |
| 59 | "### Ontology - Predefined Ontology\n", |
| 60 | "\n", |
| 61 | "We will begin with an initial ontology for the agents, based on the knowledge that interests us.\n" |
| 62 | ] |
| 63 | }, |
| 64 | { |
| 65 | "cell_type": "code", |
| 66 | "execution_count": 2, |
| 67 | "metadata": {}, |
| 68 | "outputs": [], |
| 69 | "source": [ |
| 70 | "# Restaurants Ontology\n", |
| 71 | "restaurants_ontology = Ontology()\n", |
| 72 | "\n", |
| 73 | "# Attractions Ontology\n", |
| 74 | "attractions_ontology = Ontology()\n", |
| 75 | "\n", |
| 76 | "# Manually created Ontology by adding entities and relations\n", |
| 77 | "restaurants_ontology.add_entity(\n", |
| 78 | " Entity(\n", |
| 79 | " label=\"Country\",\n", |
| 80 | " attributes=[\n", |
| 81 | " Attribute(\n", |
| 82 | " name=\"name\",\n", |
| 83 | " attr_type=AttributeType.STRING,\n", |
| 84 | " required=True,\n", |
| 85 | " unique=True,\n", |
| 86 | " ),\n", |
| 87 | " ],\n", |
| 88 | " )\n", |
| 89 | ")\n", |
| 90 | "restaurants_ontology.add_entity(\n", |
| 91 | " Entity(\n", |
| 92 | " label=\"City\",\n", |
| 93 | " attributes=[\n", |
| 94 | " Attribute(\n", |
| 95 | " name=\"name\",\n", |
| 96 | " attr_type=AttributeType.STRING,\n", |
| 97 | " required=True,\n", |
| 98 | " unique=True,\n", |
| 99 | " ),\n", |
| 100 | " Attribute(\n", |
| 101 | " name=\"weather\",\n", |
| 102 | " attr_type=AttributeType.STRING,\n", |
| 103 | " required=False,\n", |
| 104 | " unique=False,\n", |
| 105 | " ),\n", |
| 106 | " Attribute(\n", |
| 107 | " name=\"population\",\n", |
| 108 | " attr_type=AttributeType.NUMBER,\n", |
| 109 | " required=False,\n", |
| 110 | " unique=False,\n", |
| 111 | " ),\n", |
| 112 | " ],\n", |
| 113 | " )\n", |
| 114 | ")\n", |
| 115 | "restaurants_ontology.add_entity(\n", |
| 116 | " Entity(\n", |
| 117 | " label=\"Restaurant\",\n", |
| 118 | " attributes=[\n", |
| 119 | " Attribute(\n", |
| 120 | " name=\"name\",\n", |
| 121 | " attr_type=AttributeType.STRING,\n", |
| 122 | " required=True,\n", |
| 123 | " unique=True,\n", |
| 124 | " ),\n", |
| 125 | " Attribute(\n", |
| 126 | " name=\"description\",\n", |
| 127 | " attr_type=AttributeType.STRING,\n", |
| 128 | " required=False,\n", |
| 129 | " unique=False,\n", |
| 130 | " ),\n", |
| 131 | " Attribute(\n", |
| 132 | " name=\"rating\",\n", |
| 133 | " attr_type=AttributeType.NUMBER,\n", |
| 134 | " required=False,\n", |
| 135 | " unique=False,\n", |
| 136 | " ),\n", |
| 137 | " Attribute(\n", |
| 138 | " name=\"food_type\",\n", |
| 139 | " attr_type=AttributeType.STRING,\n", |
| 140 | " required=False,\n", |
| 141 | " unique=False,\n", |
| 142 | " ),\n", |
| 143 | " ],\n", |
| 144 | " )\n", |
| 145 | ")\n", |
| 146 | "restaurants_ontology.add_relation(\n", |
| 147 | " Relation(\n", |
| 148 | " label=\"IN_COUNTRY\",\n", |
| 149 | " source=\"City\",\n", |
| 150 | " target=\"Country\",\n", |
| 151 | " )\n", |
| 152 | ")\n", |
| 153 | "restaurants_ontology.add_relation(\n", |
| 154 | " Relation(\n", |
| 155 | " label=\"IN_CITY\",\n", |
| 156 | " source=\"Restaurant\",\n", |
| 157 | " target=\"City\",\n", |
| 158 | " )\n", |
| 159 | ")\n", |
| 160 | "\n", |
| 161 | "attractions_ontology.add_entity(\n", |
| 162 | " Entity(\n", |
| 163 | " label=\"Country\",\n", |
| 164 | " attributes=[\n", |
| 165 | " Attribute(\n", |
| 166 | " name=\"name\",\n", |
| 167 | " attr_type=AttributeType.STRING,\n", |
| 168 | " required=True,\n", |
| 169 | " unique=True,\n", |
| 170 | " ),\n", |
| 171 | " ],\n", |
| 172 | " )\n", |
| 173 | ")\n", |
| 174 | "attractions_ontology.add_entity(\n", |
| 175 | " Entity(\n", |
| 176 | " label=\"City\",\n", |
| 177 | " attributes=[\n", |
| 178 | " Attribute(\n", |
| 179 | " name=\"name\",\n", |
| 180 | " attr_type=AttributeType.STRING,\n", |
| 181 | " required=True,\n", |
| 182 | " unique=True,\n", |
| 183 | " ),\n", |
| 184 | " Attribute(\n", |
| 185 | " name=\"weather\",\n", |
| 186 | " attr_type=AttributeType.STRING,\n", |
| 187 | " required=False,\n", |
| 188 | " unique=False,\n", |
| 189 | " ),\n", |
| 190 | " Attribute(\n", |
| 191 | " name=\"population\",\n", |
| 192 | " attr_type=AttributeType.NUMBER,\n", |
| 193 | " required=False,\n", |
| 194 | " unique=False,\n", |
| 195 | " ),\n", |
| 196 | " ],\n", |
| 197 | " )\n", |
| 198 | ")\n", |
| 199 | "attractions_ontology.add_entity(\n", |
| 200 | " Entity(\n", |
| 201 | " label=\"Attraction\",\n", |
| 202 | " attributes=[\n", |
| 203 | " Attribute(\n", |
| 204 | " name=\"name\",\n", |
| 205 | " attr_type=AttributeType.STRING,\n", |
| 206 | " required=True,\n", |
| 207 | " unique=True,\n", |
| 208 | " ),\n", |
| 209 | " Attribute(\n", |
| 210 | " name=\"description\",\n", |
| 211 | " attr_type=AttributeType.STRING,\n", |
| 212 | " required=False,\n", |
| 213 | " unique=False,\n", |
| 214 | " ),\n", |
| 215 | " Attribute(\n", |
| 216 | " name=\"type\",\n", |
| 217 | " attr_type=AttributeType.STRING,\n", |
| 218 | " required=False,\n", |
| 219 | " unique=False,\n", |
| 220 | " ),\n", |
| 221 | " ],\n", |
| 222 | " )\n", |
| 223 | ")\n", |
| 224 | "attractions_ontology.add_relation(\n", |
| 225 | " Relation(\n", |
| 226 | " label=\"IN_COUNTRY\",\n", |
| 227 | " source=\"City\",\n", |
| 228 | " target=\"Country\",\n", |
| 229 | " )\n", |
| 230 | ")\n", |
| 231 | "attractions_ontology.add_relation(\n", |
| 232 | " Relation(\n", |
| 233 | " label=\"IN_CITY\",\n", |
| 234 | " source=\"Attraction\",\n", |
| 235 | " target=\"City\",\n", |
| 236 | " )\n", |
| 237 | ")" |
| 238 | ] |
| 239 | }, |
| 240 | { |
| 241 | "cell_type": "markdown", |
| 242 | "metadata": {}, |
| 243 | "source": [ |
| 244 | "### KG from Structure Data\n", |
| 245 | "\n", |
| 246 | "Creating the KG from the data that will be used by the agents" |
| 247 | ] |
| 248 | }, |
| 249 | { |
| 250 | "cell_type": "code", |
| 251 | "execution_count": 3, |
| 252 | "metadata": {}, |
| 253 | "outputs": [], |
| 254 | "source": [ |
| 255 | "# Define the model\n", |
| 256 | "model = OpenAiGenerativeModel(\"gpt-4o\")\n", |
| 257 | "\n", |
| 258 | "# Create the KG from the predefined ontology.\n", |
| 259 | "# Restaurants KG\n", |
| 260 | "restaurants_kg = KnowledgeGraph(\n", |
| 261 | " name=\"restaurants\",\n", |
| 262 | " ontology=restaurants_ontology,\n", |
| 263 | " model_config=KnowledgeGraphModelConfig.with_model(model),\n", |
| 264 | ")\n", |
| 265 | "\n", |
| 266 | "# Attractions KG\n", |
| 267 | "attractions_kg = KnowledgeGraph(\n", |
| 268 | " name=\"attractions\",\n", |
| 269 | " ontology=attractions_ontology,\n", |
| 270 | " model_config=KnowledgeGraphModelConfig.with_model(model),\n", |
| 271 | ")\n", |
| 272 | "\n", |
| 273 | "# Add nodes and edges to the KGs from the data\n", |
| 274 | "with open(\"data/cities.json\") as f:\n", |
| 275 | " cities = loads(f.read())\n", |
| 276 | "with open(\"data/restaurants.json\") as f:\n", |
| 277 | " restaurants = loads(f.read())\n", |
| 278 | "with open(\"data/attractions.json\") as f:\n", |
| 279 | " attractions = loads(f.read())\n", |
| 280 | "\n", |
| 281 | "for city in cities:\n", |
| 282 | " restaurants_kg.add_node(\n", |
| 283 | " \"City\",\n", |
| 284 | " {\n", |
| 285 | " \"name\": city[\"name\"],\n", |
| 286 | " \"weather\": city[\"weather\"],\n", |
| 287 | " \"population\": city[\"population\"],\n", |
| 288 | " },\n", |
| 289 | " )\n", |
| 290 | " restaurants_kg.add_node(\"Country\", {\"name\": city[\"country\"]})\n", |
| 291 | " restaurants_kg.add_edge(\n", |
| 292 | " \"IN_COUNTRY\",\n", |
| 293 | " \"City\",\n", |
| 294 | " \"Country\",\n", |
| 295 | " {\"name\": city[\"name\"]},\n", |
| 296 | " {\"name\": city[\"country\"]},\n", |
| 297 | " )\n", |
| 298 | "\n", |
| 299 | " attractions_kg.add_node(\n", |
| 300 | " \"City\",\n", |
| 301 | " {\n", |
| 302 | " \"name\": city[\"name\"],\n", |
| 303 | " \"weather\": city[\"weather\"],\n", |
| 304 | " \"population\": city[\"population\"],\n", |
| 305 | " },\n", |
| 306 | " )\n", |
| 307 | " attractions_kg.add_node(\"Country\", {\"name\": city[\"country\"]})\n", |
| 308 | " attractions_kg.add_edge(\n", |
| 309 | " \"IN_COUNTRY\",\n", |
| 310 | " \"City\",\n", |
| 311 | " \"Country\",\n", |
| 312 | " {\"name\": city[\"name\"]},\n", |
| 313 | " {\"name\": city[\"country\"]},\n", |
| 314 | " )\n", |
| 315 | "\n", |
| 316 | "for restaurant in restaurants:\n", |
| 317 | " restaurants_kg.add_node(\n", |
| 318 | " \"Restaurant\",\n", |
| 319 | " {\n", |
| 320 | " \"name\": restaurant[\"name\"],\n", |
| 321 | " \"description\": restaurant[\"description\"],\n", |
| 322 | " \"rating\": restaurant[\"rating\"],\n", |
| 323 | " \"food_type\": restaurant[\"food_type\"],\n", |
| 324 | " },\n", |
| 325 | " )\n", |
| 326 | " restaurants_kg.add_edge(\n", |
| 327 | " \"IN_CITY\",\n", |
| 328 | " \"Restaurant\",\n", |
| 329 | " \"City\",\n", |
| 330 | " {\"name\": restaurant[\"name\"]},\n", |
| 331 | " {\"name\": restaurant[\"city\"]},\n", |
| 332 | " )\n", |
| 333 | "\n", |
| 334 | "for attraction in attractions:\n", |
| 335 | " attractions_kg.add_node(\n", |
| 336 | " \"Attraction\",\n", |
| 337 | " {\n", |
| 338 | " \"name\": attraction[\"name\"],\n", |
| 339 | " \"description\": attraction[\"description\"],\n", |
| 340 | " \"type\": attraction[\"type\"],\n", |
| 341 | " },\n", |
| 342 | " )\n", |
| 343 | " attractions_kg.add_edge(\n", |
| 344 | " \"IN_CITY\",\n", |
| 345 | " \"Attraction\",\n", |
| 346 | " \"City\",\n", |
| 347 | " {\"name\": attraction[\"name\"]},\n", |
| 348 | " {\"name\": attraction[\"city\"]},\n", |
| 349 | " )" |
| 350 | ] |
| 351 | }, |
| 352 | { |
| 353 | "cell_type": "markdown", |
| 354 | "metadata": {}, |
| 355 | "source": [ |
| 356 | "### Agents\n", |
| 357 | "\n", |
| 358 | "Creating the Agents based on the KG." |
| 359 | ] |
| 360 | }, |
| 361 | { |
| 362 | "cell_type": "code", |
| 363 | "execution_count": 4, |
| 364 | "metadata": {}, |
| 365 | "outputs": [], |
| 366 | "source": [ |
| 367 | "# The following agent is specialized in finding restaurants.\n", |
| 368 | "restaurants_agent = KGAgent(\n", |
| 369 | " agent_id=\"restaurants_agent\",\n", |
| 370 | " kg=restaurants_kg,\n", |
| 371 | " introduction=\"I'm a restaurant agent, specialized in finding the best restaurants for you.\",\n", |
| 372 | ")\n", |
| 373 | "\n", |
| 374 | "# The following agent is specialized in finding tourist attractions.\n", |
| 375 | "attractions_agent = KGAgent(\n", |
| 376 | " agent_id=\"attractions_agent\",\n", |
| 377 | " kg=attractions_kg,\n", |
| 378 | " introduction=\"I'm an attractions agent, specialized in finding the best attractions for you.\",\n", |
| 379 | ")" |
| 380 | ] |
| 381 | }, |
| 382 | { |
| 383 | "cell_type": "markdown", |
| 384 | "metadata": {}, |
| 385 | "source": [ |
| 386 | "### Orchestrator - Multi-Agent System\n", |
| 387 | "\n", |
| 388 | "The orchestrator manages the usage of agents and handles questioning. Each agent is registered with the orchestrator, which determines how to engage the agents to provide the best possible answer when a question is asked." |
| 389 | ] |
| 390 | }, |
| 391 | { |
| 392 | "cell_type": "code", |
| 393 | "execution_count": 5, |
| 394 | "metadata": {}, |
| 395 | "outputs": [], |
| 396 | "source": [ |
| 397 | "\n", |
| 398 | "# Initialize the orchestrator and register agents\n", |
| 399 | "orchestrator = Orchestrator(\n", |
| 400 | " model,\n", |
| 401 | " backstory=\"You are a trip planner, and you want to provide the best possible itinerary for your clients.\",\n", |
| 402 | ")\n", |
| 403 | "orchestrator.register_agent(restaurants_agent)\n", |
| 404 | "orchestrator.register_agent(attractions_agent)\n", |
| 405 | "\n", |
| 406 | "# Query the orchestrator\n", |
| 407 | "runner = orchestrator.ask(\"Create a two-day itinerary for a trip to Rome. Please don't ask me any questions; just provide the best itinerary you can.\")" |
| 408 | ] |
| 409 | }, |
| 410 | { |
| 411 | "cell_type": "code", |
| 412 | "execution_count": 6, |
| 413 | "metadata": {}, |
| 414 | "outputs": [ |
| 415 | { |
| 416 | "name": "stdout", |
| 417 | "output_type": "stream", |
| 418 | "text": [ |
| 419 | "Thank you for your question about creating a two-day itinerary for a trip to Rome. Here is a detailed itinerary that covers key attractions and recommended dining options, thoughtfully curated to give you the best experience during your stay.\n", |
| 420 | "\n", |
| 421 | "### Day 1 Itinerary\n", |
| 422 | "\n", |
| 423 | "**Morning:**\n", |
| 424 | "\n", |
| 425 | "1. **Colosseum**: Begin your adventure with a visit to the Colosseum, an ancient amphitheater renowned for gladiatorial contests and public spectacles. It is one of Rome's most iconic landmarks, offering an incredible glimpse into the city’s historical grandeur.\n", |
| 426 | "\n", |
| 427 | "**Mid-Morning:**\n", |
| 428 | "\n", |
| 429 | "2. **Vatican Museums**: Next, head to the Vatican Museums, a complex of fascinating museums and galleries showcasing remarkable works of art collected by Popes over centuries. This is a must-see for art and history enthusiasts.\n", |
| 430 | "\n", |
| 431 | "**Afternoon:**\n", |
| 432 | "\n", |
| 433 | "3. **Trevi Fountain**: In the afternoon, visit the Trevi Fountain, a magnificent Baroque fountain famous for its impressive sculptures and the tradition of tossing coins. Relax and enjoy the beautiful surroundings.\n", |
| 434 | "\n", |
| 435 | "**Evening:**\n", |
| 436 | "\n", |
| 437 | "4. **Basilica di Santa Maria Maggiore**: End your day with an evening tour of the Basilica di Santa Maria Maggiore. This major basilica is rich in history and features stunning architecture that will leave you in awe.\n", |
| 438 | "\n", |
| 439 | "**Dining Recommendations:**\n", |
| 440 | "\n", |
| 441 | "- **Breakfast:** Start your day with breakfast at **Trattoria da Enzo**, known for its traditional Roman dishes and warm, welcoming atmosphere.\n", |
| 442 | "- **Lunch:** For lunch, indulge in an elegant dining experience at **Il Pagliaccio**, a Michelin-starred restaurant offering contemporary Italian cuisine.\n", |
| 443 | "- **Dinner:** Continue your culinary journey at **Il Pagliaccio** for an evening meal to savor the exquisite flavors once more.\n", |
| 444 | "\n", |
| 445 | "### Day 2 Itinerary\n", |
| 446 | "\n", |
| 447 | "**Morning:**\n", |
| 448 | "\n", |
| 449 | "1. **Vatican Museums**: Begin your second day by exploring more of the Vatican Museums. Take your time to appreciate the depth and breadth of the collections housed here.\n", |
| 450 | "\n", |
| 451 | "**Mid-Morning:**\n", |
| 452 | "\n", |
| 453 | "2. **Trevi Fountain**: Enjoy another visit to the Trevi Fountain to capture the morning light and experience the vibrant atmosphere of this iconic location once more.\n", |
| 454 | "\n", |
| 455 | "**Afternoon:**\n", |
| 456 | "\n", |
| 457 | "3. **Basilica di Santa Maria Maggiore**: Revisit the Basilica di Santa Maria Maggiore to explore more of its treasures and immerse yourself deeper into its historical and cultural significance.\n", |
| 458 | "\n", |
| 459 | "**Dining Recommendations:**\n", |
| 460 | "\n", |
| 461 | "- **Breakfast:** Start your second day with breakfast again at **Trattoria da Enzo**, for another delightful experience of traditional Roman cuisine.\n", |
| 462 | "- **Lunch:** For lunch, enjoy another delightful meal at **Il Pagliaccio**, where you can continue to relish the modern twists on Italian dishes.\n", |
| 463 | "- **Dinner:** For dinner, you may choose to return to either **Trattoria da Enzo** or **Il Pagliaccio**, depending on whether you prefer a traditional or contemporary Italian dining experience.\n", |
| 464 | "\n", |
| 465 | "This itinerary combines Rome's rich history, cultural landmarks, and exceptional cuisine to provide a comprehensive and enjoyable experience over your two days in the city. I hope you find this itinerary both exciting and satisfying as you explore the delights of Rome! Have a fantastic trip!\n" |
| 466 | ] |
| 467 | } |
| 468 | ], |
| 469 | "source": [ |
| 470 | "print(runner.output)" |
| 471 | ] |
| 472 | } |
| 473 | ], |
| 474 | "metadata": { |
| 475 | "kernelspec": { |
| 476 | "display_name": "Python 3 (ipykernel)", |
| 477 | "language": "python", |
| 478 | "name": "python3" |
| 479 | }, |
| 480 | "language_info": { |
| 481 | "codemirror_mode": { |
| 482 | "name": "ipython", |
| 483 | "version": 3 |
| 484 | }, |
| 485 | "file_extension": ".py", |
| 486 | "mimetype": "text/x-python", |
| 487 | "name": "python", |
| 488 | "nbconvert_exporter": "python", |
| 489 | "pygments_lexer": "ipython3", |
| 490 | "version": "3.11.9" |
| 491 | } |
| 492 | }, |
| 493 | "nbformat": 4, |
| 494 | "nbformat_minor": 4 |
| 495 | } |