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

OpenAI API で学ぶプロンプトエンジニアリング

Fouad Trad

Machine Learning Engineer

チャットボットにプロンプトエンジニアリングが必要な理由

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

モバイルフォン上のチャットボットを示す画像。

OpenAI API で学ぶプロンプトエンジニアリング

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

メッセージの3つの役割とそれらの通信を示す画像。

  • 各メッセージには指定された役割がある
OpenAI API で学ぶプロンプトエンジニアリング

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

メッセージの3つの役割と通信を示す画像(ユーザーとアシスタント間のやり取りを強調表示)。

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

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

メッセージの3つの役割と通信を示す画像(システムとアシスタント間のやり取りを強調表示)。

  • 各メッセージには指定された役割がある
  • これまではユーザープロンプトに焦点を当ててきた
  • システムプロンプトがチャットボットの動作を導く
OpenAI API で学ぶプロンプトエンジニアリング

チャットボット開発のためのチャット補完エンドポイント

  • メッセージをリストとしてモデルに送信する
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...