การใช้งาน assistant role

การทำงานกับ OpenAI API

James Chapman

Curriculum Manager, DataCamp

Chat completions สำหรับงาน single-turn

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: ตอบสนองต่อคำสั่งของ user
การทำงานกับ OpenAI API

การให้ตัวอย่าง

 

  • ชี้นำโมเดลไปในทิศทางที่ถูกต้อง
  • การใส่ข้อความ assistant เป็นรูปแบบที่มีโครงสร้างของ shot-prompting
  • ตัวอย่าง: Python Programming Tutor
    • ตัวอย่างคำถามและคำตอบจาก user

นกฮูกด้านวิทยาศาสตร์ข้อมูล

การทำงานกับ 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 → รูปแบบ template ที่สำคัญ

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

Assistant → ตัวอย่างบทสนทนา

User → บริบทสำหรับ input ใหม่ (มักเป็น single-turn)

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

Job Title: Data Engineer
...
การทำงานกับ OpenAI API

มาฝึกกันเถอะ!

การทำงานกับ OpenAI API

Preparing Video For Download...