Memanfaatkan peran assistant

Bekerja dengan OpenAI API

James Chapman

Curriculum Manager, DataCamp

Chat completion untuk tugas satu putaran

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: mengontrol perilaku assistant
  • User: menginstruksikan assistant
  • Assistant: respons terhadap instruksi user
Bekerja dengan OpenAI API

Memberi contoh

 

  • Arahkan model ke tujuan yang benar
  • Pesan assistant adalah bentuk shot-prompting yang lebih terstruktur
  • Contoh: Tutor Pemrograman Python
    • Contoh pertanyaan dan jawaban pengguna

Seekor burung hantu data science.

Bekerja dengan OpenAI API

Memberi contoh

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?"}] )
Bekerja dengan OpenAI API

Respons

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).
  • Eksperimen dengan jumlah contoh
Bekerja dengan OpenAI API

System vs. assistant vs. user

System → format templat penting

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

Assistant → contoh percakapan

User → konteks yang dibutuhkan untuk input baru (sering satu putaran)

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

Job Title: Data Engineer
...
Bekerja dengan OpenAI API

Ayo berlatih!

Bekerja dengan OpenAI API

Preparing Video For Download...