Coding My First Plugin for ChatGPT

Coding My First Plugin for ChatGPT

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.

No alt text provided for this image
Base Python code for an /Order API

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.

No alt text provided for this image
Python code for referencing the ai-plugin.json and openapi.yaml specifications files

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.

No alt text provided for this image
Python code to run the app

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".

No alt text provided for this image
OpenAPI.yaml file Part 1

The next part of the OpenAPI specification defines the possible responses.

No alt text provided for this image
OpenAPI Specification Part 2

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.

No alt text provided for this image
ai-plugin.json file specification for order entry for 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.


No alt text provided for this image
My first order using the Order Entry Plugin

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"

No alt text provided for this image
ChatGPT handling missing and incorrect information.


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.

  • Read the Open AI documentation on how to develop a plug-in. You can find that here: https://platform.openai.com/docs/plugins/introduction
  • You can design and build your code, with one. or more endpoints.
  • Create the OpenAPI specification file for your program. This is a requirement for the plugin. You can find that here:

  • Test the functionality of your endpoint. If it works as a standard API endpoint, it will likely work fine as a plugin.
  • Create your?ai-plugin.json?file. You won’t make use of it until you have developer access to plugins but coding the specification forces you to think about how your plugin will work with ChatGPT.

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.

Allan Neil

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.

要查看或添加评论,请登录

社区洞察