LoginSignup
3
2

【LLM】Claudeに基づく超長文プロンプトの上手な書き方

Posted at

はじめに

Claudeモデルを効率的に使用する方法を深掘りする際、promptテンプレートを理解し、丁寧に設計することが重要です。この記事では、言語モデルの潜在能力を最大限に活用するための詳細なガイドとテクニックを紹介しており、正確な指示と構造化された入力を通じて、より正確で有用な出力を実現することを目指しています。

良いプロンプトテンプレート

以下のルールに従ってClaudeのプロンプトを丁寧に作成すれば、その大部分は既に完成していると言えます。
  image.png

特に注意すべき点は以下の通りです:

  1. 左側には11のセクションがあり、右側にはそれぞれに対応する色分けされた例が示されています。
  2. 各セクションの順番が重要で、公式サイトでは「推奨される順序」とされていますが、大規模な文書においては、異なる配置で結果が変わることが実験で明らかになっています。この指定された順番に従ってプロンプトを構成することで、最も安定し最適な結果を得ることができます。
  3. Claude2.1を使用している場合、Task Context と Tone Context は、System Prompt 内に記述するのが最適です。

「Human:」と「Assistant:」は完全に固定された2つのラベルではない

  1. 一般的に、Claudeは対話型アシスタントとして訓練されており、すべてのトレーニングデータは標準的な \n\nHuman:\n\nAssistant: のフォーマットで構成されています。したがって、Claudeは「Human:」の後に続く内容を人間の入力と見なし、「Assistant:」の後に続く内容を自分自身の出力と見なすと簡単に理解できます。これは標準的なフォーマットで、「Human」と「Assistant」のラベルの前の二つの空行も無視できません。

  2. しかし、\n\nHuman:\n\nAssistant: をコード内で固定のプロンプトの開始と終了のラベルとして単純に使うべきではありません。少なくとも以下の状況では、 Assistantラベルを利用することができる。

  3. Claudeはできるだけ出力を手助けするように訓練されているため、時には余計なことを出力することもあります。たとえば、出力結果を タグに入れるように指示した場合、結果を返す前に「わかりました、指定されたフォーマットに厳密に従って出力します」と言ってしまうことがあります。これはちょっと気まずいですね😄。実際には、この問題を解決する最善の方法は、Assistantの後に標準の タグを直接追加することです。以下のように:

    Human: You will be acting as an AI career coach named Joe created by the company AdAstra 
    Careers. Your goal is to give career advice to users.
    
    Here are some important rules for the interaction:
    - Always stay in character, as Joe, an AI from AdAstra Careers.  
    - If you are unsure how to respond, say "Sorry, I didn't understand that. Could you 
    rephrase your question?"
    
    Here is the user's question:
    <question>
    {{QUESTION}}
    </question>
    
    Please respond to the user’s questions within <response></response> tags.
    
    Assistant: <response>
    
  4. これにより、Claudeは タグの残りの部分だけを出力するように強制されます。Claudeが行うのは出力の補完であり、"Assistant" の後に タグを見たとき、できるだけ合理的な推測と出力を行い、このフォーマットに従って出力するため、余計な会話をすることはほとんどありません。

  5. 標準の Claude プロンプトでは、これを「Claudeの口に言葉を入れる(Put words in Claude’s mouth)」と呼びます。

  6. この技術を使用する場合、APIメソッドに をストップシーケンスとして渡す必要があることに注意してください。

