/openai-gpt-plugin-pgvector

An OpenAI GTP plugin built on ChatGPT, Spring AI and Postgres pgvector

Primary LanguageJavaScriptApache License 2.0Apache-2.0

OpenAI GPT Plugin With Spring AI and PostgreSQL pgvector

Follow this guide to create and launch a custom OpenAI GPT plugin using Spring AI and PostgreSQL pgvector.

The plugin will turn ChatGPT into a movie sommelier - a fully autonomous AI agent that provides you with movie recommendations and updates your movies catalogue.

main-picture

The AI will use Postgres and its pgvector extension to retrieve information from a custom movies databases. Then, ChatGPT will be able to update your own movie catalogue with various movies selection. The overall architecture looks as follows:

custom_gpt

Prerequisites

  1. ChatGPT account
  2. OpenAI API key
  3. Java 21+ and Maven.
  4. Heroku account
  5. YugabyteDB Managed or another Postgres distribution that supports the pgvector extension.

Provision Postgres With pgvector

The ChatGPT knowledgbase will be augmented with the information stored in Postgres. The AI agent will be querying or updating data stored in Postgres by following a conversation with the users.

Deploy an instance of Postgres distribution that supports the pgvector extension. The database has to be accessable to an application backend that will be deployed on Heroku.

In this guide, we're using YugabyteDB - a distributed database built on Postgres. With YugabyteDB, the OpenAI GPT plugin will be able to scale read with write workloads and tolerate possible outages in the cloud.

Create a single-node instance or multi-node YugabyteDB Managed cluster

Deploy Application Backend to Heroku

Deploy the application backend to Heroku:

  1. Go to the backend directory:

    cd backend/
  2. Build the backend:

    mvn clean package -DskipTests
  3. Log in to your Heroku account:

    heroku login
  4. Install the Java plugin on Heroku:

    heroku plugins:install java
  5. Create a Heroku application for the backend:

    heroku create openai-gpt-plugin-backend
  6. Provide application and database-specific configuration settings to Heroku:

    heroku config:set PORT=80 -a openai-gpt-plugin-backend
    heroku config:set OPENAI_API_KEY=<YOUR_OPEN_AI_API_KEY> -a openai-gpt-plugin-backend
    heroku config:set BACKEND_API_KEY=OpenAIGPTPlugin -a openai-gpt-plugin-backend
    
    heroku config:set DB_URL="<YOUR_DB_URL>" -a openai-gpt-plugin-backend
    heroku config:set DB_USER=<YOUR_DB_USER> -a openai-gpt-plugin-backend
    heroku config:set DB_PASSWORD=<YOUR_DB_PWD> -a openai-gpt-plugin-backend

    where:

    • BACKEND_API_KEY - a custom key that the ChatGPT will be using to authenticate with the application backend.
    • DB_URL - a connection endpoint to a Postgres instance. If you use YugabyteDB Managed, the the URL format should be as follows: jdbc:postgresql://{YGABYTEDB_NODE_ADDRESS}:5433/yugabyte?ssl=true&sslmode=require
  7. Deploy the application to Heroku:

    heroku deploy:jar target/yugaplus-backend-1.0.0.jar -a openai-gpt-plugin-backend

Once deployed, check the application logs to ensure it's started without failures and managed to connect to your database.

heroku logs --tail -a openai-gpt-plugin-backend

Test the application by sending the following HTTP request with HTTPie tool:

http GET https://{YOUR_APP_URL_ON_HEROKU}/api/movie/search prompt=="A long time ago in a galaxy far, far away..." X-Api-Key:OpenAIGPTPlugin

Configure Custom Domain and SSL Certificate

ChatGPT requires you to register and use a custom verified domain for GPT plugins. Also, you need to configure an SSL certificate on Heroku for your applicaion.

Overall, you need to:

  • Register a custom doman
  • Create an SSL certificate for your app on Heroku (Heroku supports the automatically managed certificates)
  • Use the custom domain for your Heroku deployment.
  • Verify your OpenAI builder profile and custom domain with ChatGPT.

This video shows how to perfom these steps using GoDaddy as a DNS provider.

Once everything is set up properly, validate that the following API call executes succesfully replacing the YOUR_CUSTOM_DOMAIN with your domain:

http GET https://{YOUR_CUSTOM_DOMAIN}/api/movie/search prompt=="A long time ago in a galaxy far, far away..." X-Api-Key:OpenAIGPTPlugin

Creating OpenAI GPT Plugin

Next, let's create a custom GPT plugin and deploy it on the OpenAI GPT Store.

Note, as a custom GPT builder, you'll be able to earn if your custom GPT gets traction on the marketplace. This is what OpenAI says:

In Q1 we will launch a GPT builder revenue program. As a first step, US builders will be paid based on user engagement with their GPTs. We'll provide details on the criteria for payments as we get closer.

Your plugin will turn ChatGPT into a movie sommelier - a fully autonomous AI agent that provides you with movie recommendations and updates your movies catalogue.

To achieve that, ChatGPT uses actions that let the LLM connect to your application and access APIs that are defined in a configuration file following the OpenAPI Specification.

Follow the steps below to create a plugin for the movie sommelier or watch the video (TBD).

First, create a custom GPT plugin and configure the actions:

  • Go to your custom GPTs

  • Start configuring the plugin naming it Movie Sommelier

    1_start_configuring

Next, create configure a new action:

  • Open the provided gpt-spec.yaml file and change the server.url property to the name of your custom domain name that points out to the Heroku application.

  • Start configuring a new action pasting the contents of the gpt-spec.yaml file into the schema area:

    2-configure-action-specification

  • Click the Authentication menu and configure the API Key authentication method (use OpenAIGPTPlugin as a value for the API Key field):

    3-configure-authentication

  • Click the Test button for one API endpoints making sure ChatGPT can make a call to the Heroku app:

    4-test-actions

Then, go to the GPT Builder screen and provide the following input. ChatGPT will take this input a generate instructions for the plugin.

You are a world-famous movie critic who can easily come up with movie recommendations even for the most demanding users. You're using the provided Action that draws information from a third-party service. You need to recommend at least three movies for every user request.

Additionally, you can assist users in viewing their movie catalogue and updating it by adding or removing movies.

If a user asks to add or remove a movie from the catalogue, double-check first to ensure they really want to proceed with the update. If the user becomes annoyed by your requests for permission to update the catalogue, stop asking for permission and proceed with the updates.

5-generate-instructions

After that, the click on the Configure screen to see the final instructions, conversation starts and other details. For instance, the final instructions might be as follows:

As a world-famous movie critic, I use my vast knowledge and the third-party plugin to provide personalized movie recommendations and assist users with managing their movie catalogues. I can recommend movies, view user libraries, and help add or remove movies from their catalogues. I always confirm with users before making changes to their catalogue, unless they request to skip confirmation steps.

6-final-configuration-screen

Finally, click the Create button and launch the GPT plugin:

7-launch-gpt

Play With Plugin

Go ahead and give a try to the movie sommelier! Ask for some movie recommendations, aks to show your personal movie catalogue and don't hesitate asking to add or remove movies from your own collection.

8-play-with-plugin