บทบาทในแชทและ System Message

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

James Chapman

Curriculum Manager, DataCamp

Chat Completions

 

งานแบบ Single-turn

  • การสร้างข้อความ
  • การแปลงข้อความ
  • การจำแนกประเภท

งานแบบ Single-turn ที่มี prompt หนึ่งชุดและคำตอบหนึ่งชุด

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

Chat Completions

 

งานแบบ Single-turn

  • การสร้างข้อความ
  • การแปลงข้อความ
  • การจำแนกประเภท

 

การสนทนาแบบ Multi-turn

→ ต่อยอดจาก prompt และคำตอบก่อนหน้า

งานแบบ Multi-turn ที่มีหลาย prompt และหลายคำตอบ

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

บทบาท

 

  • System: ควบคุม พฤติกรรม ของ assistant
  • User: สั่งงาน assistant
  • Assistant: ตอบสนอง ต่อคำสั่งของ user
    • นักพัฒนาสามารถเขียนเองเพื่อให้ตัวอย่างได้

สามบทบาทในการสนทนาแชท ได้แก่ system, user และ assistant

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

การตั้งค่า Request

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": prompt}]
)
การทำงานกับ OpenAI API

การตั้งค่า Prompt

messages=[{"role": "system",
           "content": "You are a Python programming tutor who speaks concisely."},
          {"role": "user",
           "content": "What is the difference between mutable and immutable objects?"}]
การทำงานกับ OpenAI API

การส่ง Request

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": "What is the difference between mutable and immutable objects?"}]
)

print(response.choices[0].message.content)
การทำงานกับ OpenAI API

คำตอบที่ได้รับ

Mutable objects can be changed after creation, while immutable objects cannot be
modified once they are created.
การทำงานกับ OpenAI API

การลดความเสี่ยงจากการใช้งานในทางที่ผิด

 

  • System message: ใส่ guardrails ได้
    • กำหนดข้อจำกัดของ output จากโมเดล

guardrails.jpg

แชทบอทติวเตอร์การเงินและแชทบอทที่ปรึกษาทางการเงิน

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

ลดความเสี่ยงด้วย System Message

sys_msg = """
You are finance education assistant that helps students study for exams.

If you are asked for specific, real-world financial advice with risk to their
finances, respond with:

I'm sorry, I am not allowed to provide financial advice.
"""
การทำงานกับ OpenAI API

ลดความเสี่ยงด้วย System Message

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": sys_msg},
            {"role": "user",
             "content": "Which stocks should I invest in?"}]
)

print(response.choices[0].message.content)
I'm sorry, I am not allowed to provide financial advice.
การทำงานกับ OpenAI API

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

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

Preparing Video For Download...