提示工程入門

使用 OpenAI API 的提示工程

Fouad Trad

Machine Learning Engineer

什麼是提示工程?

為 LLM 撰寫提示或指令以獲得想要的回應

一張工程師介紹提示工程術語的圖片。

使用 OpenAI API 的提示工程

提示工程就像撰寫食譜

一位主廚設計料理的圖片。

使用 OpenAI API 的提示工程

為何需要提示工程?

  一張圖示:高品質提示帶來高品質答案

使用 OpenAI API 的提示工程

為何需要提示工程?

  一張圖示:答案品質受輸入提示品質影響。高品質提示→高品質答案,低品質提示→低品質答案。

使用 OpenAI API 的提示工程

重點回顧:OpenAI API

  • 可與 OpenAI 模型互動
  • 本課程已為你設定完成
  • 可使用 Chat Completions 端點

chatgpt_logo_white.png

使用 OpenAI API 的提示工程

重點回顧:訊息角色

每則訊息都有 3 種角色之一

使用者角色圖示。

使用 OpenAI API 的提示工程

重點回顧:訊息角色

每則訊息都有 3 種角色之一

兩種角色:user 與 assistant 的圖示。

使用 OpenAI API 的提示工程

重點回顧:訊息角色

每則訊息都有 3 種角色之一

三種角色:user、system、assistant 的圖示。

使用 OpenAI API 的提示工程

重點回顧:訊息角色

每則訊息都有 3 種角色之一

三種角色圖示,含 system 與 assistant 之間的傳遞箭頭,用於傳送 system 訊息。

  • System 訊息:引導模型行為
使用 OpenAI API 的提示工程

重點回顧:訊息角色

每則訊息都有 3 種角色之一

三種角色圖示,含 system→assistant 與 user→assistant 的傳遞箭頭。

  • System 訊息:引導模型行為
  • User 訊息:使用者的提示
使用 OpenAI API 的提示工程

重點回顧:訊息角色

每則訊息都有 3 種角色之一

三種角色圖示,含 system→assistant、user→assistant、assistant→user 的傳遞箭頭。

  • System 訊息:引導模型行為
  • User 訊息:使用者的提示
  • Assistant 訊息:回應使用者提示
使用 OpenAI API 的提示工程

重點回顧:控制參數

溫度計圖示,數值介於 0 到 2;0 代表無隨機性,2 代表最高隨機性。

  • temperature控制回答的隨機性
使用 OpenAI API 的提示工程

重點回顧:控制參數

溫度計與滑桿圖示;`max_tokens` 較低會得到較短回應。

  • temperature控制回答的隨機性
  • max_tokens控制回應長度
使用 OpenAI API 的提示工程

重點回顧:與 OpenAI API 溝通

prompt = "What is prompt engineering?"

client = OpenAI(api_key="api_key")
response = client.chat.completions.create(
model = "gpt-4o-mini",
messages = [{"role": "user", "content": prompt}],
temperature = 0 )
print(response.choices[0].message.content)
Prompt engineering refers to the process of designing and refining prompts or 
instructions given to a language model like ChatGPT in order to elicit desired 
responses or behaviors. It involves formulating specific guidelines or hints to 
guide the model's output towards a desired outcome.
使用 OpenAI API 的提示工程

建立 get_response() 函式

def get_response(prompt):

response = client.chat.completions.create( model = "gpt-4o-mini", messages = [{"role": "user", "content": prompt}], temperature = 0 )
return response.choices[0].message.content

用法

response = get_response("What is prompt engineering?")
print(response)
Prompt engineering refers to the process of designing and refining prompts or instructions given to a language model 
like ChatGPT in order to elicit desired responses or behaviors. It involves formulating specific guidelines or hints 
to guide the model's output towards a desired outcome.
使用 OpenAI API 的提示工程

優化提示

prompt = "What is prompt engineering? Explain it in terms that can be understood 
by a 5-year-old"
response = get_response(prompt)
print(response)
Imagine you have a very smart friend who can understand and answer lots of 
questions. But sometimes, they might not understand exactly what you want or give 
the wrong answer. So, prompt engineering is like giving your friend really clear 
instructions or hints to help them give you the best answer possible.
使用 OpenAI API 的提示工程

一起來練習吧!

使用 OpenAI API 的提示工程

Preparing Video For Download...