納入外部脈絡

使用 OpenAI API 的提示工程

Fouad Trad

Machine Learning Engineer

為何需要外部脈絡

  • 預訓練語言模型只識別其訓練過的資訊
  • 需要提供更多脈絡
  • 提高準確度與成效

代表手機上聊天機器人的圖示。

使用 OpenAI API 的提示工程

LLM 的資訊不足

  • 知識截止點
system_prompt = "Act as a financial expert that knows about the latest trends."

user_prompt = "What are the top financial trends in 2023?"

print(get_response(system_prompt, user_prompt))
I apologize for any inconvenience, but as of my last knowledge update in 
September 2021, I don't have information about financial trends in 2023.
使用 OpenAI API 的提示工程

LLM 的資訊不足

  • 要求非公開資訊
system_prompt = "Act as a study buddy that helps me with my studies to succeed 
in exams."

user_prompt = "What is the name of my favorite instructor?" print(get_response(system_prompt, user_prompt))
I don't have personal information about you, including the name of your 
favorite instructor.
使用 OpenAI API 的提示工程

如何提供額外資訊?

  • 範例:先前對話
  • 系統提示詞

顯示兩個訊息對話框的圖示,代表對話情境。

使用 OpenAI API 的提示工程

對話範例

  • 引導模型回答特定問題
response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": "You are a customer service chatbot that responds to user queries in a gentle way"},

{"role": "user", "content": "What services do you offer?"},
{"role": "assistant", "content": "We provide services for web application development, mobile app development, and custom software solutions."},
{"role": "user", "content": "How many services do you have?"}])
print(response.choices[0].message.content)
We have 3 services including web application development, mobile app development, and custom software solutions.
  • 缺點:可能需要很多範例
使用 OpenAI API 的提示工程

系統提示詞

  • 把必要脈絡加入聊天機器人的答案中
services = "ABC Tech Solutions, a leading IT company, offers a range of services: 
application development, mobile app development, and custom software solutions."

system_prompt = f"""You are a customer service chatbot that responds to user queries in a gentle way. Some information about our services are delimited by triple backticks. ```{services}```"""
user_prompt = "How many services do you offer?" print(get_response(system_prompt, user_prompt))
We have 3 services including web application development, mobile app development, 
and custom software solutions.
使用 OpenAI API 的提示工程

最後提醒

  • 先前方法適用於小型脈絡
  • 較大脈絡需要更進階技巧

代表警示的圖示。

使用 OpenAI API 的提示工程

一起來練習吧!

使用 OpenAI API 的提示工程

Preparing Video For Download...