使用 assistant 角色

使用 OpenAI API

James Chapman

Curriculum Manager, DataCamp

单轮任务的聊天补全

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:对用户指令的回复
使用 OpenAI API

提供示例

  • 引导模型到正确方向
  • 提供 assistant 消息是更结构化的示例提示(shot-prompting)
  • 例:Python 编程导师
    • 示例问题与答案

一只数据科学猫头鹰。

使用 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?"}] )
使用 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).
  • 试验示例数量
使用 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
...
使用 OpenAI API

Passons à la pratique !

使用 OpenAI API

Preparing Video For Download...