-
Notifications
You must be signed in to change notification settings - Fork 98
[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
Open
KilianTrunk
wants to merge
3
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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,170 @@ | ||
| # RDF / SPARQL graph store | ||
|
|
||
| This contributor package stores the AWS GraphRAG Toolkit lexical graph in an | ||
| existing SPARQL 1.1 query/update endpoint. It is endpoint-neutral by default and | ||
| has an optional Amazon Neptune IAM transport. | ||
|
|
||
| Core graph builders and retrievers identify a backend-neutral `GraphOperation`. | ||
| Property-graph stores execute their native queries, while this package provides | ||
| the native SPARQL implementation of each operation. | ||
|
|
||
| The update renderer batches related statements into a single request. Calling | ||
| RDFLib `Graph.add()` and `Graph.remove()` for every remote triple would be much | ||
| less efficient and would make multi-statement counters non-atomic. | ||
|
|
||
| ## Install | ||
|
|
||
| From a repository checkout: | ||
|
|
||
| ```bash | ||
| pip install ./lexical-graph-contrib/sparql | ||
| ``` | ||
|
|
||
| Include the optional botocore dependency only when Neptune IAM authentication is | ||
| required: | ||
|
|
||
| ```bash | ||
| pip install './lexical-graph-contrib/sparql[neptune]' | ||
| ``` | ||
|
|
||
| ## Generic SPARQL endpoint | ||
|
|
||
| Register the contributor factory once before resolving a 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) | ||
|
|
||
| graph_store = GraphStoreFactory.for_graph_store( | ||
| 'sparql+https://rdf.example.com/query', | ||
| update_endpoint='https://rdf.example.com/update', | ||
| ) | ||
| ``` | ||
|
|
||
| Connect the graph store to an existing repository or dataset. | ||
|
|
||
| Supported connection schemes are: | ||
|
|
||
| | Scheme | HTTP endpoint | | ||
| |---|---| | ||
| | `sparql://host/path` | `http://host/path` | | ||
| | `sparql+http://host/path` | `http://host/path` | | ||
| | `sparql+s://host/path` | `https://host/path` | | ||
| | `sparql+https://host/path` | `https://host/path` | | ||
| | `sparql+neptune://host:8182` | `https://host:8182/sparql`, signed with IAM | | ||
|
|
||
| If `update_endpoint` is omitted, the query endpoint is used for both operations. | ||
| It can also be supplied as an encoded `update_endpoint` query parameter. | ||
|
|
||
| ### Generic authentication | ||
|
|
||
| HTTP Basic credentials can be supplied in the connection URL, as keyword | ||
| arguments, or through `SPARQL_USER` and `SPARQL_PASSWORD`: | ||
|
|
||
| ```python | ||
| graph_store = GraphStoreFactory.for_graph_store( | ||
| 'sparql+https://rdf.example.com/query', | ||
| username='service-user', | ||
| password='secret', | ||
| headers={'X-Request-Origin': 'graphrag-toolkit'}, | ||
| ) | ||
| ``` | ||
|
|
||
| Use `headers={'Authorization': 'Bearer ...'}` for bearer-token endpoints. Avoid | ||
| putting credentials directly in a URL when the URL may be logged by surrounding | ||
| application code. | ||
|
|
||
| ## Amazon Neptune with IAM | ||
|
|
||
| The Neptune scheme changes only the transport. The RDF model, operations, and | ||
| SPARQL implementation remain the same as for every other endpoint. | ||
|
|
||
| ```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', | ||
| ) | ||
| ``` | ||
|
|
||
| `NeptuneIAMAuth` uses botocore's standard credential provider chain and obtains | ||
| credentials for every request. This allows botocore to refresh temporary role, | ||
| web-identity, IAM Identity Center, ECS, and EC2 credentials. Requests are signed | ||
| for the `neptune-db` service with SigV4 and require HTTPS. | ||
|
|
||
| The transport can also be used 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 tenant isolation | ||
|
|
||
| The default schema namespace is | ||
| `https://awslabs.github.io/graphrag-toolkit/lexical#`; the default instance | ||
| namespace is `https://awslabs.github.io/graphrag-toolkit/lexical/`. | ||
|
|
||
| They can be changed when creating the store: | ||
|
|
||
| ```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/', | ||
| ) | ||
| ``` | ||
|
|
||
| Changing namespaces changes the IRIs written for new data. Use one configuration | ||
| consistently for a repository. | ||
|
|
||
| Every tenant, including the default tenant, uses a deterministic named graph | ||
| below the instance namespace. Writes use `GRAPH` and reads pass the tenant graph | ||
| as the SPARQL Protocol `default-graph-uri`, keeping tenant selection independent | ||
| of endpoint-specific union-default-graph behavior. | ||
|
|
||
| ## RDF model | ||
|
|
||
| Lexical nodes receive deterministic IRIs, an RDF class, and the original id as | ||
| `lg:id`. Simple edges become RDF predicates. Predicates whose property-graph | ||
| name is ambiguous are specialized by domain, for example | ||
| `lg:statementMentionedIn` versus `lg:topicMentionedIn`. | ||
|
|
||
| Extracted facts are represented as statement resources: | ||
|
|
||
| ```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. | ||
|
|
||
| ## Tests | ||
|
|
||
| ```bash | ||
| pytest lexical-graph-contrib/sparql/tests | ||
| pytest lexical-graph/tests | ||
| ``` |
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.