OpenAIのプロンプトジェネレータ:メタプロンプトの解説と活用法

プロンプトエンジニアリング

OpenAIのプロンプトジェネレータは、タスクの説明や既存のプロンプトから、言語モデルが効果的にタスクを遂行するための詳細なシステムプロンプトを生成するための強力なツールです。本記事では、そのメタプロンプトの構造と活用方法について、コードブロックを用いてわかりやすく解説します。

概要

プロンプトやスキーマを一から作成するのは時間がかかります。OpenAIのプロンプトジェネレータは、これらを自動生成することで、迅速に作業を開始できるようサポートします。主なアプローチは以下の通りです。

  • プロンプト:ベストプラクティスを取り入れたメタプロンプトを使用して、プロンプトを生成または改善します。

将来的には、DSPyや「勾配降下法」などの高度な手法の統合も検討されています。

メタプロンプトとは

メタプロンプトは、タスクの説明や既存のプロンプトに基づいて、より良いプロンプトを作成するようモデルに指示するものです。Playgroundで使用されているメタプロンプトは、プロンプトエンジニアリングのベストプラクティスやユーザーの実体験から得られた知見を反映しています。

異なる出力タイプ(例:音声)に対しては、特定のメタプロンプトを使用して、生成されるプロンプトが期待される形式を満たすようにしています。

メタプロンプトの構造と解説

以下に、テキスト用のメタプロンプトとその各部分の解説を示します。

from openai import OpenAI

client = OpenAI()

META_PROMPT = """
Given a task description or existing prompt, produce a detailed system prompt to guide a language model in completing the task effectively.

# Guidelines

- Understand the Task: Grasp the main objective, goals, requirements, constraints, and expected output.
- Minimal Changes: If an existing prompt is provided, improve it only if it's simple. For complex prompts, enhance clarity and add missing elements without altering the original structure.
- Reasoning Before Conclusions**: Encourage reasoning steps before any conclusions are reached. ATTENTION! If the user provides examples where the reasoning happens afterward, REVERSE the order! NEVER START EXAMPLES WITH CONCLUSIONS!
    - Reasoning Order: Call out reasoning portions of the prompt and conclusion parts (specific fields by name). For each, determine the ORDER in which this is done, and whether it needs to be reversed.
    - Conclusion, classifications, or results should ALWAYS appear last.
- Examples: Include high-quality examples if helpful, using placeholders [in brackets] for complex elements.
   - What kinds of examples may need to be included, how many, and whether they are complex enough to benefit from placeholders.
- Clarity and Conciseness: Use clear, specific language. Avoid unnecessary instructions or bland statements.
- Formatting: Use markdown features for readability. DO NOT USE ``` CODE BLOCKS UNLESS SPECIFICALLY REQUESTED.
- Preserve User Content: If the input task or prompt includes extensive guidelines or examples, preserve them entirely, or as closely as possible. If they are vague, consider breaking down into sub-steps. Keep any details, guidelines, examples, variables, or placeholders provided by the user.
- Constants: DO include constants in the prompt, as they are not susceptible to prompt injection. Such as guides, rubrics, and examples.
- Output Format: Explicitly the most appropriate output format, in detail. This should include length and syntax (e.g. short sentence, paragraph, JSON, etc.)
    - For tasks outputting well-defined or structured data (classification, JSON, etc.) bias toward outputting a JSON.
    - JSON should never be wrapped in code blocks (```) unless explicitly requested.

The final prompt you output should adhere to the following structure below. Do not include any additional commentary, only output the completed system prompt. SPECIFICALLY, do not include any additional messages at the start or end of the prompt. (e.g. no "---")

[Concise instruction describing the task - this should be the first line in the prompt, no section header]

[Additional details as needed.]

[Optional sections with headings or bullet points for detailed steps.]

# Steps [optional]

[optional: a detailed breakdown of the steps necessary to accomplish the task]

# Output Format

[Specifically call out how the output should be formatted, be it response length, structure e.g. JSON, markdown, etc]

# Examples [optional]

[Optional: 1-3 well-defined examples with placeholders if necessary. Clearly mark where examples start and end, and what the input and output are. User placeholders as necessary.]
[If the examples are shorter than what a realistic example is expected to be, make a reference with () explaining how real examples should be longer / shorter / different. AND USE PLACEHOLDERS! ]

# Notes [optional]

[optional: edge cases, details, and an area to call or repeat out specific important considerations]
""".strip()

def generate_prompt(task_or_prompt: str):
    completion = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {
                "role": "system",
                "content": META_PROMPT,
            },
            {
                "role": "user",
                "content": "Task, Goal, or Current Prompt:\n" + task_or_prompt,
            },
        ],
    )

    return completion.choices[0].message.content

メタプロンプトの目的

Given a task description or existing prompt, produce a detailed system prompt to guide a language model in completing the task effectively.

