| 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.source import Source |
| 10 | from graphrag_sdk.models.ollama import OllamaGenerativeModel |
| 11 | from graphrag_sdk import KnowledgeGraph, KnowledgeGraphModelConfig |
| 12 | import os |
| 13 | import logging |
| 14 | from falkordb import FalkorDB |
| 15 | import pytest |
| 16 | |
| 17 | |
| 18 | logging.basicConfig(level=logging.DEBUG) |
| 19 | logger = logging.getLogger(__name__) |
| 20 | |
| 21 | class TestKGOllama(unittest.TestCase): |
| 22 | """ |
| 23 | Test Knowledge Graph |
| 24 | """ |
| 25 | |
| 26 | @classmethod |
| 27 | def setUpClass(cls): |
| 28 | |
| 29 | cls.ontology = Ontology([], []) |
| 30 | |
| 31 | cls.ontology.add_entity( |
| 32 | Entity( |
| 33 | label="Actor", |
| 34 | attributes=[ |
| 35 | Attribute( |
| 36 | name="name", |
| 37 | attr_type=AttributeType.STRING, |
| 38 | unique=True, |
| 39 | required=True, |
| 40 | ), |
| 41 | ], |
| 42 | ) |
| 43 | ) |
| 44 | cls.ontology.add_entity( |
| 45 | Entity( |
| 46 | label="Movie", |
| 47 | attributes=[ |
| 48 | Attribute( |
| 49 | name="title", |
| 50 | attr_type=AttributeType.STRING, |
| 51 | unique=True, |
| 52 | required=True, |
| 53 | ), |
| 54 | ], |
| 55 | ) |
| 56 | ) |
| 57 | cls.ontology.add_relation( |
| 58 | Relation( |
| 59 | label="ACTED_IN", |
| 60 | source="Actor", |
| 61 | target="Movie", |
| 62 | attributes=[ |
| 63 | Attribute( |
| 64 | name="role", |
| 65 | attr_type=AttributeType.STRING, |
| 66 | unique=False, |
| 67 | required=False, |
| 68 | ), |
| 69 | ], |
| 70 | ) |
| 71 | ) |
| 72 | |
| 73 | cls.graph_name = "IMDB_ollama" |
| 74 | |
| 75 | model = OllamaGenerativeModel(model_name="gemma2:2b") |
| 76 | cls.kg = KnowledgeGraph( |
| 77 | name=cls.graph_name, |
| 78 | ontology=cls.ontology, |
| 79 | model_config=KnowledgeGraphModelConfig.with_model(model), |
| 80 | ) |
| 81 | |
| 82 | @pytest.mark.skipif(condition=True, reason="Not ready for testing") |
| 83 | def test_kg_creation(self): |
| 84 | |
| 85 | file_path = "tests/data/madoff.txt" |
| 86 | |
| 87 | sources = [Source(file_path)] |
| 88 | |
| 89 | self.kg.process_sources(sources) |
| 90 | |
| 91 | answer = self.kg.ask("List a few actors") |
| 92 | |
| 93 | logger.info(f"Answer: {answer}") |
| 94 | |
| 95 | assert "Joseph Scotto" in answer[0], "Joseph Scotto not found in answer" |
| 96 | |
| 97 | @pytest.mark.skipif(condition=True, reason="Not ready for testing") |
| 98 | def test_kg_delete(self): |
| 99 | |
| 100 | self.kg.delete() |
| 101 | |
| 102 | db = FalkorDB() |
| 103 | graphs = db.list_graphs() |
| 104 | self.assertNotIn(self.graph_name, graphs) |