Validation step styles and functionality tweaks, add initial AI functionality

This commit is contained in:
2025-02-20 15:11:14 -05:00
parent bba7362641
commit 45a52cbc33
9 changed files with 800 additions and 129 deletions

View File

@@ -0,0 +1,75 @@
const express = require('express');
const router = express.Router();
const OpenAI = require('openai');
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
// Helper function to create the prompt for product validation
function createValidationPrompt(products) {
return `You are a product data validation assistant. Please review the following product data and suggest corrections or improvements. Focus on:
1. Standardizing product names and descriptions
2. Fixing any obvious errors in measurements, prices, or quantities
3. Ensuring consistency in formatting
4. Flagging any suspicious or invalid values
Here is the product data to validate:
${JSON.stringify(products, null, 2)}
Please respond with:
1. The corrected product data in the exact same JSON format
2. A list of changes made and why
3. Any warnings or suggestions for manual review
Respond in the following JSON format:
{
"correctedData": [], // Array of corrected products
"changes": [], // Array of changes made
"warnings": [] // Array of warnings or suggestions
}`;
}
router.post('/validate', async (req, res) => {
try {
const { products } = req.body;
if (!Array.isArray(products)) {
return res.status(400).json({ error: 'Products must be an array' });
}
const prompt = createValidationPrompt(products);
const completion = await openai.chat.completions.create({
model: "gpt-4-turbo-preview",
messages: [
{
role: "system",
content: "You are a product data validation assistant that helps ensure product data is accurate, consistent, and properly formatted."
},
{
role: "user",
content: prompt
}
],
temperature: 0.3, // Lower temperature for more consistent results
max_tokens: 4000,
response_format: { type: "json_object" }
});
const aiResponse = JSON.parse(completion.choices[0].message.content);
res.json({
success: true,
...aiResponse
});
} catch (error) {
console.error('AI Validation Error:', error);
res.status(500).json({
success: false,
error: error.message || 'Error during AI validation'
});
}
});
module.exports = router;

View File

@@ -18,6 +18,7 @@ const vendorsRouter = require('./routes/vendors');
const categoriesRouter = require('./routes/categories');
const testConnectionRouter = require('./routes/test-connection');
const importRouter = require('./routes/import');
const aiValidationRouter = require('./routes/ai-validation');
// Get the absolute path to the .env file
const envPath = path.resolve(process.cwd(), '.env');
@@ -93,6 +94,7 @@ async function startServer() {
app.use('/api/vendors', vendorsRouter);
app.use('/api/categories', categoriesRouter);
app.use('/api/import', importRouter);
app.use('/api/ai-validation', aiValidationRouter);
app.use('/api', testConnectionRouter);
// Basic health check route