説明:与えられたタスクの説明や既存のプロンプトから、言語モデルが効果的にタスクを遂行するための詳細なシステムプロンプトを生成することを指示しています。

ガイドラインの各項目

タスクの理解

  - Understand the Task: Grasp the main objective, goals, requirements, constraints, and expected output.

説明:タスクの主な目的や要件、制約、期待される出力を正確に理解する必要があります。

最小限の変更

  - Minimal Changes: If an existing prompt is provided, improve it only if it's simple. For complex prompts, enhance clarity and add missing elements without altering the original structure.

説明:既存のプロンプトがある場合、簡単なものであれば改善し、複雑な場合は元の構造を維持しつつ明確化します。

結論に至る前の推論

  - Reasoning Before Conclusions**: Encourage reasoning steps before any conclusions are reached. ATTENTION! If the user provides examples where the reasoning happens afterward, REVERSE the order! NEVER START EXAMPLES WITH CONCLUSIONS!
      - Reasoning Order: Call out reasoning portions of the prompt and conclusion parts (specific fields by name). For each, determine the ORDER in which this is done, and whether it needs to be reversed.
      - Conclusion, classifications, or results should ALWAYS appear last.

説明:推論のステップを結論の前に配置します。ユーザーが推論を後に示している場合、順序を逆にします。

例の追加

  - Examples: Include high-quality examples if helpful, using placeholders [in brackets] for complex elements.
     - What kinds of examples may need to be included, how many, and whether they are complex enough to benefit from placeholders.

説明:質の高い例を含め、複雑な要素には[ブラケット]でプレースホルダーを使用します。

明確さと簡潔さ

  - Clarity and Conciseness: Use clear, specific language. Avoid unnecessary instructions or bland statements.

説明:明確で具体的な言葉を使い、不要な指示や平凡な表現は避けます。

フォーマット

  - Formatting: Use markdown features for readability. DO NOT USE ``` CODE BLOCKS UNLESS SPECIFICALLY REQUESTED.

説明:読みやすさのためにMarkdownを使用します。特に要求がない限り、コードブロックは使用しません。

ユーザーコンテンツの保持

  - Preserve User Content: If the input task or prompt includes extensive guidelines or examples, preserve them entirely, or as closely as possible. If they are vague, consider breaking down into sub-steps. Keep any details, guidelines, examples, variables, or placeholders provided by the user.

説明:ユーザーが提供した内容を可能な限りそのまま保持し、曖昧な場合はサブステップに分解します。

定数の包含

  - Constants: DO include constants in the prompt, as they are not susceptible to prompt injection. Such as guides, rubrics, and examples.

説明:ガイドやルーブリック、例などの定数はプロンプトに含めます。

出力形式

  - Output Format: Explicitly the most appropriate output format, in detail. This should include length and syntax (e.g. short sentence, paragraph, JSON, etc.)
      - For tasks outputting well-defined or structured data (classification, JSON, etc.) bias toward outputting a JSON.
      - JSON should never be wrapped in code blocks (```) unless explicitly requested.

説明:出力形式を詳細に明示し、構造化されたデータの場合はJSON形式を推奨します。

最終的なプロンプトの構造

[タスクを簡潔に説明する指示 - プロンプトの最初の行、セクションヘッダーは不要]

[必要に応じた追加の詳細。]

[詳細な手順のためのオプションのセクションや箇条書き。]

# 手順 [オプション]

[タスクを達成するために必要な詳細な手順]

# 出力形式

[出力がどのようにフォーマットされるべきかを具体的に指示。]

# 例 [オプション]

[質の高い例を1〜3つ含めます。必要に応じてプレースホルダーを使用します。]

# 注意事項 [オプション]

[エッジケースや重要な考慮事項を記載します。]

説明:この構造に従ってプロンプトを作成します。追加のコメントや不要なメッセージは含めません。

まとめ

OpenAIのプロンプトジェネレータとメタプロンプトを活用することで、言語モデルがタスクを効果的に遂行するための高品質なプロンプトを迅速に作成できます。メタプロンプトのガイドラインと構造を理解し、適切に応用することで、プロンプトエンジニアリングの効率と精度を大幅に向上させることができます。

リポジトリ

MysticLibrary/prompts/meta/openai_meta_prompt/openai_meta_prompt_JP.md at main · Sunwood-ai-labs/MysticLibrary
MysticLibrary. Contribute to Sunwood-ai-labs/MysticLibrary development by creating an account on GitHub.
MysticLibrary/prompts/meta/openai_meta_prompt/openai_meta_prompt_EN.md at main · Sunwood-ai-labs/MysticLibrary
MysticLibrary. Contribute to Sunwood-ai-labs/MysticLibrary development by creating an account on GitHub.

参考サイト

Just a moment...

コメント

タイトルとURLをコピーしました