Skip to content

Commit

Permalink
refactor & feat: ITP-47 imple endpoint /api/quiz/formula-one
Browse files Browse the repository at this point in the history
  • Loading branch information
RickyRAV committed Oct 15, 2023
1 parent 329fc70 commit 29faf52
Showing 1 changed file with 42 additions and 41 deletions.
83 changes: 42 additions & 41 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,49 @@ const OpenAI = require('openai');
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const app = express();

app.use(express.json());
app.use(cors());
const configureMiddlewares = () => {
app.use(express.json());
app.use(cors());
};

// Swagger config
const swaggerOptions = {
swaggerDefinition: {
info: {
title: 'IntelliQ-BE API',
version: '2.0.0',
description: 'API for IntelliQ-BE',
const configureSwagger = () => {
const swaggerOptions = {
swaggerDefinition: {
info: {
title: 'IntelliQ-BE API',
version: '2.0.0',
description: 'API for IntelliQ-BE',
},
},
},
apis: ['server.js'],
apis: ['server.js'],
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
};

const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));

app.get('/', async (req, res) => {
res.send('Welcome to the IntelliQ-BE API!');
});

// Create OpenAI instance
const openai = new OpenAI(OPENAI_API_KEY);
app.get('/getQuiz', async (req, res) => {
const { interests, numberOfQuestions } = req.query;
const generateQuizQuestions = async (interests, numberOfQuestions) => {
const prompt = `Generate ${numberOfQuestions} multiple-choice quiz questions based on the user's interests in
${interests}. Each question should come with four answer options labeled a, b, c, and d. Include the correct answer
for each question, beginning the line with 'Answer:'.
The correct answer should be formatted exactly like its corresponding option.`;

for each question, beginning the line with 'Answer:'. The correct answer should be formatted exactly like its corresponding option.`;
const openai = new OpenAI(OPENAI_API_KEY);
const response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [
{
'role': 'system',
'content': prompt
},
{
'role': 'user',
'content': `${interests}, ${numberOfQuestions} questions`
}
],
messages: [{
'role': 'system',
'content': prompt
}, {
'role': 'user',
'content': `${interests}, ${numberOfQuestions} questions`
}],
temperature: 1,
max_tokens: 512,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});

const rawContent = response.choices[0].message.content;
const rawQuestions = rawContent.split('\n\n');

const formattedQuestions = rawQuestions.map((q) => {
return rawQuestions.map((q) => {
const parts = q.split('\n');
const questionText = parts[0];
const options = parts.slice(1, -1);
Expand All @@ -73,18 +62,30 @@ app.get('/getQuiz', async (req, res) => {
correctAnswer: correctAnswer
};
});
};

res.json({ questions: formattedQuestions });
app.get('/api/welcome', async (req, res) => {
res.send('Welcome to the IntelliQ-BE API!');
});

app.get('/api/quiz', async (req, res) => {
const { interests, numberOfQuestions } = req.query;
const questions = await generateQuizQuestions(interests, numberOfQuestions);
res.json({ questions });
});

app.get('/api/quiz/formula-one', async (req, res) => {
const questions = await generateQuizQuestions('formula-one', 4);
res.json({ questions });
});

// init jokes and start server
const port = process.env.PORT || 3000;
const startServer = async () => {
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
};

configureMiddlewares();
configureSwagger();
startServer().catch(e => console.error(e));

0 comments on commit 29faf52

Please sign in to comment.