라이브러리
OpenAI API(GPT) 사용법
순코딩
2025. 3. 30. 21:34
코드
import OpenAI from "openai";
// OpenAI 클라이언트 초기화
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
// OpenAI API 호출
const completion = await openai.chat.completions.create({
model: "gpt-4o", // 사용할 AI 모델 지정
messages: [
// 시스템 메시지로 AI의 역할과 응답 형식 정의
{ role: "system", content: systemPrompt },
// 사용자 메시지로 케이스 정보 전달
{ role: "user", content: userPrompt },
],
temperature: 0.7, // 응답의 창의성 조절 (0: 결정적, 1: 창의적)
response_format: { type: "json_object" }, // JSON 형식 응답 강제
});
// AI 응답 추출
const aiResponse = completion.choices[0].message.content;