Host your own AI Chatbot with LibreChat
Introduction​
In this guide, we will walk through the process of hosting your own AI chatbot using LibreChat. LibreChat is an open-source chatbot platform that allows you to create and deploy your own chatbots using a simple web interface. By hosting your chatbot on DigitalOcean, you can take advantage of their cloud infrastructure to ensure that your chatbot is always available and responsive.
I will be walking through the LibreChat guide here for the most part, but I will also be adding some additional steps and information to help you get started with hosting your chatbot on DigitalOcean.
If you want to get started with Digital Ocean, you can use my referral link to get $200 in credit over 60 days. We will be setting up an Ubuntu Server Droplet for this project which will cost approximately $6/month. Not too bad for a chatbot that can be accessed from anywhere in the world!
Oh, and the LibreChat project also has authentication built in, so you can secure your chatbot with a password or even a domain name if you'd like. We will walk through SSH key authentication and will create a subdomain for our chatbot in this guide.
While the LibreChat project allows for a variety of integration from multiple AI providers, we will be using OpenAI's API for this project. You will need an API key from OpenAI to get started.
Prerequisites​
- A DigitalOcean account
- A Github account
- Basic knowledge of Linux
- Basic knowledge of Docker
- An OpenAI API key
- Basic knowledge of AI and chatbots
- A domain name (optional)
Overview of What We'll Cover​
This project will walk you through the process of hosting your own AI chatbot using LibreChat on DigitalOcean. We will start by creating an Ubuntu Server Droplet on DigitalOcean, setting up SSH key authentication, and installing Docker on the server. We will then deploy the LibreChat project on the server using Docker and configure it to use the OpenAI API for AI responses. Finally, we will secure the chatbot with a password and create a subdomain for the chatbot using DigitalOcean's DNS management.
Need guidance on setting up a DigitalOcean Droplet? Check out these links
Getting Started in Digital Ocean​
Step 1: Create an Ubuntu Server Droplet on DigitalOcean​
-
Log in to your DigitalOcean account and click on the "Create" button in the top right corner. Select "Droplets" from the dropdown menu.
-
Choose your region. You'll want to choose a region that is closest to you or your target audience to ensure low latency. I'm not too far from Toronto, so I'll choose the Toronto region.
-
Scroll down to the OS section and then select "Ubuntu" as the distribution. I'll be using Ubuntu 22.04 for this project.
-
In the "Choose size" section we can choose the Droplet Type we want to use. This is essentially the "plan" for your usage of the server. I'll be using the $6/month plan for this project.
Step 2: Side Quest - SSH Key Authentication​
-
If you haven't already, you'll want to set up SSH key authentication for your DigitalOcean account. This will allow you to securely connect to your Droplet without needing to enter a password.
-
You can follow the official DigitalOcean guide or follow the walkthrough provided to add SSH keys to your account.
noteIf your on a Mac and the file location defaults to
~/.ssh/id_ed25519instead of~/.ssh/id_rsa, use this file instead. The DigitalOcean guide shows thersaas default, but I'll be using theed25519file.
Step 3: Finish Droplet Setup​
-
Below the SSH key section, you can add any additional options you'd like. I'll be adding monitoring to my Droplet. It's a free service that will monitor your Droplet's performance and alert you if there are any issues.
-
In the "Finalize Details" section we'll leave the Quantity default as 1 a give the Droplet a hostname. I'll be using "librechat-do" for this project. You can name it whatever you'd like.
-
I've created no dedicated Project so my default is the only option available. You can create a project for your Droplet if you'd like.
-
Click on the "Create Droplet" button to create your Ubuntu Server Droplet.
Your Droplet will spin up and you'll be provided with an IP address to connect to your server. You can now SSH into your Droplet using the IP address and your SSH key.
Note that your billing will start as soon as you create the Droplet, so make sure to destroy the Droplet when you're done with it if you would like to avoid any additional charges. I'm keeping mine to use as my primary instance of LibreChat.
Setting Up the Ubuntu Server on the librechat-do Droplet​
There are a few methods we can go here. We can access the Droplet via the DigitalOcean console, or we can SSH into the Droplet using our SSH key. I'll be using SSH to connect to the Droplet given I've already set up my SSH key.
Items required for to access the Droplet via SSH:
- IP Address of the Droplet
- the default username on the server
- the path to your SSH key
If, like me, you are running a Mac, your default file is usually located in ~/.ssh/id_ed25519 instead of the ~/.ssh/id_rsa file as indicated in the DigitalOcean guide. I followed their steps and pasted the contents of my id_ed25519.pub file into the SSH key section of the Droplet creation page. The other method resulted in a permission denied error when trying to connect to the Droplet.
ssh root@<your-droplet-ip> -i ~/.ssh/id_ed25519
Voila! You should now be connected to your Droplet. Let's get started with setting up the server.
Ubuntu User Setup​
This is a fairly standard process when setting up a new Linux server. The server ususally spins up with a root user, but we'll want to create a new user and give them sudo privileges. This is a security best practice to avoid running commands as the root user.
Step 1: Create a Non-root User​
adduser <username>
For me this looks like this:
adduser firesoflife
Step 2: Add Sudo Privileges​
This user will need some powers. Let's give them sudo privileges.
#use your username where I have firesoflife
usermod -aG sudo firesoflife
Check that the user has sudo privileges by running:
getent group sudo | cut -d: -f4
This should return your username.
Switch to the new user created in the droplet:
su - <username>
Firewall Setup​
The guide at LibreChat suggests using UFW to set up a firewall on the server. This is a good practice to secure your server and only allow traffic on the ports you need. Let's get this set up now.
Step 1: Install UFW​
First we need to get to ensure UFW is installed on the server.
If you are using a common Linux distribution like Ubuntu, UFW will likely already be installed on your system. You can check if UFW is installed by running the following command:
sudo ufw status
A status of inactive is A-OK. Otherwise, you'll need to install UFW using the install command above.
-
Run the following to make sure you get the latest software
sudo apt update -
Then run this command to install
sudo apt install ufw
Step 2: Configure UFW​
By default, when you enable UFW, it will deny all incoming connections, and that is great, but we still need to talk to our server. Here are the rules we want to set up:
Rules:
- SSH (Port 22) - Allow SSH traffic.
- HTTP (Port 80) - Allow HTTP traffic.
- HTTPS (Port 443) - Allow HTTPS traffic.
Ok, let's get this set up. If we simply hop into the UFW and enable it, we'll be locked out of our server. So, let's set up the rules first:
Set Rules
-
Allow SSH on Port 22'
sudo ufw allow 22/tcp -
Allow HTTP on Port 80
sudo ufw allow 80/tcp -
Allow HTTPS on Port 443
sudo ufw allow 443/tcp
Now that we have the rules set up, we can enable UFW.
ufw enable
You'll be prompted to confirm the action. Type y and hit Enter to enable the firewall. You'll see a confirmation message:
Firewall is active and enabled on system startup
Now when you run sudo ufw status, you should see the following output:
Status: active
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
443/tcp ALLOW Anywhere
22/tcp (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
443/tcp (v6) ALLOW Anywhere (v6)
Ok, we now have our droplet set up and secured with firewall rules so we can securely access it via SSH when we need to. Next up, we'll install Docker on the server where we will be hosting our LibreChat chatbot.