I’m stuck with a problem when trying to use the GPT-3.5 API. The code keeps giving me errors about missing exports from the ‘openai’ module. Here’s what I’m dealing with:
import { AiConfig, AiClient } from 'ai-helper';
import { userAuth } from '@user/auth-lib';
import { WebResponse } from 'web-utils';
const aiSetup = new AiConfig({
key: process.env.AI_KEY
});
const ai = new AiClient(aiSetup);
export async function handleRequest(req) {
try {
const user = userAuth();
const { chatHistory } = await req.json();
if (!user) {
return new WebResponse('Not allowed', 401);
}
if (!aiSetup.key) {
return new WebResponse('AI key missing', 500);
}
if (!chatHistory) {
return new WebResponse('Chat history required', 400);
}
const aiResponse = await ai.chat({
model: 'smartbot-v2',
chatHistory
});
return WebResponse.json(aiResponse.message);
} catch (err) {
console.error('Chat error:', err);
return new WebResponse('Something went wrong', 500);
}
}
The error I’m getting says the ‘openai’ module doesn’t have ‘Configuration’ or ‘OpenAIApi’ exports. Any ideas what’s causing this? I’m really confused and could use some help figuring it out.
I’ve encountered similar issues before. The problem likely stems from using a custom AI library instead of the official OpenAI package. To resolve this, install the official OpenAI package using npm install openai, then update your code to import the appropriate modules with:
import { Configuration, OpenAIApi } from ‘openai’;
Set up the configuration with your API key and create the client:
const configuration = new Configuration({
apiKey: process.env.AI_KEY,
});
const openai = new OpenAIApi(configuration);
Replace your AI client calls with the relevant OpenAI API functions. For example, use createChatCompletion to get a response. This should address the export errors and align your code with the official API’s usage.
I’ve run into this exact issue before, and it can be pretty frustrating. The problem is likely stemming from using a custom AI library instead of the official OpenAI one. Here’s what worked for me:
First, ditch the custom ‘ai-helper’ library and install the official OpenAI package:
npm install openai
Then, update your imports to use the official package:
import { Configuration, OpenAIApi } from ‘openai’;
Next, you’ll need to set up the OpenAI client differently:
const configuration = new Configuration({
apiKey: process.env.AI_KEY,
});
const openai = new OpenAIApi(configuration);
Finally, replace your AI client calls with the appropriate OpenAI API methods. For chat completions, you’d use something like:
hey there! looks like ur using a custom AI library, not the official OpenAI one. thats prolly why ur getting those errors. maybe try switching to the official openai package? u can install it with npm install openai and then import it like import { Configuration, OpenAIApi } from ‘openai’. hope that helps!