How to Make API Requests with Your Custom GPT

A reliable approach to getting structured data from Large Language Models.

Agents are the future of software. They’ll gradually be able to plan and execute more complex actions on your behalf. Imagine a world where agents work together to analyse stocks for you. This is not some futuristic wish, the technology is already here.

There’s little surprise then that OpenAI have officially entered the AI agents arena, and they’re calling their version of agents GPTs, with the developer version being the Assistants API.

While the idea of agents in software is not a new concept, they’ve become popular again because they can now be powered by Large Language Models, and soon they’ll be powered by multi-model models.

Agents can be defined as an having autonomous function. That is, if we give it a task or a list of tasks, it’s able to execute those tasks autonomously.

Instead of being limited to their internal knowledge, we can provide these models with tools to allow them to take actions on the already existing software stack (think of ChatGPT knowing when to browse the internet based on your query).

One of the major reasons why agents are gaining traction again is that these Large Language Models are incredible reasoning engines.

By providing these LLMs with tools, we can create agents (GPTs, Assistants or what have you) that can take actions in the outside world.

Actions allow GPTs to integrate external data or interact with the real-world, connect GPTs to databases, plug them into emails, or make them your shopping assistant.

For example, you could integrate a travel listings database, connect a user’s email inbox, or facilitate e-commerce orders[1].

Let’s build an intuition behind this concept of enabling your custom GPT to have access to the outside world.

We’ll create a dummy API with sheety, and have our GPT access the data behind this API. To save you the hustle of creating all these documents, I’ve gone ahead and created the spreadsheets.

From this spreadsheet, I then created a dummy API that contains two endpoints (tickerData and companyProfile), each endpoint representing one sheet in our spreadsheet.

To keep simple, we’ll only be using the GET request and we’ll not be setting up any authentication.

With our dummy API ready, all that’s left for us to do is to configure our custom GPT so that it can take actions on this API.

So every time we access the API, our custom GPT is taking an action, therefore we need to tell our GPT how it should take these actions.

We do that through an OpenAPI specification which can be written in YAML or JSON[2]. Basically, this specification allows us to an entire API, including:

  • available endpoints and operations on each endpoint
  • operation parameters Input and output for each operation
  • authentication methods
  • contact information, license, terms of use and other information.

Here’s the general structure of our OpenAPI specification:

openapi: 3.0.0
info:
  title: "___________"
  description: "___________"
  version: "___________"
servers:
  - url: '___________'
paths:
  /exampleEndpoint:
    get:
      summary: "_____________"
      description: "_____________"
      operationId: "_________"
      responses:
        '200':
          description: "Description of the 200 response"
          content:
            application/json:
              schema:
                type: object
                properties:
                  exampleProperty:
                    type: string
                    description: "Description of exampleProperty"
              example:
                exampleProperty: "Example Value"
        # Additional response codes as needed
  • The info section contains basic information about our API.
  • The servers section is where we list our API’s base URL(s)
  • Under paths, we define the endpoints (in this case, /exampleEndpoint) and their operations (like GET, POST, DELETE, PATCH)
  • The responses section under each operation details what responses can be expected, including the structure of the response data (in this case, for a successful 200 response).

Great. We’re almost there. Under the Configure tab of your custom GPT’s editor mode, disable all capabilities (for testing purposes) and create an Action.

Copy the complete specification below, and you’ll notice how the available methods and endpoints are identified 😊.

openapi: 3.0.0
info:
  title: Stock Market Data API
  description: API to access current stock market data.
  version: "1.0.0"
servers:
  - url: 'https://api.sheety.co/73568cdfddad5c48ecfd7649c3c7b37c/dummyStockApi'
paths:
  /tickerData:
    get:
      summary: Retrieve stock data
      description: Fetches an array of current stock data entries.
      operationId: getStockData
      responses:
        '200':
          description: An array of stock data was retrieved successfully.
          content:
            application/json:
              schema:
                type: object
                properties:
                  stocks:
                    type: array
                    items:
                      type: object
                      properties:
                        ticker:
                          type: string
                          description: The unique symbol used to identify the stock.
                        company:
                          type: string
                          description: The name of the company.
                        price:
                          type: number
                          format: float
                          description: The current stock price.
                        change:
                          type: number
                          format: float
                          description: The change in the stock's price since the previous closing price.
                        percentChange:
                          type: string
                          description: The percentage change in the stock's price since the previous closing price.
                        volume:
                          type: integer
                          format: int64
                          description: The number of shares traded during the current or last trading day.
                        timeStamp:
                          type: string
                          format: date-time
                          description: The date and time when the stock data was last updated.
              example:
                stocks:
                  - ticker: "AAPL"
                    company: "Apple Inc."
                    price: 150
                    change: -1.50
                    percentChange: "-0.99%"
                    volume: 12000000
                    timeStamp: "2023-11-28T15:45:00"
                  - ticker: "GOOGL"
                    company: "Alphabet Inc."
                    price: 2750
                    change: 10.00
                    percentChange: "0.36%"
                    volume: 1500000
                    timeStamp: "2023-11-28T15:45:00"
  /companyProfile:
    get:
      summary: Retrieve company profile data
      description: Fetches an array of company profile data entries.
      operationId: getCompanyProfileData
      responses:
        '200':
          description: An array of company profile data was retrieved successfully.
          content:
            application/json:
              schema:
                type: object
                properties:
                  companyProfiles:
                    type: array
                    items:
                      type: object
                      properties:
                        symbol:
                          type: string
                          description: The stock symbol of the company.
                        marketCap:
                          type: string
                          description: The market capitalization of the company.
                        companyName:
                          type: string
                          description: The full name of the company.
                        exchange:
                          type: string
                          description: The stock exchange where the company's stock is traded.
                        industry:
                          type: string
                          description: The primary industry in which the company operates.
                        website:
                          type: string
                          description: The official website of the company.
                        ceo:
                          type: string
                          description: The CEO of the company.
              example:
                companyProfiles:
                  - symbol: "AAPL"
                    marketCap: "$2.2 Trillion"
                    companyName: "Apple Inc."
                    exchange: "NASDAQ"
                    industry: "Consumer Electronics"
                    website: "https://www.apple.com"
                    ceo: "Tim Cook"
                  - symbol: "GOOGL"
                    marketCap: "$1.5 Trillion"
                    companyName: "Alphabet Inc."
                    exchange: "NASDAQ"
                    industry: "Internet Services"
                    website: "https://www.google.com"
                    ceo: "Sundar Pichai"

Thanks for your time. I hope this article was a good prep for the GPT marketplace. Go forth and build amazing GPTs. Can’t wait to see what you build.

References

[1] What is OpenAPI

[2] Actions in GPTs

[3] A conversation with OpenAI CEO Sam Altman