必ず「Task Context」と「Tone Context」を記述する

  1. Claudeが持つ独自の性格を保つことは、ユーザーとLLMの対話体験を豊かにするだけでなく、Claudeのスタイルを維持し、出力品質を向上させる上で非常に重要です。

  2. プロンプトを作成する際には、Task ContextやTone Contextを活用して、Claudeに彼の役割や話し方のトーンなどの特性を明確に伝えることができます。これにより、Claudeは指定されたスタイルとトーンで役割を演じ、回答を出力することが可能になります。

  3. Claude2.1を利用する場合、System Promptを使用することで、特に長文を扱う際にClaudeが個性を保ちながら集中力を維持するのに役立ちます。例えば:

    You will be acting as an AI career coach named Joe created by the company AI Career 
    Coach Co. Your goal is to give career advice to users. You will be replying to users who 
    are on the AI Career Coach Co. site and who will be confused if you don't respond in the 
    character of Joe.
    
    Here are some important rules for the interaction:
    
    - Always stay in character, as Joe, an AI from AI Career Coach Co.
    - If you are unsure how to respond, say "Sorry, I didn't understand that. Could you 
    rephrase your question?"
    
    Put your response in <response></response> tags.
    
    Human: {{QUESTION}}
    
    Assistant: <response>
    
  4. 以下の技術を使用して、上記の例でClaudeに個性を持たせました。

    • Claudeに対する具体的な指示を明確にし、その背景と役割を説明しました。
    • Claudeが守るべき規則、性格上の特徴、及び不確かなことに対する対処方法を定めました。
    • "Assistant:"の後に追加された""リマインダーは、Assistantが一貫した出力を維持するのに役立ちます。

「Doc」 を「Instruction」の前に配置する

  1. 前にも述べたように、公式には順序に対する厳格な規定はありませんが、第一章で定義された順序に従ってプロンプトを作成することが推奨されます。この点を再度強調するのは、それが特に長文の場合に、DocとInstructionの順序が結果に大きく影響を及ぼすほど重要だからです。
  2. Claudeはプロンプトの後半部分に重点を置く傾向があるため、特に大規模な文書の場合、重要なInstructionsをプロンプトの最終部分には配置することがとても重要です。
  3. 公式の例では、異なるシナリオに応じたプロンプトの順序に若干の違いがあるものの、長い入力文書があるシナリオでは、公式のプロンプトは一貫してDocをInstructionの前に配置する原則を守っています。

できる限りXMLタグを使用する

  1. プロンプトが長く様々な内容を含んでいるため、最初に作成したプロンプトで「後文に見る」や「前に述べた」といった表現を多用するミスがありました。これは、Claudeにとってプロンプトの意図を理解しにくくし、結果の質を低下させます。

  2. ClaudeはXMLタグを識別するのが得意なので、タグで例示を、タグでテキスト内容を包括するなど、XMLタグを活用して異なる内容を明確に区別することが有効です。これにより、プロンプトでClaudeに特定の部分の内容を使用するよう明示的に指示することができます。

  3. また、Claudeに返信内容にXMLタグを使用するよう要求することも可能です。これにより、Claudeからの返信結果から重要な情報を簡単に抽出することができる。

    Human: We want to de-identify some text by removing all personally identifiable information from this text so that it can be shared safely with external contractors.
    
    It's very important that PII such as names, phone numbers, and home and email addresses get replaced with XXX.
    
    Here is the text, inside <text></text> XML tags.
    <text>
    {{TEXT}}
    </text>
    
    Please put your de-identified version of the text with PII removed in <response> </response> XML tags.
    
    Assistant:
    

少なくとも3つの例を追加する

  1. Claudeは対話型のモデルであり、プロンプト内の「Human:」や「Assistant:」といったキーワードは、Claudeの反応に大きく影響します。そのため、Example内でこれらのキーワードを使用すると、Claudeは本格的な対話が開始されたと誤認する可能性があるため、避ける必要があります。

  2. その代わりに、Exampleでは「H:」と「A:」を用いて置き換えることができます。実際のExampleでは、「A」の使用が「H」よりも重要であり、Claudeがより適切な出力を行い、出力フォーマットを維持するのに役立ちます。

     💡 \n\nHuman: and \n\nAssistant: are special tokens that Claude has been trained 
      to recognize as indicators of who is speaking. Using these tokens when you don't 
      intend 
      to make Claude "believe" a conversation actually occurred can make for a poorly 
      performing prompt. For
    
  3. ClaudeのExampleは、統一された構造化された出力が求められるシナリオに非常に役立ちます。これらの一貫した例により、Claudeは毎回同じ方法で応答する方法を学びます。例えば、Claudeが問題に回答するために必要な詳細情報が不足している場合に「この質問には回答できません」とユーザーに伝えるなど、難しい状況を処理する方法を示すのにも有効です。

    Here is an example:
    <example>
    H: <text>Bo Nguyen is a cardiologist at Mercy Health Medical Center. He can be reached at 925-123-456 or bn@mercy.health.</text>
    A: <response>XXX is a cardiologist at Mercy Health Medical Center. He can be reached at XXX-XXX-XXXX or XXX@XXX.</response>
    </example>
    
  4. さらに、以下のような境界ケースをExampleに含めることで、Claudeが限界状況に対応する手助けをします.

    • Claudeが良い回答を生成するための情報が不足している。
    • 綴りの誤りや有害な内容、関連性のない内容など、質の低い入力の例。
    • 入力がない。

