aiFollowupQuestion
parent
060e6b0f1a
commit
da33f2ed43
File diff suppressed because it is too large
Load Diff
|
@ -63,7 +63,7 @@
|
||||||
"joi": "^17.3.0",
|
"joi": "^17.3.0",
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^8.5.1",
|
||||||
"moment": "^2.24.0",
|
"moment": "^2.24.0",
|
||||||
"mongoose": "^5.7.7",
|
"mongoose": "^8.7.1",
|
||||||
"morgan": "^1.9.1",
|
"morgan": "^1.9.1",
|
||||||
"mysql2": "^3.11.0",
|
"mysql2": "^3.11.0",
|
||||||
"nodemailer": "^6.3.1",
|
"nodemailer": "^6.3.1",
|
||||||
|
|
|
@ -38,7 +38,7 @@ module.exports = {
|
||||||
mongoose: {
|
mongoose: {
|
||||||
url: envVars.MONGODB_URL + (envVars.NODE_ENV === 'test' ? '-test' : ''),
|
url: envVars.MONGODB_URL + (envVars.NODE_ENV === 'test' ? '-test' : ''),
|
||||||
options: {
|
options: {
|
||||||
useCreateIndex: true,
|
// useCreateIndex: true,
|
||||||
useNewUrlParser: true,
|
useNewUrlParser: true,
|
||||||
useUnifiedTopology: true,
|
useUnifiedTopology: true,
|
||||||
},
|
},
|
||||||
|
|
|
@ -0,0 +1,94 @@
|
||||||
|
const aiFollowupQuestion = async (req, res) => {
|
||||||
|
|
||||||
|
const { MongoClient } = require('mongodb');
|
||||||
|
// const fetch = require('node-fetch');
|
||||||
|
const { v4: uuidv4 } = require('uuid');
|
||||||
|
const url = process.env.MONGODB_URL;
|
||||||
|
const dbName = process.env.MONGO_DB_NAME;
|
||||||
|
const client = new MongoClient(url);
|
||||||
|
|
||||||
|
await client.connect();
|
||||||
|
const database = client.db(dbName); // Replace with your database name
|
||||||
|
const conversationsCollection = database.collection('conversations'); // Replace with your collection name
|
||||||
|
|
||||||
|
async function fetchOpenAICompletion(prompt, messages, model = "gpt-4o-mini", max_tokens = 200) {
|
||||||
|
const response = await fetch('https://api.openai.com/v1/chat/completions', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${process.env.OPENAI_KEY}`,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
model: model,
|
||||||
|
messages: messages,
|
||||||
|
max_tokens: max_tokens,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
return data.choices[0].message.content;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { prompt, sessionId, model = "gpt-4o-mini", max_tokens = 200 } = req.body;
|
||||||
|
if (!conversationsCollection) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
error: 'MongoDB is not connected yet. Please try again later.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let conversation;
|
||||||
|
|
||||||
|
if (!sessionId) {
|
||||||
|
const newSessionId = uuidv4();
|
||||||
|
|
||||||
|
conversation = {
|
||||||
|
sessionId: newSessionId,
|
||||||
|
conversationHistory: [
|
||||||
|
{ role: 'system', content: 'You are a helpful assistant.' },
|
||||||
|
{ role: 'user', content: prompt },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
await conversationsCollection.insertOne(conversation);
|
||||||
|
} else {
|
||||||
|
conversation = await conversationsCollection.findOne({ sessionId: sessionId });
|
||||||
|
if (!conversation) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Invalid session ID.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
conversation.conversationHistory.push({ role: 'user', content: prompt });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call OpenAI API with the conversation history
|
||||||
|
const aiResponse = await fetchOpenAICompletion(prompt, conversation.conversationHistory, model, max_tokens);
|
||||||
|
|
||||||
|
// Add the AI's response to the conversation history
|
||||||
|
conversation.conversationHistory.push({ role: 'assistant', content: aiResponse });
|
||||||
|
|
||||||
|
// Update the conversation in MongoDB
|
||||||
|
await conversationsCollection.updateOne(
|
||||||
|
{ sessionId: conversation.sessionId },
|
||||||
|
{ $set: { conversationHistory: conversation.conversationHistory } }
|
||||||
|
);
|
||||||
|
|
||||||
|
// Send the AI's response and session ID back to the frontend
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
data: aiResponse,
|
||||||
|
sessionId: conversation.sessionId, // Return the session ID for subsequent queries
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error generating response:', error.message);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Something went wrong. Please try again later.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = aiFollowupQuestion;
|
|
@ -25,6 +25,7 @@ const generateQuestions = require("../api/generateQuestions");
|
||||||
const saveGameScore = require("../api/saveGameScore");
|
const saveGameScore = require("../api/saveGameScore");
|
||||||
const aiFeedbackOnReport = require("../api/aiFeedbackOnReport");
|
const aiFeedbackOnReport = require("../api/aiFeedbackOnReport");
|
||||||
const aiFeedbackActionParent = require("../api/aiFeedbackActionParent");
|
const aiFeedbackActionParent = require("../api/aiFeedbackActionParent");
|
||||||
|
const aiFollowupQuestion = require("../api/aiFollowupQuestion");
|
||||||
|
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
@ -155,6 +156,11 @@ router.get("/ping", (req, res) => {
|
||||||
aiFeedbackActionParent(req, res);
|
aiFeedbackActionParent(req, res);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// AI top ten action items for a parent of this child.
|
||||||
|
router.post("/aiFollowupQuestion", (req, res) => {
|
||||||
|
aiFollowupQuestion(req, res);
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue