Streamline Your Stock Screening with This ChatGPT Plugin

Published On Sat May 13 2023
Streamline Your Stock Screening with This ChatGPT Plugin

Building a Stock Screener Assistant with ChatGPT Plugins

If you're interested in building a stock screener assistant using ChatGPT Plugins, you're in the right place. In this guide, we'll walk you through the steps required to build and deploy a plugin that filters stocks based on user's query using the Financial Modeling Prep API.

Setting Up the API Functionality

To build the plugin, we'll need to create three main files:

  • main.py: this file will serve the API's functionality and other necessary files
  • ai-plugin.json: this is the plugin manifest file that ChatGPT will use to interact with our API
  • openapi.yaml: this OpenAPI specification file will document our API

Let's start by creating the main.py file. In this file, we'll import the necessary packages, initialize our Flask app, and define two API constants for the Financial Modeling Prep API.

Next, we'll define a stock_screener function that takes in the user-defined query parameters and sends a request to Financial Modeling Prep to fetch stock screener data.

After defining the stock_screener function, we'll create an API route for handling incoming user-defined query parameters, calling the stock_screener function we just created, and returning the result in JSON format.

Now that we've got our Financial Modeling Prep API functions in place, we need three more routes to serve the required ChatGPT plugin files, including the ai-plugin.json, the openapi.yaml, and the plugin logo.

We'll create a route for serving the openapi.yaml file, which is used by ChatGPT to understand what the plugin can do and how to interact with the API endpoints. Lastly, we'll create a route for serving the plugin logo.

Creating the Plugin Manifest File

The plugin manifest file, ai-plugin.json, contains information about the plugin that ChatGPT will use to interact with our API. For this plugin, we'll include the following information in our manifest:

  • name: the name of the plugin
  • description: a brief description of what the plugin does
  • version: the version number of the plugin
  • author: the name of the plugin author or organization
  • endpoints: a list of API endpoints that the plugin supports

After creating the ai-plugin.json file, you'll also need to save it in the .well-known folder for ChatGPT to use it.

Documenting the API with OpenAPI

We also need to create an OpenAPI specification that documents our API. The specification defines the /stock_screener endpoint and the parameters that user's can customize, including market cap, price, beta, dividends, and so on. The components section defines the schema for JSON responses.

With these three files created, we're now ready to deploy and test the new ChatGPT plugin. After installing and enabling the new plugin, you can test it out and specify your stock search criteria.

Conclusion

In this guide, we've seen how to build a simple stock screener ChatGPT Plugin using the Financial Modeling Prep API. We covered how to:

  • Set up the API functionality in main.py
  • Create the plugin manifest file, ai-plugin.json
  • Document the API with OpenAPI in openapi.yaml

There are still a few more steps we'd want to include to take a plugin like this to production, such as rate limiting and user authentication, which we'll cover in future tutorials.