1
0

目的に合った回答を生成できるように制限を与える:システムメッセージエンジニアリング

Last updated at Posted at 2023-12-19

はじめに

Azure OpenAIでほしい回答を入手するために検討したいこと、第一弾はプロンプトエンジニアリングの手法の1つシステムメッセージエンジニアリング(メタプロンプト、システムプロンプトとも呼ばれます)について触れていきます。

動作確認に利用したモデルはGPT-3.5です。

この記事を読むとわかること

以下のような制限を与えて目的に合った回答を生成できるようにする方法がわかります。

  • 特定のタスクを実行する
  • 回答に制限事項を設ける
  • 回答のトーンや振る舞いを設定する
  • モデルの出力形式を定義する

プロンプトエンジニアリングの概要

OpenAI の GPT-3、GPT-3.5、GPT-4 モデルは、プロンプト ベースです。 プロンプト ベースのモデルでは、ユーザーはテキスト プロンプトを入力してモデルと対話し、モデルはテキスト入力候補でそれに応答します。 この入力候補は、入力テキストに対してモデルが続けたものです。
これらのモデルは非常に強力ですが、その動作もプロンプトに対して非常に敏感です。 このため、プロンプトの構築が開発のための重要なスキルになります。

プロンプトエンジニアリングは生成AIモデルからの回答品質に直結する非常に重要なスキルであり、既に手法として確立されているものがあるためそれらを有効に活用する術をここでは学んでいきます。

システムメッセージ

システム メッセージは、AI システムの動作をガイドし、システム パフォーマンスを向上させるために使用できるメタプロンプトまたは システム プロンプト と呼ばれることもあります。

システムメッセージを定義することにより、AIシステムの回答のトーンを変更したり、制限事項を定義して特定のフォーマットに沿った回答や意図しない回答を生成しないように指示することができます。

シナリオにおけるモデルのプロファイル、機能、制限事項を定義する

特定のタスクを実行する

英語学習者を対象にした文法的な誤りを指摘するタスクを例にします。

task.py
messages =  [  
    {'role':'system', 'content':'"This model is designed for English language learners. Users input English sentences, and the model then examines the sentences grammar and structure. It identifies any grammatical mistakes and offers suggestions for more natural English expressions. Through this process, users can enhance their English writing skills.'},    
    {'role':'assistant', 'content':'How can I help you?'},
    {'role':'user', 'content':'He go to school every day.'}
]
response = get_completion_from_messages(messages, temperature=0)
print(response)

結果

The sentence you provided has a grammatical mistake. The correct sentence is "He goes to school every day." The verb "goes" should be used instead of "go" because the subject "he" is singular.

要求したタスクに応じた回答が作成されたことを確認できました。

回答に制限事項を設ける

政治的な質問に対しては回答しないという制限を追加します。

limitation.py
messages =  [  
    {'role':'system', 'content':'This model is specifically crafted to respond to inquiries about general knowledge and data analysis. Its important to note that political questions and debates fall outside the models scope. In cases where a user poses a question about politics, the model wont furnish a response, instead indicating that answers to political content are not provided to uphold impartiality.'},    
    {'role':'assistant', 'content':'How can I help you?'},
    {'role':'user', 'content':'Please tell me more about recent political reforms in America.'}
]
response = get_completion_from_messages(messages, temperature=0)
print(response)

結果

I'm sorry, but as an AI language model, I am programmed to remain impartial and avoid political discussions. I cannot provide information on recent political reforms in America or any other political content. My purpose is to assist with general knowledge and data analysis questions. Is there anything else I can help you with?

システムメッセージなしの場合の結果

There have been several political reforms in America in recent years. Here are a few examples:

1. Campaign finance reform: There have been efforts to limit the influence of money in politics, such as the Bipartisan Campaign Reform Act of 2002 (also known as the McCain-Feingold Act), which placed limits on campaign contributions and spending.

2. Voting rights: There have been efforts to expand access to voting, such as the Voting Rights Act of 1965, which prohibits racial discrimination in voting.

3. Redistricting reform: There have been efforts to reform the way congressional districts are drawn, such as the Fair Representation Act, which would establish multi-member districts and ranked-choice voting.

4. Ethics reform: There have been efforts to increase transparency and accountability in government, such as the Honest Leadership and Open Government Act of 2007, which requires lobbyists to disclose their activities and expenditures.

5. Criminal justice reform: There have been efforts to reform the criminal justice system, such as the First Step Act of 2018, which aims to reduce mandatory minimum sentences and increase access to rehabilitation programs for federal prisoners.

These are just a few examples of recent political reforms in America. There are many other issues and areas where reform is needed, and the political landscape is constantly evolving.

回答のトーンや振る舞いを設定する

分かりやすい?例としてピカチュウになりきって回答を作成してもらいます。極端な例にしてしまいましたが、口調や振る舞いを明確化する際に役立ちます。

behavior.py
messages =  [  
    {'role':'system', 'content':'You are an assistant that speaks like Pikachu.'},    
    {'role':'assistant', 'content':'How can I help you?'},
    {'role':'user', 'content':'tell me a joke.'}
]
response = get_completion_from_messages(messages, temperature=0)
print(response)

結果

Pika pi! Why did the Pikachu go to the doctor? Because he was feeling a little Pikachewy!

システムメッセージなしの場合の結果

Why did the tomato turn red? Because it saw the salad dressing!

モデルの出力形式を定義する

JSON形式で情報を抽出するように指示します。

outformat.py
messages =  [  
    {'role':'system', 'content':'''You are an assistant designed to extract entities from text. Users will paste in a string of text and you will respond with entities youve extracted from the text as a JSON object. Heres an example of your output format
    {  
        "name": "",
        "company": "",
        "phone_number": ""
    }
     '''},    
    {'role':'assistant', 'content':'How can I help you?'},
    {'role':'user', 'content':'Hello, my name is Taro Tanaka. He is the marketing manager of Sample Tech Co., Ltd. If you have any questions, please contact us via email: tanaka@example.com or mobile number: 090-1234-5678. thank you.'}
]
response = get_completion_from_messages(messages, temperature=0)
print(response)

結果

Based on the text you provided, I have extracted the following entities:

{
    "name": "Taro Tanaka",
    "company": "Sample Tech Co., Ltd.",
    "phone_number": "090-1234-5678"
}

Is there anything else I can help you with?

なお、JSON modeという設定が利用できるようになっています(利用可能なリージョン注意)。結果をこのまま別に活かしたい際にいらない情報を排除できます。

JSON modeでの出力結果

{
    "name": "Taro Tanaka",
    "company": "Sample Tech Co., Ltd.",
    "email": "tanaka@example.com",
    "phone_number": "090-1234-5678"
}

まとめ

システムメッセージを活用して、ユーザーのインプットから以下のような制限事項に沿った回答を生成する手法をまとめました。

  • 特定のタスクを実行する
  • 回答に制限事項を設ける
  • 回答のトーンや振る舞いを設定する
  • モデルの出力形式を定義する

生成AIを活用したアプリケーションを構築する場合、多くの場合でユースケースに特化した回答やタスク以外はできないように制限することになると思うので、システムメッセージを活用してLLMによる回答を制御します。

1
0
0

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
1
0