見出し画像

GPTsのActionsでYouTubeの外部APIを利用して動画検索できるようにしてみた

Actionの概要

GPTにカスタムアクションを定義することで、一つ以上のAPIを利用可能になります。これらのアクションはプラグインのように機能し、GPTが外部データと統合したり、実世界との相互作用を行えるようにします。具体的な応用例としては、GPTをデータベース、電子メール、ショッピングアシスタントに接続することが可能です。

https://platform.openai.com/docs/actions

実装

Actionの設定

スキーマは、以下のテンプレートと

{
  "openapi": "3.1.0",
  "info": {
    "title": "Untitled",
    "description": "Your OpenAPI specification",
    "version": "v1.0.0"
  },
  "servers": [
    {
      "url": ""
    }
  ],
  "paths": {},
  "components": {
    "schemas": {}
  }
}

対象のAPIの仕様書ページをChatGPTに渡して作成してもらいました。

ChatGPTに作成してもらったYouTubeの検索APIのスキーマ例

{
  "openapi": "3.1.0",
  "info": {
    "title": "YouTube Data API",
    "description": "Interface for interacting with YouTube to retrieve video, channel, and playlist information.",
    "version": "v3"
  },
  "servers": [
    {
      "url": "https://www.googleapis.com/youtube/v3"
    }
  ],
  "paths": {
    "/search": {
      "get": {
        "description": "Returns a collection of search results that match the query parameters specified in the API request.",
        "operationId": "youtubeSearch",
        "parameters": [
          {
            "name": "part",
            "in": "query",
            "description": "The part parameter specifies a comma-separated list of one or more search resource properties that the API response will include.",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "q",
            "in": "query",
            "description": "The q parameter specifies the query term to search for.",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "maxResults",
            "in": "query",
            "description": "The maxResults parameter specifies the maximum number of items that should be returned in the result set.",
            "required": false,
            "schema": {
              "type": "integer"
            }
          },
          {
            "name": "key",
            "in": "query",
            "description": "Your API key.",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "order",
            "in": "query",
            "description": "Sorts API response. Default: 'date' (newest first). Options: 'rating' (highest first), 'relevance' (most relevant), 'title' (alphabetical), 'videoCount' (most videos), 'viewCount' (most views).",
            "required": false,
            "schema": {
              "type": "string",
              "default": "date"
            }
          },
          {
            "name": "regionCode",
            "in": "query",
            "description": "The regionCode parameter instructs the API to return search results for videos that can be viewed in the specified country. The parameter value is an ISO 3166-1 alpha-2 country code.",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "A successful response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "kind": {
                      "type": "string"
                    },
                    "etag": {
                      "type": "string"
                    },
                    "nextPageToken": {
                      "type": "string"
                    },
                    "prevPageToken": {
                      "type": "string"
                    },
                    "regionCode": {
                      "type": "string"
                    },
                    "pageInfo": {
                      "type": "object",
                      "properties": {
                        "totalResults": {
                          "type": "integer"
                        },
                        "resultsPerPage": {
                          "type": "integer"
                        }
                      }
                    },
                    "items": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "kind": {
                            "type": "string"
                          },
                          "etag": {
                            "type": "string"
                          },
                          "id": {
                            "type": "object",
                            "properties": {
                              "kind": {
                                "type": "string"
                              },
                              "videoId": {
                                "type": "string"
                              },
                              "channelId": {
                                "type": "string"
                              },
                              "playlistId": {
                                "type": "string"
                              }
                            }
                          },
                          "snippet": {
                            "type": "object",
                            "properties": {
                              "publishedAt": {
                                "type": "string",
                                "format": "date-time"
                              },
                              "channelId": {
                                "type": "string"
                              },
                              "title": {
                                "type": "string"
                              },
                              "description": {
                                "type": "string"
                              },
                              "thumbnails": {
                                "type": "object"
                              },
                              "channelTitle": {
                                "type": "string"
                              },
                              "liveBroadcastContent": {
                                "type": "string"
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

作成したGPTs

上記のActoinを使用してプロンプトからYouTubeを検索してくれるGPTsを作成しました。

https://chat.openai.com/g/g-jP0xMx19K-playful-tanuki-video-search

会話例

API KEYについて

API KEYの取得方法

GPTsが説明してくれます。

API KEYの渡し方

APIキーの設定に苦戦しており、現在はプロンプトから直接渡しています。「Use conversation data in your GPT to improve our models」は無効にしていますが、これがセキュリティ上適切かどうかは不明です。
そもそも開発者が自分のAPIキーをユーザーに使用させることはコストが大きく、GPTsの画面上でユーザーが自身のAPIキーを簡単に設定できるようになるといいかもです。





この記事が参加している募集

AIとやってみた

GPTsつくってみた

この記事が気に入ったらサポートをしてみませんか?