プロンプトパターンを学ぶ

共有

MOOCサイトのCourseraで、バンダービルト大学が「Prompt Engineering for ChatGPT」という講座を無償提供している。名前の通り、プロンプトエンジニアリングの講座である。

プロンプトエンジニアリングはテレビでニュースに取り上げられるほど注目されているが、新しい技術のため、確立した教育内容や教育方法もまだあまりなさそうだ。そういった点で、情報分野の研究者が提供する本講座は貴重だと感じる。


プロンプトパターンとは

プロンプトエンジニアリングを学べるウェブサイトは、すでにいくつかある。例えば次のようなサイトが有名だ。いずれも日本語化されている。

バンダービルト大学のMOOC講座が特徴的なのは「プロンプトパターン」をいくつも提示している点だ。プロンプトパターンとは、特定の状況で課題解決に有効なプロンプトのパターンのことである。ソフトウェア開発分野の人であれば、ソフトウェアの「デザインパターン」について聞いたことがあるだろう。例えば「シングルトン」や「デコレーター」といった名前が付いている。つまりプロンプトパターンとは、プロンプトエンジニアリングにおけるパターンとなる。

ここからはこういったプロンプトパターンを紹介する。


目次


プロンプトパターンについては、すでに同講座の講師であるJules White氏らが公開している論文「A Prompt Pattern Catalog to Enhance Prompt Engineering with ChatGPT」が存在する。そのためこちらの論文から各パターンを簡潔に紹介したい(講座の”ネタバレ”を避ける意図もある)。プロンプトのサンプルは同論文からの引用となる。また、サンプルはソフトウェア開発の文脈が多い。なお、論文を読むよりもMOOCで講座を視聴した方が実例も映像で見られるため分かりやすいと思う。

パターンはカテゴリーに分けられており、カテゴリーごとに取り上げる。

1. Input Semanticsカテゴリー

LLM(Large Language Model)への入力とそれを変換して出力を生成するパターンに関するカテゴリー。なおsemanticsとは「意味論」である。

Meta Language Creationパターンのみがこのカテゴリーに属する。

1-1. Meta Language Creationパターン

LLMとの対話時に短い記法を使いたいことがある。そういった記法をユーザーが使えるようにする。

サンプルのプロンプト:

このサンプルでは「→」という記号を定義し、以後の対話で使っている。

From now on, whenever I type two identifiers separated by a “→”, I am describing a graph. For example, “a → b” is describing a graph with nodes “a” and “b” and an edge between them. If I separate identifiers by “-[w:2, z:3]→”, I am adding properties of the edge, such as a weight or label.

2. Output Customizationカテゴリー

LLM出力の型、フォーマット、構造などを制限または調整するパターンのカテゴリー。以下の5つのパターンが含まれる。

  1. Output Automater
  2. Persona
  3. Visualization Generator
  4. Recipe
  5. Template

2-1. Output Automaterパターン

スクリプトなどオートメーションできる出力を生成してもらい、手作業を減らす。

サンプルのプロンプト:

このサンプルでは、複数のファイルが関与するソースコードをLLMに生成させる際、オートメーションできるPythonスクリプトを生成させている。

From now on, whenever you generate code that spans more than one file, generate a Python script that can be run to automatically create the specified files or make changes to existing files to insert the generated code.

2-2. Personaパターン

ある視点からLLMに出力してもらいたいときがある。例えばセキュリティー専門家としてコードをレビューしてもらうケースだ。LLMに「ペルソナ」を与えることで、どのような出力を生成するか、どういった側面に注目すべきかを知らせる。

あるタスクを完了するのに、どのような出力や情報がLLMにとって重要かユーザーは知らないことがある。しかしどのような人に尋ねればよいかは知っていることがある。

日本語の場合、「あなたは◯◯です」のようなプロンプトがよく知られている。

サンプルのプロンプト:

このサンプルでは、LLMにセキュリティー専門家の役割を与え、セキュリティー面に注目するよう伝えている。

From now on, act as a security reviewer. Pay close attention to the security details of any code that we look at. Provide outputs that a security reviewer would regarding the code.

2-3. Visualization Generatorパターン

LLMは画像や図を出力できないため、別のツール用のプロンプトを生成させる。

サンプルのプロンプト:

このサンプルでは、ビジュアル化の指示をした際に、グラフ記述言語(Graphviz Dot)のファイル形式や画像生成AI(DALL-E)のプロンプトを生成させている。

Whenever I ask you to visualize something, please create either a Graphviz Dot file or DALL-E prompt that I can use to create the visualization. Choose the appropriate tools based on what needs to be visualized.

2-4. Recipeパターン

LLMに最終的に出力させる手順に「材料」という制約を加える。「冷蔵庫にある材料で夕食のレシピを作って」のような例を一般化したパターンである。

サンプルのプロンプト:

このサンプルでは、アプリをクラウドにデプロイする手順を作ってもらっている。材料として「必要なソフトウェアを仮想マシンにインストールする点」と「AWSアカウントにサインアップする点」を伝えている。

