Skip to main content

Running Vercel AI Chatbot on your own server

Ruslan Gainutdinov

Vercel has an example app to run a streaming AI chatbot, your own ChatGPT alternative. It uses Vercel's AI Gateway, so that you can use many different language models with it.

Let's see how we can run it on your own server!

Next.js AI Chatbot
A full-featured, hackable Next.js AI chatbot built by Vercel

Next.js AI Chatbot

Creating a server

To run on your own server, you need to create or add an existing server first. With our integrations with DigitalOcean, Hetzner, and DataCrunch, it is very easy. Simply add a new host, set the configuration details, and click Create. DollarDeploy will automatically provision a new server for you.

0:00
/0:43

Creating new server automatically with DollarDeploy

You will also need to install Redis and Postgres services on the server. Add them to the Managed Host Services and press Prepare. It will download, install and automatically configure them.

Here is an example server I created with Hetzner:

Server ready for our AI chatbot

Creating storage for file uploads (MinIO)

AI chatbot also have the ability to upload files for AI to understand. This functionality uses Vercel's proprietary @vercel/blob but we can easily replicated it with S3 compatible storage, such as AWS s3 or MinIO.

You can install MinIO on your server in a single click

Deploy MinIO | DollarDeploy
Deploy MinIO in one click to your VPS.

Once that is installed, configure it properly to allow publicly accessible links for files (I followed the guide here: https://gist.github.com/harshavardhana/f05b60fe6f96803743f38bea4b565bbf)

# Connect to your server
mc alias set mycloud https://minio.NNNN.dollardeploy.app $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY

# Create new bucket uploads
mc mb mycloud/uploads

# Allow uploaded files to be publically accessible without auth
mc anonymous set download mycloud/uploads

Creating the app in DollarDeploy

Just point to your own fork of AI chat. You can use my fork https://github.com/huksley/ai-chatbot-vercel so you have @vercel/blob already replaced with uploading image attachements to an S3 compatible storage.

Just accept the defaults. Most build parameters are fine, except some environment variables. POSTGRES_URL, REDIS_URL are automatically populated from the services you installed on your server.

  • AI_GATEWAY_API_KEY=nnn

Go to Vercel to generate an AI gateway token. You can have $5 free credits when you start.

  • S3_BUCKET=uploads
  • S3_REGION=eu-central-1
  • S3_ACCESS_KEY_ID=NkZUJdf4BP
  • S3_SECRET_ACCESS_KEY=MuIfucE34mXQm6voVujJaGK65axiPStO

All these are copied from MinIO instance you just created. You also need to add S3_ENDPOINT=MINIO_URL which points to the MinIO instance you deployed before.

Allow next/image to process images from MinIO

To allow, add the following to the next.config.js:

...
  images: {
    remotePatterns: [
      {
        hostname: "127.0.0.1",
      },
      {
        hostname: "MINIO_URL",
      },
    ],
  },
...

Automatically apply migrations before starting the app

Just add to the prestart command the following

npm i -g pnpm && pnpm i && pnpm run db:migrate

This will run migrations automatically.

Final polishing

You might want to do final polishing to make sure your app works in the long run.

Server Actions use encrypted identifiers that change with every build by default. During rolling deployments, this causes the dreaded error: "Failed to find Server Action "XYZ". This request might be from an older or newer deployment."

Make sure to set NEXT_SERVER_ACTIONS_ENCRYPTION_KEY in the app environment variables, generate using node -e "const crypto = require('node:crypto'); const key = crypto.randomBytes(32); console.log(key.toString('base64'))"

Done

App deployed successfully!
0:00
/0:25

AI chatbot example, streaming works!