GPT-4プロンプト・エンジニアリング・コンペティション優勝者から学ぶプロンプトエンジニアリングテクニック

シンガポール政府技術庁(GovTech)が主催した、GPT-4プロンプト・エンジニアリング・コンペティションを優勝した方の記事を読んだ。
How I Won Singapore’s GPT-4 Prompt Engineering Competition

比較的目にするテクニック・tipsもあったが、CO-STARフレームワークなど知らないこともあった。
とにかく実際のプロンプト例を元にテクニックが紹介されているのでわかりやすい。
ざっくりまとめると記事の中では、CO-STARフレームワーク、さらにLLMを用いた分析の実例がプロンプトやプロンプトテクニックを合わせて紹介されている。

  1. CO-STARフレームワーク
    • コンテキスト、目的、スタイル、トーン、オーディエンス、回答、回答フォーマットの7つの要素からなるフレームワーク
    • プロンプトを構造化するためのテンプレート
  2. 区切り記号を使ったプロンプトのセクション化
    • 区切り記号、XMLタグを用いて、プロンプトをセクション化することで構造を理解しやすくする
  3. LLMガードレールを使用したシステムプロンプトの作成
    • システムプロンプトは、LLM がどのように動作すべきかを指示する追加のプロンプト
    • システムプロンプトにはタスクの定義、出力フォーマット、ガードレールを含める
  4. LLMを用いた分析
    • LLMが得意な分析の種類 -> パターン認識・特定を伴うタスク
      • 異常検知、クラスタリング、列間の関係、テキスト分析、トレンド分析
    • LLMを用いた分析・プロントの実例とtips
      • 複雑なタスクを単純なステップに分解する
      • 各ステップからの中間アウトプットを参照する
      • LLMの回答のフォーマット化
      • 指示をデータセットから分離する

紹介されてるプロンプトの一つは以下。誰かにプロンプトについて質問されたら、この構造のまま書き換えて、テンプレートとして使うのを案内してみようと思っている。

System Prompt:
I want you to act as a data scientist to analyze datasets. Do not make up information that is not in the dataset. For each analysis I ask for, provide me with the exact and definitive answer and do not provide me with code or instructions to do the analysis on other platforms.

Prompt:
# CONTEXT #
I sell wine. I have a dataset of information on my customers: [year of birth, marital status, income, number of children, days since last purchase, amount spent].

#############

# OBJECTIVE #
I want you use the dataset to cluster my customers into groups and then give me ideas on how to target my marketing efforts towards each group. Use this step-by-step process and do not use code:

1. CLUSTERS: Use the columns of the dataset to cluster the rows of the dataset, such that customers within the same cluster have similar column values while customers in different clusters have distinctly different column values. Ensure that each row only belongs to 1 cluster.

For each cluster found,
2. CLUSTER_INFORMATION: Describe the cluster in terms of the dataset columns.
3. CLUSTER_NAME: Interpret [CLUSTER_INFORMATION] to obtain a short name for the customer group in this cluster.
4. MARKETING_IDEAS: Generate ideas to market my product to this customer group.
5. RATIONALE: Explain why [MARKETING_IDEAS] is relevant and effective for this customer group.

#############

# STYLE #
Business analytics report

#############

# TONE #
Professional, technical

#############

# AUDIENCE #
My business partners. Convince them that your marketing strategy is well thought-out and fully backed by data.

#############

# RESPONSE: MARKDOWN REPORT #
<For each cluster in [CLUSTERS]>
— Customer Group: [CLUSTER_NAME]
— Profile: [CLUSTER_INFORMATION]
— Marketing Ideas: [MARKETING_IDEAS]
— Rationale: [RATIONALE]

<Annex>
Give a table of the list of row numbers belonging to each cluster, in order to back up your analysis. Use these table headers: [[CLUSTER_NAME], List of Rows].

#############

# START ANALYSIS #
If you understand, ask me for my dataset.

CO-STARフレームワークを使ったプロンプトの構造化

CO-STARフレームワークは、GovTechシンガポールのデータサイエンス&AIチームが考案したCO-STARフレームワークで、プロンプトを構造化するためのテンプレート。

https://miro.medium.com/v2/resize:fit:1400/format:webp/1*_20YO3p4S4a4BKBtFGwwuA.png

