Role w czacie i komunikaty systemowe

Praca z API OpenAI

James Chapman

Curriculum Manager, DataCamp

Chat Completions

 

Zadania jednoturowe

  • Generowanie tekstu
  • Transformacja tekstu
  • Klasyfikacja

Zadanie jednoturowe z jednym promptem i jedną odpowiedzią.

Praca z API OpenAI

Chat Completions

 

Zadania jednoturowe

  • Generowanie tekstu
  • Transformacja tekstu
  • Klasyfikacja

 

Konwersacje wieloturowe

→ Nawiązują do poprzednich promptów i odpowiedzi

Zadanie wieloturowe z wieloma promptami i odpowiedziami.

Praca z API OpenAI

Role

 

  • System: kontroluje zachowanie asystenta
  • Użytkownik: instruuje asystenta
  • Asystent: odpowiada na instrukcje użytkownika
    • Może być też tworzony przez dewelopera w celu podania przykładów

Trzy role w interakcji czatu: system, użytkownik i asystent.

Praca z API OpenAI

Konfiguracja żądania

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": prompt}]
)
Praca z API OpenAI

Konfiguracja promptu

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?"}]
Praca z API OpenAI

Wysyłanie żądania

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)
Praca z API OpenAI

Odpowiedź

Mutable objects can be changed after creation, while immutable objects cannot be
modified once they are created.
Praca z API OpenAI

Ograniczanie nadużyć

 

  • Komunikat systemowy: może zawierać zabezpieczenia
    • Ograniczenia wyników modelu

guardrails.jpg

Chatbot-tutor finansowy i chatbot-doradca finansowy.

Praca z API OpenAI

Ograniczanie nadużyć za pomocą komunikatów systemowych

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.
"""
Praca z API OpenAI

Ograniczanie nadużyć za pomocą komunikatów systemowych

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.
Praca z API OpenAI

Czas na ćwiczenia!

Praca z API OpenAI

Preparing Video For Download...