채팅 역할 및 시스템 메시지

OpenAI API 활용하기

James Chapman

Curriculum Manager, DataCamp

채팅 완성

단일 턴 작업

  • 텍스트 생성
  • 텍스트 변환
  • 분류

하나의 프롬프트와 하나의 응답이 있는 단일 턴 작업.

OpenAI API 활용하기

채팅 완성

단일 턴 작업

  • 텍스트 생성
  • 텍스트 변환
  • 분류

멀티 턴 대화

→ 이전 프롬프트와 응답을 바탕으로 확장

여러 프롬프트와 여러 응답이 있는 멀티 턴 작업.

OpenAI API 활용하기

역할

  • System: 어시스턴트의 _동작_ 제어
  • User: 어시스턴트에 _지시_
  • Assistant: 사용자 지시에 대한 _응답_
    • 개발자가 예시를 제공하기 위해 작성할 수도 있습니다

채팅 상호작용의 세 가지 역할: system, user, assistant.

OpenAI API 활용하기

설정 요청

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

설정 프롬프트

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 활용하기

요청 만들기

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 활용하기

오용 완화하기

  • 시스템 메시지: 가드레일 포함 가능
    • 모델 출력에 대한 제한

가드레일.jpg

금융 튜터 챗봇 및 금융 자문 챗봇.

OpenAI API 활용하기

시스템 메시지로 오용 완화하기

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 활용하기

시스템 메시지로 오용 완화하기

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...