Deploying R2R on Google Cloud Platform

Google Cloud Platform (GCP) offers a robust and scalable environment for deploying R2R (RAG to Riches). This guide will walk you through the process of setting up R2R on a Google Compute Engine instance, making it accessible both locally and publicly.

Overview

Deploying R2R on GCP involves the following main steps:

  1. Creating a Google Compute Engine instance
  2. Installing necessary dependencies
  3. Setting up R2R
  4. Configuring port forwarding for local access
  5. Exposing ports for public access (optional)

This guide assumes you have a Google Cloud account and the necessary permissions to create and manage Compute Engine instances.

Creating a Google Compute Engine Instance

  1. Log in to the Google Cloud Console.
  2. Navigate to “Compute Engine” > “VM instances”.
  3. Click “Create Instance”.
  4. Choose the following settings:
    • Name: Choose a name for your instance
    • Region and Zone: Select based on your location/preferences
    • Machine Configuration:
      • Series: N1
      • Machine type: n1-standard-4 (4 vCPU, 15 GB memory) or higher
    • Boot disk:
      • Operating System: Ubuntu
      • Version: Ubuntu 22.04 LTS
      • Size: 500 GB
    • Firewall: Allow HTTP and HTTPS traffic
  5. Click “Create” to launch the instance.

Installing Dependencies

SSH into your newly created instance using the Google Cloud Console or gcloud command:

gcloud compute ssh --zone "your-zone" "your-instance-name"

Now, run the following commands to install the necessary R2R dependencies:

# Update package list and install Python and pip
sudo apt update
sudo apt install python3-pip

# Install R2R
pip install r2r

# Add R2R to PATH
echo 'export PATH=$PATH:$HOME/.local/bin' >> ~/.bashrc
source ~/.bashrc

# Install Docker
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# Add your user to the Docker group
sudo usermod -aG docker $USER
newgrp docker

# Verify Docker installation
docker run hello-world

Setting up R2R

  1. Serve your R2R backend:
# Set required remote providers
export OPENAI_API_KEY=sk-...

# Optional - pass in a custom configuration here
r2r serve --docker
  1. Double check the health of the system:
r2r health
  1. Test ingesting and searching a sample document from a remote environment:
# From your local machine
r2r --base-url=http://<your-instance-external-ip>:7272 ingest-sample-file
sleep 10
r2r --base-url=http://<your-instance-external-ip>:7272 search --query='Who was aristotle?'

Replace <your-instance-external-ip> with your Google Compute Engine instance’s external IP address.

Configuring Port Forwarding for Local Access

To access R2R from your local machine, use SSH port forwarding:

gcloud compute ssh --zone "your-zone" "your-instance-name" -- -L 7273:localhost:7273 -L 7274:localhost:7274

Exposing Ports for Public Access (Optional)

To make R2R publicly accessible:

  1. In the Google Cloud Console, go to “VPC network” > “Firewall”.

  2. Click “Create Firewall Rule”.

  3. Configure the rule:

    • Name: Allow-R2R
    • Target tags: r2r-server
    • Source IP ranges: 0.0.0.0/0
    • Specified protocols and ports: tcp:7272
  4. Click “Create”.

  5. Add the network tag to your instance:

    • Go to Compute Engine > VM instances.
    • Click on your instance name.
    • Click “Edit”.
    • Under “Network tags”, add “r2r-server”.
    • Click “Save”.
  6. Ensure R2R is configured to listen on all interfaces (0.0.0.0).

After starting your R2R application, users can access it at:

http://<your-instance-external-ip>:7272

Security Considerations

  • Use HTTPS (port 443) with a valid SSL certificate for production.
  • Restrict source IP addresses in the firewall rule if possible.
  • Regularly update and patch your system and applications.
  • Monitor incoming traffic for suspicious activities.
  • Remove or disable the firewall rule when not needed for testing.

Conclusion

You have now successfully deployed R2R on Google Cloud Platform. The application should be accessible locally through SSH tunneling and optionally publicly through direct access to the Compute Engine instance. Remember to configure authentication and implement proper security measures before exposing your R2R instance to the public internet.

For more information on configuring and using R2R, refer to the configuration documentation.