GPT API 사용해보기 (JS)

gptJavaScriptAPIopenai
avatar
2025.04.10
·
1 min read

1. OpenAi 사이트 접속

https://openai.com/

2. 회원가입 및 로그인 후 마이페이지 이동

https://platform.openai.com/account/org-settings

3. API 정보 확인

  1. Settings -> Organization ID 확인

  2. API keys -> Create new secret key 버튼 클릭 후 API 키 발급 -> KEY 확인

4. 코드 작성

const { Configuration, OpenAIApi } = require('openai');

const config = new Configuration({
  apiKey: '*******',
  organization: '*******'
});
const openai = new OpenAIApi(config);
const form = {
  model: 'gpt-3.5-turbo',
  messages: [{ role: 'user', content: '질문할 내용 작성' }],
};

openai.createChatCompletion(form).then(({ data }) => {
  let answerList = data?.choices ?? [];
  if (!answerList.length) return '답변 없음';
  let answer = answerList[0]?.message?.content ?? '';
  let result = `답변: ${answer}`;
  return result;
});






- 컬렉션 아티클