| 1 | from typing import Iterator |
| 2 | from abc import ABC, abstractmethod |
| 3 | from graphrag_sdk.document import Document |
| 4 | from graphrag_sdk.document_loaders import ( |
| 5 | PDFLoader, |
| 6 | TextLoader, |
| 7 | URLLoader, |
| 8 | HTMLLoader, |
| 9 | CSVLoader, |
| 10 | JSONLLoader, |
| 11 | ) |
| 12 | |
| 13 | |
| 14 | def Source(path: str, instruction: str | None = None) -> "AbstractSource": |
| 15 | """ |
| 16 | Creates a source object |
| 17 | |
| 18 | Parameters: |
| 19 | path (str): path to source |
| 20 | instruction (str): source specific instruction for the LLM |
| 21 | |
| 22 | Returns: |
| 23 | AbstractSource: source |
| 24 | """ |
| 25 | |
| 26 | if not isinstance(path, str) or path == "": |
| 27 | raise Exception("Invalid argument, path should be a none empty string.") |
| 28 | |
| 29 | s = None |
| 30 | |
| 31 | if ".pdf" in path.lower(): |
| 32 | s = PDF(path) |
| 33 | elif ".html" in path.lower(): |
| 34 | s = HTML(path) |
| 35 | elif "http" in path.lower(): |
| 36 | s = URL(path) |
| 37 | elif ".csv" in path.lower(): |
| 38 | s = CSV(path) |
| 39 | elif ".jsonl" in path.lower(): |
| 40 | s = JSONL(path) |
| 41 | else: |
| 42 | s = TEXT(path) |
| 43 | |
| 44 | # Set source instructions |
| 45 | s.instruction = instruction |
| 46 | |
| 47 | return s |
| 48 | |
| 49 | |
| 50 | class AbstractSource(ABC): |
| 51 | """ |
| 52 | Abstract class representing a source file |
| 53 | """ |
| 54 | |
| 55 | def __init__(self, path: str): |
| 56 | """ |
| 57 | Initializes a new instance of the Source class. |
| 58 | |
| 59 | Args: |
| 60 | path (str): The path to the source file. |
| 61 | |
| 62 | Attributes: |
| 63 | path (str): The path to the source file. |
| 64 | loader: The loader object associated with the source file. |
| 65 | instruction (str): The instruction for the source file. |
| 66 | """ |
| 67 | self.path = path |
| 68 | self.loader = None |
| 69 | self.instruction = "" |
| 70 | |
| 71 | def load(self) -> Iterator[Document]: |
| 72 | """ |
| 73 | Loads documents from the source. |
| 74 | |
| 75 | Returns: |
| 76 | An iterator of Document objects. |
| 77 | """ |
| 78 | return self.loader.load() |
| 79 | |
| 80 | def __eq__(self, other) -> bool: |
| 81 | """ |
| 82 | Check if this source object is equal to another source object. |
| 83 | |
| 84 | Args: |
| 85 | other: The other source object to compare with. |
| 86 | |
| 87 | Returns: |
| 88 | bool: True if the source objects are equal, False otherwise. |
| 89 | """ |
| 90 | if not isinstance(other, AbstractSource): |
| 91 | return False |
| 92 | |
| 93 | return self.path == other.path |
| 94 | |
| 95 | def __hash__(self): |
| 96 | """ |
| 97 | Calculates the hash value of the Source object based on its path. |
| 98 | |
| 99 | Returns: |
| 100 | int: The hash value of the Source object. |
| 101 | """ |
| 102 | return hash(self.path) |
| 103 | |
| 104 | |
| 105 | class PDF(AbstractSource): |
| 106 | """ |
| 107 | PDF resource |
| 108 | """ |
| 109 | |
| 110 | def __init__(self, path): |
| 111 | super().__init__(path) |
| 112 | self.loader = PDFLoader(self.path) |
| 113 | |
| 114 | |
| 115 | class TEXT(AbstractSource): |
| 116 | """ |
| 117 | TEXT resource |
| 118 | """ |
| 119 | |
| 120 | def __init__(self, path): |
| 121 | super().__init__(path) |
| 122 | self.loader = TextLoader(self.path) |
| 123 | |
| 124 | |
| 125 | class URL(AbstractSource): |
| 126 | """ |
| 127 | URL resource |
| 128 | """ |
| 129 | |
| 130 | def __init__(self, path): |
| 131 | super().__init__(path) |
| 132 | self.loader = URLLoader(self.path) |
| 133 | |
| 134 | |
| 135 | class HTML(AbstractSource): |
| 136 | """ |
| 137 | HTML resource |
| 138 | """ |
| 139 | |
| 140 | def __init__(self, path): |
| 141 | super().__init__(path) |
| 142 | self.loader = HTMLLoader(self.path) |
| 143 | |
| 144 | |
| 145 | class CSV(AbstractSource): |
| 146 | """ |
| 147 | CSV resource |
| 148 | """ |
| 149 | |
| 150 | def __init__(self, path, rows_per_document: int = 50): |
| 151 | super().__init__(path) |
| 152 | self.loader = CSVLoader(self.path, rows_per_document) |
| 153 | |
| 154 | |
| 155 | class JSONL(AbstractSource): |
| 156 | """ |
| 157 | JSONL resource |
| 158 | """ |
| 159 | |
| 160 | def __init__(self, path, rows_per_document: int = 50): |
| 161 | super().__init__(path) |
| 162 | self.loader = JSONLLoader(self.path, rows_per_document) |