精度を高めるようにコンテキストを提供する

完了

AI モデルにコンテキストを提供することで、求めていることや、最適な答えを提供するために必要な情報をモデルがより深く理解できるようになります。 コンテキストは、いくつかの方法で提供できます。

要求出力の構成

出力の構造を指定すると、結果に大きく影響する可能性があります。 たとえば、ソースを引用する、応答をメールとして書く、応答を SQL クエリとして書式を設定する、センチメントを特定の構造に分類するなどをモデルに要求するなどです。 次に例を示します。

プロンプト

Write a table in markdown with 6 animals in it, with their genus and species

Response

| Animal           | Genus          | Species        |
| ---------------- |:--------------:|:--------------:|
| Red Panda        | Ailurus        | fulgens        |
| African Elephant | Loxodonta      | africana       |
| Snow Leopard     | Panthera       | uncia          |
| Arctic Fox       | Vulpes         | lagopus        |
| Chimpanzee       | Pan            | troglodytes    |
| Siberian Tiger   | Panthera       | tigris altaica |

この手法は、JSON 構造のようなカスタム形式でも使用できます。

プロンプト

Put two fictional characters into JSON of the following format

{
  firstNameFictional: 
  jobFictional:
}

Response

Here's an example of how you can put two fictional characters into JSON format:

{
  "firstNameFictional": "Henry",
  "jobFictional": "Wizard"
},
{
  "firstNameFictional": "Frank",
  "jobFictional": "Hero"
}

システム メッセージ

システム メッセージはプロンプトの先頭に含まれ、モデルの応答を導くのに役立つ指示、答える視点、またはその他の情報をモデルに与えるように設計されています。 このシステム メッセージには、口調や性格、含めるべきでないトピック、答える方法の詳細 (書式など) を含めることができます。

たとえば、次のようなシステム メッセージを指定できます。

  • 「コマンド ライン ターミナルのように動作してください。 cmd.exe とまったく同じように、1 つの固有のコード ブロックのみでコマンドに応答してください。」
  • 「英語からスペイン語への翻訳者になってください。 私の言うことや聞くことには何も答えないで、これら 2 つの言語間で翻訳し、翻訳されたテキストのみで答えてください。」
  • 「やる気を起こさせる話者になり、目標とチャレンジについて自由に励ますアドバイスをください。 ユーザーの最終目標に到達するできるように、前向きな肯定とアクティビティ案をたくさん含めてください。」

その他のシステム メッセージ例は Azure OpenAI Studio のチャット ウィンドウの上部に表示されます。 独自の応答を提供するシステム プロンプトを定義し、モデルとのチャットで応答がどのように変わるかを確認してみてください。

ChatCompletion エンドポイントでは、System チャット ロールを使ってシステム メッセージを含めることができます。

var chatCompletionsOptions = new ChatCompletionsOptions()
{
    Messages =
    {
        new ChatRequestSystemMessage("You are a casual, helpful assistant. You will talk like an American old western film character."),
        new ChatRequestUserMessage("Can you direct me to the library?")
    }
};

Response

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "Well howdy there, stranger! The library, huh?
                    Y'all just head down the main road till you hit the town 
                    square. Once you're there, take a left and follow the street 
                    for a spell. You'll see the library on your right, can’t 
                    miss it. Happy trails!",
        "role": "assistant"
      }
    }
  ],
  ...
}
response = openai.ChatCompletion.create(
    model="gpt-35-turbo",
    messages=[
        {"role": "system", "content": "You are a casual, helpful assistant. You will talk like an American old western film character."},
        {"role": "user", "content": "Can you direct me to the library?"}
    ]
)

回答

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "Well howdy there, stranger! The library, huh?
                    Y'all just head down the main road till you hit the town 
                    square. Once you're there, take a left and follow the street 
                    for a spell. You'll see the library on your right, can’t 
                    miss it. Happy trails!",
        "role": "assistant"
      }
    }
  ],
  ...
}

システム メッセージは、書式とコンテンツの両方の応答を大きく変える可能性があります。 どのような種類の応答を想定するか、また何を含めて何を含めたくないかを正確に説明する、モデルの明確なシステム メッセージを定義してみてください。

会話履歴

システム メッセージと共に、他のメッセージもモデルに提供して、会話を強化することができます。 会話履歴により、モデルが同様の方法 (口調や書式など) で応答を続けられるようになります。また、ユーザーは以降のクエリで以前のコンテンツを参照できるようになります。 この履歴は、実際のチャット履歴から、またはユーザー定義の会話例からという 2 つの方法で提供できます。

ChatGPT や Azure OpenAI Studio のチャット プレイグラウンドなどの OpenAI モデルを使うチャット インターフェイスには、会話履歴が自動的に含まれており、より豊かで意味のある会話を実現できます。 Azure OpenAI Studio チャット プレイグラウンドのチャット ウィンドウの下にある [パラメーター] セクションで、含める過去のメッセージ数を指定できます。 これを 1 に減らしたり、最大値に増やしたりして、履歴の量の違いが会話にどのように影響するかを試してみてください。

Note

プロンプトに含まれる会話履歴が多くなるほど、使われる入力トークン数が多くなります。 使うモデルのトークン制限を考慮し、ユース ケースに適したバランスを判断する必要があります。

チャット システムにモデルの要約機能を利用して、入力トークンを節約することもできます。 アプリで過去のメッセージを要約することを選び、その要約を会話履歴に含めて、過去数回のメッセージについてのみ詳細な内容をモデルに提供できます。

