구조화된 출력과 조건부 프롬프트

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

Fouad Trad

Machine Learning Engineer

구조화된 출력

  • LLM은 명시적 지시가 있을 때만 구조화된 출력을 생성함
  • 출력 구조 유형:
    • 목록
    • 구조화된 단락
    • 사용자 정의 형식

구조화된 출력을 요청하면 지정한 구조로 응답이 생성되지만, 구조를 지정하지 않으면 무작위 또는 비구조적 응답이 생성됨을 보여주는 다이어그램.

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

  • 필요한 열을 명확히 지정
prompt = "Generate a table containing 5 movies I should watch if I am an action 
lover, with columns for Title and Rating."
print(get_response(prompt))
| Title                           | Rating |
|---------------------------------|--------|
| Mad Max: Fury Road              | 8.1    |
| John Wick                       | 7.4    |
| The Dark Knight                 | 9.0    |
| Die Hard                        | 8.2    |
| Gladiator                       | 8.5    |
OpenAI API로 배우는 프롬프트 엔지니어링

목록

  • 열거에 유용
prompt = "Generate a list containing the names of the top 5 cities to visit."
print(get_response(prompt))
1. Tokyo, Japan
2. Paris, France
3. Rome, Italy
4. New York City, United States
5. Sydney, Australia
OpenAI API로 배우는 프롬프트 엔지니어링

목록

  • 번호 매기기 요구사항을 프롬프트에 명시
prompt = "Generate an unordered list containing the names of the top 5 cities to visit."
print(get_response(prompt))
- Paris, France
- Tokyo, Japan
- Rome, Italy
- New York City, United States
- Sydney, Australia
OpenAI API로 배우는 프롬프트 엔지니어링

구조화된 단락

  • 구조 요구사항을 프롬프트에 명시
prompt = "Provide a structured paragraph with clear headings and subheadings 
about the benefits of regular exercise on overall health and well-being."
print(get_response(prompt))
OpenAI API로 배우는 프롬프트 엔지니어링

구조화된 단락

I. Introduction
Regular exercise is essential for maintaining good health and overall well-being. [...]

II. Physical Health Benefits
a. Weight Management: Regular exercise is [...]
b. Reduced Risk of Chronic Diseases: Exercise has been linked to [...]
c. Increased Strength and Flexibility: Regular exercise improves muscle strength and [...]

III. Mental Health Benefits
a. Reduced Stress and Anxiety: Exercise has a profound impact on [...]
b. Improved Sleep Quality: Regular exercise is associated with improved sleep patterns [...]
c. Boosted Brain Function: Studies show that exercise enhances cognitive function [...]
OpenAI API로 배우는 프롬프트 엔지니어링

사용자 정의 출력 형식

text = "Once upon a time in a quaint little village, there lived a curious young boy named 
David. David was [...]"

instructions = "You will be provided with a text delimited by triple backticks. Generate a suitable title for it. "
output_format = """Use the following format for the output: - Text: <text we want to title> - Title: <the generated title>"""
prompt = instructions + output_format + f"```{text}```" print(get_response(prompt))
- Text: Once upon a time in a quaint little village, there lived a curious young boy [...]
- Title: The Extraordinary Adventure of David and the Mysterious Book
OpenAI API로 배우는 프롬프트 엔지니어링

조건부 프롬프트

  • 논리 또는 조건 포함 가능
  • 조건부 프롬프트는 if-else 방식을 따름

조건 테스트를 나타내는 다이어그램. 조건이 참이면 X를 수행하고, 거짓이면 Y를 수행함.

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

조건부 프롬프트

text = "Le printemps est ma saison préférée. Quand les premières fleurs commencent 
à éclore, et que les arbres se parent de feuilles vertes et tendres, je me sens 
revivre [...] "
prompt = f"""You will be provided with a text delimited by triple backticks. 
         If the text is written in English, suggest a suitable title for it.
         Otherwise, write 'I only understand English'.
         ```{text}```"""

print(get_response(prompt))
I only understand English
OpenAI API로 배우는 프롬프트 엔지니어링

조건부 프롬프트

  • 여러 조건 포함 가능
text = "In the heart of the forest, sunlight filters through the lush green canopy, 
creating a tranquil atmosphere [...] "
prompt = f"""You will be provided with a text delimited by triple backticks. 
         If the text is written in English, check if it contains the keyword 'technology'. 


         ```{text}```"""
print(get_response(prompt))
OpenAI API로 배우는 프롬프트 엔지니어링

조건부 프롬프트

  • 여러 조건 포함 가능
text = "In the heart of the forest, sunlight filters through the lush green canopy, 
creating a tranquil atmosphere [...] "
prompt = f"""You will be provided with a text delimited by triple backticks. 
         If the text is written in English, check if it contains the keyword 'technology'. 
         If it does, suggest a suitable title for it, otherwise, write 'Keyword not found'.

         ```{text}```"""
print(get_response(prompt))
OpenAI API로 배우는 프롬프트 엔지니어링

조건부 프롬프트

  • 여러 조건 포함 가능
text = "In the heart of the forest, sunlight filters through the lush green canopy, 
creating a tranquil atmosphere [...] "
prompt = f"""You will be provided with a text delimited by triple backticks. 
         If the text is written in English, check if it contains the keyword 'technology'. 
         If it does, suggest a suitable title for it, otherwise, write 'Keyword not found'.
         If the text is not written in English, reply with 'I only understand English'.
         ```{text}```"""
print(get_response(prompt))
Keyword not found
OpenAI API로 배우는 프롬프트 엔지니어링

연습해 봅시다!

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

Preparing Video For Download...