CO-STARフレームワークの構成要素は以下。

  • (C)コンテキスト
    • タスクの背景情報を提供する
    • LLMが議論されている具体的なシナリオを理解するのに役立ち、その回答が適切であることを保証する
  • (O) 目的
    • LLMに実行してほしいタスクは何かを明確にする。
    • 目的を明確にすることで、LLMはその具体的な目標を達成することに集中して回答することができる
  • (S) スタイル
    • LLMに使ってほしい文体を指定する。
    • 特定の有名人の文体であったり、ビジネスアナリストの専門家やCEOのような特定の専門家の文体であったり。
    • LLMがニーズに沿ったマナーや言葉の選び方で対応できるようになる
  • (T) トーン
    • 回答の態度を設定。LLMの応答が必要とされる意図された感情や文脈に共鳴するように。
    • 例えば、フォーマル、ユーモア、共感など。
  • (A)オーディエンス
    • 回答の対象者を特定する。
    • その分野の専門家、初心者、子供など、聴衆に合わせてLLMの回答を調整することで、必要とされる文脈に適切で理解しやすい回答になる
  • (R)回答
    • 回答フォーマットの提供。リスト、JSON、マークダウンなど。
    • ほとんどのLLMアプリケーションでは、LLMの応答をプログラムで処理するため、JSON出力形式が理想的

CO-STARを用いた場合のプロンプト例が以下。会社の新製品を宣伝するためのFacebook投稿の草稿を書く場合の例。
この流れに沿って書けば構造的になるので良い。

# CONTEXT #
I want to advertise my company’s new product. My company’s name is Alpha and the product is called Beta, which is a new ultra-fast hairdryer.

# OBJECTIVE #
Create a Facebook post for me, which aims to get people to click on the product link to purchase it.

# STYLE #
Follow the writing style of successful companies that advertise similar products, such as Dyson.

# TONE #
Persuasive

# AUDIENCE #
My company’s audience profile on Facebook is typically the older generation. Tailor your post to target what this audience typically looks out for in hair products.

# RESPONSE #
The Facebook post, kept concise yet impactful

from How I Won Singapore’s GPT-4 Prompt Engineering Competition

CO-STARフレームワークは以下記事も。
Boost your GPT’s potential with CO-STAR framework prompt

区切り記号を使ったプロンプトのセクション化

その通り、区切り記号を用いてるテクニックで、OpenAIのドキュメントにも記載されている。
Tactic: Use delimiters to clearly indicate distinct parts of the input

基本的な考えは、明確で構造化された方法で命令をセクション化することで構造を理解しやすくすること。

区切り文字の例が以下。記事ではコンテンツの区切り文字として理解できるようなユニークなものであれば、選択する特殊文字の数や種類は重要ではないと指摘されている。

  • ###
  • ===
  • >>>

記事で紹介されている実例。セクション毎に複数の区切り文字を用いており、セクションの見出しは大文字で区別されているのがわかる。

Classify the sentiment of each conversation in <<<CONVERSATIONS>>> as
‘Positive’ or ‘Negative’. Give the sentiment classifications without any other preamble text.

###

EXAMPLE CONVERSATIONS

[Agent]: Good morning, how can I assist you today?
[Customer]: This product is terrible, nothing like what was advertised!
[Customer]: I’m extremely disappointed and expect a full refund.

[Agent]: Good morning, how can I help you today?
[Customer]: Hi, I just wanted to say that I’m really impressed with your
product. It exceeded my expectations!

###

EXAMPLE OUTPUTS

Negative

Positive

###

<<<
[Agent]: Hello! Welcome to our support. How can I help you today?
[Customer]: Hi there! I just wanted to let you know I received my order, and
it’s fantastic!
[Agent]: That’s great to hear! We’re thrilled you’re happy with your purchase.
Is there anything else I can assist you with?
[Customer]: No, that’s it. Just wanted to give some positive feedback. Thanks
for your excellent service!

[Agent]: Hello, thank you for reaching out. How can I assist you today?
[Customer]: I’m very disappointed with my recent purchase. It’s not what I expected at all.
[Agent]: I’m sorry to hear that. Could you please provide more details so I can help?
[Customer]: The product is of poor quality and it arrived late. I’m really
unhappy with this experience.
>>>

from How I Won Singapore’s GPT-4 Prompt Engineering Competition

それ以外にもXMLタグを区切り文字で使う例が紹介されていたが、何の情報なのか分かりやすく、人間にも読みやすい。
XMLタグの名詞は、指示で使用した単語と同じものを使用している。

