I am trying to implement a feature where users can input their own api and this is how i implement it:
import express from 'express';
import * as dotenv from 'dotenv';
import cors from 'cors';
import { Configuration, OpenAIApi } from 'openai';
dotenv.config();
const app = express();
app.use(cors());
app.use(express.json());
app.get('/', async (req, res) => {
res.status(200).send({
message: 'Hello!',
})
});
app.post('/', async (req, res) => {
try {
const prompt = req.body.prompt;
const bot_type = req.body.bot_type;
const API_KEY = req.body.custom_api || process.env.OPENAI_API_KEY;
const configuration = new Configuration({
apiKey: API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
//messages here blah blah..
],
temperature: 0.2,
max_tokens: 1500,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});
console.error(response.data.choices[0].message.content)
res.status(200).send({
bot: response.data.choices[0].message.content
});
} catch (error) {
console.error(error)
res.status(500).send(error || `Something went wrong, please try again.`);
}
})
app.listen(5500, () => console.log('AI server started on http://localhost:5500'))