kikocis/GraphRAG-SDK public
Changesets · Branches · Tags · Compare
| Changeset | efeadb5297a5fd8437f16bea4955d2d649d56a3ec04b0a6e11ca123023fec758 |
|---|---|
| Author | Roi Lipman <roilipman@gmail.com> · 2024-06-04 20:43 +03:00 |
| Committer | Roi Lipman <roilipman@gmail.com> · 2024-06-04 20:43 +03:00 |
| Parents | 59294a248544 |
3 files changed
modified README.md +11 −7
| @@ -7,7 +7,7 @@ | ||
| 7 | 7 | |
| 8 | 8 | Start FalkorDB locally: |
| 9 | 9 | ```sh |
| 10 | -docker run -p 6379:6379 -p 3000:3000 -it --rm falkordb/falkordb:latest | |
| 10 | +docker run -p 6379:6379 -it --rm -v ./data:/data falkordb/falkordb:edge --dir /data | |
| 11 | 11 | ``` |
| 12 | 12 | |
| 13 | 13 | Export your OpenAI API KEY: |
| @@ -50,13 +50,10 @@ | ||
| 50 | 50 | For example, the main entities in your data are: Movies, Actors, and Directors. |
| 51 | 51 | These are interconnected via `ACT` and `DIRECTED` edges. |
| 52 | 52 | |
| 53 | -Although not mandatory, the specification of a knowledge graph schema helps with | |
| 54 | -the extraction of knowledge from unstructured data. | |
| 55 | - | |
| 56 | -We provide two approaches to schema creation: | |
| 53 | +Two approaches to schema creation are available: | |
| 57 | 54 | |
| 58 | 55 | #### Manual schema creation |
| 59 | -Use this method when you know exactly what your data looks like. | |
| 56 | +Use this method when you know exactly how your data should be structured. | |
| 60 | 57 | |
| 61 | 58 | ```python |
| 62 | 59 | s = Schema() |
| @@ -84,7 +81,14 @@ | ||
| 84 | 81 | ```python |
| 85 | 82 | sources = [Source("./data/madoff.txt")] |
| 86 | 83 | s = Schema.auto_detect(sources) |
| 87 | -print(f"Schema: {s.to_JSON()}") | |
| 84 | +json_schema = s.to_JSON() | |
| 85 | +print(f"Schema: {json_schema}") | |
| 86 | + | |
| 87 | +# Adjust json to your liking and reload the schema from JSON | |
| 88 | +json_schema['entities'].append({"name": "Movie", "attributes": [ {"name": "Title", "type": "str", "desc": "Movie's title", "unique": true, "mandatory": true}]) | |
| 89 | + | |
| 90 | +# Recreate refined schema | |
| 91 | +s = Schema.from_JSON(json_schema) | |
| 88 | 92 | |
| 89 | 93 | # Output: |
| 90 | 94 | # Schema: {"entities": [ |
modified rag_sdk/kg.py +20 −2
| @@ -33,6 +33,9 @@ | ||
| 33 | 33 | model (str): OpenAI model to use. |
| 34 | 34 | """ |
| 35 | 35 | |
| 36 | + if not isinstance(name, str) or name == "": | |
| 37 | + raise Exception("name should be a non empty string") | |
| 38 | + | |
| 36 | 39 | # connect to database |
| 37 | 40 | self.db = FalkorDB(host=host, port=port, username=username, password=password) |
| 38 | 41 | self.graph = self.db.select_graph(name) |
| @@ -44,6 +47,14 @@ | ||
| 44 | 47 | self.sources = set([]) |
| 45 | 48 | self.ontolegy_ratio = 0.1 # Sampaling ratio for ontolegy detection |
| 46 | 49 | |
| 50 | + # in case schema is None | |
| 51 | + # try to load schema from FalkorDB | |
| 52 | + if schema is None: | |
| 53 | + schema_name = self._schema_name() | |
| 54 | + if schema_name in self.db.list_graphs(): | |
| 55 | + schema_graph = self.db.select_graph() | |
| 56 | + self.schema = self.schema.from_graph(schema_graph) | |
| 57 | + | |
| 47 | 58 | def list_sources(self) -> list[AbstractSource]: |
| 48 | 59 | """ |
| 49 | 60 | List of sources associated with knowledge graph |
| @@ -73,9 +84,16 @@ | ||
| 73 | 84 | for src in sources: |
| 74 | 85 | self.sources.add(src) |
| 75 | 86 | |
| 87 | + def _schema_name(self) -> str: | |
| 88 | + """ | |
| 89 | + Generate a name for the schema based on the Knowledge Graph name | |
| 90 | + """ | |
| 91 | + | |
| 92 | + return f"{self.name}_schema" | |
| 93 | + | |
| 76 | 94 | def _createKGWithSchema(self): |
| 77 | 95 | # Save schema as an ontology graph |
| 78 | - schema_graph = self.db.select_graph(f"{self.name}_schema") | |
| 96 | + schema_graph = self.db.select_graph(self._schema_name()) | |
| 79 | 97 | self.schema.save_graph(schema_graph) |
| 80 | 98 | |
| 81 | 99 | # Create Knowledge Graph construction assistant |
| @@ -102,7 +120,7 @@ | ||
| 102 | 120 | exec(code, globals()) |
| 103 | 121 | |
| 104 | 122 | def _createKGWithoutSchema(self): |
| 105 | - pass | |
| 123 | + raise Exception("Can not create knowledge graph, schema missing") | |
| 106 | 124 | |
| 107 | 125 | def _create(self) -> None: |
| 108 | 126 | """ |
modified rag_sdk/source.py +26 −15
| @@ -3,23 +3,17 @@ | ||
| 3 | 3 | from bs4 import BeautifulSoup |
| 4 | 4 | from abc import ABC, abstractmethod |
| 5 | 5 | |
| 6 | -def download_resource(url): | |
| 7 | - print(f"Downloading: {url}") | |
| 8 | - | |
| 9 | - # Send an HTTP GET request to the URL | |
| 10 | - response = requests.get(url) | |
| 11 | - | |
| 12 | - # Check if the request was successful (status code 200) | |
| 13 | - content = None | |
| 14 | - if response.status_code == 200: | |
| 15 | - # Retrieve content | |
| 16 | - content = response.text | |
| 17 | - else: | |
| 18 | - print(f"Failed to retrieve content. Status code: {response.status_code}") | |
| 6 | +def Source(source:str) ->'AbstractSource': | |
| 7 | + """ | |
| 8 | + Creates a source object | |
| 19 | 9 | |
| 20 | - return content | |
| 10 | + Parameters: | |
| 11 | + source (str): path to source | |
| 12 | + | |
| 13 | + Returns: | |
| 14 | + AbstractSource: source | |
| 15 | + """ | |
| 21 | 16 | |
| 22 | -def Source(source:str): | |
| 23 | 17 | if not isinstance(source, str) or source == "": |
| 24 | 18 | raise Exception("Invalid argument, source should be a none empty string.") |
| 25 | 19 | |
| @@ -31,6 +25,10 @@ | ||
| 31 | 25 | return TEXT(source) |
| 32 | 26 | |
| 33 | 27 | class AbstractSource(ABC): |
| 28 | + """ | |
| 29 | + Abstract class representing a source file | |
| 30 | + """ | |
| 31 | + | |
| 34 | 32 | def __init__(self, source:str): |
| 35 | 33 | self.source = source |
| 36 | 34 | self.content = None |
| @@ -49,14 +47,23 @@ | ||
| 49 | 47 | return hash(self.source) |
| 50 | 48 | |
| 51 | 49 | class PDF(AbstractSource): |
| 50 | + """ | |
| 51 | + PDF resource | |
| 52 | + """ | |
| 53 | + | |
| 52 | 54 | def __init__(self, source): |
| 53 | 55 | super().__init__(source) |
| 54 | 56 | |
| 55 | 57 | def load(self): |
| 58 | + raise Exception("Not implemented") | |
| 56 | 59 | # extract text from PDF, populate self.content |
| 57 | 60 | self.content = None |
| 58 | 61 | |
| 59 | 62 | class TEXT(AbstractSource): |
| 63 | + """ | |
| 64 | + TEXT resource | |
| 65 | + """ | |
| 66 | + | |
| 60 | 67 | def __init__(self, source): |
| 61 | 68 | super().__init__(source) |
| 62 | 69 | |
| @@ -69,6 +76,10 @@ | ||
| 69 | 76 | return self.content |
| 70 | 77 | |
| 71 | 78 | class HTML(AbstractSource): |
| 79 | + """ | |
| 80 | + HTML resource | |
| 81 | + """ | |
| 82 | + | |
| 72 | 83 | def __init__(self, source): |
| 73 | 84 | super().__init__(source) |
| 74 | 85 |