How to run PrivateGPT on Amazon EC2

David Min
4 min readJun 11, 2023

--

Stable Diffusion AI Art

privateGPT

Ask questions to your documents without an internet connection, using the power of LLMs. 100% private, no data leaves your execution environment at any point. You can ingest documents and ask questions without an internet connection!

Built with LangChain, GPT4All, LlamaCpp, Chroma and SentenceTransformers.

Source: https://github.com/imartinez/privateGPT

How run PrivateGPT on Amazon EC2

Step 1 — Launch Amazon EC2 instance

Create a Amazon EC2 instance using AWS CloudFormation

  • Region: us-east-1
  • AMI: “ami-0649417d1ede3c91a” # Deep Learning AMI
  • Instance: g5.2xlarge
  • EBS volume: 500GB

PrivateGPT.yaml

AWSTemplateFormatVersion: '2010-09-09'
Description: EC2 Instance
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.

Mappings:
RegionToAmiId:
us-east-1:
AMI: ami-0649417d1ede3c91a

Resources:
SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub ${AWS::StackName}-sg
GroupDescription: Security group for EC2 instance
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
EC2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: g5.2xlarge
ImageId: !FindInMap [RegionToAmiId, !Ref AWS::Region, AMI]
KeyName: !Ref KeyName
BlockDeviceMappings:
- DeviceName: /dev/sda1
Ebs:
VolumeSize: 500
VolumeType: gp2
"Tags" : [
{"Key" : "Name", "Value" : "PrivateGPT"},
]
SecurityGroups:
- Ref: SecurityGroup

Outputs:
PublicDNS:
Description: Public DNSName of the newly created EC2 instance
Value: !GetAtt [EC2Instance, PublicDnsName]
PublicIP:
Description: Public IP address of the newly created EC2 instance
Value: !GetAtt [EC2Instance, PublicIp]

AWS CloudFormation > Create stack

AWS CloudFormation

AWS CloudFormation — Step 1 Create stack

Upload LocalGPT.yaml template file and Next.

AWS CloudFormation — Step 1 Create stack

AWS CloudFormation — Step 2 Specify stack details

Specify Stack name and KeyName and Next.

AWS CloudFormation — Step 2 Specify stack details

AWS CloudFormation — Step 3 Configure stack options

Use default settings and Next.

AWS CloudFormation — Step 3 Configure stack options

AWS CloudFormation — Step 4 Review and Submit.

Step 2 — Install PrivateGPT

SSH to Amazon EC2 instance and start JupyterLab

# Start JupyterLab
cd /home/ubuntu
jupyter lab --notebook-dir=/home/ubuntu

Set up SSH tunnel using local port forwarding to JupyterLab

# Syntax: ssh -L <LOCAL_PORT>:<REMOTE_HOST>:<REMOTE_PORT> <GATEWAY>
ssh -i "us-east-1-key.pem" -N -L 8888:localhost:8888 ubuntu@ec2-###-##-##-###.compute-1.amazonaws.com

Open JupyterLab in your local browser

To access the JupyterLab, copy and paste http://127.0.0.1:8888/lab or http://localhost:8888/lab with the token in your local browser after setting up a SSH tunnel.

JupyterLab

PrivateGPT Installation

Install PrivateGPT from JupyterLab terminal.

# Install Conda
curl -sL "https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh" > "Miniconda3.sh"
bash Miniconda3.sh -b -u -p miniconda
source /home/ubuntu/miniconda/bin/activate

# Create a new conda environment
conda create -n privateGPT python=3.10.9 -y
conda activate privateGPT
pip install --quiet ipykernel
python -m ipykernel install --user --name privateGPT --display-name privateGPT

git clone https://github.com/imartinez/privateGPT
cd privateGPT
pip install -r requirements.txt

Download LLM Model

mkdir models
cd models
wget https://huggingface.co/vicuna/ggml-vicuna-13b-1.1/resolve/main/ggml-vic13b-q5_1.bin
cd ..
cp example.env .env

example.env

PERSIST_DIRECTORY=db
MODEL_TYPE=GPT4All
MODEL_PATH=models/ggml-gpt4all-j-v1.3-groovy.bin
EMBEDDINGS_MODEL_NAME=all-MiniLM-L6-v2
MODEL_N_CTX=1000
MODEL_N_BATCH=8
TARGET_SOURCE_CHUNKS=4

Update .env file

PERSIST_DIRECTORY=db
MODEL_TYPE=LlamaCpp
MODEL_PATH=models/ggml-vic13b-q5_1.bin
EMBEDDINGS_MODEL_NAME=all-MiniLM-L6-v2
MODEL_N_CTX=1000
MODEL_N_BATCH=1024
TARGET_SOURCE_CHUNKS=4

Step 3 — Run PrivateGPT

Upload your documents

Upload your documents in source_documents folder using JupyterLab File Browser.

JupyterLab — File Browser

Ingest documents

python ingest.py

Run PrivateGPT

python privateGPT.py

python privateGPT.py -S (disable source)

Wait for the script to require your input.

> Enter a query:

Enter a query and hit enter.

You’ll need to wait 20–30 seconds (depending on your machine) while the LLM model consumes the prompt and prepares the answer. Once done, it will print the answer and the 4 sources it used as context from your documents; you can then ask another question without re-running the script, just wait for the prompt again.

Type exit to finish the script.

Useful Links:

Related Articles:

--

--