| 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 | import logging |
| 12 | |
| 13 | logging.basicConfig(level=logging.DEBUG) |
| 14 | logger = logging.getLogger(__name__) |
| 15 | |
| 16 | |
| 17 | class TestKG(unittest.TestCase): |
| 18 | """ |
| 19 | Test Knowledge Graph |
| 20 | """ |
| 21 | |
| 22 | @classmethod |
| 23 | def setUpClass(cls): |
| 24 | |
| 25 | cls.ontology = Ontology([], []) |
| 26 | |
| 27 | cls.ontology.add_entity( |
| 28 | Entity( |
| 29 | label="Actor", |
| 30 | attributes=[ |
| 31 | Attribute( |
| 32 | name="name", |
| 33 | attr_type=AttributeType.STRING, |
| 34 | unique=True, |
| 35 | required=True, |
| 36 | ), |
| 37 | ], |
| 38 | ) |
| 39 | ) |
| 40 | cls.ontology.add_entity( |
| 41 | Entity( |
| 42 | label="Movie", |
| 43 | attributes=[ |
| 44 | Attribute( |
| 45 | name="title", |
| 46 | attr_type=AttributeType.STRING, |
| 47 | unique=True, |
| 48 | required=True, |
| 49 | ), |
| 50 | ], |
| 51 | ) |
| 52 | ) |
| 53 | cls.ontology.add_relation( |
| 54 | Relation( |
| 55 | label="ACTED_IN", |
| 56 | source="Actor", |
| 57 | target="Movie", |
| 58 | attributes=[ |
| 59 | Attribute( |
| 60 | name="role", |
| 61 | attr_type=AttributeType.STRING, |
| 62 | unique=False, |
| 63 | required=False, |
| 64 | ), |
| 65 | ], |
| 66 | ) |
| 67 | ) |
| 68 | |
| 69 | cls.graph_name = "test_kg" |
| 70 | |
| 71 | model = GeminiGenerativeModel(model_name="gemini-1.5-flash-001") |
| 72 | cls.kg = KnowledgeGraph( |
| 73 | name=cls.graph_name, |
| 74 | ontology=cls.ontology, |
| 75 | model_config=KnowledgeGraphModelConfig.with_model(model), |
| 76 | ) |
| 77 | |
| 78 | # Delete graph after tests |
| 79 | @classmethod |
| 80 | def tearDownClass(cls): |
| 81 | cls.kg.delete() |
| 82 | |
| 83 | def test_add_node_valid(self): |
| 84 | self.kg.add_node("Actor", {"name": "Tom Hanks"}) |
| 85 | |
| 86 | def test_add_node_invalid(self): |
| 87 | with self.assertRaises(Exception): |
| 88 | self.kg.add_node("Actor", {"title": "Tom Hanks"}) |
| 89 | |
| 90 | def test_add_relation_valid(self): |
| 91 | self.kg.add_edge( |
| 92 | "ACTED_IN", |
| 93 | "Actor", |
| 94 | "Movie", |
| 95 | {"name": "Tom Hanks"}, |
| 96 | {"title": "Forrest Gump"}, |
| 97 | {"role": "Forrest Gump"}, |
| 98 | ) |
| 99 | |
| 100 | def test_add_relation_invalid(self): |
| 101 | with self.assertRaises(Exception): |
| 102 | self.kg.add_edge( |
| 103 | "ACTED_IN", |
| 104 | "Actor", |
| 105 | "Movie", |
| 106 | {"title": "Tom Hanks"}, |
| 107 | {"title": "Forrest Gump"}, |
| 108 | {"role": "Forrest Gump"}, |
| 109 | ) |
| 110 | |
| 111 | with self.assertRaises(Exception): |
| 112 | self.kg.add_edge( |
| 113 | "ACTED_IN", |
| 114 | "Actor", |
| 115 | "Movie", |
| 116 | {"name": "Tom Hanks"}, |
| 117 | {"name": "Forrest Gump"}, |
| 118 | {}, |
| 119 | ) |