Closed9

GPTs触ってく

ななねななね

筋トレ&ダイエットのサポートをしてくれるパーソナルトレーナーをGPTsで作る

ななねななね

トレーニング記録をDBに収納する予定なので、そのDBにアクセスできるようにAPIを作っていく。
Node.js + Typescriptで構築する。
環境はさくらVPSのDocker
nodeは21-alpine3.17
SQLiteを使う予定

ここはLambda+DynamoDBの方がはやいと思うけど、API環境が既に構築済みだったので。

ななねななね
{
  "openapi": "3.1.0",
  "info": {
    "title": "Get training data",
    "description": "Retrieves current training data",
    "version": "v1.0.0"
  },
  "servers": [
    {
      "url": "http://hoge:8888"
    }
  ],
  "paths": {
    "/get_training": {
      "post": {
        "description": "Get training data",
        "operationId": "GetTraining",
        "parameters": [
          {
            "name": "shoko",
            "in": "query",
            "description": "Get training data",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "deprecated": false,
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/TrainingData"
                  }
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "message": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "TrainingData": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "date": {
            "type": "string",
            "format": "date-time"
          },
          "exercise": {
            "type": "string"
          },
          "set_number": {
            "type": "integer"
          },
          "weight": {
            "type": "number"
          },
          "reps": {
            "type": "integer"
          },
          "interval": {
            "type": "integer"
          },
          "sec": {
            "type": "integer"
          }
        },
        "required": ["id", "date", "exercise", "set_number", "weight", "reps", "interval", "sec"]
      }
    }
  }
}

とすると
None of the provided servers is under the root origin https://hoge
Server URL http://hoge:8888 is not under the root origin https://hoge; ignoring it
https化してこい・・と・・・

ななねななね

https化して無事にアクセスできるようになったと思ったら、ResponseTooLargeError

ななねななね

5件に減らすとエラー出ずにちゃんと取得できるので、データ周りについて考え直す必要がある

ななねななね

結局月ごとに出力するようにしてGPT側で複数回叩いてもらうことにした

ななねななね

長いし美しくないけれどとりあえずスキーマは以下の感じでGPTに書いてもらった

{
  "openapi": "3.1.0",
  "info": {
    "title": "Get training data",
    "description": "Retrieves current training data",
    "version": "v1.0.0"
  },
  "servers": [
    {
      "url": "https://api.hoge.com"
    }
  ],
  "paths": {
    "/get_training": {
      "post": {
        "description": "Get training data",
        "operationId": "GetTraining",
        "requestBody": {
          "description": "Request parameters for getting training data",
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "month": {
                    "type": "string",
                    "description": "Month for which to retrieve training data"
                  }
                },
                "required": ["month"]
              }
            }
          }
        },
        "deprecated": false,
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/TrainingData"
                  }
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "message": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/get_condition": {
      "post": {
        "description": "Get condition data",
        "operationId": "GetCondition",
        "requestBody": {
          "description": "Request parameters for getting training data",
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "month": {
                    "type": "string",
                    "description": "Month for which to retrieve training data"
                  }
                },
                "required": ["month"]
              }
            }
          }
        },
        "deprecated": false,
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/ConditionData"
                  }
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "message": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "TrainingData": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "date": {
            "type": "string",
            "format": "date-time"
          },
          "exercise": {
            "type": "string"
          },
          "set_number": {
            "type": "integer"
          },
          "weight": {
            "type": "number"
          },
          "reps": {
            "type": "integer"
          },
          "interval": {
            "type": "integer"
          },
          "sec": {
            "type": "integer"
          }
        },
        "required": ["id", "date", "exercise", "set_number", "weight", "reps", "interval", "sec"]
      },
      "ConditionData": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "date": {
            "type": "string",
            "format": "date-time"
          },
          "weight": {
            "type": "number"
          },
          "body_fat_percentage": {
            "type": "number"
          },
          "bedtime": {
            "type": "string"
          },
          "wake_up_time": {
            "type": "string"
          },
          "sleep_quality": {
            "type": "integer"
          },
          "physical_condition": {
            "type": "integer"
          },
          "motivation_level": {
            "type": "integer"
          },
          "protein_intake": {
            "type": "integer"
          }
        },
        "required": ["id", "date", "weight", "body_fat_percentage", "bedtime", "wake_up_time", "sleep_quality", "physical_condition", "motivation_level", "protein_intake"]
      }
    }
  }
}
ななねななね

GPTにget_hogeなのにpostとはこれいかにと怒られた。
おっしゃる通りです

ななねななね

これで今日のコンディションと、トレーニング内容をGPTが取得しにいってくれるようになった。
API側はNode.jsを使ってSQLiteを読み込みなので面白くもないので詳細はとりあえず後回し

このスクラップは2024/01/07にクローズされました