
ChatGPT Plugins support is rolling out in beta this week! To help you get up and running quickly, we're releasing a plugin template written in TypeScript and running on Supabase Edge Runtime!
Want to get started right away? Fork the template on GitHub!
Serving the manifest file
The ai-plugin.json
manifest file is required for ChatGPT to identify our plugin, know what kind of authentication mechanism is used, understand where to find the OpenAPI definition, and some other details about our plugin. You can find the full list of supported parameters in the OpenAI docs.
Supabase Edge Runtime does currently not support hosting/serving of static files, however, we can import JSON files in our function and serve them as a JSON response. As this needs to be at the root of our domain, we add this to our main function handler:
Now, when running Edge Runtime locally via Docker, our plugin manifest will be available at http://localhost:8000/.well-known/ai-plugin.json
Generating the OpenAPI definition with swagger-jsdoc
The OpenAPI definition is required for ChatGPT to know how to underact with our API. Only endpoints included in there will be exposed to ChatGPT, which allows you to selectively make our endpoints available, or add specific endpoints for ChatGPT.
The OpenAPI definition can be either in YAML or JSON format. We’ll be using JSON and the same approach as above to serve it. Writing an OpenAPI definition is not something we will want to do by hand, luckily there is an open source tool called swagger-jsdoc which we can use to annotate our endpoints with JSDoc comments and generate the OpenAPI definition with a little script.
Since this script is run outside of the function execution, e.g. as a GitHub Action, we can use npm specifiers to import swagger-jsdoc
.
Next, we create our /functions/chatgpt-plugin/index.ts
file where we use the Deno oak router to build our API and annotate it with JSDOC comments.
With our JSDoc annotation in place, we can now run the generation script in the terminal:
Adding the CORS headers
Lastly, we need to add some CORS headers to make the browser happy. We define them in a /functions/_shared/cors.ts
file so we can easily reuse them across our main
and chatgpt-plugins
function.
Now we can easily add them to all our chatgpt-plugin
routes a middleware for our oak application.
Running locally with Docker
Now that we’ve got all the pieces in place, let’s spin up Edge Runtime locally and test things out. For this, we need a Dockerfile and for convenience, we can add a docker-compose file also.
This will pull down Edge Runtime v1.2.18 (you can check the latest release here) and start up the main service (our /functions/main/index.ts
function).
Edge Runtime will serve requests on port 9000
, so we’re creating a mapping from [localhost:8000](http://localhost:8000)
where we want to serve our requests locally (of course you can adapt this to your needs) to port 9000
of our Docker container.
Furthermore, we’re using bind mounts to mount our functions directory into the container. This allows us to make modifications to our functions without needing to rebuild the container after, making for a great local developer experience.
That’s it, now we can build and spin up our container from the terminal:
Go ahead and try it out by visiting:
- http://localhost:8000/chatgpt-plugin
- http://localhost:8000/.well-known/ai-plugin.json
- http://localhost:8000/chatgpt-plugin/openapi.json
- http://localhost:8000/chatgpt-plugin/todos/user
Installing and testing the plugin locally
You can conveniently test your plugin while running it on localhost using the ChatGPT UI:
- Select the plugin model from the top drop down, then select “Plugins”, “Plugin Store”, and finally “Develop your own plugin”.
- Enter
localhost:8000
and click "Find manifest file". - Confirm with “Install localhost plugin”.
That’s it, now go ahead and ask some questions, e.g. you can start with “Do I have any todos?”
There you are, now go ahead and build your own plugin as it says on your todo list ;)
Deploying to Fly.io
Once you’re happy with the functionality of your plugin, go ahead and deploy it to Fly.io. After installing the flyctl cli, it only takes a couple of steps:
- Change
http://localhost:8000
to your Fly domain in the/main/ai-plugins.json
file - Open
fly.toml
and update the app name and optionally the region etc. - In your terminal, run
fly apps create
and specify the app name you just set in yourfly.toml
file. - Finally, run
fly deploy
.
There you go, now you’re ready to release your plugin to the world \o/
Conclusion
ChatGPT is a powerful new interface and its usage is growing rapidly. With ChatGPT Plugins you can allow your users to access your service directly from ChatGPT, using cutting edge technologies like TypeScript and Deno.
In a next step you can add authentication to your plugin, let us know on Twitter if you’d be interested in a tutorial for that. We can’t wait to see what you will build!