galleryApi
parent
41c2ceb675
commit
7252cfcce4
|
@ -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;
|
|
@ -0,0 +1,116 @@
|
|||
const aiMarkDrawing = async (req, res) => {
|
||||
|
||||
|
||||
const express = require('express');
|
||||
const fetch = require('node-fetch'); // For making HTTP requests
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
// Create an Express app
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
|
||||
// Middleware to handle JSON and URL-encoded form data
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
// Replace with your OpenAI API key
|
||||
const OPENAI_API_KEY = 'your-openai-api-key';
|
||||
|
||||
// Route to handle image upload and forward it to OpenAI API
|
||||
app.post('/upload-image', async (req, res) => {
|
||||
try {
|
||||
// Check if the file is included in the request
|
||||
if (!req.headers['content-type']?.includes('multipart/form-data')) {
|
||||
return res.status(400).send({ message: 'No file uploaded' });
|
||||
}
|
||||
|
||||
// Extract the raw body data (binary) from the request
|
||||
let imageBuffer = Buffer.alloc(0);
|
||||
|
||||
// Collect the image data from the stream (raw body)
|
||||
req.on('data', chunk => {
|
||||
imageBuffer = Buffer.concat([imageBuffer, chunk]);
|
||||
});
|
||||
|
||||
req.on('end', async () => {
|
||||
// Forward the image to OpenAI's API for scoring
|
||||
const score = await sendImageToOpenAI(imageBuffer);
|
||||
|
||||
// Respond with the score
|
||||
res.json({ score: score });
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error processing the image:', error);
|
||||
res.status(500).send({ message: 'Internal Server Error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Function to send image to OpenAI API and get the response
|
||||
async function sendImageToOpenAI(imageBuffer) {
|
||||
try {
|
||||
// Convert binary image data to base64 format
|
||||
const imageBase64 = imageBuffer.toString('base64');
|
||||
|
||||
// Make a request to the OpenAI API using fetch
|
||||
const response = await fetch('https://api.openai.com/v1/chat/completions', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${OPENAI_API_KEY}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: "gpt-4-vision", // Use the appropriate model
|
||||
messages: [
|
||||
{ role: "system", content: "You are a grading assistant for 2nd grade students." },
|
||||
{ role: "user", content: "Evaluate this student's drawing or coloring." }
|
||||
],
|
||||
functions: {
|
||||
function_call: {
|
||||
name: "vision_function",
|
||||
args: {
|
||||
image: imageBase64 // Send the image in base64 format
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Parse the response
|
||||
const data = await response.json();
|
||||
|
||||
// Extract the score or other relevant information from the response
|
||||
const aiResponse = data.choices[0].message.content;
|
||||
|
||||
// You can parse this response and extract the score, for example:
|
||||
const score = extractScoreFromResponse(aiResponse);
|
||||
return score;
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error interacting with OpenAI API:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Example function to extract a score from OpenAI's response (you need to define this based on the actual response structure)
|
||||
function extractScoreFromResponse(responseText) {
|
||||
// You can use a regex or logic to extract the score from OpenAI's response
|
||||
const scoreMatch = responseText.match(/score:\s*(\d+)/i);
|
||||
if (scoreMatch) {
|
||||
return parseInt(scoreMatch[1], 10);
|
||||
} else {
|
||||
// If no score is found, return a default value
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Start the server
|
||||
app.listen(port, () => {
|
||||
console.log(`Server running at http://localhost:${port}`);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
module.exports = aiMarkDrawing;
|
|
@ -26,6 +26,9 @@ const saveGameScore = require("../api/saveGameScore");
|
|||
const aiFeedbackOnReport = require("../api/aiFeedbackOnReport");
|
||||
const aiFeedbackActionParent = require("../api/aiFeedbackActionParent");
|
||||
const aiFollowupQuestion = require("../api/aiFollowupQuestion");
|
||||
const aiMarkDrawing = require("../api/aiMarkDrawing");
|
||||
const aiFeedbackOnReportWithFollowup = require("../api/aiFeedbackOnReportWithFollowup");
|
||||
|
||||
|
||||
|
||||
const router = express.Router();
|
||||
|
@ -150,17 +153,27 @@ router.get("/ping", (req, res) => {
|
|||
router.post("/aiFeedbackOnReport", (req, res) => {
|
||||
aiFeedbackOnReport(req, res);
|
||||
});
|
||||
// AI feedback for teacher on children's report card
|
||||
router.post("/aiFeedbackOnReportWithFollowup", (req, res) => {
|
||||
aiFeedbackOnReportWithFollowup(req, res);
|
||||
});
|
||||
|
||||
// AI top ten action items for a parent of this child.
|
||||
router.post("/aiFeedbackActionParent", (req, res) => {
|
||||
aiFeedbackActionParent(req, res);
|
||||
});
|
||||
|
||||
// AI top ten action items for a parent of this child.
|
||||
// AI to respond on initial and followup questions.
|
||||
router.post("/aiFollowupQuestion", (req, res) => {
|
||||
aiFollowupQuestion(req, res);
|
||||
});
|
||||
|
||||
// AI to provide star for drawing.
|
||||
router.post("/aiMarkDrawing", (req, res) => {
|
||||
aiMarkDrawing(req, res);
|
||||
});
|
||||
|
||||
|
||||
module.exports = router;
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue