Elffuss BraidHub

kikocis/GraphRAG-SDK public

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