AI tweaks and make column name matching case insensitive
This commit is contained in:
@@ -33,15 +33,19 @@ Respond in the following JSON format:
|
||||
router.post('/validate', async (req, res) => {
|
||||
try {
|
||||
const { products } = req.body;
|
||||
console.log('🔍 Received products for validation:', JSON.stringify(products, null, 2));
|
||||
|
||||
if (!Array.isArray(products)) {
|
||||
console.error('❌ Invalid input: products is not an array');
|
||||
return res.status(400).json({ error: 'Products must be an array' });
|
||||
}
|
||||
|
||||
const prompt = createValidationPrompt(products);
|
||||
console.log('📝 Generated prompt:', prompt);
|
||||
|
||||
console.log('🤖 Sending request to OpenAI...');
|
||||
const completion = await openai.chat.completions.create({
|
||||
model: "gpt-4-turbo-preview",
|
||||
model: "gpt-4o-mini",
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
@@ -52,19 +56,50 @@ router.post('/validate', async (req, res) => {
|
||||
content: prompt
|
||||
}
|
||||
],
|
||||
temperature: 0.3, // Lower temperature for more consistent results
|
||||
temperature: 0.3,
|
||||
max_tokens: 4000,
|
||||
response_format: { type: "json_object" }
|
||||
});
|
||||
|
||||
const aiResponse = JSON.parse(completion.choices[0].message.content);
|
||||
console.log('✅ Received response from OpenAI');
|
||||
const rawResponse = completion.choices[0].message.content;
|
||||
console.log('📄 Raw AI response:', rawResponse);
|
||||
|
||||
const aiResponse = JSON.parse(rawResponse);
|
||||
console.log('🔄 Parsed AI response:', JSON.stringify(aiResponse, null, 2));
|
||||
|
||||
// Compare original and corrected data
|
||||
if (aiResponse.correctedData) {
|
||||
console.log('📊 Changes summary:');
|
||||
products.forEach((original, index) => {
|
||||
const corrected = aiResponse.correctedData[index];
|
||||
if (corrected) {
|
||||
const changes = Object.keys(corrected).filter(key =>
|
||||
JSON.stringify(original[key]) !== JSON.stringify(corrected[key])
|
||||
);
|
||||
if (changes.length > 0) {
|
||||
console.log(`\nProduct ${index + 1} changes:`);
|
||||
changes.forEach(key => {
|
||||
console.log(` ${key}:`);
|
||||
console.log(` - Original: ${JSON.stringify(original[key])}`);
|
||||
console.log(` - Corrected: ${JSON.stringify(corrected[key])}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
...aiResponse
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('AI Validation Error:', error);
|
||||
console.error('❌ AI Validation Error:', error);
|
||||
console.error('Error details:', {
|
||||
name: error.name,
|
||||
message: error.message,
|
||||
stack: error.stack
|
||||
});
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: error.message || 'Error during AI validation'
|
||||
|
||||
Reference in New Issue
Block a user