forked from run-llama/llama-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into github-reader-test-…
…and-fix
- Loading branch information
Showing
27 changed files
with
441 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# RDF Loader | ||
|
||
This loader extracts triples from a local [RDF](https://en.wikipedia.org/wiki/Resource_Description_Framework) file using the `rdflib` Python package. The loader currently supports the RDF and RDF Schema namespaces. A single local file is passed in each time you call `load_data`. | ||
|
||
## Usage | ||
|
||
To use this loader, you need to pass in a `Path` to a local file. | ||
|
||
```python | ||
from pathlib import Path | ||
from gpt_index import download_loader | ||
|
||
RDFReader = download_loader("RDFReader") | ||
|
||
loader = RDFReader() | ||
documents = loader.load_data(file=Path('./knowledge-graph.nt')) | ||
``` | ||
|
||
This loader is designed to be used as a way to load data into [LlamaIndex](https://github.com/jerryjliu/gpt_index/tree/main/gpt_index) and/or subsequently used as a Tool in a [LangChain](https://github.com/hwchase17/langchain) Agent. See [here](https://github.com/emptycrown/llama-hub/tree/main) for examples. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Init file.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"""Read RDF files.""" | ||
|
||
from pathlib import Path | ||
from typing import Any, Dict, List, Optional | ||
|
||
from llama_index.readers.base import BaseReader | ||
from llama_index.readers.schema.base import Document | ||
|
||
|
||
class RDFReader(BaseReader): | ||
"""RDF reader.""" | ||
|
||
def __init__( | ||
self, | ||
*args: Any, | ||
**kwargs: Any, | ||
) -> None: | ||
"""Initialize loader.""" | ||
super().__init__(*args, **kwargs) | ||
|
||
from rdflib import Graph, URIRef | ||
from rdflib.namespace import RDF, RDFS | ||
|
||
self.Graph = Graph | ||
self.RDF = RDF | ||
self.RDFS = RDFS | ||
|
||
def fetch_labels(self, uri: Any, graph: Any, lang: str): | ||
"""Fetch all labels of a URI by language.""" | ||
|
||
return list( | ||
filter( | ||
lambda x: x.language in [lang, None], | ||
graph.objects(uri, self.RDFS.label), | ||
) | ||
) | ||
|
||
def fetch_label_in_graphs(self, uri: Any, lang: str = "en"): | ||
"""Fetch one label of a URI by language from the local or global graph.""" | ||
|
||
labels = self.fetch_labels(uri, self.g_local, lang) | ||
if len(labels) > 0: | ||
return labels[0].value | ||
|
||
labels = self.fetch_labels(uri, self.g_global, lang) | ||
if len(labels) > 0: | ||
return labels[0].value | ||
|
||
raise Exception(f"Label not found for: {uri}") | ||
|
||
def load_data( | ||
self, file: Path, extra_info: Optional[Dict] = None | ||
) -> List[Document]: | ||
"""Parse file.""" | ||
|
||
lang = extra_info["lang"] if extra_info is not None else "en" | ||
|
||
self.g_local = self.Graph() | ||
self.g_local.parse(file) | ||
|
||
self.g_global = self.Graph() | ||
self.g_global.parse(str(self.RDF)) | ||
self.g_global.parse(str(self.RDFS)) | ||
|
||
text_list = [] | ||
|
||
for s, p, o in self.g_local: | ||
if p == self.RDFS.label: | ||
continue | ||
triple = ( | ||
f"<{self.fetch_label_in_graphs(s, lang=lang)}> " | ||
f"<{self.fetch_label_in_graphs(p, lang=lang)}> " | ||
f"<{self.fetch_label_in_graphs(o, lang=lang)}>" | ||
) | ||
text_list.append(triple) | ||
|
||
text = "\n".join(text_list) | ||
|
||
return [Document(text, extra_info=extra_info)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rdflib~=6.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.