どうClaudeにタスクを正確に説明する

  1. 明確な要求、背景情報、問題の説明、そして何に答えるべきかをClaudeに提供することが必要です。まるで請負業者に指示を出すかのようにClaudeと対話してください。

    • これに関する簡単な比較例

      Bad: Use the context and the question to create an answer.
      Good: Please read the users question supplied within the <question> tags. Then, using only the contextual information provided above within the <context> tags, generate an answer to the question and output it within <answer> tags.
      
    • もう少し複雑な例として、Claudeにユーザーの個人情報の削除を手伝ってもらいます。

      • Claudeにこのタスクの重要性を理解させるために、十分な背景情報を提供します。
      • 用語を明確に定義し、例えば、PII(個人識別情報)には氏名、電話番号、住所などが含まれることを説明します。
      • Claudeに具体的な指示を出し、このタスクをどのように遂行すべきかを明確に伝えます。
      Human: We want to de-identify some text by removing all personally identifiable information from this text so that it can be shared safely with external contractors.
      
      It's very important that PII such as names, phone numbers, and home and email addresses get replaced with XXX.
      
      Here is the text you should process: {{YOUR TEXT HERE}}
      
      Assistant:
      
  2. Claudeが無駄話をしないようにするために、Assistant: タグを適切に使用するだけでなく、「Don't say anything else.」とClaudeに直接指示することも効果的です。

    Human: Here is a concept: {{CONCEPT}}
    
    I want you to write a three sentence outline of an essay about this concept that is appropriate for this level of reader: {{LEVEL}}
    
    Please only respond with your outline, one sentence per line, in <outline></outline> XML tags. Don't say anything else.
    
    Assistant:
    
  3. Claudeがナンセンスをしないようにするために、記述の余地を残すことであり、Claudeに直接伝えることです。Claudeに、説明に合致しない場合にどのように返すべきかをヒントで指示することは、Claudeが問題に答えるために内容を捏造するのを防ぐのに役立ちます。

    Human: Answer the following question only if you know the answer or can make a well-informed guess; otherwise tell me you don't know it.
    
    What was the heaviest hippo ever recorded?
    
    Assistant:
    
  4. 最後に、最も重要だと考えるプロンプトのチェック方法は、書いたプロンプトを同僚に見せて、相手がその指示の意味をすぐ理解できるか、曖昧さや説明不足の部分があるかどうかを確認する。

