I'm trying to do an API call to the GPT-3.5 API, but anything imported from OpenAI is giving me a has no exported member error.
import { Configuration, OpenAIApi } from "openai";
import { auth } from "@clerk/nextjs";
import { NextResponse } from "next/server";
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY
});
const openai = new OpenAIApi(configuration);
export async function POST(
req: Request
) {
try {
const { userId } = auth();
const body = await req.json();
const { messages } = body;
if (!userId) {
return new NextResponse("Unauthorised", { status: 401 });
}
if (!configuration.apiKey) {
return new NextResponse("OpenAI API Key not configured", { status: 500 });
}
if (!messages) {
return new NextResponse("Messages are required", { status: 400 });
}
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages
});
return NextResponse.json(response.data.choices[0].message);
} catch (error) {
console.log("[CONVERSATION_ERROR]", error)
return new NextResponse("Internal error", { status: 500 });
}
}
The error message is:
Module '"openai"' has no exported member 'Configuration'. Did you mean to use 'import Configuration from "openai"' instead?
warn ./app/api/conversation/route.ts
Attempted import error: 'Configuration' is not exported from 'openai' (imported as 'Configuration').
Import trace for requested module:
./app/api/conversation/route.ts
./app/api/conversation/route.ts
Attempted import error: 'OpenAIApi' is not exported from 'openai' (imported as 'OpenAIApi').
Import trace for requested module:
./app/api/conversation/route.ts
- error app\api\conversation\route.ts (5:22) @ eval
- error Error [TypeError]: openai__WEBPACK_IMPORTED_MODULE_0__.Configuration is not a constructor
at eval (webpack-internal:///(rsc)/./app/api/conversation/route.ts:11:23)
at (rsc)/./app/api/conversation/route.ts (C:\Users\ctcco\ai-saas\.next\server\app\api\conversation\route.js:312:1)
at __webpack_require__ (C:\Users\ctcco\ai-saas\.next\server\webpack-runtime.js:33:42)
at eval (webpack-internal:///(rsc)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fconversation%2Froute&page=%2Fapi%2Fconversation%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fconversation%2Froute.ts&appDir=C%3A%5CUsers%5Cctcco%5Cai-saas%5Capp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=C%3A%5CUsers%5Cctcco%5Cai-saas&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!:16:109)
at (rsc)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fconversation%2Froute&page=%2Fapi%2Fconversation%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fconversation%2Froute.ts&appDir=C%3A%5CUsers%5Cctcco%5Cai-saas%5Capp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=C%3A%5CUsers%5Cctcco%5Cai-saas&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D! (C:\Users\ctcco\ai-saas\.next\server\app\api\conversation\route.js:302:1)
at __webpack_require__ (C:\Users\ctcco\ai-saas\.next\server\webpack-runtime.js:33:42)
at __webpack_exec__ (C:\Users\ctcco\ai-saas\.next\server\app\api\conversation\route.js:2726:39)
at C:\Users\ctcco\ai-saas\.next\server\app\api\conversation\route.js:2727:28
at Object.<anonymous> (C:\Users\ctcco\ai-saas\.next\server\app\api\conversation\route.js:2730:3)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Module.require (node:internal/modules/cjs/loader:1141:19)
at require (node:internal/modules/cjs/helpers:110:18)
at requirePage (C:\Users\ctcco\ai-saas\node_modules\next\dist\server\require.js:112:75)
at C:\Users\ctcco\ai-saas\node_modules\next\dist\server\load-components.js:80:84
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async loadComponentsImpl (C:\Users\ctcco\ai-saas\node_modules\next\dist\server\load-components.js:80:26)
at async DevServer.findPageComponentsImpl (C:\Users\ctcco\ai-saas\node_modules\next\dist\server\next-server.js:439:36) {
digest: undefined
}
3 | import { NextResponse } from "next/server";
4 |
> 5 | const configuration = new Configuration({
| ^
6 | apiKey: process.env.OPENAI_API_KEY
7 | });
8 |
- warn ./app/api/conversation/route.ts
Attempted import error: 'Configuration' is not exported from 'openai' (imported as 'Configuration').
Import trace for requested module:
./app/api/conversation/route.ts
./app/api/conversation/route.ts
Attempted import error: 'OpenAIApi' is not exported from 'openai' (imported as 'OpenAIApi').
Import trace for requested module:
./app/api/conversation/route.ts
- wait compiling /_error (client and server)...
- warn ./app/api/conversation/route.ts
Attempted import error: 'Configuration' is not exported from 'openai' (imported as 'Configuration').
Import trace for requested module:
./app/api/conversation/route.ts
./app/api/conversation/route.ts
Attempted import error: 'OpenAIApi' is not exported from 'openai' (imported as 'OpenAIApi').
Import trace for requested module:
./app/api/conversation/route.ts
- warn ./app/api/conversation/route.ts
Attempted import error: 'Configuration' is not exported from 'openai' (imported as 'Configuration').
Import trace for requested module:
./app/api/conversation/route.ts
./app/api/conversation/route.ts
Attempted import error: 'OpenAIApi' is not exported from 'openai' (imported as 'OpenAIApi').
Import trace for requested module:
./app/api/conversation/route.ts
- wait compiling /(dashboard)/(routes)/conversation/page (client and server)...
- warn ./app/api/conversation/route.ts
Attempted import error: 'Configuration' is not exported from 'openai' (imported as 'Configuration').
Import trace for requested module:
./app/api/conversation/route.ts
./app/api/conversation/route.ts
Attempted import error: 'OpenAIApi' is not exported from 'openai' (imported as 'OpenAIApi').
Import trace for requested module:
./app/api/conversation/route.ts
- wait compiling...
- warn ./app/api/conversation/route.ts
Attempted import error: 'Configuration' is not exported from 'openai' (imported as 'Configuration').
Import trace for requested module:
./app/api/conversation/route.ts
./app/api/conversation/route.ts
Attempted import error: 'OpenAIApi' is not exported from 'openai' (imported as 'OpenAIApi').
Import trace for requested module:
./app/api/conversation/route.ts
Any idea why?