ChatGPTを使ってコードを書く方法を解説しています。
⏱️ タイムスタンプ
00:00 イントロ
01:25 概要
02:57 企画
04:30 スケルトンの作成
07:19 コードの記入
08:25 リファクタリング
11:05 テスト
13:18 デバッギング
15:32 機能を追加する
19:27 その他の注意点
20:14 アウトロ
公開日:2023年4月25日
ChatGPTは、開発者のコード構築の方法を永遠に変えてしまいました。
今後、開発者は2種類に分かれるでしょう。
AIを活用することを学んだ人は超生産的になり、そうでない人は職を失うことになるでしょう。
それは、あなたが思っているよりも早く起こりそうです。
ここで、10Xエンジニアになるためのハードルはものすごく下がっている。
誰でも達成できますが、AIの使い方を知っていればの話です。
私はCopilotを使って大量のコーディングをしてきましたが、今はChatGPTが私の生産性をもう一段階上げてくれました。
ビルダーであることから指揮者であることに意識を変える必要があるのです。
このビデオでは、ChatGPTのパワーを活用して、スキルに関係なく、どんな言語でもコーディングする方法を紹介します。
必要なものは以下の通りです。
コーディングの仕組みについて基本的に理解していること。
それほど多くの知識は必要ありません。
Pythonから始めましょう。
その構文は、英語を読むのと非常によく似ており、以前は言語を非常に深いレベルで理解しなければ意味のあるものを構築できませんでした。
今は、ChatGPTを使いながら学ぶことができます。
次に、使用する各言語のファイル構造を理解する必要があります。
このチュートリアルではPythonを使いますが、どこに何があるのかを知っておくことは重要です。
このチュートリアルはPythonを使います。
ChatGPTでは、特定の言語において、どこに何があるべきかを説明することも可能です。
まず、いくつかのことを説明しましょう。
これがChatGPTです。
GPT-4を使うつもりです。GPT-4にアクセスできない人は絶対に登録してください。今はGPT-3.5で大丈夫です。
GPT-3.5とGPT-4の違いについても触れておこう。
コーディングには間違いなく4が良いですが、3.5でも十分です。
それに、GPT-4を導入したとしても、メッセージがかなり制限されることになる。
今現在、3時間ごとに25通のメッセージが送られてきますが、これは何もないことです。
そこで、プロンプトを非常に効率的に使用する方法も紹介するつもりです。
コードに入る前に注意すべきは、トークンの制限です。トークンとは、基本的に単語の一部です。
プロンプトで使用できる単語の数です。
GPT-3.5 は約4,000トークンが制限で、それはプロンプトとレスポンスの間になりますが、GPT-4は最大32,000トークンを持っているため、はるかに大きく、それはあなたが構築する際にコンテキストとしてより多くのコードを提供できることを意味します。
しかし、コードベースが大きくなると、必要なコンテキストをすべて提供することが難しくなってきます。
したがって、ファイル名を適切に付け、メソッド名も適切に付ける必要があり、ChatGPTは各ファイルと各メソッドの機能について推測できるようになります。
また、コンテキストを圧縮する方法と表現することもできます。
では、コードに入りましょう。
私たちはChatGPTとの連携を構築し、基本的にはChatGPTインターフェースを再現するが、コマンドラインでローカルに有効にすることを目指します。
まず最初に、ChatGPTの高レベルな計画について議論したいと思います。
では、それをやってみましょう。
コマンドラインから質問できるようにChatGPの統合を構築したい。
GPT 3.5ターボを使いたいのですが、これは非常に安く使えて、しかも非常に速いからです。
そして、Pythonを使いたい。
ファイル構成はどうすればいいのでしょうか?
そして、コードのアウトラインを提供してください。
3時間あたり25通のメッセージを効率的に使いたいので、各プロンプトで質問する量は多ければ多いほどよい。
25メッセージの制限に達すると、GPT3.5に切り替わりますが、これも問題ありませんが、今はGPT-4の方がはるかに優れています。
では、Enterを押して、出力を待ってみましょう。
さて、出力されている間に、もうひとつ言っておくと、私はVisual Studio Codeを使用しています。
何も入っていない状態でロードし、ゼロからすべてを作成することになります。
GPT 3.5ターボとPythonを使ってチャットボットを作るには、OpenAI APIとOpenAIパッケージを使います。
まず、まだインストールしていない場合は、パッケージをインストールする必要があります。
というわけで、この続きを出力している間に、それをやってしまいましょう。
コピーしてきますが、その場で「コードをコピー」をクリックすることもできます。
コピーしてみましょう。
Visual Studio Codeに切り替えて、コマンドラインを開きます。
コマンドライン、ターミナル、右上の小さなボタンを開きます。
ロードされるのを待つと、そこにあります。
今いる場所のすべてのファイルとディレクトリを表示するlsを入力します。
ディレクトリをcdしてデスクトップに移動し、ディレクトリを作成します。
ChatGPT CLI、アンダースコアCLIです。
そうです。
そのフォルダにディレクトリを変更し、Tabキーを押して完了です。
これでフォルダに入りました。
まず最初にやることは、このモジュールをインストールすることです。
コピーして、貼り付けます。
私はすでにインストールしていますが、そうでない場合は、インストールされます。
インストールが完了したら、今度は新しいディレクトリを作ると表示されるので、それを作成します。
envファイルが必要です。
requirements.txtが必要で、chatgpt.pyが必要です。
では、これら3つをすべて作成しましょう。
では、フォルダを作成したら、それを開いてみましょう。
すでに作成済みですが、このExploreボタンをクリックします。
開いたフォルダが表示されるので、そのフォルダに移動します。
今、私たちはchatgpt.cliフォルダにいます。
ここで、新規ファイルというボタンをクリックし、chatgpt.pyという名前を付けます。
.pyとすることで、Pythonファイルであることを自動的に認識します。
保存してください。
次に必要なのは.envファイルだと言われました。
もう一度クリックします。
d.envと入力し、Enter。
これで出来上がりです。
まだ何も入れていませんが、chatgptにお願いすることにします。
それから、requirements.txt ファイルが必要です。
もう一度、新規ファイル、requirements.txt、Enterを押してください。
これで、必要な3つのファイルが揃いました。
次に、requirements.txt ファイルに依存関係を追加します。
これを取得します。
requirements.txtをコピーして、ペーストし、Saveをクリックします。
次に、OpenAIのAPIキーが必要です。
これは.envファイルに書くので、それをコピーします。
.envファイルに切り替えて、貼り付けます。
ここで実際に値を入れる必要があるので、それを取得します。
新しい秘密鍵を作成します。
名前は付けません。
これをコピーします。
この鍵はビデオを公開する前に回転させます。
だから、コメントで言う必要はない。
でも、ありがとうございます。
では、スイッチバックします。
APIキーをここに貼り付けます。
保存をクリックします。
ChatGPTに戻ります。
スクロールダウンすると、ChatGPT.pyのコードのアウトラインを作成しますと表示されます。
文字通り、必要なコードの骨格が出来上がりました。
コピーしましょう。
ChatGPT.pyに戻り、それを貼り付けます。
ChatGPTの良いところは、多くの場合、コメントも含まれていることです。
黄色い下線が引かれているのは、モジュールを解決できないからです。
これはPythonのバージョンの問題です。
そこで、3.11.2を選択することにします。
では、「保存」をクリックします。
このメソッドには何も入っていないので、まだ問題がありますが、とりあえずは大丈夫です。
次に、ChatGPTに戻りましょう。
このスクリプトは、GPT 3.5 Turboと対話するためのシンプルなコマンドラインインターフェイスをセットアップします。
ChatGPTの機能を完成させるには、OpenAI APIを呼び出して、以下のようにレスポンスを処理する必要があります。
いいことに、途中ですべて説明してくれています。
この関数をコピーして、スイッチバックすると、この関数がここに配置されます。
この関数はこの関数を置き換えるものです。
この関数はこの関数を置き換えるもので、今、私たちのために記入されています。
保存」をクリックします。
さて、見てみましょう。
一つすぐに目立つことは、ChatGPT 3.5 Turboを使用するように指示しているにもかかわらず、通常は古いバージョンであるDaVinci 002にデフォルト設定されていることです。
なぜそうなるのかは分かりませんが。
コメントには、3.5Turboが利用可能になったら、これを3.5Turboモデルに置き換えてくださいと書かれています。
利用可能です。
だから、そう言ってあげましょう。
ChatGPTを使ったデバッグはこれが初めてです。
ChatGPT 3.5 Turbo APIエンドポイントが利用可能です、と言うことにしましょう。
3.5TurboでChatGPTのメソッドを教えてください。
素晴らしい。
GPT 3.5 Turbo APIエンドポイントが利用できるようになったので、以下のようにメソッドを更新します。
つまり、モデルエンジンGPT 3.5 Turboに切り替わるわけです。
興味深いことに、これはまだ正しいとは言えません。
これはまだ、古いアーキテクチャを使用していることになります。
そこで、次にやることはこれです。
GPT 3.5 TurboのAPIドキュメントを開き、本来あるべき形式をコピーペーストします。
これがそうです、いいですか?
これをコピーして、元に戻して、「これは正しくない」と言いましょう。
GPT 3.5 Turboの正しい使い方はこちらです。
そして、それを貼り付けると、今度はそれがわかるようになります。
何か間違うたびに謝ってきますが、これはかなり迷惑な話です。
謝らなくてもいいんだよ、と言ってもいいんですが、その価値はないんです。
それで、ChatGPTメソッドを3.5で書き換えているところです。
これがChatCompletionで、これが新しいバージョンです。
これをコピーしてみましょう。
会話の履歴を維持する方法についてすでに考えています。
では、このメソッドの準備ができたので、スイッチバックしましょう。
全体をハイライトして貼り付けると、これでうまくいくはずです。
これで、システムの役割、あなたは親切なアシスタント、そしてメッセージ、つまりプロンプトができました。
保存して、スイッチバックし、ここで会話の履歴を維持することができます。
これを丸ごとコピーして、メイン機能を置き換えるんだ、いいね?
モジュールをインポートし、.mファイルを読み込み、APIキーを読み込み、OpenAIの準備をし、そして2つの関数で動作させるわけです。
何が起こるか見てみましょう、Pythonファイルを実行してみましょう。
右上の小さな再生ボタンをクリックし、再生してみましょう。
ChatGPT CLIへようこそ、終了するにはexitと入力してください。
では、プロンプトの例を入力してみましょう。
AIについて10語の詩を書いてください。
人工知能、無限の可能性、無限の可能性。
というわけで、10語未満ですが、それでも大丈夫です。
では、Pythonで100まで数えるコードを書いてくださいと言えばいい。
100まで数える、これです。実際にフォーマットするものがあれば、フォーマットして出力することも可能です。
AI in range one to one to one, print AI, perfect.
さて、これからもう少し拡張していきますが、ChatGPTを使ってコーディングする際に知っておくべきいくつかのニュアンスを説明したいと思います。
まず、間違ってしまうことがあります。
だから、実際に見て、テストして、何が間違っているのかを確認する必要があるんだ。
コーディングの腕が上がれば、テストする前から小さな問題に気づくようになります。
でも、それが間違っていることに気づかないかもしれません。
ですから、あなたがしなければならないのは、ガイダンスを与えることです。そして、ガイダンスは重く評価されます。
つまり、「こういうものを作ってほしい」と言えば、たとえその方法が間違っていたとしても、それに従おうとするのです。
ですから、ChatGPTへの提案に自信を持ってから実行するようにしてください。
これで、プロンプトを表示して、実際に何かを達成する方法について会話することができます。
プロンプトは、「私の指示に盲目的に従わないで、私に相談してください」と伝えるために、ある方法で構成する必要があります。
そのためには、文字通り、こう言うのです。
私の指示に従うだけでなく、最適な方法を教えてください、私はこう考えています、と言うのです。
次に、ChatGPTは、会話の履歴の中で、文脈をうまく維持することができます。
3.5では少し悪いですが、4.0ではかなり良くなっています。
つまり、私がすでに入力したメッセージは、すべて把握されているのです。
だから、今後のプロンプトでは、「そういえば、こんなものを作っているんだった」と言う必要がなく、そのまま続けることができます。
では、デバッグの方法について少しお話ししましょう。
実際に例を挙げて説明していきます。
Visual Studioのコードに戻り、このコードにいくつかのバグを発生させます。
ここで、3.0ターボに変更しましょう。これは正しくありませんね。
だからCでコマンドラインを終了してもう一度再生をクリックします。
ChatGPT CLIへようこそ、exitと入力して終了してください。
では、「詩を教えてください、10語」とします。
これで、バグが発生しました。
エラー出力があるのは素晴らしいことです。
そこで私は、エラー出力全体を取得することにしました。
このファイルを実行しているところまでをハイライトします。
それをコピーします。
ChatGPTに切り替えて、「コードにバグがあります」と言う。
これが出力です、新しい行を貼り付けます。
エラーの最終行には 何が問題なのかが書かれています。
ChatGPTがそれを見てくれることを祈ります。
それでエンターキーを押して、モデル名を混乱させたことをお詫びします。
正しいモデル名はGPT 3.5ターボであるべきです。
ここで修正しました。
これで、すぐにバグを発見してくれました。
本当に、本当に素晴らしい。
これが、あなたが取るべきワークフローです。
コードを作成し、自分でテストする。
何か問題があれば、そのエラー出力をChatGPTに持ち帰る。
繰り返しになりますが、以前のメッセージの文脈があるので、毎回すべてのコードを貼り付ける必要はありません。
しかし、ある時点でChatGPTはコンテキストを失うことになります。
GPT-4.0では、長い時間、元のコードに関する追加のコンテキストを提供することなく、質問を続けることができます。
しかし、3.5では、より頻繁にそれを行う必要があるかもしれません。
難しいルールはありません。
ただ、試してみて、何がうまくいくかを見てください。
何度も間違った提案や意味をなさないコードの提案を受け取っていることに気付いたら、コードを再度提供する必要があります。
そこで、コードを修正し、もう一度テストしてみます。
もう1回テストしてみましょう。
詩を書いてください、10文字で、エンターキーを押してください。
そうです、素晴らしい。
では次に、このコードを少し拡張して、もう少し複雑にしてみましょう。
どのようにコードを拡張するかは、私が決めることではありません。
ChatGPTにどうすればいいのか聞いてみよう。
このコードをもっと拡張したい。
どんなクールな機能を追加すればいいのかわからない。
どうでしょう?
なるほど、追加できる機能はたくさんありそうですね。
いくつか提案します。
会話コンテキストの管理、会話の履歴をファイルに保存して、チャットボットが過去の会話を記憶できるようにする。
マルチユーザサポート、いいですね。
リッチメディアのサポート、クールですね。
パーソナライゼーション、時間指定応答、ChatGPTのように実際にタイピングをシミュレートできる、とてもクールです。
センチメント分析、言語翻訳、音声インターフェース、外部APIとの統合など、10個の候補が出ました。
では、5番が気に入ったので、ChatGPTにお願いしてみましょう。
5番の構築方法を教えてください。
タイピングをシミュレートする時間指定応答を実装するには、time.sleep関数を使って、チャットボットの応答を表示する前に遅延を加えることができます。
よし、これでメソッドと必要なモジュールが揃ったぞ。
このコードを出力し終えたら、試してみましょう。
では、いくつかの部品を手に取ってみましょう。
mainを含む必要なメソッドは出力されましたが、ChatGPTメソッドは出力されませんでした。
ですから、これはそのままにして、他のものを追加する必要があります。
そこで、一番上に行って、importとtimeを追加し、一番上に行って、保存します。
このsimulate typingメソッドを取り、上部に置き、このmainメソッドが必要になります。それらを下部に貼り付けて上書きします。そこにあります。
というわけで、数秒のうちにできました。
ここからCをコントロールし、Kコマンドでコンソールをクリアして、もう一度再生ボタンを押します。
そして、うまくいくかどうか見てみましょう。
AIについて50語の詩を書いてくれ
エンターキーを押すと チャットGPTが入力される
いいんだ、違うのがいい。
ChatGPTのように打ち出すのがいいんだ、だから誤解したんだ。
それは私の望むところとはちょっと違うんです。
ChatGPTと同じように、誰かが単語を入力するのをシミュレートして、テキストを出力したいのです。
そうだ、そうだ。
誰かがレスポンスの各文字を入力するのをシミュレートするには、各文字を印刷する間に遅延を追加することができます。
そして、これが反復ループです。
ChatGPTに何かを要求し、それを試し、何がうまくいき、何がうまくいかないかを見つけ、ChatGPTと反復を続けていくのです。
本当に素敵なプロセスです。
さて、それでは必要なものを手に入れましょう。
import timeとimport randomはすでにあります。
文字入力のシミュレートはないので、一番上に置いておきましょう。
シミュレートタイピングはもう必要ないかもしれませんが、今は残しておきましょう。
ディスプレイ・レスポンスが必要なので、それをここに置くと、再びメイン関数が表示されます。
それをコピーして、一番下まで行って、それを貼り付けます。
そして、ここを出て、コンソールをクリアして、もう一度やってみましょう。
詩を書いてください、50語です。
ほら、できました。
実際に一文字一文字入力しているところを見ることができます。
ChatGPTの出力方法とは異なりますが、ChatGPTはチャンク単位で出力するので、それをお願いすることもできます。
最後に再度強調したいことは、コードベースが大きく複雑になるにつれて、ChatGPTにすべてのコンテキストを提供するのが難しくなることで、それは大丈夫です。
そして、私たちがコーディングが上手になるにつれて、私たちが求める回答を得るために必要なコンテキストと思われるコードの一部を取り、必要に応じてChatGPTに貼り戻すことにします。
そして、コードでChatGPTと協力するほど、その限界を理解し、それを使用するのが上手になり、速くなります。
もちろん、ChatGPTは時間が経てば経つほど良くなっていくでしょう。
最後に、私はGitHub Copilot Xにとても期待しています。これはGitHub CopilotとChatGPTがひとつになったものです。
これはGitHub CopilotとChatGPTを1つにしたもので、発売されれば、このようなコンテキストの問題の多くを解決してくれると思います。
ChatGPTをコーディングパートナーとして使う方法について何か質問があれば、下のコメント欄に書き込んでください。
私はそのような質問にすべて答えることができます。
もしかしたら、もっと深く答えるフォローアップビデオを作るかもしれませんね。
このビデオが気に入ったら、「いいね!」と「購読」をお願いします!また次のビデオでお会いしましょう。
そして、次のビデオでお会いしましょう。