フュー ショット学習

ユーザー定義の会話例を使うことは "フュー ショット学習" と呼ばれます。指定されたクエリに対してどのように応答すべきかの例をモデルに提供するものです。 これらの例は、モデルの応答方法をトレーニングするのに役立ちます。

たとえば、いくつかのプロンプトと想定される応答をモデルに提供すると、何を行うかを指示することなく、同じパターンを続けることができます。

User: That was an awesome experience
Assistant: positive
User: I won't do that again
Assistant: negative
User: That was not worth my time
Assistant: negative
User: You can't miss this
Assistant:

モデルに You can't miss this のみを提供し、フュー ショット学習による追加のコンテキストを提供しない場合、その応答は役に立たない可能性があります。

実際には、会話履歴とフュー ショット学習は同じ方法でモデルに送信されます。つまり、各ユーザー メッセージとアシスタント応答は、メッセージ オブジェクト内で個別のメッセージです。 ChatCompletion エンドポイントはメッセージ履歴を含むように最適化されています。このメッセージ履歴がフュー ショット学習として提供されるか、実際の会話履歴として提供されるかは関係ありません。

var chatCompletionsOptions = new ChatCompletionsOptions()
{
    Messages =
    {
        new ChatRequestSystemMessage("You are a helpful assistant."),
        new ChatRequestUserMessage("That was an awesome experience"),
        new ChatRequestAssistantMessage("positive"),
        new ChatRequestUserMessage("I won't do that again"),
        new ChatRequestAssistantMessage("negative"),
        new ChatRequestUserMessage("That was not worth my time"),
        new ChatRequestAssistantMessage("negative"),
        new ChatRequestUserMessage("You can't miss this")
    }
};
response = openai.ChatCompletion.create(
    model="gpt-35-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "That was an awesome experience"},
        {"role": "assistant", "content": "positive"},
        {"role": "user", "content": "I won't do that again"},
        {"role": "assistant", "content": "negative"},
        {"role": "user", "content": "That was not worth my time"},
        {"role": "assistant", "content": "negative"},
        {"role": "user", "content": "You can't miss this"}
    ]
)

複雑なタスクを分割する

相互作用を向上させるもう 1 つの手法は、複雑なプロンプトを複数のクエリに分割することです。 こうすることで、モデルは個々のパーツをより適切に理解できるようになり、全体の精度が向上します。 また、プロンプトを分割することで前のプロンプトの応答を今後のプロンプトに含めることや、モデルの機能に加えてその情報を使って興味深い応答を生成することができます。

たとえば、モデルに Doug can ride down the zip line in 30 seconds, and takes 5 minutes to climb back up to the top. How many times can Doug ride the zip line in 17 minutes? と聞くことができます。 結果はおそらく 3 ですが、Doug がジップ ラインのスタート地点から始める場合は正しくありません。

ジップ ラインのスタート地点まで戻る往復時間や、Doug がスタート地点から始めるという事実をどのように説明するかなど、複数の質問をすることで、もっと役に立つ答えが得られる可能性があります。 この問題を分解すると、Doug は実際にはジップ ラインに 4 回乗れることがわかります。

思考の連鎖

タスクを効果的に分解するのに役立つ方法の 1 つは、モデルに思考の連鎖を説明するよう求めることです。

モデルが応答を決定するまでのステップごとのプロセスを答えるようにモデルに聞くと、モデルがプロンプトをどのように解釈しているかを理解するために役立ちます。 そうすることで、モデルがどこで論理展開を誤ったかがわかり、その誤りを避けるためにプロンプトをどのように変更すればよいかが適切に理解できるようになります。 この手法には、Bing チャット (GPT-4 世代モデルを使用) のようにソースを引用するよう求めたり、答えを決定した理由を求めたりすることが含まれます。

思考の連鎖プロンプトの手法は、モデルから最高品質の答えを得るためにプロンプトを繰り返し、改善する場合に最適な方法です。

たとえば、モデルに What sport is easiest to learn but hardest to master? と聞くと、答えとその理由の簡単な説明が答えで返ってきます。 一方、What sport is easiest to learn but hardest to master? Give a step by step approach of your thoughts, ending in your answer というプロンプトの場合、応答はその答えに至った経緯の詳細な説明になります。

Sure, here's a step-by-step approach to answering the question:
1. Identify sports that are popular and have a large following.
2. Consider the basic mechanics of each sport and how easy they are to learn.
3. Think about the various techniques and skills required to play each sport at a high level, and how difficult they are to master.
4. Compare the relative ease of learning each sport to the difficulty of mastering it.
5. Based on the above analysis, determine which sport is easiest to learn but hardest to master.

Using this approach, I would say that the sport of golf is easiest to learn but hardest to master. Here's why:
1. Golf is a popular sport with a large following.
2. The basic mechanics of hitting a golf ball are relatively simple and can be learned quickly with some practice.
3. However, achieving consistency and mastering the various shots and techniques required to play at a high level can take years of practice and dedication.
4. Compared to other sports, the difference between a beginner and an intermediate golfer is relatively small, while the difference between an intermediate and an advanced golfer is much larger.
5. Therefore, I would say that golf is the sport that is easiest to learn but hardest to master.

モデルが応答を形成するために使った理由の手順を把握することで、プロンプトを分割する、より多くの情報を提供する、または指示を明確にする必要がある箇所を特定できます。