天気予報APIと連携するGPTsの作成手順について解説します

ChatGPT

今回はGPTs天気予報の機能を追加する方法について分かりやすく解説します。APIとしてはOpenWeatherMapを使用し、必要なAPIキーの取得方法、週間天気予報を教えてくれるユニークなお天気アシスタントGPTsを実現する方法もご紹介します。このステップバイステップのガイドを通じて、技術的な知識が初心者レベルの方でも、GPTsを作成できるようにします

スポンサーリンク

OpenWeatherMapからのAPIキーの取得方法

OpenWeatherMapのデータにアクセスするためには、個別のAPIキーが必要です。
APIキーとは、サービスを使用する際に、ユーザーを識別し認証するための一意のコードです。
このキーを取得することで、天気予報、歴史的気候データなど、OpenWeatherMapが提供する様々な情報にアクセスできるようになります。

取得方法については以下をご覧ください。

ベースのGPTsの作成方法

天気情報を絵文字などを含めて伝えてくれる「週間天気を教えてくれるお天気お姉さん」のGPTsを作成してみます。

GPTsの使い方からご覧になりたい方は以下をご覧ください

今回は以下の様に作成しました。

Name:週間天気を教えてくれるお天気お姉さん

Description:市町村を伝えれば5日間先の天気と気温、風速を絵文字を使って教えてくれます

Instructions

スタートと言われたらユーザーに住んでいる市町村と聞いてください。
OpenWeatherMapのAPIを使用し、出力例に従って1日ごとの天気と気温、風速を教えてください。またその日ごとにワンポイントを添えてください。
絵文字などを使って親しみのある文章を作成してください。

出力例
1月21日(日): 🌧️曇り空で、時々小雨が降る予報です。最高気温は約10.8°C、最低気温は約6.6°C。風速は約2.2〜7.3m/sとやや強めです。
ワンポイントアドバイス:折り畳み傘を持って出かけると安心ですね。
1月22日(月): ☁️曇り空が続き、午前中には小雨が降る見込みです。最高気温は約8.2°C、最低気温は約0.7°C。風速は約2.2〜9.9m/sで、冷たい風が吹くかもしれません。
ワンポイントアドバイス:温かい格好でお出かけください。

Conversation starters:スタート

Capabilities:すべてのチェックを外します。Web Browsingを外す理由は検索で天気情報を取得させないようにするためです。

Actionsについては次項で説明します。

Actionsの作成方法

GPTsにActionsを追加するには、専用コードの開発が必要になります。ここで「BetterActionsGPT」という強力なツールが役立ちます。

BetterActionsGPTとは

BetterActionsGPTは、OpenAPI 3.0仕様をYAMLやJSON形式で作成するGPTsです。この GPT はAPIの仕様を書いたURLを貼るだけで、OpenAPI仕様のSchemaを出力します。

このツールを使用して、Actionsを作成していきます。

最初の一歩:Schema作成

OpenWeatherMapの仕様書については「5 Day / 3 Hour Forecast」を使います。仕様書のサイトはこちらになります。

BetterActionsGPTに以下の指示をします。

Make JSON to get translation: https://openweathermap.org/forecast5

得られたJSONコードは以下になります。yaml形式の場合は再度JSONを指定してください。

Schema

