| 1 | from dotenv import load_dotenv |
| 2 | |
| 3 | load_dotenv() |
| 4 | from graphrag_sdk.ontology import Ontology |
| 5 | from graphrag_sdk.entity import Entity |
| 6 | from graphrag_sdk.relation import Relation |
| 7 | from graphrag_sdk.attribute import Attribute, AttributeType |
| 8 | import unittest |
| 9 | from graphrag_sdk.models.gemini import GeminiGenerativeModel |
| 10 | from graphrag_sdk import KnowledgeGraph, KnowledgeGraphModelConfig |
| 11 | from graphrag_sdk.orchestrator import Orchestrator |
| 12 | from graphrag_sdk.agents.kg_agent import KGAgent |
| 13 | import vertexai |
| 14 | import os |
| 15 | import logging |
| 16 | from json import loads |
| 17 | |
| 18 | logging.basicConfig(level=logging.DEBUG) |
| 19 | logger = logging.getLogger(__name__) |
| 20 | |
| 21 | vertexai.init(project=os.getenv("PROJECT_ID"), location=os.getenv("REGION")) |
| 22 | |
| 23 | |
| 24 | class TestMultiAgent(unittest.TestCase): |
| 25 | |
| 26 | @classmethod |
| 27 | def setUpClass(cls): |
| 28 | |
| 29 | cls.restaurants_ontology = Ontology() |
| 30 | cls.restaurants_ontology.add_entity( |
| 31 | Entity( |
| 32 | label="Country", |
| 33 | attributes=[ |
| 34 | Attribute( |
| 35 | name="name", |
| 36 | attr_type=AttributeType.STRING, |
| 37 | required=True, |
| 38 | unique=True, |
| 39 | ), |
| 40 | ], |
| 41 | ) |
| 42 | ) |
| 43 | cls.restaurants_ontology.add_entity( |
| 44 | Entity( |
| 45 | label="City", |
| 46 | attributes=[ |
| 47 | Attribute( |
| 48 | name="name", |
| 49 | attr_type=AttributeType.STRING, |
| 50 | required=True, |
| 51 | unique=True, |
| 52 | ), |
| 53 | Attribute( |
| 54 | name="weather", |
| 55 | attr_type=AttributeType.STRING, |
| 56 | required=False, |
| 57 | unique=False, |
| 58 | ), |
| 59 | Attribute( |
| 60 | name="population", |
| 61 | attr_type=AttributeType.NUMBER, |
| 62 | required=False, |
| 63 | unique=False, |
| 64 | ), |
| 65 | ], |
| 66 | ) |
| 67 | ) |
| 68 | cls.restaurants_ontology.add_entity( |
| 69 | Entity( |
| 70 | label="Restaurant", |
| 71 | attributes=[ |
| 72 | Attribute( |
| 73 | name="name", |
| 74 | attr_type=AttributeType.STRING, |
| 75 | required=True, |
| 76 | unique=True, |
| 77 | ), |
| 78 | Attribute( |
| 79 | name="description", |
| 80 | attr_type=AttributeType.STRING, |
| 81 | required=False, |
| 82 | unique=False, |
| 83 | ), |
| 84 | Attribute( |
| 85 | name="rating", |
| 86 | attr_type=AttributeType.NUMBER, |
| 87 | required=False, |
| 88 | unique=False, |
| 89 | ), |
| 90 | Attribute( |
| 91 | name="food_type", |
| 92 | attr_type=AttributeType.STRING, |
| 93 | required=False, |
| 94 | unique=False, |
| 95 | ), |
| 96 | ], |
| 97 | ) |
| 98 | ) |
| 99 | cls.restaurants_ontology.add_relation( |
| 100 | Relation( |
| 101 | label="IN_COUNTRY", |
| 102 | source="City", |
| 103 | target="Country", |
| 104 | ) |
| 105 | ) |
| 106 | cls.restaurants_ontology.add_relation( |
| 107 | Relation( |
| 108 | label="IN_CITY", |
| 109 | source="Restaurant", |
| 110 | target="City", |
| 111 | ) |
| 112 | ) |
| 113 | |
| 114 | cls.attractions_ontology = Ontology() |
| 115 | cls.attractions_ontology.add_entity( |
| 116 | Entity( |
| 117 | label="Country", |
| 118 | attributes=[ |
| 119 | Attribute( |
| 120 | name="name", |
| 121 | attr_type=AttributeType.STRING, |
| 122 | required=True, |
| 123 | unique=True, |
| 124 | ), |
| 125 | ], |
| 126 | ) |
| 127 | ) |
| 128 | cls.attractions_ontology.add_entity( |
| 129 | Entity( |
| 130 | label="City", |
| 131 | attributes=[ |
| 132 | Attribute( |
| 133 | name="name", |
| 134 | attr_type=AttributeType.STRING, |
| 135 | required=True, |
| 136 | unique=True, |
| 137 | ), |
| 138 | Attribute( |
| 139 | name="weather", |
| 140 | attr_type=AttributeType.STRING, |
| 141 | required=False, |
| 142 | unique=False, |
| 143 | ), |
| 144 | Attribute( |
| 145 | name="population", |
| 146 | attr_type=AttributeType.NUMBER, |
| 147 | required=False, |
| 148 | unique=False, |
| 149 | ), |
| 150 | ], |
| 151 | ) |
| 152 | ) |
| 153 | cls.attractions_ontology.add_entity( |
| 154 | Entity( |
| 155 | label="Attraction", |
| 156 | attributes=[ |
| 157 | Attribute( |
| 158 | name="name", |
| 159 | attr_type=AttributeType.STRING, |
| 160 | required=True, |
| 161 | unique=True, |
| 162 | ), |
| 163 | Attribute( |
| 164 | name="description", |
| 165 | attr_type=AttributeType.STRING, |
| 166 | required=False, |
| 167 | unique=False, |
| 168 | ), |
| 169 | Attribute( |
| 170 | name="type", |
| 171 | attr_type=AttributeType.STRING, |
| 172 | required=False, |
| 173 | unique=False, |
| 174 | ), |
| 175 | ], |
| 176 | ) |
| 177 | ) |
| 178 | cls.attractions_ontology.add_relation( |
| 179 | Relation( |
| 180 | label="IN_COUNTRY", |
| 181 | source="City", |
| 182 | target="Country", |
| 183 | ) |
| 184 | ) |
| 185 | cls.attractions_ontology.add_relation( |
| 186 | Relation( |
| 187 | label="IN_CITY", |
| 188 | source="Attraction", |
| 189 | target="City", |
| 190 | ) |
| 191 | ) |
| 192 | |
| 193 | cls.model = GeminiGenerativeModel("gemini-1.5-flash-001") |
| 194 | cls.restaurants_kg = KnowledgeGraph( |
| 195 | name="restaurants", |
| 196 | ontology=cls.restaurants_ontology, |
| 197 | model_config=KnowledgeGraphModelConfig.with_model(cls.model), |
| 198 | ) |
| 199 | cls.attractions_kg = KnowledgeGraph( |
| 200 | name="attractions", |
| 201 | ontology=cls.attractions_ontology, |
| 202 | model_config=KnowledgeGraphModelConfig.with_model(cls.model), |
| 203 | ) |
| 204 | |
| 205 | cls.import_data(cls, cls.restaurants_kg, cls.attractions_kg) |
| 206 | |
| 207 | cls.restaurants_agent = KGAgent( |
| 208 | agent_id="restaurants_agent", |
| 209 | kg=cls.restaurants_kg, |
| 210 | introduction=""" |
| 211 | I'm a restaurant agent, specialized in finding the best restaurants for you. |
| 212 | """, |
| 213 | ) |
| 214 | |
| 215 | cls.attractions_agent = KGAgent( |
| 216 | agent_id="attractions_agent", |
| 217 | kg=cls.attractions_kg, |
| 218 | introduction=""" |
| 219 | I'm an attractions agent, specialized in finding the best attractions for you. |
| 220 | """, |
| 221 | ) |
| 222 | |
| 223 | cls.orchestrator = Orchestrator( |
| 224 | cls.model, |
| 225 | backstory="You are a trip planner, and you want to provide the best possible itinerary for your clients.", |
| 226 | ) |
| 227 | |
| 228 | cls.orchestrator.register_agent(cls.restaurants_agent) |
| 229 | cls.orchestrator.register_agent(cls.attractions_agent) |
| 230 | |
| 231 | def import_data( |
| 232 | self, |
| 233 | restaurants_kg: KnowledgeGraph, |
| 234 | attractions_kg: KnowledgeGraph, |
| 235 | ): |
| 236 | with open("tests/data/cities.json") as f: |
| 237 | cities = loads(f.read()) |
| 238 | with open("tests/data/restaurants.json") as f: |
| 239 | restaurants = loads(f.read()) |
| 240 | with open("tests/data/attractions.json") as f: |
| 241 | attractions = loads(f.read()) |
| 242 | |
| 243 | for city in cities: |
| 244 | restaurants_kg.add_node( |
| 245 | "City", |
| 246 | { |
| 247 | "name": city["name"], |
| 248 | "weather": city["weather"], |
| 249 | "population": city["population"], |
| 250 | }, |
| 251 | ) |
| 252 | restaurants_kg.add_node("Country", {"name": city["country"]}) |
| 253 | restaurants_kg.add_edge( |
| 254 | "IN_COUNTRY", |
| 255 | "City", |
| 256 | "Country", |
| 257 | {"name": city["name"]}, |
| 258 | {"name": city["country"]}, |
| 259 | ) |
| 260 | |
| 261 | attractions_kg.add_node( |
| 262 | "City", |
| 263 | { |
| 264 | "name": city["name"], |
| 265 | "weather": city["weather"], |
| 266 | "population": city["population"], |
| 267 | }, |
| 268 | ) |
| 269 | attractions_kg.add_node("Country", {"name": city["country"]}) |
| 270 | attractions_kg.add_edge( |
| 271 | "IN_COUNTRY", |
| 272 | "City", |
| 273 | "Country", |
| 274 | {"name": city["name"]}, |
| 275 | {"name": city["country"]}, |
| 276 | ) |
| 277 | |
| 278 | for restaurant in restaurants: |
| 279 | restaurants_kg.add_node( |
| 280 | "Restaurant", |
| 281 | { |
| 282 | "name": restaurant["name"], |
| 283 | "description": restaurant["description"], |
| 284 | "rating": restaurant["rating"], |
| 285 | "food_type": restaurant["food_type"], |
| 286 | }, |
| 287 | ) |
| 288 | restaurants_kg.add_edge( |
| 289 | "IN_CITY", |
| 290 | "Restaurant", |
| 291 | "City", |
| 292 | {"name": restaurant["name"]}, |
| 293 | {"name": restaurant["city"]}, |
| 294 | ) |
| 295 | |
| 296 | for attraction in attractions: |
| 297 | attractions_kg.add_node( |
| 298 | "Attraction", |
| 299 | { |
| 300 | "name": attraction["name"], |
| 301 | "description": attraction["description"], |
| 302 | "type": attraction["type"], |
| 303 | }, |
| 304 | ) |
| 305 | attractions_kg.add_edge( |
| 306 | "IN_CITY", |
| 307 | "Attraction", |
| 308 | "City", |
| 309 | {"name": attraction["name"]}, |
| 310 | {"name": attraction["city"]}, |
| 311 | ) |
| 312 | |
| 313 | def test_multi_agent(self): |
| 314 | |
| 315 | response = self.orchestrator.ask( |
| 316 | "Write me a two-day itinerary for a trip to Rome. Do not ask any questions to me, just provide your best itinerary." |
| 317 | ) |
| 318 | |
| 319 | print(response) |
| 320 | assert response is not None |
| 321 | |
| 322 | |
| 323 | assert ( |
| 324 | "itinerary" in response.output.lower() or "day" in response.output.lower() |
| 325 | ), f"Response should contain the 'itinerary' or 'day' string: {response.output}" |