-
Notifications
You must be signed in to change notification settings - Fork 100
[FEATURE] Add RDF/SPARQL graph store with optional Neptune IAM authentication #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
KilianTrunk
wants to merge
4
commits into
awslabs:main
Choose a base branch
from
Ortecha:feat/native-sparql-rdflib-iam
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9a8bed6
feat(sparql): add generic SPARQL endpoint graph store
KilianTrunk a133fbb
test(sparql): cover endpoint storage behavior
KilianTrunk 752c6d5
docs(sparql): document RDF graph store
KilianTrunk 2b01764
feat(sparql): ship the lexical-graph ontology and a SPARQL setup note…
mommi84 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,54 @@ | ||
| # .dockerignore for building the self-contained Docker Hub image | ||
| # `ortecha/graphrag-toolkit-rdf` (see | ||
| # examples/lexical-graph-local-dev/docker/jupyter/Dockerfile.hub). | ||
| # | ||
| # The build context is the repo ROOT, but only three paths are COPYd into the | ||
| # image: lexical-graph/, lexical-graph-contrib/sparql/, and the example | ||
| # notebooks. Everything else here just keeps the context small and — for the | ||
| # notebooks — keeps secrets and heavy generated data OUT of a published image. | ||
| # | ||
| # NOTE: the dev stack (start-containers.sh --dev) builds from the ./jupyter | ||
| # context, so it is unaffected by this file. | ||
|
|
||
| # --- VCS, CI, tooling, OS cruft --- | ||
| .git | ||
| .github | ||
| .gitignore | ||
| .gitallowed | ||
| .pre-commit-config.yaml | ||
| **/.DS_Store | ||
| **/.ipynb_checkpoints | ||
| **/__pycache__ | ||
| **/*.pyc | ||
| **/.pytest_cache | ||
| **/htmlcov | ||
| **/.coverage | ||
| **/*.egg-info | ||
|
|
||
| # --- Local virtualenvs --- | ||
| .venv | ||
| .venv-* | ||
| **/.venv | ||
|
|
||
| # --- Repo areas this image does not use --- | ||
| graphrag-ui | ||
| inputs | ||
| byokg-rag | ||
| benchmark-tests | ||
| integration-tests | ||
| docs-site | ||
| images | ||
| examples/lexical-graph-hybrid-dev | ||
| examples/lexical-graph-local-dev/docker | ||
|
|
||
| # --- Tests (not needed at runtime) --- | ||
| lexical-graph/tests | ||
| lexical-graph-contrib/sparql/tests | ||
| lexical-graph-contrib/falkordb | ||
|
|
||
| # --- Secrets + heavy/generated notebook data (do NOT bake into a published image) --- | ||
| examples/lexical-graph-local-dev/notebooks/.env | ||
| examples/lexical-graph-local-dev/notebooks/nltk_data | ||
| examples/lexical-graph-local-dev/notebooks/artifacts | ||
| examples/lexical-graph-local-dev/notebooks/soup | ||
| examples/lexical-graph-local-dev/notebooks/extracted |
This file contains hidden or 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 hidden or 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
152 changes: 152 additions & 0 deletions
152
docs-site/src/content/docs/lexical-graph/graph-store-sparql.mdx
This file contains hidden or 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,152 @@ | ||
| --- | ||
| title: RDF / SPARQL stores | ||
| --- | ||
|
|
||
| The RDF / SPARQL contributor package stores the lexical graph in an existing | ||
| SPARQL 1.1 query/update endpoint. Generic standards-compatible endpoints are the | ||
| default; Amazon Neptune IAM authentication is an optional transport. | ||
|
|
||
| The adapter executes native SPARQL. Builders and retrievers provide a | ||
| backend-neutral operation identifier and structured parameters. Property-graph | ||
| stores execute their native queries, while the RDF store selects the | ||
| corresponding native SPARQL operation. | ||
|
|
||
| ### Install and register | ||
|
|
||
| Install the contributor package from a repository checkout: | ||
|
|
||
| ```bash | ||
| pip install ./lexical-graph-contrib/sparql | ||
| ``` | ||
|
|
||
| Register its factory before creating the graph store: | ||
|
|
||
| ```python | ||
| from graphrag_toolkit.lexical_graph.storage import GraphStoreFactory | ||
| from graphrag_toolkit_contrib.lexical_graph.storage.graph.sparql import ( | ||
| SPARQLGraphStoreFactory, | ||
| ) | ||
|
|
||
| GraphStoreFactory.register(SPARQLGraphStoreFactory) | ||
| ``` | ||
|
|
||
| Connect the graph store to an existing repository or dataset. | ||
|
|
||
| ### Generic endpoint | ||
|
|
||
| Provide the query URL and, if different, the update URL: | ||
|
|
||
| ```python | ||
| graph_store = GraphStoreFactory.for_graph_store( | ||
| 'sparql+https://rdf.example.com/query', | ||
| update_endpoint='https://rdf.example.com/update', | ||
| ) | ||
| ``` | ||
|
|
||
| Supported schemes are: | ||
|
|
||
| | Scheme | Endpoint transport | | ||
| |---|---| | ||
| | `sparql://host/path` | HTTP | | ||
| | `sparql+http://host/path` | HTTP | | ||
| | `sparql+s://host/path` | HTTPS | | ||
| | `sparql+https://host/path` | HTTPS | | ||
| | `sparql+neptune://host:8182` | HTTPS with Neptune IAM SigV4 | | ||
|
|
||
| If `update_endpoint` is omitted, the query URL is used for both operations. | ||
|
|
||
| HTTP Basic credentials can be passed in the URL, through `username` and | ||
| `password`, or through the `SPARQL_USER` and `SPARQL_PASSWORD` environment | ||
| variables. Custom headers support bearer tokens and endpoint-specific headers: | ||
|
|
||
| ```python | ||
| graph_store = GraphStoreFactory.for_graph_store( | ||
| 'sparql+https://rdf.example.com/query', | ||
| headers={'Authorization': 'Bearer token'}, | ||
| ) | ||
| ``` | ||
|
|
||
| ### Amazon Neptune IAM | ||
|
|
||
| Install the optional botocore dependency: | ||
|
|
||
| ```bash | ||
| pip install './lexical-graph-contrib/sparql[neptune]' | ||
| ``` | ||
|
|
||
| Then use the Neptune scheme and AWS region: | ||
|
|
||
| ```python | ||
| graph_store = GraphStoreFactory.for_graph_store( | ||
| 'sparql+neptune://my-cluster.cluster-abcdefghijkl.eu-central-1.neptune.amazonaws.com:8182', | ||
| region_name='eu-central-1', | ||
| ) | ||
| ``` | ||
|
|
||
| Each request is signed for the `neptune-db` service using credentials from | ||
| botocore's standard provider chain. Credentials are resolved on every request, | ||
| allowing botocore to refresh temporary role, web-identity, IAM Identity Center, | ||
| ECS, and EC2 credentials. IAM transport requires HTTPS. | ||
|
|
||
| The same transport is available as a plain RDFLib graph: | ||
|
|
||
| ```python | ||
| from graphrag_toolkit_contrib.lexical_graph.storage.graph.sparql.neptune_iam import ( | ||
| neptune_iam_graph, | ||
| ) | ||
|
|
||
| graph = neptune_iam_graph( | ||
| 'https://my-cluster.cluster-abcdefghijkl.eu-central-1.neptune.amazonaws.com:8182', | ||
| region_name='eu-central-1', | ||
| ) | ||
| try: | ||
| rows = graph.query('SELECT * WHERE { ?s ?p ?o } LIMIT 10') | ||
| finally: | ||
| graph.close() | ||
| ``` | ||
|
|
||
| ### Namespaces and tenants | ||
|
|
||
| The default schema namespace is | ||
| `https://awslabs.github.io/graphrag-toolkit/lexical#`; the default instance | ||
| namespace is `https://awslabs.github.io/graphrag-toolkit/lexical/`. | ||
|
|
||
| Customize them only when creating a new dataset: | ||
|
|
||
| ```python | ||
| graph_store = GraphStoreFactory.for_graph_store( | ||
| 'sparql+https://rdf.example.com/query', | ||
| update_endpoint='https://rdf.example.com/update', | ||
| lexical_prefix='gt', | ||
| lexical_schema_namespace='https://example.com/graph/schema#', | ||
| lexical_instance_namespace='https://example.com/graph/data/', | ||
| ) | ||
| ``` | ||
|
|
||
| Every tenant, including the default tenant, uses a deterministic named graph. | ||
| Writes target that graph explicitly, and reads select it with the standard | ||
| SPARQL Protocol `default-graph-uri` parameter. | ||
|
|
||
| ### RDF model | ||
|
|
||
| Sources, chunks, topics, statements, facts, and entities retain their existing | ||
| lexical-graph identities. A node becomes a deterministic IRI with an RDF class | ||
| and `lg:id`; node properties become literal-valued predicates. Simple edges | ||
| become RDF predicates. | ||
|
|
||
| Extracted facts record their subject, predicate, and object directly: | ||
|
|
||
| ```turtle | ||
| <fact/f1> a lg:Fact ; | ||
| lg:subject <entity/amazon> ; | ||
| lg:predicate <relation/produces> ; | ||
| lg:object <entity/ec2> ; | ||
| lg:supports <statement/s1> ; | ||
| lg:value "Amazon PRODUCES EC2" . | ||
|
|
||
| <relation/produces> a lg:Relation ; | ||
| lg:value "PRODUCES" . | ||
| ``` | ||
|
|
||
| This is the toolkit's fact model, with the fact resource carrying its subject, | ||
| predicate, object, and supporting statement. | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| name: local-dev | ||
| name: graphrag-toolkit-rdf-dev | ||
| services: | ||
| neo4j-local: | ||
| image: neo4j:5.25-community | ||
|
|
||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are all these bespoke scheme extensions really nessecary? It measn that developers will have to do some string concatination any time they want to pass in a SPARQL endpoint. I would prefer we found another way to specify specific headers for authentication.