{
“openapi”: “3.0.0”,
“info”: {
“title”: “OpenWeatherMap Forecast API”,
“description”: “This API provides a 5-day weather forecast with data every 3 hours by geographic coordinates in JSON format.”,
“version”: “1.0.0”
},
“servers”: [
{
“url”: “http://api.openweathermap.org/data/2.5”,
“description”: “OpenWeatherMap API server”
}
],
“paths”: {
“/forecast”: {
“get”: {
“operationId”: “getWeatherForecast”,
“summary”: “Get 5-day weather forecast with 3-hour step by geographic coordinates.”,
“parameters”: [
{
“name”: “lat”,
“in”: “query”,
“required”: true,
“description”: “Latitude of the location”,
“schema”: {
“type”: “number”,
“format”: “double”
}
},
{
“name”: “lon”,
“in”: “query”,
“required”: true,
“description”: “Longitude of the location”,
“schema”: {
“type”: “number”,
“format”: “double”
}
},
{
“name”: “appid”,
“in”: “query”,
“required”: true,
“description”: “Your unique API key”,
“schema”: {
“type”: “string”
}
},
{
“name”: “units”,
“in”: “query”,
“required”: false,
“description”: “Units of measurement. Standard, metric, and imperial units are available.”,
“schema”: {
“type”: “string”,
“default”: “standard”,
“enum”: [
“standard”,
“metric”,
“imperial”
]
}
},
{
“name”: “cnt”,
“in”: “query”,
“required”: false,
“description”: “Number of timestamps, which will be returned in the API response.”,
“schema”: {
“type”: “integer”
}
},
{
“name”: “lang”,
“in”: “query”,
“required”: false,
“description”: “Language in which you want to get the output.”,
“schema”: {
“type”: “string”
}
}
],
“responses”: {
“200”: {
“description”: “Successful response with weather forecast data.”,
“content”: {
“application/json”: {
“schema”: {
“type”: “object”,
“properties”: {
“cod”: {
“type”: “string”,
“description”: “Internal parameter”
},
“message”: {
“type”: “number”,
“description”: “Internal parameter”
},
“cnt”: {
“type”: “integer”,
“description”: “Number of timestamps returned in the API response”
},
“list”: {
“type”: “array”,
“items”: {
“$ref”: “#/components/schemas/ForecastTimestamp”
}
},
“city”: {
“$ref”: “#/components/schemas/City”
}
}
}
}
}
},
“400”: {
“description”: “Bad request”
},
“401”: {
“description”: “Unauthorized”
},
“404”: {
“description”: “Not Found”
}
}
}
}
},
“components”: {
“schemas”: {
“ForecastTimestamp”: {
“type”: “object”,
“properties”: {
“dt”: {
“type”: “integer”,
“description”: “Time of data forecasted, unix, UTC”
},
“main”: {
“$ref”: “#/components/schemas/Main”
},
“weather”: {
“type”: “array”,
“items”: {
“$ref”: “#/components/schemas/Weather”
}
},
“clouds”: {
“$ref”: “#/components/schemas/Clouds”
},
“wind”: {
“$ref”: “#/components/schemas/Wind”
},
“visibility”: {
“type”: “integer”,
“description”: “Average visibility, metres”
},
“pop”: {
“type”: “number”,
“description”: “Probability of precipitation”
},
“rain”: {
“$ref”: “#/components/schemas/Rain”
},
“snow”: {
“$ref”: “#/components/schemas/Snow”
},
“sys”: {
“$ref”: “#/components/schemas/Sys”
},
“dt_txt”: {
“type”: “string”,
“description”: “Time of data forecasted, ISO, UTC”
}
}
},
“Main”: {
“type”: “object”,
“properties”: {
“temp”: {
“type”: “number”,
“description”: “Temperature”
},
“feels_like”: {
“type”: “number”,
“description”: “This temperature parameter accounts for the human perception of weather”
},
“temp_min”: {
“type”: “number”,
“description”: “Minimum temperature at the moment of calculation”
},
“temp_max”: {
“type”: “number”,
“description”: “Maximum temperature at the moment of calculation”
},
“pressure”: {
“type”: “integer”,
“description”: “Atmospheric pressure on the sea level by default, hPa”
},
“humidity”: {
“type”: “integer”,
“description”: “Humidity, %”
}
}
},
“Weather”: {
“type”: “object”,
“properties”: {
“id”: {
“type”: “integer”,
“description”: “Weather condition id”
},
“main”: {
“type”: “string”,
“description”: “Group of weather parameters (Rain, Snow, Clouds, etc.)”
},
“description”: {
“type”: “string”,
“description”: “Weather condition within the group”
},
“icon”: {
“type”: “string”,
“description”: “Weather icon id”
}
}
},
“Clouds”: {
“type”: “object”,
“properties”: {
“all”: {
“type”: “integer”,
“description”: “Cloudiness, %”
}
}
},
“Wind”: {
“type”: “object”,
“properties”: {
“speed”: {
“type”: “number”,
“description”: “Wind speed. Unit Default: meter/sec”
},
“deg”: {
“type”: “integer”,
“description”: “Wind direction, degrees (meteorological)”
}
}
},
“Rain”: {
“type”: “object”,
“properties”: {
“3h”: {
“type”: “number”,
“description”: “Rain volume for the last 3 hours”
}
}
},
“Snow”: {
“type”: “object”,
“properties”: {
“3h”: {
“type”: “number”,
“description”: “Snow volume for the last 3 hours”
}
}
},
“Sys”: {
“type”: “object”,
“properties”: {
“pod”: {
“type”: “string”,
“description”: “Part of the day (n – night, d – day)”
}
}
},
“City”: {
“type”: “object”,
“properties”: {
“id”: {
“type”: “integer”,
“description”: “City ID”
},
“name”: {
“type”: “string”,
“description”: “City name”
},
“coord”: {
“type”: “object”,
“properties”: {
“lat”: {
“type”: “number”,
“description”: “Geo location, latitude”
},
“lon”: {
“type”: “number”,
“description”: “Geo location, longitude”
}
}
},
“country”: {
“type”: “string”,
“description”: “Country code (GB, JP, etc.)”
},
“population”: {
“type”: “integer”,
“description”: “City population”
},
“timezone”: {
“type”: “integer”,
“description”: “Shift in seconds from UTC”
},
“sunrise”: {
“type”: “integer”,
“description”: “Sunrise time, Unix, UTC”
},
“sunset”: {
“type”: “integer”,
“description”: “Sunset time, Unix, UTC”
}
}
}
}
}
}