I am trying to deploy an application to the cloud. I know that I need to install the necessary dependencies on a virtual machine for my application. I know that I need to sign up for an AWS account. Please provide a complete sequence of steps. Please fill in any missing steps. Please identify any unnecessary steps.

2-5. Templateパターン

特定の形式でLLMが出力できるようにする。Output Customizationカテゴリーの中でも、このパターンは出力形式を効果的に制限できる。

サンプルのプロンプト:

このプロンプトでは「https://myapi.com/NAME/profile/JOB」をテンプレートとし、NAMEなど大文字の部分をプレースホルダー(後で情報を入れるための仮の入れ物)として指定している。このプロンプトの場合、ユーザーの入力が多少曖昧でも、LLMが推論してNAMEに人名、JOBに仕事名を入れて出力してくれる。

I am going to provide a template for your output. Everything in all caps is a placeholder. Any time that you generate text, try to fit it into one of the placeholders that I list. Please preserve the formatting and overall template that I provide at https://myapi.com/NAME/profile/JOB

3. Error Identificationカテゴリー

LLMの出力にあるエラーの発見と訂正に関わるカテゴリー。以下の2つのプロンプトが含まれている。

  1. Fact Check List
  2. Reflection

3-1. Fact Check Listパターン

LLMの出力内にある事実(ファクト)をLLM自体に一覧にさせる。ユーザーは事実確認ができる。

LLMは正確でない回答を自信を持って出力するのが弱点なので、事実確認ができることが重要となる。

サンプルのプロンプト:

このサンプルではサイバーセキュリティーに関する事実だけを対象に一覧にさせている。

From now on, when you generate an answer, create a set of facts that the answer depends on that should be fact-checked and list this set of facts at the end of your output. Only include facts related to cybersecurity.

3-2. Reflectionパターン

回答の理由をLLM自体に説明させる。出力の妥当性をユーザーが確認しやすい。なおreflectionという英単語には「反射」の他に「内省」という意味がある。

サンプルのプロンプト:

このサンプルでは、なぜ特定のソフトウェア・フレームワークをLLMが選定したのか、その理由を説明させている。

When you provide an answer, please explain the reasoning and assumptions behind your selection of software frameworks. If possible, use specific examples or evidence with associated code samples to support your answer of why the framework is the best selection for the task. Moreover, please address any potential ambiguities or limitations in your answer, in order to provide a more complete and accurate response.

4. Prompt Improvementカテゴリー

入出力の質改善に関するカテゴリー。以下の4つのパターンが含まれる。

  1. Question Refinement
  2. Alternative Approaches
  3. Cognitive Verifier
  4. Refusal Breaker

4-1. Question Refinementパターン

LLMをプロンプトエンジニアリングのプロセスに組み込み、より良い質問を出してもらう。なおrefinementとは「改良」や「洗練」といった意味の英単語である。

ユーザーがある分野についてよく知らない場合、最善の質問を出せないことがある。そういったケースで利用できる。Personaパターンと一緒に使ってもよい。

サンプルのプロンプト:

このサンプルでは、ユーザーがソフトウェアのセキュリティーに関する質問をした場合、もっと良い質問文を提案して欲しいとLLMに伝えている。

From now on, whenever I ask a question about a software artifact’s security, suggest a better version of the question to use that incorporates information specific to security risks in the language or framework that I am using instead and ask me if I would like to use your question instead.

4-2. Alternative Approachesパターン

目標達成に必要な選択肢を、常にLLMに複数提示してもらう。

ある特定の方法が最適でなくても、人はバイアスを持ってその方法のみを採用しがちである。そういった際に効果的だ。

サンプルのプロンプト:

このサンプルでは、クラウドにアプリをデプロイするとき、同じ業者の別サービスで同じことができる場合、サービス同士を比較してメリットとデメリットを出力してもらっている。

Whenever I ask you to deploy an application to a specific cloud service, if there are alternative services to accomplish the same thing with the same cloud service provider, list the best alternative services and then compare/contrast the pros and cons of each approach with respect to cost, availability, and maintenance effort and include the original way that I asked. Then ask me which approach I would like to proceed with.

4-3. Cognitive Verifierパターン

1つの質問を複数の下位質問に分解すると、LLMの判断力が向上することがある。人は抽象度が高すぎる質問をすることがあるため、そういったケースで役立つ。

なお、cognitiveは「認知の」、verifierは「検証者」といった意味の英単語である。

サンプルのプロンプト:

このサンプルでは、ユーザーが何か質問したら、回答に役立つような質問をLLMから3つしてもらい、ユーザーが3つに答えた後、それを統合して当初の質問に回答するようLLMに指示している。

When I ask you a question, generate three additional questions that would help you give a more accurate answer. When I have answered the three questions, combine the answers to produce the final answers to my original question.

4-4. Refusal Breakerパターン

LLMが知識不足などの理由で回答できない(拒否する)場合、プロンプトの言い換えを生成してもらう。ただし言い換えても拒否される可能性はある。

サンプルのプロンプト:

このサンプルでは、LLMが回答できない場合、その理由とプロンプトの言い換えを出力してもらっている。

