Tutorials

How to Make a ChatGPT Plugin With Ruby and Rails

Sean Moriarity
#openai#chatgpt#ruby#ruby on rails#chatgpt plugins

ChatGPT plugins provide a new medium for developers to take advantage of OpenAI’s powerful GPT-4 model. plugins give ChatGPT access to tools via a REST API. When a plugin is installed in a user’s ChatGPT session, the model may decide to use the plugin as a part of it’s response.

You can develop plugins that enable ChatGPT to plan travel, go shopping, or do complex mathematics. In this tutorial, you’ll learn the basics of developing a ChatGPT plugin using Ruby and Rails.

This tutorial assumes you have Ruby and Rails installed. For information about how to create a plugin in other languages, see:

Step 1: Set up the Rails project

Create a new Rails project by running:

rails new ruby_chatgpt_plugin --api

This example is API-only because ChatGPT plugins are API-only. You can omit the API flags from the generation if you plan to have a frontend for your application. This will create a new Rails project.

You can confirm your Rails application is setup properly by running rails server from the project directory and navigating to the localhost:3000:

Ruby Rails Home Page

Now you’re ready to create the plugin API!

Step 2: Create the plugin API

ChatGPT interacts with plugins via a defined REST API. To start defining your REST API, create a new route in config/routes.rb inside an api scope:

  scope path: '/api' do
    get 'hello', to: 'chat_gpt#hello'
  end

This will create a new route at /api/hello which dispatches to the hello method in the ChatGPT controller. Next, create app/controllers/chat_gpt_controller.rb and define a new controller:

class ChatGptController < ApplicationController
end

Next, define the hello method:

  def hello
    message = { message: "Hello from the plugin!" }
    render json: message
  end

This will return a JSON response from the server with the message Hello from the plugin!. Your entire controller will look like this:

class ChatGptController < ApplicationController
  def hello
    message = { message: "Hello from the plugin" }
    render json: message
  end
end

With your API defined, you need to define an OpenAPI Spec. Rails doesn’t automatically generate OpenAPI specs, so you need to generate them yourself.

Step 3: Create the OpenAPI Spec

The OpenAPI Spec is how your ChatGPT plugin knows which endpoints are available to interact with. ChatGPT will use your OpenAPI spec to structure its requests and parse the responses from your plugin. For your Ruby plugin, create a new file public/openapi.yaml. Next, open up openapi.yaml and add the following:

openapi: 3.0.0
info:
  title: Ruby ChatGPT Plugin
  version: 1.0.0
servers:
  - url: http://localhost:3000/api
paths:
  /hello:
    get:
      summary: Say hello
      description: Says hello from the plugin
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
      operationId: sayHello

This spec defines your single /hello endpoint which is available from the url http://localhost:3000/api/hello. Now you’re ready to define the ai-plugin.json for your Ruby plugin.

Step 4: Create the plugin manifest

Every ChatGPT plugin must ship with a plugin manifest hosted on the same domain as the API. ChatGPT looks for the plugin specifically at the path /.well-known/ai-plugin.json. You can read more about the specifics of the plugin manifest in our What is the ChatGPT plugin manifest? post.

For your plugin, start by creating a new directory public/.well-known. Next, create a new file in that directory called ai-plugin.json. Then, add the following to ai-plugin.json:

{
  "schema_version": "v1",
  "name_for_human": "My First plugin",
  "name_for_model": "ruby_plugin",
  "description_for_human": "My first ChatGPT plugin",
  "description_for_model": "plugin which says hello.",
  "auth": {
    "type": "none"
  },
  "api": {
    "type": "openapi",
    "url": "http://localhost:3000/openapi.yaml",
    "is_user_authenticated": false
  },
  "logo_url": "http://localhost:3000/logo.png",
  "contact_email": "support@example.com",
  "legal_info_url": "http://www.example.com/legal"
}

In this example, the “auth” field is set to “none” because this plugin doesn’t require authentication. However, depending on your use case, you might need your plugin to handle authentication. For more information about how to implement authentication in a ChatGPT plugin, you can refer to our ChatGPT Plugin Authentication Guide.

Both openapi.yaml and .well-known/ai-plugin.json should be served from your application. You can verify it worked by starting your server and accessing both files from the browser.

Step 5: Set up CORS for development

In order to test your plugin locally before deploying to production, you need to configure your application to allow Cross-Origin Resource Sharing (CORS) from the ChatGPT website. CORS enables ChatGPT to request access to resources from your locally running webserver. To setup CORS with Ruby and Rails, you can use the rack-cors gem. First, install the rack-cors gem by adding it to your application’s Gemfile:

gem 'rack-cors'

Then run bundle install to install your new dependencies. Finally, add the following code to config/application.rb:

    # Configure CORS
    config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins ['https://chat.openai.com']
        resource '*',
          headers: :any,
          methods: [:get, :post]
      end
    end

This will configure CORS correctly in your application for local development. Now you’re all set for testing!

Step 5: Install and test your plugin

First, start your plugin server locally by running:

rails server

You can verify it worked by navigating to http://localhost:3000 in your browser:

rails home page

Next, navigate to the ChatGPT UI and select the plugin model:

Selecting the plugin model

Then, you’ll want to select the plugins dropdown, navigate to the plugin store, and click “Develop your own plugin” in the bottom right:

Selecting develop your own plugin

Finally, type your localhost URL into the plugin form:

Typing the address of your plugin

After awhile, you’ll see the following:

Successfully adding your plugin

Click “Install localhost plugin” to continue. With your plugin enabled, you can start to interact with it using ChatGPT. For example, try typing in “Say hello from my plugin” into the chat window:

Successfully using the plugin

Congratulations! You’ve successfully made your first ChatGPT plugin with Ruby and Rails and confirmed it works locally. Next, check out our deployment and hosting guide to learn about deploying your plugin to production.

Enjoyed this post?
Subscribe for more!

Get updates on new content, exclusive offers, and exclusive materials by subscribing to our newsletter.

← Back to Blog