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