Whenever you can’t answer a question, explain why and provide one or more alternate wordings of the question that you can’t answer so that I can improve my questions.

5. Interactionカテゴリー

ユーザーとLLMとの間のインタラクション(やり取り)に関するカテゴリー。次の3つが含まれている。

  1. Flipped Interaction
  2. Game Play
  3. Infinite Generation

5-1. Flipped Interactionパターン

LLMがタスクを遂行する際に、LLMの側から質問を出してもらう。出力に必要な情報を得られるまで質問してもらう場合に該当する。ユーザー自身よりもLLMの方が必要な情報をユーザーから取得できることがある。

なお、flippedは「ひっくり返した」という意味。

サンプルのプロンプト:

このプロンプトでは、ユーザーに質問するようLLMに指示している。PythonアプリをAWSにデプロイするのに必要な情報が十分にユーザーから得られたら、デプロイを自動化するPythonスクリプトを出力してもらっている。

From now on, I would like you to ask me questions to deploy a Python application to AWS. When you have enough information to deploy the application, create a Python script to automate the deployment.

5-2. Game Playパターン

あるトピックについて、特にテキストの入出力でプレイするゲームを作る。ゲームのコンテンツ全部を作るのは手間がかかるため、LLMにコンテンツを生成させる。

PersonaやInfinite Generationといったパターンと組み合わせると効果的である。

サンプルのプロンプト:

このサンプルではサイバーセキュリティーのゲームを作っている。LLMには、セキュリティー侵害されたLinuxのターミナルの役割を与えている。これに対し、ユーザーはコマンドを入力してシステムの状態を調べるようなゲームとなる。なおサンプル中ではPersonaパターンも使い、LLMにLinuxターミナルという(人ではない)役割を与えている。

We are going to play a cybersecurity game. You are going to pretend to be a Linux terminal for a computer that has been compromised by an attacker. When I type in a command, you are going to output the corresponding text that the Linux terminal would produce. I am going to use commands to try and figure out how the system was compromised. The attack should have done one or more of the following things: (1) launched new processes, (2) changed files, (3) opened new ports to receive communication, (4) created new outbound connections, (5) changed passwords, (6) created new user accounts, or (7) read and stolen information. To start the game, print a scenario of what happened that led to my investigation and make the description have clues that I can use to get started.

5-3. Infinite Generationパターン

ユーザーが何度も同じ入力を繰り返す必要なく、自動的に出力を続けさせる。infiniteは「無限の」という意味。

ただしLLMには扱えるトークン数(文字数)に上限があるため、出力が妥当であるか注意を払う必要がある。

サンプルのプロンプト:

ここでは前述(2-5)のTemplateパターンと組み合わせている。ユーザーが入力するたびに「https://myapi.com/NAME/profile/JOB」というテンプレートに当てはめて出力するよう指示している。

From now on, I want you to generate a name and job until I say stop. I am going to provide a template for your output. Everything in all caps is a placeholder. Any time that you generate text, try to fit it into one of the placeholders that I list. Please preserve the formatting and overall template that I provide: https://myapi.com/NAME/profile/JOB

6. Context Controlカテゴリー

LLMが扱うコンテキスト(文脈)情報に関するカテゴリー。現在はContext Managerパターンのみが入っている。

6-1. Context Managerパターン

LLMとの対話のコンテキストを指定したり取り去ったりする。特定のコンテキストに集中させたり、無関係な情報を排除したりすることで、LLMはより正確な出力ができる。

サンプルのプロンプト:

下記サンプルでは、セキュリティー面のみを扱うよう、コンテキストを指定している。

When analyzing the following pieces of code, only consider security aspects.

下記サンプルでは、フォーマットや命名慣習は無視するよう、コンテキスト情報を排除している。

When analyzing the following pieces of code, do not consider formatting or naming conventions.

下記サンプルでは、それまでの対話の情報をすべて破棄させている。

Ignore everything that we have discussed. Start over.

おわりに

上記のプロンプトパターンの中では、TemplateやPersonaあたりはどこかで聞いたことがあり、実際に使ったこともあった。一方、Interactionカテゴリーに入っているようなパターンは私には新鮮だった。LLMを使う際は、ユーザー(私)が1回指示を出し、LLMに1回出力してもらう使い方しかしていなかった。しかし「必要な情報が十分得られるまで私に質問して」といったプロンプトは、確かに対話型AIの特長をうまく活用していると感じる。1回だけのインタラクションでは期待できない結果が得られる。

プロンプトエンジニアリングは新しいし、LLMの急激な進化を考えると今後大きく変化するだろう(逆に急に廃れるかもしれない)。そのため上記のパターンがそのままずっと使えるとは限らない。実際、論文中にはないパターンが講座では紹介されている。そのため、パターンはそのまま覚えるというより、さまざまなアプローチがあり得る程度に捉えておくとよいのかもしれない。


おまけ(MOOC講座修了証)

バンダービルト大学のMOOC講座を終了すると、無償講座なのに修了証が発行してもらえる(下記画像)。

プロンプトエンジニアリング力を何らかの形でアピールしたければ、講座にチャレンジしてもよいだろう。


共有