為聊天機器人開發進行提示工程

使用 OpenAI API 的提示工程

Fouad Trad

Machine Learning Engineer

聊天機器人為何需要提示工程

  • 使用者問題難以預測
  • 難以保證回應有效
  • 提示工程可引導聊天機器人行為

在手機上顯示聊天機器人的圖片。

使用 OpenAI API 的提示工程

使用 OpenAI API 進行聊天機器人的提示工程

顯示訊息三種角色及其互動的圖片。

  • 每則訊息都有指定角色
使用 OpenAI API 的提示工程

使用 OpenAI API 進行聊天機器人的提示工程

顯示訊息三種角色及其互動的圖片,並特別標示使用者與助理的互動。

  • 每則訊息都有指定角色
  • 先前重點在使用者提示
使用 OpenAI API 的提示工程

使用 OpenAI API 進行聊天機器人的提示工程

顯示訊息三種角色及其互動的圖片,並特別標示系統與助理的互動。

  • 每則訊息都有指定角色
  • 先前重點在使用者提示
  • 系統提示用來引導機器人行為
使用 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()

  • 傳送單一提示
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))
  • 傳送兩個提示
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...