Elffuss BraidHub

kikocis/GraphRAG-SDK public

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