Elffuss BraidHub

kikocis/GraphRAG-SDK public

c1b0da01842e · 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.DEBUG)
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. (Organization)-[OWNS]->(Repository)
59 # 6. (User)-[STAR]->(Repository)
60 # 7. (User)-[PART_OF]->(Organization)
61
62 s.add_relation("CONTAINS", repo, file)
63 s.add_relation("CONTAINS", repo, dir)
64 s.add_relation("CONTAINS", repo, commit)
65 s.add_relation("CONTAINS", repo, branch)
66 s.add_relation("OWNS", organization, repo)
67 s.add_relation("STAR", user, repo)
68 s.add_relation("PART_OF", user, organization)
69
70 # Print schema
71 # print(s.to_JSON())
72
73 # Create Knowledge Graph
74 g = KnowledgeGraph("Github", schema=s, model="gpt-3.5-turbo-0125")
75
76 # Ingest
77 # Define sources from which knowledge will be extracted
78 sources = [
79 Source("https://github.com/FalkorDB/FalkorDB",
80 instruction="Extract organization, repository, users, files and directories. make sure to form connections"),
81 Source("https://github.com/FalkorDB/FalkorDB/stargazers",
82 instruction="Extract users who've stared this repo, make sure to create each user and connect it via an edge to the repo node."),
83 Source("https://github.com/FalkorDB/FalkorDB/branches", instruction="Extract branches and connect them to the repository"),
84 Source("https://github.com/FalkorDB/FalkorDB/commits", instruction="Extract commits and connect them to the repository")]
85
86 g.process_sources(sources)
87
88 # Query the repo
89 msgs = []
90 questions = ["List a few stargazers",
91 "List a few commits",
92 "How many Markdown files are there in the FalkorDB repository?",
93 "Which organization owns the FalkorDB repository?",
94 "Any of the Stargazers are also associated with the repo's organization?",
95 "Does the FalkorDB repository has a 'main' branch?"]
96
97 for q in questions:
98 print(f"Question: {q}")
99 ans, msgs = g.ask(q, history=msgs)
100 print(f"Answer: {ans}")
101
102if __name__ == "__main__":
103 main()