This is a sample notebook and web application which shows how Amazon Bedrock and Titan can be used with Neo4j. We will explore how to leverage generative AI to build and consume a knowledge graph in Neo4j.
The dataset we're using is from the SEC's EDGAR system. It was downloaded using these scripts.
The dataflow in this demo consists of two parts:
To get started setting up the demo, clone this repo into a SageMaker Studio environment and then run through the notebooks numbered 1 and 2.
This demo requires a Neo4j instance. You can deploy that using the AWS Marketplace listing here.
The AWS identity you assume from your notebook environment (which is the Studio/notebook Execution Role from SageMaker, or could be a role or IAM User for self-managed notebooks), must have sufficient AWS IAM permissions to call the Amazon Bedrock service.
To grant Bedrock access to your identity, you can:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "BedrockFullAccess",
"Effect": "Allow",
"Action": ["bedrock:*"],
"Resource": "*"
}
]
}
️ Note: With Amazon SageMaker, your notebook execution role will typically be separate from the user or role that you log in to the AWS Console with. If you'd like to explore the AWS Console for Amazon Bedrock, you'll need to grant permissions to your Console user/role too.
For more information on the fine-grained action and resource permissions in Bedrock, check out the Bedrock Developer Guide.
The UI application is based on Streamlit. In this example we're going to show how to run it on an AWS EC2 Instance (EC2) VM. First, deploy a VM. You can use this guide to spin off an Amazon Linux VM
We are going to use AWS CLI. You need to follow these steps to configure the credentials to use the CLI option
Login using AWS credentials via the aws
cli.
aws configure
Next, login to the new VM instance using AWS CLI:
export INSTANCE_ID=
aws ec2-instance-connect ssh --instance-id $INSTANCE_ID
We're going to be running the application on port 80. That requires root access, so first:
sudo su
Then you'll need to install git and clone this repo:
yum install -y git
mkdir -p /app
cd /app
git clone https://github.com/neo4j-partners/neo4j-generative-ai-aws.git
cd neo4j-generative-ai-aws
Let's install python & pip first:
yum install -y python
yum install -y pip
Now, let's create a Virtual Environment to isolate our Python environment and activate it
yum install -y virtualenv
python3 -m venv /app/venv/genai
source /app/venv/genai/bin/activate
To install Streamlit and other dependencies:
cd ui
pip install -r requirements.txt
Check if streamlit
command is accessible from PATH by running this command:
streamlit --version
If not, you need to add the streamlit
binary to PATH variable like below:
export PATH="/app/venv/genai/bin:$PATH"
Next up you'll need to create a secrets file for the app to use. Open the file and edit it:
cd streamlit
cd .streamlit
cp secrets.toml.example secrets.toml
vi secrets.toml
You will now need to edit that file to reflect your credentials. The file has the following variables:
SERVICE_NAME = "" #e.g. bedrock-runtime
REGION_NAME = "" #e.g. us-west-2
CYPHER_MODEL = "" #e.g. anthropic.claude-v2
ACCESS_KEY = "AWS ACCESS KEY" #provide the access key with bedrock access
SECRET_KEY = "AWS SECRET KEY" #provide the secret key with bedrock access
NEO4J_HOST = "" #NEO4J_AURA_DS_URL
NEO4J_PORT = "7687"
NEO4J_USER = "neo4j"
NEO4J_PASSWORD = "" #Neo4j password
NEO4J_DB = "neo4j"
Now we can run the app with the commands:
cd ..
streamlit run Home.py --server.port=80
Optionally, you can run the app in another screen session to ensure the app continues to run even if you disconnect from the ec2 instance:
screen -S run_app
cd ..
streamlit run Home.py --server.port=80
You can use Ctrl+a
d
to exit the screen with the app still running and enter back into the screen with screen -r
. To kill the screen session, use the command screen -XS run_app quit
.
On the VM to run on port 80:
Once deployed, you will be able to see the Dashboard and Chat UI:
From the Chat UI, you can ask questions like: