G-gen の堂原です。 ChatGPT (GPT-4) を使ってほぼ頭を使うことなく、Cloud Functions を作成してみたので、その過程を紹介します。 はじめに 事前準備 事前準備も ChatGPT を使用 Cloud Functions コード生成 一発で Python コード生成 エラー修正も可能 動作確認 さいごに はじめに 今や知らない人はいないであろう、OpenAI 社が公開している AI チャットサービスである ChatGPT。対話能力もさることながら、コード生成能力の高さも注目されています。 G-gen では ChatGPT の利用が会社で補助されていることもあり、早速使ってみました。 prtimes.jp 当記事では ChatGPT を使って、以下のような仕様の Cloud Functions を作成、デプロイしてみました。 言語は Python。 Google Cloud Storage (GCS) に CSV ファイルがアップロードされたことをトリガーに実行される。 GCS から、アップロード CSV ファイルを読み取る。 読み取ったデータは BigQuery のテーブルに保存する。 事前準備 今回、事前準備として以下のことを実施しました。 サンプルの CSV ファイル生成 GCS バケット作成 BigQuery データセット作成 BigQuery テーブル作成 生成されたコード的には不要でしたが今回は事前に用意しました。 事前準備も ChatGPT を使用 サンプルとなる CSV ファイルと、それに対応するテーブル作成についても ChatGPT におまかせしました。 [ 質問文 ] Please create a sample CSV file. すると以下のような CSV を出力してくれました。 id,name,category,price 1,Wireless Mouse,Electronics,25.99 2,Bluetooth Keyboard,Electronics,49.99 3,32GB USB Flash Drive,Electronics,15.99 4,Portable Charger,Electronics,29.99 5,Water Bottle,Sports,12.99 6,Yoga Mat,Sports,21.99 7,Dumbbell Set,Sports,59.99 8,Running Shoes,Sports,89.99 続けて、上記 CSV に対応する BigQuery テーブルを作成してもらいます。 [ 質問文 ] Please tell me the query to create a BigQuery table for this CSV file. すると以下のようなクエリを出力してくれました。 CREATE TABLE your_dataset.products ( id INT64, name STRING, category STRING, price FLOAT64 ); ちなみにコンソールでどうやってこのクエリを実行するかも解説してくれてます。 BigQuery テーブルの作成方法についての回答文 Cloud Functions コード生成 一発で Python コード生成 では、本命であるコード生成を実行してもらいます。 [ 質問文 ] I would like to deploy a function on Google Cloud Functions that retrieves CSV files from Google Cloud Storage and stores their data in BigQuery. Please provide me with the corresponding code and a requirements.txt file. すると以下のようなコード、requirements.txt 及び Cloud Functions をデプロイする gcloud コマンドを出力してくれました。 requirements.txt google-cloud-storage==1.42.3 google-cloud-bigquery==2.28.1 main.py import os import tempfile from google.cloud import storage from google.cloud import bigquery def csv_to_bigquery (event, context): """Triggered by a change to a Cloud Storage bucket. Args: event (dict): Event payload. context (google.cloud.functions.Context): Metadata for the event. """ bucket_name = event[ 'bucket' ] file_name = event[ 'name' ] dataset_id = os.environ.get( 'BIGQUERY_DATASET_ID' ) table_id = os.environ.get( 'BIGQUERY_TABLE_ID' ) # Create BigQuery client bigquery_client = bigquery.Client() # Create Storage client storage_client = storage.Client() bucket = storage_client.get_bucket(bucket_name) blob = storage.Blob(file_name, bucket) # Download the CSV file to a temporary file with tempfile.NamedTemporaryFile(mode= 'wb' , delete= True ) as temp_csv: blob.download_to_file(temp_csv) temp_csv.flush() # Load CSV into BigQuery dataset_ref = bigquery_client.dataset(dataset_id) table_ref = dataset_ref.table(table_id) job_config = bigquery.LoadJobConfig() job_config.source_format = bigquery.SourceFormat.CSV job_config.autodetect = True with open (temp_csv.name, "rb" ) as source_file: job = bigquery_client.load_table_from_file(source_file, table_ref, job_config=job_config) job.result() # Wait for the job to complete print (f "Loaded {job.output_rows} rows into {dataset_id}:{table_id} from {file_name}." ) gcloud コマンド gcloud functions deploy csv_to_bigquery \ --runtime python310 \ --trigger-resource YOUR_TRIGGER_BUCKET_NAME \ --trigger-event google.storage.object.finalize \ --set-env-vars BIGQUERY_DATASET_ID=YOUR_DATASET_ID,BIGQUERY_TABLE_ID=YOUR_TABLE_ID requirements.txt と main.py をカレントディレクトリに設置し、 YOUR_TRIGGER_BUCKET_NAME 、 YOUR_DATASET_ID 及び YOUR_TABLE_ID に適切な値を設定してコマンドを実行します。 エラー修正も可能 しかし、コマンドを実行すると google-cloud-bigquery のバージョンエラーが表示されてしまいました。 そこでエラー文をそのまま記載して、対処法を聞いてみます。 [ 質問文 ] I encounterd the following error: """ ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: Collecting google-cloud-storage==1.42.3 Downloading google_cloud_storage-1.42.3-py2.py3-none-any.whl (105 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 106.0/106.0 kB 3.0 MB/s eta 0:00:00 ERROR: Ignored the following versions that require a different python version: 2.10.0 Requires-Python >=3.6, <3.10; 2.11.0 Requires-Python >=3.6, <3.10; 2.12.0 Requires-Python >=3.6, <3.10; 2.13.0 Requires-Python >=3.6, <3.10; 2.13.1 Requires-Python >=3.6, <3.10; 2.14.0 Requires-Python >=3.6, <3.10; 2.15.0 Requires-Python >=3.6, <3.10; 2.16.0 Requires-Python >=3.6, <3.10; 2.16.1 Requires-Python >=3.6, <3.10; 2.17.0 Requires-Python >=3.6, <3.10; 2.18.0 Requires-Python >=3.6, <3.10; 2.19.0 Requires-Python >=3.6, <3.10; 2.20.0 Requires-Python >=3.6, <3.10; 2.21.0 Requires-Python >=3.6, <3.10; 2.22.0 Requires-Python >=3.6, <3.10; 2.22.1 Requires-Python >=3.6, <3.10; 2.23.0 Requires-Python >=3.6, <3.10; 2.23.1 Requires-Python >=3.6, <3.10; 2.23.2 Requires-Python >=3.6, <3.10; 2.23.3 Requires-Python >=3.6, <3.10; 2.24.0 Requires-Python >=3.6, <3.10; 2.24.1 Requires-Python >=3.6, <3.10; 2.25.0 Requires-Python >=3.6, <3.10; 2.25.1 Requires-Python >=3.6, <3.10; 2.25.2 Requires-Python >=3.6, <3.10; 2.26.0 Requires-Python >=3.6, <3.10; 2.27.0 Requires-Python >=3.6, <3.10; 2.27.1 Requires-Python >=3.6, <3.10; 2.28.0 Requires-Python >=3.6, <3.10; 2.28.1 Requires-Python >=3.6, <3.10; 2.29.0 Requires-Python >=3.6, <3.10; 2.6.2 Requires-Python >=3.6, <3.9; 2.7.0 Requires-Python >=3.6, <3.10; 2.8.0 Requires-Python >=3.6, <3.10; 2.9.0 Requires-Python >=3.6, <3.10 ERROR: Could not find a version that satisfies the requirement google-cloud-bigquery==2.28.1 (from versions: 0.20.0, 0.21.0, 0.22.0, 0.22.1, 0.23.0, 0.24.0, 0.25.0, 0.26.0, 0.27.0, 0.28.0, 0.29.0, 0.30.0, 0.31.0, 0.32.0, 1.0.0, 1.1.0, 1.2.0, 1.3.0, 1.4.0, 1.5.0, 1.5.1, 1.5.2, 1.6.0, 1.6.1, 1.6.2, 1.7.0, 1.7.2, 1.8.0, 1.8.1, 1.8.2, 1.9.0, 1.9.1, 1.10.0, 1.10.1, 1.11.1, 1.11.2, 1.11.3, 1.11.4, 1.12.0, 1.12.1, 1.12.2, 1.13.0, 1.13.1, 1.14.0, 1.14.1, 1.15.0, 1.15.1, 1.16.0, 1.16.1, 1.17.0, 1.17.1, 1.18.0, 1.18.1, 1.19.0, 1.19.1, 1.20.0, 1.21.0, 1.22.0, 1.23.0, 1.23.1, 1.24.0, 1.25.0, 1.26.0, 1.26.1, 1.27.2, 1.28.0, 1.28.1, 1.28.2, 1.28.3, 2.0.0, 2.1.0, 2.2.0, 2.3.1, 2.4.0, 2.5.0, 2.6.0, 2.6.1, 2.30.0, 2.30.1, 2.31.0, 2.32.0, 2.33.0, 2.34.0, 2.34.1, 2.34.2, 2.34.3, 2.34.4, 3.0.0b1, 3.0.0, 3.0.1, 3.1.0, 3.2.0, 3.3.0, 3.3.1, 3.3.2, 3.3.3, 3.3.5, 3.3.6, 3.4.0, 3.4.1, 3.4.2, 3.5.0, 3.6.0, 3.7.0, 3.8.0, 3.9.0, 3.10.0) ERROR: No matching distribution found for google-cloud-bigquery==2.28.1; Error ID: 656dd225 """ How can I resolve this issue? すると requirements.txt を次のように修正してくださいと返信されました。 requirements.txt google-cloud-storage==1.43.0 google-cloud-bigquery==3.10.0 ということで、上記の通り修正し gcloud コマンドを再実行すると、今度はちゃんとデプロイすることができました。 動作確認 事前準備で ChatGPT に作ってもらった CSV ファイルをトリガー指定した GCS バケットにアップロードしてみます。 すると数分もしないうちに BigQuery テーブルにデータが追加されました。 BigQuery テーブルに格納されたデータ さいごに コードに関しても一回ぐらいはエラー修正が必要になるのかなと思っていたのですが、まさかの一発成功でした。 今回は検証としてシンプルなコードを生成してもらいました。 このコードをもとに自身で機能を追加していっても良いですし、追加したい機能をそのまままた ChatGPT にお願いするのも良いと思います。 最近日本でも公開された Google の AI チャットサービスである「Bard」ですが コード生成機能がつい先週実装された ということで、そちらのほうも近日検証してみたいと思います。 堂原 竜希 (記事一覧) データアナリティクス準備室。2023年4月より、G-genにジョイン。 Google Cloud Partner Top Engineer 2023, 2024に選出 (2024年はRookie of the yearにも選出)。休みの日はだいたいゲームをしているか、時々自転車で遠出をしています。 Follow @matayuuuu