챗봇 개발을 위한 프롬프트 엔지니어링

OpenAI API로 배우는 프롬프트 엔지니어링

Fouad Trad

Machine Learning Engineer

챗봇에 프롬프트 엔지니어링이 필요한 이유

  • 사용자 질문 예측이 어려움
  • 효과적인 응답 보장이 어려움
  • 프롬프트 엔지니어링으로 챗봇 동작을 유도

모바일 폰의 챗봇 이미지.

OpenAI API로 배우는 프롬프트 엔지니어링

OpenAI API를 활용한 챗봇 프롬프트 엔지니어링

메시지의 세 가지 역할과 역할 간 통신을 나타내는 이미지.

  • 각 메시지에는 지정된 역할이 있음
OpenAI API로 배우는 프롬프트 엔지니어링

OpenAI API를 활용한 챗봇 프롬프트 엔지니어링

메시지의 세 가지 역할과 역할 간 통신을 나타내는 이미지로, 사용자와 어시스턴트 간 상호작용이 강조되어 있음.

  • 각 메시지에는 지정된 역할이 있음
  • 지금까지는 사용자 프롬프트에 집중
OpenAI API로 배우는 프롬프트 엔지니어링

OpenAI API를 활용한 챗봇 프롬프트 엔지니어링

메시지의 세 가지 역할과 역할 간 통신을 나타내는 이미지로, 시스템과 어시스턴트 간 상호작용이 강조되어 있음.

  • 각 메시지에는 지정된 역할이 있음
  • 지금까지는 사용자 프롬프트에 집중
  • 시스템 프롬프트는 챗봇의 동작을 유도
OpenAI API로 배우는 프롬프트 엔지니어링

챗봇 개발을 위한 채팅 완성 엔드포인트

  • 메시지 목록을 모델에 순서대로 전송
response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": "You are an expert data scientist that explains complex concepts in simple terms"},

{"role": "user", "content": "What is prompt engineering?"}] )
print(response.choices[0].message.content)
Imagine you're giving instructions to a computer program, like teaching a robot to make a sandwich. 
Prompt engineering is all about crafting those instructions, or "prompts," in a way that helps the 
computer understand and perform the task better.
OpenAI API로 배우는 프롬프트 엔지니어링

챗봇을 위한 get_response() 수정

  • 프롬프트 한 개 전송
def get_response(prompt):
    messages = [
      {"role": "user", "content": prompt}
    ]
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message.content

prompt = "<PROMPT>" print(get_response(prompt))
  • 프롬프트 두 개 전송
def get_response(system_prompt, user_prompt):

messages = [ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}]
response = client.chat.completions.create( model="gpt-4o-mini", messages=messages, temperature=0, ) return response.choices[0].message.content
system_prompt = "<SYSTEM_PROMPT>" user_prompt = "<USER_PROMPT>" print(get_response(system_prompt, user_prompt))
OpenAI API로 배우는 프롬프트 엔지니어링

시스템 메시지: 목적 정의

system_prompt = "You are a chatbot that answers financial questions."

user_prompt = "Who are you?" print(get_response(system_prompt, user_prompt))
I'm a financial chatbot that answers financial questions. How can I help you?
  • 챗봇이 도메인에 맞는 답변을 제공하도록 허용
  • 목적 미정의 시 맥락에 맞지 않는 답변이 발생할 수 있음
OpenAI API로 배우는 프롬프트 엔지니어링

시스템 메시지: 응답 지침

  • 대상 독자, 어조, 길이, 구조 지정
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective"""

user_prompt = "What do you think about cryptocurrencies?" print(get_response(system_prompt, user_prompt))
OpenAI API로 배우는 프롬프트 엔지니어링

시스템 메시지: 응답 지침

Cryptocurrencies are digital or virtual currencies that use cryptography for security and operate on 
decentralized networks based on blockchain technology. 
[...]

Advantages of cryptocurrencies include: - Decentralization: [...] - Security:[...] - Global Accessibility: [...]
However, there are also notable concerns: - Volatility: [...] - Regulatory Uncertainty: [...] - Lack of Consumer Protection: [...]
In summary, cryptocurrencies have the potential to offer various benefits, but their adoption and impact on the financial landscape are still evolving [...]
OpenAI API로 배우는 프롬프트 엔지니어링

시스템 메시지: 동작 지침

  • 질문에 응답하는 조건부 프롬프트
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.


"""
OpenAI API로 배우는 프롬프트 엔지니어링

시스템 메시지: 동작 지침

  • 질문에 응답하는 조건부 프롬프트
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.
If the question you receive is within the financial field, answer it to the best of your knowledge.

"""
OpenAI API로 배우는 프롬프트 엔지니어링

시스템 메시지: 동작 지침

  • 질문에 응답하는 조건부 프롬프트
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.
If the question you receive is within the financial field, answer it to the best of your knowledge.
Otherwise, answer with 'Sorry, I only know about finance.'
"""
user_prompt = "How's the weather today?"

print(get_response(system_prompt, user_prompt))
Sorry, I only know about finance.
OpenAI API로 배우는 프롬프트 엔지니어링

연습해 봅시다!

OpenAI API로 배우는 프롬프트 엔지니어링

Preparing Video For Download...