ChatGPT API로 프롬프트 템플릿 시스템 구축하기
배경
ChatGPT가 공개되고 한 달 정도 지나면서 팀 내에서 AI 활용 요청이 늘어났다. 고객 문의 분류, 코드 리뷰 자동화, 문서 요약 등 다양한 use case가 있었지만, 매번 프롬프트를 수동으로 작성하는 건 비효율적이었다.
프롬프트 템플릿 설계
변수 치환 방식으로 간단하게 시작했다.
interface PromptTemplate {
id: string;
template: string;
variables: string[];
}
function renderPrompt(template: PromptTemplate, values: Record<string, string>): string {
let result = template.template;
template.variables.forEach(variable => {
result = result.replace(new RegExp(`{{${variable}}}`, 'g'), values[variable] || '');
});
return result;
}
실제 활용 예시
고객 문의 분류 템플릿을 만들었다.
const classifyTemplate: PromptTemplate = {
id: 'customer-inquiry-classifier',
template: `다음 고객 문의를 카테고리로 분류해주세요.
문의 내용: {{inquiry}}
가능한 카테고리: {{categories}}
JSON 형식으로 답변해주세요: {"category": "...", "confidence": 0.0~1.0}`,
variables: ['inquiry', 'categories']
};
OpenAI API 연동
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
async function executePrompt(prompt: string) {
const response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: prompt }],
temperature: 0.3
});
return response.choices[0].message.content;
}
배운 점
- temperature 값이 결과 일관성에 큰 영향을 미쳤다. 분류 작업은 0.3, 생성 작업은 0.7~0.9가 적당했다.
- JSON 응답을 요청할 때도 파싱 실패 케이스가 있어서 retry 로직이 필요했다.
- 프롬프트 버저닝이 중요하다는 걸 깨달았다. 같은 템플릿도 계속 개선되기 때문에 v1, v2 형태로 관리하기 시작했다.
다음 단계로는 프롬프트 A/B 테스트와 성능 모니터링을 추가할 계획이다.