運用 assistant 角色

Working with the OpenAI API

James Chapman

Curriculum Manager, DataCamp

單輪任務的 Chat completions

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": "You are a data science tutor."},
            {"role": "user",
             "content": "What is the difference between mutable and immutable objects?"}]
)
  • System:控制 assistant 的「行為」
  • User:向 assistant 下「指示」
  • Assistant:回應使用者指示
Working with the OpenAI API

提供範例

 

  • 引導模型朝正確方向
  • 提供 assistant 訊息是一種更結構化的「shot-prompting」
  • 範例:Python 程式設計家教
    • 範例提問與解答

一隻資料科學貓頭鷹。

Working with the OpenAI API

提供範例

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": "You are a Python programming tutor who speaks concisely."},

{"role": "user", "content": "How do you define a Python list?"},
{"role": "assistant", "content": "Lists are defined by enclosing a comma-separated sequence of objects inside square brackets [ ]."},
{"role": "user", "content": "What is the difference between mutable and immutable objects?"}] )
Working with the OpenAI API

回應

print(response.choices[0].message.content)
Mutable objects can be changed after creation (e.g., lists, dictionaries). Immutable objects
cannot be altered once created (e.g., strings, tuples).
  • 試驗範例數量
Working with the OpenAI API

System vs. assistant vs. user

System → 重要的樣板格式

Output the information in this format:
name | age | occupation

Assistant → 範例對話

User → 新輸入所需的脈絡(多為單輪)

Create a job advert for an AI Engineer. Use this job advert as a template:

Job Title: Data Engineer
...
Working with the OpenAI API

一起來練習吧!

Working with the OpenAI API

Preparing Video For Download...