しかしこれをActionsのShcemaに貼り付けるだけではうまくいきません。

Schema下に「

None of the provided servers is under the root origin https://api.openweathermap.org Server URL http://api.openweathermap.org/data/2.5 is not under the root origin https://api.openweathermap.org; ignoring it

」と出ます。

これはURLが提供されたサーバーURLがルートオリジン https://api.openweathermap.org と一致しないために起こっているものです。

これはservers -> /urlの部分をhttpsに変更することで解消できます

これでschema記述の際のエラーはなくなるはずです。

認証に必要な情報を入れる

Schemaが完成したので右のプレビュー画面で以下の指示をします。例えばこんな感じです。

東京の天気を教えて

ただ以下の様に通信エラーが出ます。

>をクリックすると詳細な結果を見ることができます

このエラーをBetterActionsGPTに貼り付けると以下の回答が返ってきます。

2つ目の記述からクエリパラメーター‘appid’にAPI Keyを入れればよいことがわかります。

先ほどのコードから’appid’を探した後、OpenWeatherMapから取得したAPIキーを「Your unique API key」に入れてください。この際、ダブルクォーテーション(””)はそのまま残してください。

これでエラーも出なくなると思います。

最終版のschemaは以下になります。Your unique APIに取得したAPIキーを入れてください

Schema

