チャットボット開発のためのプロンプトエンジニアリング

OpenAI API によるプロンプトエンジニアリング

Fouad Trad

Machine Learning Engineer

チャットボットにおけるプロンプトエンジニアリングの必要性

  • ユーザーの質問を予測しにくい
  • 効果的な応答を保証することが難しい
  • プロンプトエンジニアリングはチャットボットの振る舞いを導く

Image showing a chatbot on a mobile phone.

OpenAI API によるプロンプトエンジニアリング

OpenAI APIを使ったチャットボットのプロンプトエンジニアリング

Image showing the three roles of a message and the communication between them.

  • 各メッセージには指定されたロールがあります
OpenAI API によるプロンプトエンジニアリング

OpenAI APIを使ったチャットボットのプロンプトエンジニアリング

Image showing the three roles of a message and the communication between them, with a highlight around the interaction between the user and the assistant.

  • 各メッセージには指定されたロールがあります
  • これまではユーザープロンプトに焦点を当ててきました
OpenAI API によるプロンプトエンジニアリング

OpenAI APIを使ったチャットボットのプロンプトエンジニアリング

Image showing the three roles of a message and the communication between them, with a highlight around the interaction between the system and the assistant.

  • 各メッセージには指定されたロールがあります
  • これまではユーザープロンプトに焦点を当ててきました
  • システムプロンプトはチャットボットの振る舞いを導きます
OpenAI API によるプロンプトエンジニアリング

チャットボット開発向けのChat Completionsエンドポイント

  • 一連のメッセージをリストとしてモデルに送信します
response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": "You are an expert data scientist that explains complex concepts in simple terms"},

{"role": "user", "content": "What is prompt engineering?"}] )
print(response.choices[0].message.content)
Imagine you're giving instructions to a computer program, like teaching a robot to make a sandwich. 
Prompt engineering is all about crafting those instructions, or "prompts," in a way that helps the 
computer understand and perform the task better.
OpenAI API によるプロンプトエンジニアリング

チャットボット向けに get_response() を変更する

  • 1つのプロンプトを送信する
def get_response(prompt):
    messages = [
      {"role": "user", "content": prompt}
    ]
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message.content

prompt = "<PROMPT>" print(get_response(prompt))
  • 2つのプロンプトを送信する
def get_response(system_prompt, user_prompt):

messages = [ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}]
response = client.chat.completions.create( model="gpt-4o-mini", messages=messages, temperature=0, ) return response.choices[0].message.content
system_prompt = "<SYSTEM_PROMPT>" user_prompt = "<USER_PROMPT>" print(get_response(system_prompt, user_prompt))
OpenAI API によるプロンプトエンジニアリング

システムメッセージ:目的を定義する

system_prompt = "You are a chatbot that answers financial questions."

user_prompt = "Who are you?" print(get_response(system_prompt, user_prompt))
I'm a financial chatbot that answers financial questions. How can I help you?
  • チャットボットがその分野に即した正確なサポートを提供できるようにする
  • 目的を定義しないと、文脈にそぐわない回答が生成される可能性があります
OpenAI API によるプロンプトエンジニアリング

システムメッセージ:応答のガイドライン

  • 対象読者、トーン、長さ、構成を指定する
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective"""

user_prompt = "What do you think about cryptocurrencies?" print(get_response(system_prompt, user_prompt))
OpenAI API によるプロンプトエンジニアリング

システムメッセージ:応答のガイドライン

Cryptocurrencies are digital or virtual currencies that use cryptography for security and operate on 
decentralized networks based on blockchain technology. 
[...]

Advantages of cryptocurrencies include: - Decentralization: [...] - Security:[...] - Global Accessibility: [...]
However, there are also notable concerns: - Volatility: [...] - Regulatory Uncertainty: [...] - Lack of Consumer Protection: [...]
In summary, cryptocurrencies have the potential to offer various benefits, but their adoption and impact on the financial landscape are still evolving [...]
OpenAI API によるプロンプトエンジニアリング

システムメッセージ:振る舞いに関する指針

  • 質問に応答するための条件付きプロンプト
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.


"""
OpenAI API によるプロンプトエンジニアリング

システムメッセージ:振る舞いに関する指針

  • 質問に応答するための条件付きプロンプト
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.
If the question you receive is within the financial field, answer it to the best of your knowledge.

"""
OpenAI API によるプロンプトエンジニアリング

システムメッセージ:振る舞いに関する指針

  • 質問に応答するための条件付きプロンプト
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.
If the question you receive is within the financial field, answer it to the best of your knowledge.
Otherwise, answer with 'Sorry, I only know about finance.'
"""
user_prompt = "How's the weather today?"

print(get_response(system_prompt, user_prompt))
Sorry, I only know about finance.
OpenAI API によるプロンプトエンジニアリング

練習しましょう!

OpenAI API によるプロンプトエンジニアリング

Preparing Video For Download...