Elffuss BraidHub

kikocis/GraphRAG-SDK public

3034d86cfbbe · 3.7 KiBRawPermalink
1from graphrag_sdk.schema import Schema
2from graphrag_sdk import KnowledgeGraph, Source
3
4import logging
5logger = logging.getLogger("graphrag_sdk")
6
7# Configure the root logger
8handler = logging.StreamHandler()
9handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
10logger.addHandler(handler)
11logger.setLevel(logging.INFO)
12
13def main():
14 # Manually define schema
15 s = Schema()
16
17 # Entities:
18 # 1. File
19 # 2. Directory
20 # 3. Commit
21 # 4. Branch
22 # 5. Repository
23 # 6. Organization
24 # 7. User
25
26 file = s.add_entity('File')
27 file.add_attribute('Name', str, unique=True, mandatory=True)
28 file.add_attribute('Extension', str, unique=True, mandatory=True)
29
30 dir = s.add_entity('Directory')
31 dir.add_attribute('Name', str, unique=True, mandatory=True)
32
33 commit = s.add_entity('Commit')
34 commit.add_attribute('Message', str)
35 commit.add_attribute('Date', int, desc="Commit date as UNIX timestamp")
36 commit.add_attribute('Hash', str, unique=True, mandatory=True)
37
38 branch = s.add_entity('Branch')
39 branch.add_attribute('Name', str, unique=True, mandatory=True)
40
41 repo = s.add_entity('Repository')
42 repo.add_attribute('Name', str, unique=True, mandatory=True)
43 repo.add_attribute('Description', str)
44 repo.add_attribute('Issues', int, desc="Number of issues")
45
46 organization = s.add_entity('Organization')
47 organization.add_attribute('Name', str, unique=True, mandatory=True)
48
49 user = s.add_entity('User')
50 user.add_attribute('Name', str, unique=True, mandatory=True)
51 user.add_attribute('Location', str, desc="Where the user is from")
52
53 # Relations:
54 # 1. (Repository)-[CONTAINS]->(File)
55 # 2. (Repository)-[CONTAINS]->(Directory)
56 # 3. (Repository)-[CONTAINS]->(Commit)
57 # 4. (Repository)-[CONTAINS]->(bracnh)
58 # 5. (Directory)-[CONTAINS]->(File)
59 # 6. (Organization)-[OWNS]->(Repository)
60 # 7. (Commit)-[MODIFIES]->(File)
61 # 8. (User)-[STAR]->(Repository)
62 # 0. (User)-[PART_OF]->(Organization)
63
64 s.add_relation("CONTAINS", repo, file)
65 s.add_relation("CONTAINS", repo, dir)
66 s.add_relation("CONTAINS", repo, commit)
67 s.add_relation("CONTAINS", repo, branch)
68 s.add_relation("CONTAINS", dir, file)
69 s.add_relation("OWNS", organization, repo)
70 s.add_relation("MODIFIES", commit, file)
71 s.add_relation("STAR", user, repo)
72 s.add_relation("PART_OF", user, organization)
73
74 # Print schema
75 # print(s.to_JSON())
76
77 # Create Knowledge Graph
78 g = KnowledgeGraph("Github", schema=s, model="gpt-3.5-turbo-0125")
79
80 # Ingest
81 # Define sources from which knowledge will be extracted
82 sources = [
83 Source("https://github.com/FalkorDB/FalkorDB",
84 instruction="Extract organization, repository, users, files and directories. make sure to form connections"),
85 Source("https://github.com/FalkorDB/FalkorDB/stargazers",
86 instruction="Extract users who've stared this repo, make sure to create each user and connect it via an edge to the repo node."),
87 Source("https://github.com/FalkorDB/FalkorDB/branches", instruction="Extract branches and connect them to the repository")]
88
89 g.process_sources(sources)
90
91 # Query the repo
92 msgs = []
93 questions = ["List a few Stargazers",
94 "How many Markdown files are there in the FalkorDB repository?",
95 "Which organization owns the FalkorDB repository?",
96 "Any of the Stargazers are also associated with the repo's organization?",
97 "Does the FalkorDB repository has a 'main' or a 'master' branch?"]
98
99 for q in questions:
100 print(f"Question: {q}")
101 ans, msgs = g.ask(q, history=msgs)
102 print(f"Answer: {ans}")
103
104if __name__ == "__main__":
105 main()