/home/by-natures/dev*

データ界隈で働くエンジニアとしての技術的なメモと、たまに普通の日記。

2023/05/20 ChatGPT で CSV から様々なモデルの生成

blog.gopenai.com

CSV ファイルから ChatGPT で概念モデルを生成するブログ記事を見つけました。私も試してみて、ついでにデータボルトモデリングにも挑戦しました。

複雑なことをさせようとしたので試行錯誤しましたが面白いですね。プロンプトエンジニアリングという言葉が出てくるのも納得です。(余談ですが、個人的にはあまり「エンジニアリング」ぽくはない気がしていて、「ヒューリスティック(発見法)」が近いのではということを仰る方を見かけて、こちらの方がしっくりきます)。

同じような聞き方でも結構違うモデルが返ってくることがあって試行錯誤しました。ChatGPT の癖になれる必要がありそうです。地味な利点としてモデルの可視化に PlantUML のコードを出してもらっているのですが、実際に書くとなると面倒なのでとてもありがたいです。

もくじ

データ

HR Case Study | Kaggle

上記ブログ記事に倣い、こちらの Kaggle のデータを利用しました。

概念データモデル

冒頭のブログ記事に倣い、概念データモデルに挑戦してみます。

[Request] Based on this sample CSV data, please create a conceptual data model:

Name,Gender,Department,Annual Salary,Location,Rating,Distance to Office,Age,Tenure in Company
Aarti Panchal,Female,CEO,10000000,Mumbai,Very Good,25,31,10.4
...
[ChatGPT]
Table 1: Employee
  EmployeeID (Primary Key)
  Name
  Gender
  Age
  TenureInCompany
  DistanceToOffice
  Rating
  DepartmentID (Foreign Key)
  LocationID (Foreign Key)
  SalaryID (Foreign Key)

Table 2: Department
  DepartmentID (Primary Key)
  DepartmentName

Table 3: Location
  LocationID (Primary Key)
  LocationName

Table 4: Salary
  SalaryID (Primary Key)
  AnnualSalary

Now, let's define the relationships between these tables:

Employee to Department: One-to-Many from Department to Employee. A department can have many employees, but an employee belongs to exactly one department.
Employee to Location: One-to-Many from Location to Employee. A location can have many employees, but an employee works at exactly one location.
Employee to Salary: One-to-One. Each employee has one salary, and each salary is associated with one employee.

だいぶそれらしいモデルが返ってきました。Employee to Salary が 1対1なのが気になるので、そこだけ修正します。

[Request] Please create a plantuml code for a ER diagram with the following correction:
- The relationship between Employee and Salary should be One-to-Many.

ChatGPT が PlantUML コードを返してくれるので、可視化します:

ChatGPTによるER図(可視化は PlantUML Web Server にて)

型情報まで付けてくれました。いくつか気になる点はありますし、同じ文言で問い合わせてもかなり違うモデルが返ってくることも多々ありました。まだ ChatGPT を使うのには慣れが必要そうですが、CSV からここまで簡単に構造化できるなら十分そうです。

Data Vault Modeling

データボルトモデリングも挑戦してみました。試行錯誤しましたが、次の聞き方でうまくいきました:

[Request] Create a data model based on data vault modeling with the following sample CSV data:

Name,Gender,Department,Annual Salary,Location,Rating,Distance to Office,Age,Tenure in Company
Aarti Panchal,Female,CEO,10000000,Mumbai,Very Good,25,31,10.4
...
[ChatGPT]
Hubs:
  Hub_Employee: Attributes: Employee Key (hash key), Name
  Hub_Department: Attributes: Department Key (hash key), Department
  Hub_Location: Attributes: Location Key (hash key), Location

Links:
  Link_Employee_Department: Attributes: Link Key (hash key), Employee Key (hash key), Department Key (hash key)
  Link_Employee_Location: Attributes: Link Key (hash key), Employee Key (hash key), Location Key (hash key)

Satellites:
  Sat_Employee: Attributes: Employee Key (hash key), Gender, Age, Tenure in Company, Record Source
  Sat_Department: Attributes: Department Key (hash key), Annual Salary, Rating, Record Source
  Sat_Location: Attributes: Location Key (hash key), Distance to Office, Record Source

ついでに可視化します:

[Request] Create a plantuml code for a ER diagram for the data model above.

ChatGPTによるデータボルトモデルの可視化

データボルトらしいモデルが作れました。3つの Hub が Link を通じて連携されており、詳しい属性は Satellite で表現されていることが分かります。

Happy data modeling!