
Build a GraphRAG proof of concept
A GraphRAG proof of concept built using LlamaIndex, Amazon Bedrock, and Amazon Neptune
1
2
3
4
5
6
# Install dependencies
%pip install boto3
%pip install llama-index-llms-bedrock
%pip install llama-index-embeddings-bedrock
%pip install llama-index-graph-stores-neptune
%pip install llama-index
1
2
3
4
5
6
7
8
9
10
11
12
# Import features
from llama_index.llms.bedrock import Bedrock
from llama_index.embeddings.bedrock import BedrockEmbedding
from llama_index.core import (
StorageContext,
SimpleDirectoryReader,
KnowledgeGraphIndex,
Settings
)
from llama_index.core.query_engine import KnowledgeGraphQueryEngine
from llama_index.graph_stores.neptune import NeptuneDatabaseGraphStore
from IPython.display import Markdown, display
1
2
3
4
5
# For the variables that follow, update the region, give Neptune notebook instance IAM role permission to invoke Bedrock using the two models specified below, and update the neptune_endpoint based on your Neptune database endpoint; also, check on AWS Bedrock console and make sure you have access to the Bedrock models
region_name = "us-west-2"
llmodel = "anthropic.claude-3-sonnet-20240229-v1:0"
embed_model = "amazon.titan-embed-text-v1"
neptune_endpoint = "db-neptune-1.cluster-cu4cwa2hwkdb.us-east-1.neptune.amazonaws.com"
1
2
3
4
5
6
7
8
9
# Define LLM
llm = Bedrock(
model=llmodel,
region_name=region_name,
)
embed_model = BedrockEmbedding(model=embed_model)
Settings.llm = llm
Settings.chunk_size = 512
Settings.embed_model = embed_model
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Load test data about Newton and Edison using sample text about Newton and Edison
import os
import shutil
data_path = './data'
if os.path.isdir(data_path):
shutil.rmtree(data_path)
os.makedirs(data_path)
about_newton = ['Isaac Newton, known for his laws of motion and universal gravitation, laid the groundwork for classical mechanics.',
'Newton’s work in the 17th century provided the foundation for much of modern physics.']
with open(data_path + '/newton.txt', mode='w') as f:
f.writelines(about_newton)
about_edison = ['Albert Einstein developed the theory of relativity, which revolutionized theoretical physics and astronomy.',
'The theory of relativity was formulated in the early 20th century and has had a profound impact on our understanding of space and time.',
'In 1915, Einstein presented the general theory of relativity, expanding on his earlier work on special relativity.']
with open(data_path + '/edison.txt', mode='w') as f:
f.writelines(about_edison)
documents = SimpleDirectoryReader(data_path).load_data()
1
2
3
4
5
6
# Create index in Neptune database using the test data - automated knowledge graph construction from unstructured text
graph_store = NeptuneDatabaseGraphStore(host=neptune_endpoint, port=8182)
storage_context = StorageContext.from_defaults(graph_store=graph_store)
index = KnowledgeGraphIndex.from_documents(
documents,
storage_context=storage_context)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "bedrock:InvokeModel",
"Resource": [
"arn:aws:bedrock:us-west-2::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0",
"arn:aws:bedrock:us-west-2::foundation-model/amazon.titan-embed-text-v1"
]
}
]
}
1
2
3
4
5
# Query
response = index.as_query_engine().query(
"How did the scientific contributions of the 17th century influence early 20th-century physics?",
)
display(Markdown(f"<b>{response}</b>"))
The scientific contributions of the 17th century, particularly the work of Isaac Newton, laid the foundation for classical mechanics and provided the groundwork for much of modern physics. Newton's laws of motion and his theory of universal gravitation established a framework for understanding the behavior of objects and the forces acting upon them. This classical understanding of mechanics and gravitation remained influential and formed the basis for early 20th-century physics, even as new discoveries and theories emerged to challenge and expand upon Newton's work. The advancements made during the 17th century set the stage for the revolutionary developments in physics that occurred in the early 20th century, such as the theories of relativity and quantum mechanics.
- From the AWS Neptune console, go to the Notebooks menu to stop and delete the Neptune Python notebook.
- From the AWS Neptune console, go to the Clusters menu to delete the Neptune database cluster.
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.