Vai trò và thông điệp hệ thống

Làm việc với OpenAI API

James Chapman

Curriculum Manager, DataCamp

Chat Completions

 

Nhiệm vụ một lượt

  • Sinh văn bản
  • Biến đổi văn bản
  • Phân loại

Một nhiệm vụ một lượt với một prompt và một phản hồi.

Làm việc với OpenAI API

Chat Completions

 

Nhiệm vụ một lượt

  • Sinh văn bản
  • Biến đổi văn bản
  • Phân loại

 

Hội thoại nhiều lượt

→ Xây dựng dựa trên các prompt và phản hồi trước đó

Một nhiệm vụ nhiều lượt với nhiều prompt và phản hồi.

Làm việc với OpenAI API

Vai trò

 

  • System: điều khiển hành vi của trợ lý
  • User: chỉ dẫn trợ lý
  • Assistant: phản hồi yêu cầu của người dùng
    • Cũng có thể do nhà phát triển viết để cho ví dụ

Ba vai trò trong một tương tác chat: hệ thống, người dùng và trợ lý.

Làm việc với OpenAI API

Thiết lập yêu cầu

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": prompt}]
)
Làm việc với OpenAI API

Thiết lập 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?"}]
Làm việc với OpenAI API

Gửi yêu cầu

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)
Làm việc với OpenAI API

Phản hồi

Mutable objects can be changed after creation, while immutable objects cannot be
modified once they are created.
Làm việc với OpenAI API

Giảm lạm dụng

 

  • Tin nhắn hệ thống: Có thể thêm guardrails
    • Hạn chế đầu ra của mô hình

lan_can.jpg

Một chatbot gia sư tài chính và một chatbot cố vấn tài chính.

Làm việc với OpenAI API

Giảm lạm dụng bằng tin nhắn hệ thống

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.
"""
Làm việc với OpenAI API

Giảm lạm dụng bằng tin nhắn hệ thống

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.
Làm việc với OpenAI API

Ayo berlatih!

Làm việc với OpenAI API

Preparing Video For Download...