Creating a Minimum Viable ChatGPT Plugin using Elixir Phoenix
If you recently got access to the ChatGPT Plugin beta and want to integrate it with your database of knowledge, this guide will show you how to create a minimum viable plugin using Elixir Phoenix and OpenAPI. ChatGPT Plugin is essentially a manifest file and an OpenAPI spec to tell ChatGPT how to consume an API. Phoenix is ideal for building the kind of APIs that ChatGPT consumes, so let's get started with a fresh project.
Generating a JSON API
When building a ChatGPT Plugin, we're actually building a standard JSON API with an OpenAPI spec configuration. So we're generating a new Phoenix Application with basically just an Endpoint and Router. If you have an existing Phoenix Application, you should be able to copy the code directly into your code base.
We can build more complex plugins for ChatGPT to consume, but this guide walks through only the most basic example, to set a foundation for us to build on. Let's start by generating a JSON API:
Tell the Phoenix generator to skip generating the context with the --no-context flag. Open up the controller and make some edits, removing everything except the index function.
Adding a Route to the Router
After editing the controller, it's time to add a route to our Router.
Updating the View Code
Update your view code by editing document_json.ex. Remove the match on %Document{} as it doesn't exist.
Adding CORS
ChatGPT has a couple specific needs for your local plugin to work. Add CORS, and we'll need to add the cors_plug dep to our mix.exs. Then add a line to our endpoint.ex to clean up the file.
Update the ChatGptPluginWeb.static_paths() function
Update the ChatGptPluginWeb.static_paths() function in our chat_gpt_plugin_web.ex file by adding the .well-known folder and openapi.yaml file to known static paths.
Create an openapi.yaml file at priv/static/openapi.yaml
Create an openapi.yaml file at priv/static/openapi.yaml. It's describing our API, the get/gpt-search route we made and the query parameter we expect, and finally explains the shape of the JSON we're returning.
Creating an ai-plugin.json file
Create an ai-plugin.json file at priv/static/.well-known/ai-plugin.json. Add a description_for_model as this is your prompt when searching the API.
Testing the ChatGPT Plugin
If you mix phx.server the running application, open up https://chat.openai.com, click the Plugins Dropdown at the top then this little "Develop your own plugin" link, and then enter your URL http://localhost:4000/, it should find it and start working!
Conclusion
Creating a ChatGPT Plugin with Elixir Phoenix is a straightforward process. With just a few simple steps, you can build your own plugin to integrate with ChatGPT. Remember, the limit is your imagination on what you can build with Plugins and on top of OpenAI's ChatGPT.
If you found this guide helpful, please let us know what you've built. Deploy a Phoenix app today with Fly.io to jump-start your next project.