본문 바로가기
🎪 놀고있네/AI

[OpenAI] Prompt 사용하기(.json)

by 냥장판 2025. 7. 21.
반응형

 

 

OpenAI 채팅 API에서 메시지 역할(Message Roles)과 지시문 준수(Instruction Following)를 잘 따르면,
“누가”, “무엇을”, “어떻게” 말하는지를 명확하게 구분할 수 있고, LLM을 보다 통제 가능하고 예측 가능한 방식으로 사용할 수 있다.

 

Prompt Engineering 관련글 참고

2025.07.07 - [🎪 놀고있네/OpenAI] - [OpenAI] Prompt Engineering - instructions

 

[OpenAI] Prompt Engineering - instructions

어떻게 묻느냐가 AI의 무엇을, 어떻게 답할지를 결정한다. 프롬프트 엔지니어링(Prompt Engineering)이란, AI—특히 대형 언어 모델(LLM)—에게 원하는 답을 얻기 위해 “어떤 질문을, 어떤 방식으로”

miaow-miaow.tistory.com

 

 

 

.json 로드하기

 

미리 작성해둔 프롬프트 json 파일이다.

 

아래 코드에 추가해서 실행

import json
from openai import OpenAI

client = OpenAI()

# 프롬프트 파일 읽기
with open("prompts/weather_report.json", "r", encoding="utf-8") as f:
    system_prompt_msg = json.load(f)

response = client.chat.completions.create(
    model="gpt-4.1",
    messages=[
        system_prompt_msg,
        {"role": "user", "content": "오늘 한국의 날씨를 알려줘"}
    ]
)

print(response) # 전체 response 를 얻어옴
print(response.choices[0].message.content)  # 답변만 얻어옴

 

https://platform.openai.com/docs/guides/text#message-roles-and-instruction-following

https://platform.openai.com/docs/guides/text#reusable-prompts

반응형

댓글