LoginSignup
5
4

GPTs の Actions を使って外部APIを使用する

Last updated at Posted at 2023-12-02

デジタルキューブグループの Advent Calendar 2023 2日目です。

ChatGPT では GPTs の Actions を使うことで、チャットベースで外部APIを気軽に利用することができます。
触ってみないことには、便利さが実感できないかと思ったので、試しに PunkAPI という、みんな大好き Brewdog のビール情報を取得できる API を Actions に登録してみました。

外部のAPIを呼び出すには GPTs の Actions に Scheme を登録してあげる必要があります。
Example があるので、それに合わせて作ってみました。

{
  "openapi": "3.1.0",
  "info": {
    "title": "Get Beers data",
    "description": "PUNK API.",
    "version": "v1.0.0"
  },
  "servers": [
    {
      "url": "https://api.punkapi.com/v2"
    }
  ],
  "paths": {
    "/beers": {
      "get": {
        "description": "Get beers",
        "operationId": "GetBeers",
        "parameters": [
        ],
        "deprecated": false
      }
    }
  },
  "components": {
    "schemas": {}
  }
}

これだけでも、ビールの情報を取得できるようになりました。
スクリーンショット 2023-12-01 18.04.11.png

ただ、pathsparameters を定義してないので、細かい指示ができないですね。
PunkAPI の API 仕様書を読んで JSON 組み上げればいいんだよなと思ったんですが、結構パラメータ多くて面倒ですね。

そんな時こそ ChatGPT の出番です。
仕様を伝えるだけで、サクッと作ってくれました。
GPT4 が最初に作ってくれた JSON がこんな感じ

{
  "name": "PunkAPI",
  "version": "v2",
  "rootEndpoint": "https://api.punkapi.com/v2/",
  "authentication": {
    "type": "None",
    "rateLimit": {
      "limit": 3600,
      "timeWindow": "1 hour"
    }
  },
  "endpoints": [
    {
      "name": "GetBeers",
      "path": "/beers",
      "method": "GET",
      "parameters": [
        {"name": "abv_gt", "type": "number", "description": "Returns all beers with ABV greater than the supplied number"},

フォーマットが GPTs Actions の Scheme に渡す形式ではないですね。
そこを指摘して修正してもらったら、そのまま使える JSON を作ってくれました。

{
  "openapi": "3.1.0",
  "info": {
    "title": "Punk API",
    "description": "Access a wide range of beer data from the Punk API, including beer characteristics, brewing methods, and pairing suggestions.",
    "version": "v2.0.0"
  },
  "servers": [
    {
      "url": "https://api.punkapi.com/v2"
    }
  ],
  "paths": {
    "/beers": {
      "get": {
        "description": "Retrieve a list of beers with optional filtering parameters.",
        "operationId": "GetBeers",
        "parameters": [
          {
            "name": "abv_gt",
            "in": "query",
            "description": "Returns all beers with ABV greater than the supplied number",
            "required": false,
            "schema": {
              "type": "number"
            }
          },
          {
            "name": "abv_lt",
            "in": "query",
            "description": "Returns all beers with ABV less than the supplied number",
            "required": false,
            "schema": {
              "type": "number"
            }
          },
          // Additional parameters go here...
        ],
        "deprecated": false
      }
    },
    "/beers/{id}": {
      "get": {
        "description": "Retrieve data for a single beer using its ID.",
        "operationId": "GetSingleBeer",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The ID of the beer to retrieve",
            "required": true,
            "schema": {
              "type": "integer"
            }
          }
        ],
        "deprecated": false
      }
    },
    "/beers/random": {
      "get": {
        "description": "Retrieve a random beer.",
        "operationId": "GetRandomBeer",
        "parameters": [],
        "deprecated": false
      }
    }
  },
  "components": {
    "schemas": {}
  }
}

やばい、GPT4 有能すぎる。その時のチャット履歴。
Create GPTs PunkAPI JSON

ほんの5分ほどで PunkAPI との連携ができるようになりました。恐るべし、ChatGPT
スクリーンショット 2023-12-02 11.49.56.png

ちゃんと適切な種類のパラメータ渡して API を呼んでる。
スクリーンショット 2023-12-02 12.08.45.png

僕が作った GPTs は以下のURLから使用できます
PunkAPI

ロゴも DALL E が作ってくれた。
DALL·E 2023-12-02 10.51.24 - Create a logo for 'Punk API' featuring a stylized, edgy pirate character. The pirate should have a rugged look, with a bandana, an eye patch, and a sc.png

現場からは以上です。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4