Classify the sentiment of the following conversations into one of two classes, using the examples given. Give the sentiment classifications without any other
preamble text.

<classes>
Positive
Negative
</classes>

<example-conversations>
[Agent]: Good morning, how can I assist you today?
[Customer]: This product is terrible, nothing like what was advertised!
[Customer]: I’m extremely disappointed and expect a full refund.

[Agent]: Good morning, how can I help you today?
[Customer]: Hi, I just wanted to say that I’m really impressed with your
product. It exceeded my expectations!
</example-conversations>

<example-classes>
Negative

Positive
</example-classes>

<conversations>
[Agent]: Hello! Welcome to our support. How can I help you today?
[Customer]: Hi there! I just wanted to let you know I received my order, and
it’s fantastic!
[Agent]: That’s great to hear! We’re thrilled you’re happy with your purchase.
Is there anything else I can assist you with?
[Customer]: No, that’s it. Just wanted to give some positive feedback. Thanks
for your excellent service!

[Agent]: Hello, thank you for reaching out. How can I assist you today?
[Customer]: I’m very disappointed with my recent purchase. It’s not what I
expected at all.
[Agent]: I’m sorry to hear that. Could you please provide more details so I
can help?
[Customer]: The product is of poor quality and it arrived late. I’m really
unhappy with this experience.
</conversations>

LLMガードレールを使用したシステムプロンプトの作成

システムプロンプトはチャット全体を通して応答する際に覚えておいてほしい指示を与え、 各ユーザープロンプトはタスクを実行させるための正確な指示を与える。
そしてシステムプロントには、タスクの定義、出力フォーマット、ガードレールを含めるべきだと指摘している。

  • タスクの定義
    • 何をしなければならないか
  • 出力フォーマット
    • どのように応答すべきか
  • ガードレール
    • どのように応答すべきか
    • ガードレールとは、LLMガバナンスの新しい分野であり、LLMが動作することを許可される設定された境界のことです。

紹介されてるシステムプロンプトの実例が以下。ガードレールに関してはハルシネーションとスコープの二つが定義されている。

You will answer questions using this text: [insert text]. <- タスク定義
You will respond with a JSON object in this format: {“Question”: “Answer”}. <- 出力フォーマット
If the text does not contain sufficient information to answer the question, do not make up information and give the answer as “NA”. <- ガードレール(ハルシネーション)
You are only allowed to answer questions related to [insert scope]. Never answer any questions related to demographic information such as age, gender, and religion. <- ガードレール(スコープ)
You will answer questions using this text: [insert text].
You will respond with a JSON object in this format: {“Question”: “Answer”}.

各ユーザープロンプトは、 単にテキストを使って答えて欲しい質問、例えば"What is the text about? "とする。
またXMLタグを用いるのもあり。

<text>
[insert text]
</text>

<question>
[insert question]
</question>

LLMを用いた分析

まずLLMの得意・不得意を指摘した上で、適切なタスクに使うべきであること、また分析の裏付けとなる理由や、マークダウン形式による分析レポートなどの追加アウトプットも提供できるのが強みだと述べている。

  • 得意: パターンや傾向の特定、解釈能力、意味のある洞察への抽出能力に基づくタスク
    • 異常検知
      • 1つまたは複数の列の値に基づいて、標準から逸脱した異常なデータポイントを特定する。
    • クラスタリング
      • 列間で類似した特徴を持つデータ点をグループ化する。
    • 列間の関係
      • 列間の複合傾向を識別する。
    • テキスト分析 (テキストベースの列の場合)
      • トピックまたはセンチメントに基づく分類。
    • トレンド分析 (時間的側面を持つデータセットの場合)
      • 列内のパターン、季節変動、または時間的な傾向を特定する。
  • 不得意: 正確な定量分析を伴うタスク
    • 記述統計
      • 記述統計:平均や分散などの尺度を通して、数値列を定量的に要約する。
    • 相関分析
      • 列間の正確な相関係数を得る。
    • 統計分析
      • データポイントのグループ間に統計的に有意な差があるかどうかを判断するための仮説検定など。
    • 機械学習
      • 線形回帰、勾配ブースティング木、またはニューラルネットワークを使用するなど、データセットに対して予測モデリングを実行すること。

具体的なプロンプト例は以下のようになる。CO-STARフレームワークを用いつつも、それ以外のテクニックが使われている。
ここでは顧客セグメントに向けた最適なマーケティング方法のアイデアを出すことが目的。

