Usare il ruolo dell’assistente

Lavorare con l'API di OpenAI

James Chapman

Curriculum Manager, DataCamp

Chat completions per task a singolo turno

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: controlla il comportamento dell’assistente
  • User: istruisce l’assistente
  • Assistant: risposta all’istruzione dell’utente
Lavorare con l'API di OpenAI

Fornire esempi

 

  • Guida il modello nella direzione giusta
  • Fornire messaggi dell’assistente è una forma più strutturata di shot-prompting
  • Esempio: Tutor di programmazione Python
    • Domande e risposte utente di esempio

Un gufo di data science.

Lavorare con l'API di OpenAI

Fornire esempi

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?"}] )
Lavorare con l'API di OpenAI

La risposta

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).
  • Sperimenta con il numero di esempi
Lavorare con l'API di OpenAI

System vs. assistant vs. user

System → formattazione importante del template

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

Assistant → conversazioni di esempio

User → contesto richiesto per il nuovo input (spesso singolo turno)

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

Job Title: Data Engineer
...
Lavorare con l'API di OpenAI

Passiamo alla pratica !

Lavorare con l'API di OpenAI

Preparing Video For Download...