Coding My First Plugin for ChatGPT
Dennis Layton
A Senior IT architect and a proponent for the responsible adoption of AI
Chat plugins are a game-changing innovation in the world of AI conversation. With plugins, developers can seamlessly integrate powerful features and vital information right into the heart of any chat. Users will be able to choose?from a diverse array of plugins in a dedicated plugin store then watch ChatGPT work its magic, invoking them just when you need them.?All of this is in an Alpha phase of testing by OpenAI but I have early developer access and I have just written my first plugin, so please read on.
The simplest plugin I could think of
This is probably the simplest example of a plugin that I could think of. The Python script below defines one API (Application Programming Interface) endpoint named /Order. This endpoint processes an order for products in the world's smallest company -- it only has three products. Even though this is a single Python script called "main.py" I have chosen to show it in three parts below.
As shown this is a fully functional API endpoint. I can, and did run this to test it as an API. So, what is needed to turn this into a Chat plugin. There are two kinds of specifications that needed to be coded.
Coding the specification files needed
One of these specifications is very familiar to anyone that has defined a Swagger User Interface for a standard API endpoint. It's called an OpenAPI specification and it's identical to what you have to write for a Swagger User Interface. See the reference to the openapi.yaml file in the code below.
The other specification is one that provides the additional information a plugin needs to interoperate with GPT-4. See the reference to the ai-plugin.json file, also in the code below.
To finish the Python code all you need is to run it on a host machine. There are no differences to this code from what would be needed to run this as a standard API endpoint.
We mentioned the two specification files, openapi.yaml and ai-plugin.json. The first of these is shown below. The openapi.yaml file is a single file but I have chosen to show it below in two parts. The first part shown below shows the header information as well as the specification for the single API endpoint, "/Order".
The next part of the OpenAPI specification defines the possible responses.
Where does the magic happen?
So where does the magic happen? It all happens in a deceptively simple specification, known as the ai-plugin.json file. Here is what I specified (see below) for my order entry plugin. I decided to name my imaginary company that has the world's smallest product database, MyCompany.
Incredibly all of the magic in this specification is in one line that begins with "description_for_model". This is an explanation as to when to use the plugin. I kept this one simple and it works well.
After I installed and enabled my new plugin. I was ready to go. I won't go into how this is done to keep this article short, instead I will show the plugin from a users perspective.
My first order is shown below. I entered the prompt, "Place an order for MyCompany for 3 items of product 1". ChatGPT identified the plug-in needed and used it to process my order. From there it took the json object that was passed back and formatted the output and added the message at the bottom, indicating that GPT-4 had discerned that this was a user shopping and purchasing a product.
How does it handle errors or missing information ?
The next question was how would ChatGPT handle missing or incorrect information? I created a prompt for a product that I knew did not exist in MyCompany and I left out how many items I needed. The conversation is below, beginning with the prompt, "I'd like to place an order for product 99 from MyCompany"
Summary
I got my access to ChatGPT plugins, just over a week ago. The example I used in this article is an extremely simple plugin, but the plugin technology became even more exciting to me when I used my plugin for the first time.
In hindsight I realize I could have done much of the work in advance of getting ChatGPT developer access.
For those who do not yet have access, there is a lot you can do towards developing a plug-in.
It's clear that a lot of opportunities exist out there for plug-ins, especially in more specialized domains, whether they be corporate, academic, government or other.
Senior B2B Product Management Professional
1 年Dennis Layton you are choosing your overlords early. I am sure you will be rewarded with a good role when they take over.