검색
검색
공개 노트 검색
회원가입로그인

OpenAI ChatGPT API V1 변경 사항 (자바스크립트 버전)

OpenAI 에서 openai 모듈이 버전 1으로 정식 버전으로 출시되면서 문법이 바뀌었습니다. 그래서 이전 코드로 하면 에러가 납니다. (11월 6일 v1 버전 출시) 생각보다 이 문제를 겪으시는 분들이 많은 것 같아서 따로 정리해 보았습니다다. 미리 말씀 드리면 Configuration 등이 없어졌습니다.

에러 메시지

import { Configuration, OpenAIApi } from "openai";
         ^^^^^^^^^^^^^
SyntaxError: The requested module 'openai' does not provide an export named 'Configuration'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

javascript 버전으로 진행합니다.

이전 코드

import { Configuration, OpenAIApi } from "openai"
import dotenv from "dotenv"
dotenv.config();

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});

const openai = new OpenAIApi(configuration);
const chat = await openai.createChatCompletion({
  model: "gpt-3.5-turbo",
  messages: [
    { role: "system", content: "You are the philosopher Socrates specialized in answering philosophical questions. Talk in friendly manner." },
    { role: "user", content: "What is the meaning of life?" },
    {
      role: "assistant",
      content: "You have to find meaning of your life. it's good to live.",
    },
    { role: "user", content: "How can I find the meaning?" },
  ],
});
console.log(chat.data.choices[0].message.content);

바뀐 코드

import OpenAI from "openai"
import dotenv from "dotenv"
dotenv.config();

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

const chat = await openai.chat.completions.create({
  model: "gpt-3.5-turbo",
  messages: [
    { role: "system", content: "You are the philosopher Socrates specialized in answering philosophical questions. Talk in friendly manner." },
    { role: "user", content: "What is the meaning of life?" },
    {
      role: "assistant",
      content: "You have to find meaning of your life. it's good to live.",
    },
    { role: "user", content: "How can I find the meaning?" },
  ],
});
console.log(chat.choices[0].message.content);

이렇게 바뀌었습니다. Configuration 이 없어지고 chat.completions.create 로 호출하는 부분이 바뀌었습니다. 그리고 결과 부분에 chat.data.choices[0].message.content 에서 data 부분이 빠졌습니다. 그래서 chat.choices[0].message.content 로 바로 접근하시면 됩니다.

공유하기
카카오로 공유하기
페이스북 공유하기
트위터로 공유하기
url 복사하기
조회수 : 280
heart
T
페이지 기반 대답
AI Chat