Elffuss BraidHub

kikocis/GraphRAG-SDK public

updated readme, added additional docs
Changesetefeadb5297a5fd8437f16bea4955d2d649d56a3ec04b0a6e11ca123023fec758
AuthorRoi Lipman <roilipman@gmail.com> · 2024-06-04 20:43 +03:00
CommitterRoi Lipman <roilipman@gmail.com> · 2024-06-04 20:43 +03:00
Parents59294a248544

Browse the files at this changeset

3 files changed

modified README.md +11 −7
@@ -7,7 +7,7 @@
77
88 Start FalkorDB locally:
99 ```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
1111 ```
1212
1313 Export your OpenAI API KEY:
@@ -50,13 +50,10 @@
5050 For example, the main entities in your data are: Movies, Actors, and Directors.
5151 These are interconnected via `ACT` and `DIRECTED` edges.
5252
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:
5754
5855 #### 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.
6057
6158 ```python
6259 s = Schema()
@@ -84,7 +81,14 @@
8481 ```python
8582 sources = [Source("./data/madoff.txt")]
8683 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)
8892
8993 # Output:
9094 # Schema: {"entities": [
modified rag_sdk/kg.py +20 −2
@@ -33,6 +33,9 @@
3333 model (str): OpenAI model to use.
3434 """
3535
36+ if not isinstance(name, str) or name == "":
37+ raise Exception("name should be a non empty string")
38+
3639 # connect to database
3740 self.db = FalkorDB(host=host, port=port, username=username, password=password)
3841 self.graph = self.db.select_graph(name)
@@ -44,6 +47,14 @@
4447 self.sources = set([])
4548 self.ontolegy_ratio = 0.1 # Sampaling ratio for ontolegy detection
4649
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+
4758 def list_sources(self) -> list[AbstractSource]:
4859 """
4960 List of sources associated with knowledge graph
@@ -73,9 +84,16 @@
7384 for src in sources:
7485 self.sources.add(src)
7586
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+
7694 def _createKGWithSchema(self):
7795 # 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())
7997 self.schema.save_graph(schema_graph)
8098
8199 # Create Knowledge Graph construction assistant
@@ -102,7 +120,7 @@
102120 exec(code, globals())
103121
104122 def _createKGWithoutSchema(self):
105- pass
123+ raise Exception("Can not create knowledge graph, schema missing")
106124
107125 def _create(self) -> None:
108126 """
modified rag_sdk/source.py +26 −15
@@ -3,23 +3,17 @@
33 from bs4 import BeautifulSoup
44 from abc import ABC, abstractmethod
55
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
199
20- return content
10+ Parameters:
11+ source (str): path to source
12+
13+ Returns:
14+ AbstractSource: source
15+ """
2116
22-def Source(source:str):
2317 if not isinstance(source, str) or source == "":
2418 raise Exception("Invalid argument, source should be a none empty string.")
2519
@@ -31,6 +25,10 @@
3125 return TEXT(source)
3226
3327 class AbstractSource(ABC):
28+ """
29+ Abstract class representing a source file
30+ """
31+
3432 def __init__(self, source:str):
3533 self.source = source
3634 self.content = None
@@ -49,14 +47,23 @@
4947 return hash(self.source)
5048
5149 class PDF(AbstractSource):
50+ """
51+ PDF resource
52+ """
53+
5254 def __init__(self, source):
5355 super().__init__(source)
5456
5557 def load(self):
58+ raise Exception("Not implemented")
5659 # extract text from PDF, populate self.content
5760 self.content = None
5861
5962 class TEXT(AbstractSource):
63+ """
64+ TEXT resource
65+ """
66+
6067 def __init__(self, source):
6168 super().__init__(source)
6269
@@ -69,6 +76,10 @@
6976 return self.content
7077
7178 class HTML(AbstractSource):
79+ """
80+ HTML resource
81+ """
82+
7283 def __init__(self, source):
7384 super().__init__(source)
7485