Amazon Neptune with SPARQL
Amazon Neptune is a high-performance graph analytics and serverless database for superior scalability and availability.
This example shows the QA chain that queries Resource Description Framework (RDF) data in an
Amazon Neptune
graph database using theSPARQL
query language and returns a human-readable response.SPARQL is a standard query language for
RDF
graphs.
This example uses a NeptuneRdfGraph
class that connects with the Neptune database and loads its schema.
The create_neptune_sparql_qa_chain
is used to connect the graph and LLM to ask natural language questions.
This notebook demonstrates an example using organizational data.
Requirements for running this notebook:
- Neptune 1.2.x cluster accessible from this notebook
- Kernel with Python 3.9 or higher
- For Bedrock access, ensure IAM role has this policy
{
"Action": [
"bedrock:ListFoundationModels",
"bedrock:InvokeModel"
],
"Resource": "*",
"Effect": "Allow"
}
- S3 bucket for staging sample data. The bucket should be in the same account/region as Neptune.
Setting upโ
Seed the W3C organizational dataโ
Seed the W3C organizational data, W3C org ontology plus some instances.
You will need an S3 bucket in the same region and account as the Neptune cluster. Set STAGE_BUCKET
as the name of that bucket.
STAGE_BUCKET = "<bucket-name>"
%%bash -s "$STAGE_BUCKET"
rm -rf data
mkdir -p data
cd data
echo getting org ontology and sample org instances
wget http://www.w3.org/ns/org.ttl
wget https://raw.githubusercontent.com/aws-samples/amazon-neptune-ontology-example-blog/main/data/example_org.ttl
echo Copying org ttl to S3
aws s3 cp org.ttl s3://$1/org.ttl
aws s3 cp example_org.ttl s3://$1/example_org.ttl
We will use the %load
magic command from the graph-notebook
package to insert the W3C data into the Neptune graph. Before running %load
, use %%graph_notebook_config
to set the graph connection parameters.
!pip install --upgrade --quiet graph-notebook
%load_ext graph_notebook.magics
%%graph_notebook_config
{
"host": "<neptune-endpoint>",
"neptune_service": "neptune-db",
"port": 8182,
"auth_mode": "<[DEFAULT|IAM]>",
"load_from_s3_arn": "<neptune-cluster-load-role-arn>",
"ssl": true,
"aws_region": "<region>"
}
Bulk-load the org ttl - both ontology and instances.
%load -s s3://{STAGE_BUCKET} -f turtle --store-to loadres --run
%load_status {loadres['payload']['loadId']} --errors --details