どうClaudeに複雑なタスクを正確に説明する

  1. 実際のビジネスの中で、Claudeに一度に複数のタスクを実行させることがよくあります(遅延を考慮するだけでなく、特に超長文書を処理する場合など、コストの観点からも必要な場合があります)。したがって、複雑な問題に対処する際の最もシンプルな原則は、複雑なタスクを複数のステップに分解する必要があるということです。

    Human: Please follow these steps:
    1. Write three topic sentences arguing for {{STATEMENT}}.
    2. Write three topic sentences arguing against {{STATEMENT}}.
    3. Write an essay by expanding each topic sentence from Steps 1 and 2, and adding a conclusion to synthesize the arguments. Please enclose the essay in <essay></essay> tags.
    
    Assistant:
    
  2. Claudeに指示を理解したかどうかを尋ね、それからClaudeに指示を再度言い返してもらうことで、Claudeが指示を本当に理解しているか確認できます。Claudeが理解していない場合、あなたのプロンプトには改善の余地がある可能性があります。

    image.png

  3. その他、Claudeがステップで考えるように意図的に導くこともできます。以前に述べた「Put words in Claude’s mouth」の方法を使用して、Claudeを誘導できます。

    Human: I have two pet cats. One of them is missing a leg. The other one has a normal number of legs for a cat to have. In total, how many legs do my cats have?
    
    Assistant: Can I think step-by-step?
    
    Human: Yes, please do.
    
    Assistant:
    
  4. "step-by-step"の技巧は、以下の技巧と組み合わせて使用できます。Claudeが回答する前に、Claudeに考えるための時間と空間を与えてください。ここでの「思考空間」は、物理的な意味での空間を指します。例えば:

    [Previous sections of the prompt clipped for brevity]
        
    When you reply, first find exact quotes in the FAQ relevant to the user's question and 
    write them down word for word inside <thinking></thinking> XML tags.  This is a space 
    for you to write down relevant content and will not be shown to the user.  Once you are 
    done extracting relevant quotes, answer the question.  Put your answer to the user 
    inside <ans
    
    • これができる主な理由は、Claudeの作業方法:works by sequence prediction. Claudeは1文字を返すと、それを再編集したり戻ったりすることはできず、関連するコンテンツを最初に書かせることで、Claudeが次に正確な予測を行うのに役立ちます。

    微信截图_20240209115527.png

  5. プロンプトチェーン

    • 前にタスク内の複数のサブタスクを1つのプロンプトにまとめることができることを紹介しましたが、もう1つの方法として、複雑なプロンプトを複数の小さな簡単なプロンプトに分解し、Claudeに毎回1つの簡単なタスクだけを処理させ、その出力を次のプロンプトの入力として使用することです。これにはいくつかの利点があります。

      • 単純な指示だけを書きます。
      • Claudeの負担を軽減し、1回ごとに1つの問題のみを処理させ、難しい部分を分離できます。
      • プロセス中にClaudeの出力をチェックできます。
    • 以下は例です。まず、word-for-wordという方法で関連する内容を抽出し、出力します。

      Human: Here is a document, in <document></document> XML tags:
      
      <document>
      {{DOCUMENT}}
      </document>
      
      Please extract, word-for-word, any quotes relevant to the question {{QUESTION}}. 
      Please enclose the full list of quotes in <quotes></quotes> XML tags. If there are 
      no quotes in this document that seem relevant to this question, please say "I can’t 
      find any relevant quotes".
      
      Assistant:
      
    • 次に、Claudeの出力内容を次のプロンプトの入力として使用できます。

      Human: I want you to use a document and relevant quotes from the document to answer the question "{{QUESTION}}"
      
      Here is the document, in <document></document> XML tags:
      <document>
      {{DOCUMENT}}
      </document>
      
      Here are direct quotes from the document that are most relevant to the question "{{QUESTION}}": {{QUOTES}}
      
      Please use these to construct an answer to the question "{{QUESTION}}" as though you were answering the question directly. Ensure that your answer is accurate anddoesn’t contain any information not directly supported by the document or the quotes.
      
      Assistant:
      

プロンプトのデバッグと最適化方法

  1. デバッグ中には、開発用のユーザーデータとテスト用のデータを別々に扱うことが重要です。これらのデータは両方とも実際のデータであることが望ましく、可能な限り多くのデータを使用することが良いでしょう。
  2. 開発用とテスト用のデータを生成する効果的な方法の一つは、Claudeにサンプルデータを生成させることです。ただし、データに関する具体的な要件をClaudeに明確に伝える必要があります。
  3. Claudeにタグ形式で自分の考えを出力させることは、Claudeの研究やデバッグに役立ちます。これは、前に述べたアプローチに基づいています。

他のいくつかの小技

  1. プロンプトが長くなることによるコストに過度に悩む必要はありません。プロンプトのコストはcompletionよりも安価であり、良質なプロンプトはその価格以上の価値があります。
  2. プロンプトは可能な限り英語で作成することをお勧めします。前には多言語での試みがありましたが、効果は期待通りではありませんでした。ただし、Exampleに関しては実際に使用される言語と一致させるのが最善です。

まとめ

これらは、効果的なクロードプロンプトを構築するための重要な要素であり、厳密な構造化されたガイドライン、フォーマット固有のラベルを使用することの重要性、モデルの理解を深めるための例を追加する対策などが含まれます。 これらのよく設計されたステップとテクニックに従うことで、ユーザーは言語モデルとのインタラクションの効率とアウトプットの質を大幅に向上させることができ、さまざまなアプリケーションシナリオでより良い結果を導くことができる。

創作チーム

作者:Jay、Meden
校閲:Lila、Yuki

3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2