I am trying to build an assistant with openAI. Everything is complete but when I am trying to make a conversation its showing an error.
`require('dotenv').config();
const express = require('express');
const { OpenAI } = require('openai');
const app = express();
const port = 3000;
app.use(express.json()); // Parse JSON bodies
// Initialize the OpenAI API client with your API key
const apiKey = process.env.OPENAI_API_KEY;
const openaiClient = new OpenA`your text`I(apiKey);
// API endpoint to ask the assistant a question
app.post('/ask', async (req, res) => {
try {
const { question } = req.body;
const response = await openaiClient.complete({
engine: "davinci", // Adjust this based on the engine you want to use
prompt: question,
max_tokens: 150,
});
const assistantReply = response.data.text.trim();
res.json({ reply: assistantReply });
} catch (error) {
console.error('Error asking question:', error);
res.status(500).json({ error: 'Failed to ask question' });
}
});
// Start the Express server
app.listen(port, () => {
console.log(`Server is listening at http://localhost:${port}`);
});
#error
Error asking question: TypeError: openaiClient.complete is not a function
at /Users/md.golamrabbani/Desktop/ai-assisnt/index.js:18:45
at Layer.handle [as handle_request] (/Users/md.golamrabbani/Desktop/ai-assisnt/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/md.golamrabbani/Desktop/ai-assisnt/node_modules/express/lib/router/route.js:149:13)
at Route.dispatch (/Users/md.golamrabbani/Desktop/ai-assisnt/node_modules/express/lib/router/route.js:119:3)
at Layer.handle [as handle_request] (/Users/md.golamrabbani/Desktop/ai-assisnt/node_modules/express/lib/router/layer.js:95:5)
at /Users/md.golamrabbani/Desktop/ai-assisnt/node_modules/express/lib/router/index.js:284:15
at Function.process_params (/Users/md.golamrabbani/Desktop/ai-assisnt/node_modules/express/lib/router/index.js:346:12)
at next (/Users/md.golamrabbani/Desktop/ai-assisnt/node_modules/express/lib/router/index.js:280:10)
at /Users/md.golamrabbani/Desktop/ai-assisnt/node_modules/body-parser/lib/read.js:137:5
at AsyncResource.runInAsyncScope (node:async_hooks:199:9)
`
Golam Rabbani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.