{"repo": "abzdevs18/gabay-docs", "files": {"gabay-form/ai-feedback-system.md": {"d3e454a95df563f63f3969aae55983575d0842e4": {"t": 1785746444.2392492, "n": 13224, "h": "bd49acde9ffb8553", "c": "# Gabay Form - AI Feedback System\n\n> Automatic personalized feedback for every student submission\n\n---\n\n## Overview\n\nThe AI Feedback System automatically generates personalized, encouraging feedback for students after they complete assessments. Using advanced GPT models, it analyzes their responses and provides constructive guidance.\n\n---\n\n## How It Works\n\n### 1. Student Submits Form\n\n```\nStudent fills out exam → Clicks Submit\n ↓\n API saves response\n ↓\n Job queued for processing\n ↓\n Student sees confirmation\n```\n\n**Time:** <200ms (synchronous)\n\n### 2. Background Processing\n\n```\nWorker picks up job → Fetches form data\n ↓\n Analyzes answers\n ↓\n Generates AI prompt\n ↓\n Calls OpenAI API\n ↓\n Receives feedback\n ↓\n Creates email HTML\n ↓\n Sends email\n```\n\n**Time:** 5-8 seconds (asynchronous)\n\n### 3. Student Receives Email\n\nBeautiful HTML email with:\n- Personal greeting\n- Completion statistics\n- AI-generated feedback\n- Study recommendations\n- Encouragement\n\n---\n\n## AI Models\n\n### Supported Models\n\n| Model | Provider | Cost | Speed | Quality |\n|-------|----------|------|-------|---------|\n| `gpt-4o-mini` | OpenAI | $0.15/1M | Fast | High |\n| `gpt-5-nano` | OpenAI | $0.10/1M | Fastest | Good |\n| `deepseek-chat` | DeepSeek | Varies | Fast | Good |\n\n### Configuration\n\n```bash\n# .env\nOPENAI_API_KEY=sk-...\nOPENAI_CHAT_MODEL=gpt-4o-mini\nOPENAI_FEEDBACK_MODEL=gpt-4o-mini # Override for feedback\nDEEPSEEK_API_KEY=sk-... # Fallback\n```\n\n---\n\n## Automatic Scoring\n\nBefore AI feedback generation, the system automatically scores student responses:\n\n### Supported Question Types\n\n| Question Type | Answer Field | Scoring Method |\n|--------------|-------------|----------------|\n| **Multiple Choice** | `correctAnswerIds` | Checks if student's answer ID is in array |\n| **True/False** | `correctAnswerIds` | Same as multiple choice |\n| **Checkbox** | `correctAnswerIds` | All selected IDs must match (exact match) |\n| **Dropdown** | `correctAnswerIds` | Same as multiple choice |\n| **Fill-in-Blank** | `correctAnswers` | Case-insensitive string matching |\n| **Short Answer** | `correctAnswers` | Multiple acceptable answers supported |\n| **Identification** | `correctAnswers` | Trimmed, case-insensitive comparison |\n| **Essay** | N/A | Manual grading (no auto-scoring) |\n\n### Scoring Logic\n\n**Choice-Based Questions** (Multiple Choice, True/False, Dropdown):\n```typescript\n// Student answer: \"choice_1\"\n// Correct answers: [\"choice_1\"]\nisCorrect = correctAnswerIds.includes(studentAnswer);\n```\n\n**Multi-Select (Checkbox)**:\n```typescript\n// Student answer: [\"choice_1\", \"choice_3\"]\n// Correct answers: [\"choice_1\", \"choice_3\"]\nisCorrect = studentAnswer.length === correctAnswerIds.length && \n studentAnswer.every(ans => correctAnswerIds.includes(ans));\n```\n\n**Text-Based Questions**:\n```typescript\n// Student answer: \"Photosynthesis\"\n// Correct answers: [\"photosynthesis\", \"photo synthesis\"]\nnormalizedStudent = studentAnswer.trim().toLowerCase();\nisCorrect = correctAnswers.some(ca => \n ca.trim().toLowerCase() === normalizedStudent\n);\n```\n\n### Correct Answer Detection\n\nThe system intelligently finds correct answers from multiple possible locations:\n\n1. **Primary Fields:**\n - `correctAnswerIds` - For choice-based questions\n - `correctAnswers` - For text-based questions\n\n2. **Legacy Fields:**\n - `correctAnswer`, `answer`, `correct_answer`\n\n3. **Embedded in Options:**\n - `options[].isCorrect = true`\n - `choices[].isCorrect = true`\n\n4. **Validation Objects:**\n - `validation.correctAnswer`\n - `settings.correctAnswer`\n\nThis multi-path detection ensures compatibility with forms created by different builders and versions.\n\n### Score Calculation\n\n```\ncorrectCount = number of correct answers\ntotalQuestions = number of questions with defined correct answers\nscorePercentage = (correctCount / totalQuestions) × 100\n```\n\n**Example:**\n- Total Questions: 10\n- Correct Answers: 7\n- Score: 70%\n\n---\n\n## Feedback Generation\n\n### AI Prompt\n\nThe system uses a carefully crafted prompt:\n\n```\nYou are an encouraging and supportive K-12 teacher providing \npersonalized feedback to students after completing an exam.\n\nYour feedback should:\n1. Be warm, encouraging, and age-appropriate\n2. Acknowledge effort and completion\n3. Identify 2-3 specific strengths based on their answers\n4. Suggest 1-2 areas for improvement (gently and constructively)\n5. Provide specific study tips related to the exam content\n6. End with an encouraging message\n\nKeep the tone positive and motivating.\nLength: 250-350 words.\n```\n\n### Context Provided\n\n```\nExam: Math Quiz\n\nACTUAL SCORE:\n- Correct Answers: 7 out of 10\n- Incorrect Answers: 3\n- Points Earned: 70 out of 100\n- Score Percentage: 70%\n\nStatistics:\n- Total Questions: 10\n- Questions Answered: 10\n- Total Possible Points: 100\n- Time Taken: 5 minutes\n\nQuestions and Answers (with correctness marked):\nQuestion 1: What is 2+2?\nType: MULTIPLE_CHOICE\nPoints: 10\nStudent's Answer: choice_1\nCorrect Answer: choice_1\n✅ CORRECT\n\nQuestion 2: What is the capital of France?\nType: MULTIPLE_CHOICE\nPoints: 10\nStudent's Answer: choice_2\nCorrect Answer: choice_3\n❌ INCORRECT\n\n...\n```\n\n### Example Output\n\n```\nGreat job on completing the Math Quiz, John! 🎉\n\nI'm impressed by your effort and dedication. You answered all \n10 questions and showed a strong understanding of the material.\n\nYour Strengths:\n✅ Excellent work on arithmetic problems! Your calculations \n were accurate and well-thought-out.\n✅ Strong grasp of geography concepts, especially with \n European capitals.\n✅ Great time management - you completed the quiz efficiently \n without rushing.\n\nAreas for Growth:\n💡 Review fraction operations, particularly when working with \n mixed numbers.\n💡 Practice identifying prime numbers - a quick review of \n factors will help!\n\nStudy Tips:\n- Create flashcards for key math formulas\n- Try online practice quizzes for geography\n- Review your notes on number theory\n\nKeep up the fantastic work! Your dedication to learning shows, \nand I'm confident you'll continue to improve. Remember, every \nchallenge is an opportunity to grow stronger! 🌟\n\nWell done!\n```\n\n---\n\n## Email Template\n\n### Structure\n\n```html\n\n\n
\n \n\n\nHi John Doe,
\n \n \nHi John Doe,
\n \n \n