본문 바로가기
Coding/TIL & 배운것들

Prompt 실습

by 코딩짜는 머글 2024. 11. 15.

다양한 파라미터 적용해보기!


response_format

모델이 출력해야 하는 형식을 지정하는 객체이다. GPT-4o, GPT-4o-mini, GPT-3.5-turbo-1106 모델에서 호환된다. 

{ "type": "json_object" }를 사용하여 모델이 생성하는 메시지가 유효한 JSON인지 확인할 수 있다. 

from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
    model="gpt-3.5-turbo-1106",
    response_format={ "type": "json_object"},
    messages=[
        {"role": "system", "content": "You are a helpful assistant designed to output JSON"},
        {
            "role": "user", "content": "please recommend 5 classic novels"
        }
    ],
    temperature=0.3
)

print(completion.choices[0].message.content)
# 출력 
{
  "classic_novels": [
    "Pride and Prejudice by Jane Austen",
    "To Kill a Mockingbird by Harper Lee",
    "1984 by George Orwell",
    "Moby Dick by Herman Melville",
    "The Great Gatsby by F. Scott Fitzgerald"
  ]
}

이런식으로 출력이 JSON으로 된것을 확인할 수 있다. 


 

N

각 입력 메시지에 대해 생성할 채팅의 수이다. 기본값은 1이다.

completion = client.chat.completions.create(
    model="gpt-3.5-turbo-1106",
    messages=[
        {"role": "system", "content": "You are a creative poet."},
        {
            "role": "user", "content": "한줄짜리 시를 지어줘"
        }
    ],
    temperature=0.7,
    n=2
)

print(completion.choices)

# 출력
작은 별들이 빛나는 밤, 당신의 꿈을 따라 날아가줄게요.
너와 함께 피어난 봄꽃처럼, 사랑은 아름다움을 안겨준다.

 

출력값이 2개가 된것을 확인할 수 있다. 


 

Stop 

특정 상황을 string으로 전달하여 해당 생황에 답변을 멈추도록 하는 기능. string 또는 array<string>으로 전달할 수 있다. 

completion = client.chat.completions.create(
    model="gpt-3.5-turbo-1106",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {
            "role": "user", "content": "openai의 장점을 말해줘"
        }
    ],
    temperature=0.7,
    stop='3'
)

print(completion.choices[0].message.content)

# 출력
OpenAI의 장점은 다음과 같습니다:
1. 혁신적인 기술: OpenAI는 자연어 처리, 기계 학습 및 인공 지능 분야에서 선도적인 기술을 개발하고 있습니다.
2. 오픈 소스: OpenAI는 많은 프로젝트와 라이브러리를 오픈 소스로 제공하여 개발자들이 자유롭게 활용할 수 있습니다.

 

Seed 

모델의 출력을 생성할 때 랜덤성을 제어하기 위한 파라미터이다. 동일한 값을 사용하면 동일한 입력에 대해 항상 같은 출력을 얻을 수 있어, 실험의 재현성을 높일 수 있다. 하지만 단순 시드값이 같다해도 모델 구조, 학습 하이퍼파라미터, 데이터 전처리 방식 등의 차이로 최적의 결과가 나오지 않을 수 있다. 

'Coding > TIL & 배운것들' 카테고리의 다른 글

Prompt Engineering 평가 기준 설정  (0) 2024.11.17
Prompt Development Cycle  (2) 2024.11.16
Prompt Engineering 기초  (1) 2024.11.14
prompt engineering vs RAG vs Fine-tuning  (1) 2024.11.13
파이썬 코딩 컨벤션  (0) 2024.11.13