{
“openapi”: “3.0.0”,
“info”: {
“title”: “OpenWeatherMap Forecast API”,
“description”: “This API provides a 5-day weather forecast with data every 3 hours by geographic coordinates in JSON format.”,
“version”: “1.0.0”
},
“servers”: [
{
“url”: “https://api.openweathermap.org/data/2.5”,
“description”: “OpenWeatherMap API server”
}
],
“paths”: {
“/forecast”: {
“get”: {
“operationId”: “getWeatherForecast”,
“summary”: “Get 5-day weather forecast with 3-hour step by geographic coordinates.”,
“parameters”: [
{
“name”: “lat”,
“in”: “query”,
“required”: true,
“description”: “Latitude of the location”,
“schema”: {
“type”: “number”,
“format”: “double”
}
},
{
“name”: “lon”,
“in”: “query”,
“required”: true,
“description”: “Longitude of the location”,
“schema”: {
“type”: “number”,
“format”: “double”
}
},
{
“name”: “appid”,
“in”: “query”,
“required”: true,
“description”: “Your unique API key”,
“schema”: {
“type”: “string”
}
},
{
“name”: “units”,
“in”: “query”,
“required”: false,
“description”: “Units of measurement. Standard, metric, and imperial units are available.”,
“schema”: {
“type”: “string”,
“default”: “standard”,
“enum”: [
“standard”,
“metric”,
“imperial”
]
}
},
{
“name”: “cnt”,
“in”: “query”,
“required”: false,
“description”: “Number of timestamps, which will be returned in the API response.”,
“schema”: {
“type”: “integer”
}
},
{
“name”: “lang”,
“in”: “query”,
“required”: false,
“description”: “Language in which you want to get the output.”,
“schema”: {
“type”: “string”
}
}
],
“responses”: {
“200”: {
“description”: “Successful response with weather forecast data.”,
“content”: {
“application/json”: {
“schema”: {
“type”: “object”,
“properties”: {
“cod”: {
“type”: “string”,
“description”: “Internal parameter”
},
“message”: {
“type”: “number”,
“description”: “Internal parameter”
},
“cnt”: {
“type”: “integer”,
“description”: “Number of timestamps returned in the API response”
},
“list”: {
“type”: “array”,
“items”: {
“$ref”: “#/components/schemas/ForecastTimestamp”
}
},
“city”: {
“$ref”: “#/components/schemas/City”
}
}
}
}
}
},
“400”: {
“description”: “Bad request”
},
“401”: {
“description”: “Unauthorized”
},
“404”: {
“description”: “Not Found”
}
}
}
}
},
“components”: {
“schemas”: {
“ForecastTimestamp”: {
“type”: “object”,
“properties”: {
“dt”: {
“type”: “integer”,
“description”: “Time of data forecasted, unix, UTC”
},
“main”: {
“$ref”: “#/components/schemas/Main”
},
“weather”: {
“type”: “array”,
“items”: {
“$ref”: “#/components/schemas/Weather”
}
},
“clouds”: {
“$ref”: “#/components/schemas/Clouds”
},
“wind”: {
“$ref”: “#/components/schemas/Wind”
},
“visibility”: {
“type”: “integer”,
“description”: “Average visibility, metres”
},
“pop”: {
“type”: “number”,
“description”: “Probability of precipitation”
},
“rain”: {
“$ref”: “#/components/schemas/Rain”
},
“snow”: {
“$ref”: “#/components/schemas/Snow”
},
“sys”: {
“$ref”: “#/components/schemas/Sys”
},
“dt_txt”: {
“type”: “string”,
“description”: “Time of data forecasted, ISO, UTC”
}
}
},
“Main”: {
“type”: “object”,
“properties”: {
“temp”: {
“type”: “number”,
“description”: “Temperature”
},
“feels_like”: {
“type”: “number”,
“description”: “This temperature parameter accounts for the human perception of weather”
},
“temp_min”: {
“type”: “number”,
“description”: “Minimum temperature at the moment of calculation”
},
“temp_max”: {
“type”: “number”,
“description”: “Maximum temperature at the moment of calculation”
},
“pressure”: {
“type”: “integer”,
“description”: “Atmospheric pressure on the sea level by default, hPa”
},
“humidity”: {
“type”: “integer”,
“description”: “Humidity, %”
}
}
},
“Weather”: {
“type”: “object”,
“properties”: {
“id”: {
“type”: “integer”,
“description”: “Weather condition id”
},
“main”: {
“type”: “string”,
“description”: “Group of weather parameters (Rain, Snow, Clouds, etc.)”
},
“description”: {
“type”: “string”,
“description”: “Weather condition within the group”
},
“icon”: {
“type”: “string”,
“description”: “Weather icon id”
}
}
},
“Clouds”: {
“type”: “object”,
“properties”: {
“all”: {
“type”: “integer”,
“description”: “Cloudiness, %”
}
}
},
“Wind”: {
“type”: “object”,
“properties”: {
“speed”: {
“type”: “number”,
“description”: “Wind speed. Unit Default: meter/sec”
},
“deg”: {
“type”: “integer”,
“description”: “Wind direction, degrees (meteorological)”
}
}
},
“Rain”: {
“type”: “object”,
“properties”: {
“3h”: {
“type”: “number”,
“description”: “Rain volume for the last 3 hours”
}
}
},
“Snow”: {
“type”: “object”,
“properties”: {
“3h”: {
“type”: “number”,
“description”: “Snow volume for the last 3 hours”
}
}
},
“Sys”: {
“type”: “object”,
“properties”: {
“pod”: {
“type”: “string”,
“description”: “Part of the day (n – night, d – day)”
}
}
},
“City”: {
“type”: “object”,
“properties”: {
“id”: {
“type”: “integer”,
“description”: “City ID”
},
“name”: {
“type”: “string”,
“description”: “City name”
},
“coord”: {
“type”: “object”,
“properties”: {
“lat”: {
“type”: “number”,
“description”: “Geo location, latitude”
},
“lon”: {
“type”: “number”,
“description”: “Geo location, longitude”
}
}
},
“country”: {
“type”: “string”,
“description”: “Country code (GB, JP, etc.)”
},
“population”: {
“type”: “integer”,
“description”: “City population”
},
“timezone”: {
“type”: “integer”,
“description”: “Shift in seconds from UTC”
},
“sunrise”: {
“type”: “integer”,
“description”: “Sunrise time, Unix, UTC”
},
“sunset”: {
“type”: “integer”,
“description”: “Sunset time, Unix, UTC”
}
}
}
}
}
}

プレビューの再実行と保存

再度プレビュー画面にて実行します。以下の様に紫の✓が出ればOKです。

保存するには、以下のステップに従ってください:

1 画面右上にある「保存」ボタン(表示は中国語ですが、「保存」を意味します)をクリックします。
2 公開先を指定し、「確認」ボタンをクリックして設定を保存します。
セキュリティに関する重要な注意:

このコードにはAPIキーなどの個人情報が含まれています。リンクを知っている人だけがアクセスできる設定や完全に公開する設定では、コードの内容が漏洩する可能性があります。
そのため、公開設定は個人のみに限定することを強く推奨します。これにより、あなたの機密情報が保護されます。

まとめ

この記事では、APIキーの取得方法から、それを使用して「週間天気を教えてくれるお天気お姉さん」というGPTsを構築するプロセスまでを詳しく解説しました。この過程を通じて、APIの基本的な仕組み、GPTsのカスタマイズ、そしてBetterActionsGPTを使用したActionsの作成方法についての理解が深まれば幸いです。

コメント

タイトルとURLをコピーしました