import { config } from 'dotenv';
import { Configuration, OpenAIApi } from 'openai';
config();
export const callChatGPT = async (prompt) => {
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
try {
const openai = new OpenAIApi(configuration);
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: "Hello world" }],
});
return response.data.choices[0].message;
} catch (error) {
console.error('Error calling ChatGPT API:', error);
return null;
}
};
위처럼 코드를 작성하니 아래의 에러가 떳다.
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:134:21) at async ModuleJob.run (node:internal/modules/esm/module_job:217:5) at async ModuleLoader.import (node:internal/modules/esm/loader:323:24) at async loadESM (node:internal/process/esm_loader:28:7) at async handleMainPromise (node:internal/modules/run_main:113:12) |
원인을 찾아보니 버전이 틀려서 그런 것 같다.
아래 링크의 글을 보고 해결했다.
그래서 다음과 같이 수정했다.
import { OpenAI } from 'openai';
import { config } from 'dotenv';
config();
export const callChatGPT = async (prompt) => {
try {
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: prompt }],
});
return response.choices[0].message.content;
} catch (error) {
console.error('Error calling ChatGPT API:', error);
throw error;
}
};
'Server > node.js' 카테고리의 다른 글
[node.js] Error calling ChatGPT API: RateLimitError: 429 You exceeded your current quota, please check your plan and billing details. (1) | 2024.10.13 |
---|---|
[node.js] 서버 API 만들기 (0) | 2024.08.31 |
[node.js] express, restful api (0) | 2024.08.31 |
[node.js] Swagger 세팅 (0) | 2024.08.31 |
[node.js] DB 연동 (0) | 2024.08.31 |