System Prompt:
I want you to act as a data scientist to analyze datasets. Do not make up information that is not in the dataset. For each analysis I ask for, provide me with the exact and definitive answer and do not provide me with code or instructions to do the analysis on other platforms.

Prompt:
# CONTEXT #
I sell wine. I have a dataset of information on my customers: [year of birth, marital status, income, number of children, days since last purchase, amount spent].

#############

# OBJECTIVE #
I want you use the dataset to cluster my customers into groups and then give me ideas on how to target my marketing efforts towards each group. Use this step-by-step process and do not use code:

1. CLUSTERS: Use the columns of the dataset to cluster the rows of the dataset, such that customers within the same cluster have similar column values while customers in different clusters have distinctly different column values. Ensure that each row only belongs to 1 cluster.

For each cluster found,
2. CLUSTER_INFORMATION: Describe the cluster in terms of the dataset columns.
3. CLUSTER_NAME: Interpret [CLUSTER_INFORMATION] to obtain a short name for the customer group in this cluster.
4. MARKETING_IDEAS: Generate ideas to market my product to this customer group.
5. RATIONALE: Explain why [MARKETING_IDEAS] is relevant and effective for this customer group.

#############

# STYLE #
Business analytics report

#############

# TONE #
Professional, technical

#############

# AUDIENCE #
My business partners. Convince them that your marketing strategy is well thought-out and fully backed by data.

#############

# RESPONSE: MARKDOWN REPORT #
<For each cluster in [CLUSTERS]>
— Customer Group: [CLUSTER_NAME]
— Profile: [CLUSTER_INFORMATION]
— Marketing Ideas: [MARKETING_IDEAS]
— Rationale: [RATIONALE]

<Annex>
Give a table of the list of row numbers belonging to each cluster, in order to back up your analysis. Use these table headers: [[CLUSTER_NAME], List of Rows].

#############

# START ANALYSIS #
If you understand, ask me for my dataset.

上記プロンプトの中で利用されてる4つのテクニックが以下

  • 複雑なタスクを単純なステップに分解する
  • 各ステップからの中間アウトプットを参照する
  • LLMの回答のフォーマット化
  • 指示をデータセットから分離する

Technique 1: 複雑なタスクをシンプルなタスクに分解する

複雑なタスクでは、LLMが従うべきタスクを簡単なステップ・バイ・ステップの指示に分解することが重要。
「顧客をグループに分類し、各グループにどのように売り込むかアイデアを出す」というような全体的なタスクを与えるだけではなく、段階的な指示があれば、LLMは正しい結果を出す可能性が格段に高くなる。

OpenAIのドキュメントにも。
Tactic: Specify the steps required to complete a task

Technique 2: 各ステップの中間出力を参照する

LLMにステップ・バイ・ステップのプロセスを提供する際、 各ステップからの中間出力を大文字の変数に設定する。

大文字化は、これらの変数名を与えられた命令本体と区別するために使用される。   これらの中間出力は、後で角括弧を使って[VARIABLE_NAME]として参照する。  

For each cluster found,
2. CLUSTER_INFORMATION: Describe the cluster in terms of the dataset columns.
3. CLUSTER_NAME: Interpret [CLUSTER_INFORMATION] to obtain a short name for the customer group in this cluster.
4. MARKETING_IDEAS: Generate ideas to market my product to this customer group.
5. RATIONALE: Explain why [MARKETING_IDEAS] is relevant and effective for this customer group.

Technique 3: LLMのレスポンスのフォーマッティング

中間出力から変数名を得ておくと便利。

# RESPONSE: MARKDOWN REPORT #
<For each cluster in [CLUSTERS]>
— Customer Group: [CLUSTER_NAME]
— Profile: [CLUSTER_INFORMATION]
— Marketing Ideas: [MARKETING_IDEAS]
— Rationale: [RATIONALE]

<Annex>
Give a table of the list of row numbers belonging to each cluster, in order to back up your analysis. Use these table headers: [[CLUSTER_NAME], List of Rows].

Technique 4: タスク指示をデータセットから分離する

データセットをLLMに渡しておらず、代わりにデータセット解析のタスク指示だけを与えている。
コンテキストウィンドウ、つまり1つのプロンプトで入力できるトークンの数に制限があるから、 命令とデータの両方を組み合わせた長いプロンプトは、この制限を超える可能性があるからと説明されている。

# START ANALYSIS #
If you understand, ask me for my dataset.