Syntax Sunday: GPT Actions
Last updated
Last updated
Previously I went through how to create GTPs using the OpenAi's GPT builder. I explained exactly what GPTs are and their potential use cases. One aspect of GPTs that I did not touch on was Actions. Here's a quick overview of their function and capabilities:
Actions allow you to connect with external data. This allows you to use your own, paid, or free data sources (databases, etc..) and either retrieve or send data. This involves using APIs and the OpenAPI Schema, so you need to understand how they work at a minimum.
By providing Actions, the GPT becomes aware of available API query's and decides whether to use them during your conversation. It takes the information you provide, like a question, and uses the OpenAPI schema as a reference to construct and utilize the API endpoints.
To learn more, visit: https://platform.openai.com/docs/actions
So this was something I was eager to experiment with. I started searching for some APIs that I could use to create a simple example. I experimented with several free financial APIs, but they all had daily limits which made them tough to work with. Eventually, I landed on the NHL (National Hockey League) API, which has lots of great data, so let me show you what I did....
The Hockey Stats and Analysis Expert Bot provides in-depth analysis for NHL stats. You can query a variety of data points for: players, goalies, and teams. Explore top goal scorers, goalie stats, current/past season team standings, and more. Analyze data, compare players or teams, and export the results to Excel. I will be showing a couple examples so you can see exactly what it can do.
I will not be going through the process of creating a GPT, as that is what I did in the previous article. Instead I will be showing a few things:
How I found the NHL API endpoints
How to create OpenAPI schemas for these endpoints
How to create Actions to a GPT using these OpenAPI schema's
The schemas and GPT configuration will be published to this GitHub repo: https://github.com/bloodlinealpha/NHL-Stats-and-Analysis-Expert. So if you want to try or improve upon this GPT, you should have everything you need. If not, send me a message!
The process for finding the relevant API endpoints for the NHL was pretty simple, but I will warn you the API parameters and filters are not the easiest to work with.
1.) I went to the NHL Stats (https://www.nhl.com/stats/skaters) page. This page contains the data about the players I am looking for. Next I opened up the chrome developer tools (f12), went to the network tab, refreshed the page, and sorted by the 'type'. I found the 'fetch' types and searched through the list until I found the request that contained the player data.
2.) The "Headers", "Payload", and "Preview" tabs each hold important information. We need the 'Request URL' and information from the 'Payload' tab to help create the OpenAPI schema.
3.) I repeated this process for each statistic I wanted. As this is an example, I only found the endpoints for a few stats relating to players, goalies, and teams. This GitLab repo has some information on the API: https://gitlab.com/dword4/nhlapi/-/blob/master/new-api.md.
OpenAI require you use OpenAPI schemas when creating Actions. You can use either YAML or JSON format. I chose to use JSON, as I prefer the style of it.
The easiest way I found to get started created an OpenAPI schema is using ChatGPT. I grabbed the 'Request URL' and pasted it into ChatGPT using this prompt:
Now this most likely will not be perfect, but it will be a start. You can then copy the JSON code into a text editor like Visual Studio Code or Sublime Text, as the Actions editor is currently not ideal for making changes.
1.) Create a new GPT and go to the configure tab, and you will see the option to "Create new action".
2.) Copy your edited OpenAPI JSON schema into the "Schema" text area. You will be alerted if there are any errors in your schema. One thing I noticed is to make sure your path items contain an "operationId" as this is what the builder uses for your "Available actions" name.
Now there are a few other options I will touch on:
Import from URL - You can import your API schema if you've created one or if your service provides one. You can use tools such as Swagger ( https://swagger.io/) to help you create this also.
Examples - OpenAI has some examples for you to look at and try.
Authentication - Add an API Key or OAuth to your Actions to enable authorization for endpoints that require it. For my example I will use "None".
Privacy policy - If your GPT has Actions and you want to make it public, this is a requirement.
3.) Test your endpoints. You are able to test these endpoints right in the builder. Simply click the "Test" button and the Action will start in the preview window on the right.
It will ask you if you want to allow the request, click "Always Allow" as we know the endpoint is safe.
Sometimes, the output may disappear. Simply click 'Test' again and it should reappear. This is a minor bug or glitch that I expect will address soon.
4.) Fix and update your schema endpoints. For the NHL API, I did not create it, so I did have to figure out what a few of the parameters and sort options were. This is a trial and error process to ensure everything is working as required.
I discovered that the "description" property in the schema is crucial for GPTs when creating an API call. It seems to help steer the GPT when generating the API call parameters. When looking through my schemas you might notice this.
5.) Update your GPT instructions to include specific guidance when using Actions. This will depend on your specific use case and how your API functions.
For this example, I made sure the GPT knew how to handle specific values from the API calls and what to do in case of an error with an Action.
6.) Test some more! It is a very iterative process to ensure the GPT calls the Action when necessary and includes the correct data when making the API request.
Make sure your Action schemas have clear names and descriptions so that GPT understands their purpose.
Use the GPT builder chat to update your configuration effortlessly by specifying what improvements you want from your GPT.
If you have GPT Plus, you can use this link: https://chat.openai.com/g/g-h1JcfZzg8-hockey-stats-and-analysis-expert to access the GPT. I will demonstrate some the capabilities of this GPT with an example:
You can follow along with this example using: Chat Example Link. I'd say it gets about 80% of what I want. It's usually great, but it does on occasion give incorrect information, so if something looks off you can always ask it to confirm its output. Overall it's exciting to see how close we are to GPTs being able to automate complex tasks with some guidance and a simple question/prompt. Lets get started...
I will be intentionally vague to show you that the GPT is smart enough to decide how to make the API call sing the Actions. By simply asking:
It assumes we are looking for the players based points, which is great! I will then ask:
Again it will make some assumptions, it even includes both regular season and playoffs points. If we want just regular season totals we can ask:
Now this is the cool part, we can drill down into a specific player's stats, over any time frame. Using a prompt like this:
It will use the built-in code interpreter (Python) and create a graph. You can even output this to excel if you wanted. Below is the output of the code interpreter.
Just beware this is not perfect and sometimes it mixes up data, so if something looks off you may need to ask it to double check. It will usually get it on the second try!
As you can see, Actions can be very useful, especially if you have some structured data you want inserted to ChatGPT. This is just an example but it shows ChatGPT is able to handle a fairly complicated API pretty well, I was impressed. I think with some more refactoring and a better instructions this could be a really cool bot!
A few other notes:
Currently the Actions editor in the GPT builder is pretty buggy and lacking some features. It is still in beta so this is expected. OpenAI recently added debugging into the preview window which is very helpful. Also, I recommend you create, edit and save your OpenAPI schemas in a text editor and just paste it into the Actions editor after making changes.
As they continue to improve GPT4 this will only get better! The recent release of the ability to create custom GPTs is impressive. Actions function similarly to plugins, and it's impressive how well custom GPTs perform with personalized instructions.
Links to the GPT, GitHub Repo and all future examples will be on: https://bloodlinealpha.com/
If you have any you have any questions about using or building GPTs send me a LinkedIn message or email me at: bloodlinealpha@gmail.com.
Syntax Sunday
KH