見出し画像

【プロンプトエンジニアリング】:⑤AIと学ぶプログラミング編

こんにちは、Choimirai Schoolのサンミンです。

【主要なアップデート】
(2022.08.24)正規表現への対応を追加

0  はじめに

いくつかの事例を挙げながらPrompt Engineeringに関する話を複数のnoteにわけて紹介する予定です。ご興味のある方は他のnoteも合わせて参考にしてください。

第5弾は、プログラミングに関することを確かめるとき使えるプロンプトです。

1  利用するモデル

プロンプトエンジニアリング第4弾までとは違って今回はプログラミングに関する質問となります。

プログラミングに特化したモデルとして「code-davinci-002」がありますが、敢えてGPT-3の汎用性を確認するため「text-davinci-002」を利用します。チャットの相手は「Rileyと学ぶ、英語」のRileyです。

DeepMindが汎用的なAIエイジェントとして「Gato」を発表したのが2022年5月。OpenAIの言語モデルもより汎用的な方向で進化していくと考えています。

2  JSで二つの配列を結合するには?

Rileyにまず、こう聞きます。

👩🏻‍💻 Human: how do I combine arrays in JavaScript?
🤖 AI: There are a few different ways to combine arrays in Java Script. The most common methods are using the concat() or slice() methods.

「concat()」か「slice()」メソッド使えるよ、と教えてくれたので続けて、サンプルコードを見せて、と書きます。

Human: Can I see a sample code?

すると、サンプルコードと予想されるアウトプットまで親切に教えてくれます。

var arr1 = (1, 2, 3];
var arr2 = (4, 5, 6]:

// Use the concat!) method to combine arrays
var combinedArray = arr1.concat(arr2);
console.log(combinedArray): // Output: [1, 2, 3, 4, 5, 6]

// Use the slice() method to combine arrays
var combinedArray = arr1. slice (.concat(arr2);
console.log(combinedArray): // Output: [1, 2, 3, 4, 5, 6] 
Rileyは基本WhatsAppのスマホアプリから操作しますが、WhatsAppのPC/Mac用のアプリをインストールすればパソコンからアクセスすることも可能です

プログラミングが初めての方は、教えてもらったコードを実行し、動きを確認するのをお勧めしたいです。

Visual Studio Codeで教えてもらったコードの動きを確認する時の様子

3  正規表現にも対応

開発者であれば正規表現の書き方がわからなくて何度もググった経験は誰にもあると思います。Rileyならば、正規表現も簡単に参照することができます。まず、会話の意図を明確にするのが重要ですので、正規表現を知っているか聞きます。

Human: Do you know regular expression?
AI: Yes, I know regular expression.

意図の確認が終わりましたら求めている正規表現を英語で説明します。

Human: How to match "minimum eight characters, at least one letter and one number"?
AI: The regular expression for that would be ^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$

返ってくる結果は説明の仕方によって大きく左右されますのでなるべく丁寧に書くようにしてください。

返ってくる結果は説明の仕方によって大きく左右される

4  時には背景情報が必要な場合も

JavaScriptの例では、Zero-shotで教えてくれたのですが、時には背景知識を与える必要もあります。例えば、下記のツイートはGitHubのレポジトリーをクローンするコマンドを教えてもらうために交わしたチャットの履歴です。

まず聞いたのが、

Human: How to clone a github repository ?
AI: I'm sorry, I don't understand what you mean by "clone a github repository." Can you please give me an example?

Rileyは、githubのレポジトリーをクローンする意味がわからなかったようです。Can you please give me an example? と素直に助けを求めてきた(笑)ので、GitHubとgitコマンドについて説明をします。

Human: Do you know GitHub?
AI: I'm sorry, I don't understand what you mean by "GitHub." Can you please give me an example?
Human: GitHub is a website where people share programming codes.
AI: I see.
Human: There are a set of commands that we can use to control versions of those source codes.
AI: I see.
Human: To commit codes, we use "git commit".
AI: I see.

ここまで説明をしますと「git clone」のコマンドも教えてくれます。

Human: I remember there is also a command to clone a source code repository from GitHub.
AI: Yes, the command is "git clone".
Human: Let's say the URL of the repository is https://github.com/gijigae/test.git. How can I clone this repository?
AI: To clone this repository, you would use the command "git clone https://github.com/gijigae/test.git".

一旦必要な背景知識を覚えれば、他のコマンドにも問題なく対応してくれるのはホントすごいです。

Human: How to create a new branch for the given repository?
AI: To create a new branch, you would use the command "git checkout-b <branch_name>".

5  機械学習用のコード

RileyとはData Science関連コードの確認もできます。例えば、あるデータセットに対し、学習用とテスト用に分けるときはこんな聞き方をします。

Human: How do split data into train and test sets using Python? I want to use 80% of the data for training and the remaining for testing.
AI: You can use the train_test_split function from the sklearn.model_selection library.

「train_test_split」を教えてくれたので、JavaScriptの時と同様、サンプルコードをお願いします。但し、今度はコードを書いてくれるのではなく関連リンクが返ってきます。教えてくれたリンクをクリックしますとscikit-learnのページへ遷移。

ページを読んで、使い方を覚えるのもいいですが、どんなコードを書けばいいのかをもう一度聞いて見ます。その時に便利なプロンプトが、

Human: Can you write the snippet of the code for me?

です。すると、必要な箇所だけをピックアップし、コードを書いてくれます。

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = 
    train_test_split(X, y, test_size=0.2, random_state=42)

テスト用データの割合を変えることも簡単です。

Human: What if I want to have 30% of data for testing instead of 20%?
AI: You can just adjust the test_size parameter accordingly, like SO:

ここで「You can just adjust …」というAIの対応にはほんと感心します。

6  コードの書き方をAIとの会話で教わるメリット①

GitHubの例でも見れたように、時には人工知能に必要な背景知識を与えるのが重要です。

人は他人に教えるとき最も多く学ぶ、というのはよく知られている話。何故教えると学びが多いのかといいますと自分が覚えた情報をもう一度思い起こす(想起学習)ことで知識として定着しやすくなるからです。

Retrieval practice possibly causes the learning benefits of teaching.

The learning benefits of teaching: A retrieval practice hypothesis

人工知能に必要な背景知識を教える時も、既知の情報を思い起こすことで情報をより効果的に知識へと変えることができると期待しています。

7  コードの書き方をAIとの会話で教わるメリット②

GitHub CopilotやTabNineの補完機能でも必要なコードを書いてもらうことはできます。が、疑問を会話を通じて解決するのはまた別の達成感が感じられると思っています。

特に、会話の履歴が保持されますので、テスト用のデータセットのパーセンテージを変える時も前の話を繰り返す必要なく、次のように聞けるメリットは大きいです。

Human: What if I want to have 30% of data for testing instead of 20%?
AI: You can just adjust the test_size parameter accordingly, like SO:

こうした作業の中で「プロンプトエンジニアリング」の能力も向上していきますので色々と試して見てください。

8  まとめ

プロンプトエンジニアリングはAIの思考を助け、必要な情報を取り出すための手法。工夫次第では、プログラミングの書き方を教えてもらうこともできます。上述した例でも見たように、質問の範囲はJavaScriptやPythonから始め、Gitのコマンドまで幅広い。

こうした質疑の中で、人間のプログラマーからは想像もしなかったコードが提案される日も来るかもしれません。まるで、AlphaGoが打った黒37の手のように。


この記事が気に入ったらサポートをしてみませんか?