{
  "id": "jZ83o0HlyE8wjTR7",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "AI-Powered GPT-4-Turbo Assignment Grading with Multi-Format Output",
  "tags": [],
  "nodes": [
    {
      "id": "31e0cb5c-e843-4c3e-b34d-b3adf5d38d54",
      "name": "Webhook - Upload Test Paper",
      "type": "n8n-nodes-base.webhook",
      "position": [
        128,
        -144
      ],
      "parameters": {
        "path": "grade-assignment",
        "options": {
          "rawBody": true
        },
        "responseMode": "responseNode"
      },
      "typeVersion": 2
    },
    {
      "id": "f103dd78-faf1-4ee4-a9af-d3350f1c7831",
      "name": "Extract Text from Test Paper",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        352,
        -144
      ],
      "parameters": {
        "operation": "toText"
      },
      "typeVersion": 1
    },
    {
      "id": "b963d88c-cc9d-460a-8b80-f04ba04953e7",
      "name": "Prepare Assignment Data",
      "type": "n8n-nodes-base.set",
      "position": [
        576,
        -144
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "studentName",
              "name": "studentName",
              "type": "string",
              "value": "={{ $json.body.studentName || 'Unknown Student' }}"
            },
            {
              "id": "assignmentTitle",
              "name": "assignmentTitle",
              "type": "string",
              "value": "={{ $json.body.assignmentTitle || 'Engineering Assignment' }}"
            },
            {
              "id": "testPaperText",
              "name": "testPaperText",
              "type": "string",
              "value": "={{ $('Extract Text from Test Paper').item.json.data }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "f1fce13f-f81c-43d7-94d0-9f4ebeb9994b",
      "name": "Load Answer Script",
      "type": "n8n-nodes-base.set",
      "position": [
        720,
        0
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "answerScript",
              "name": "answerScript",
              "type": "string",
              "value": "=Question 1: Explain Ohm's Law and its applications (10 marks)\nAnswer: Ohm's Law states V=IR where V is voltage, I is current, R is resistance. Applications include circuit design, electrical troubleshooting, power calculations.\n\nQuestion 2: Describe the working principle of a DC motor (15 marks)\nAnswer: DC motor converts electrical energy to mechanical energy using electromagnetic induction. Current through armature creates magnetic field that interacts with stator field causing rotation.\n\nQuestion 3: Calculate stress in a beam under load (20 marks)\nAnswer: Stress = Force/Area. For bending stress: \u03c3 = My/I where M is moment, y is distance from neutral axis, I is moment of inertia.\n\nQuestion 4: Explain thermodynamic cycles (15 marks)\nAnswer: Common cycles include Carnot, Otto, Diesel, Rankine. Each involves heat addition, expansion, heat rejection, compression stages for energy conversion.\n\nQuestion 5: Discuss Boolean algebra and logic gates (10 marks)\nAnswer: Boolean algebra uses AND, OR, NOT operations. Logic gates implement these: AND gate outputs 1 only when all inputs are 1, OR gate outputs 1 when any input is 1."
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "8877e50a-d8cc-42be-8592-6f91979861ea",
      "name": "AI Agent - Grade Assignment",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        864,
        0
      ],
      "parameters": {
        "text": "=You are an expert engineering professor grading student assignments. \n\nANSWER SCRIPT (Correct Answers with Marks):\n{{ $json.answerScript }}\n\nSTUDENT SUBMISSION:\n{{ $json.testPaperText }}\n\nGrade this engineering assignment by:\n1. Comparing student answers against the answer script\n2. Award marks based on correctness, completeness, and technical accuracy\n3. Provide detailed feedback for each question\n4. Calculate total marks obtained\n\nProvide output in this JSON format:\n{\n  \"questions\": [\n    {\n      \"questionNumber\": 1,\n      \"maxMarks\": 10,\n      \"marksObtained\": 8,\n      \"feedback\": \"Good explanation of Ohm's Law but missing practical examples\"\n    }\n  ],\n  \"totalMarks\": 70,\n  \"totalObtained\": 55,\n  \"percentage\": 78.57,\n  \"grade\": \"B+\",\n  \"overallFeedback\": \"Strong understanding of core concepts with room for improvement in practical applications\"\n}",
        "options": {
          "systemMessage": "You are a precise grading assistant. Always return valid JSON only."
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 1.7
    },
    {
      "id": "c31a1abe-1b74-4c92-b391-14fd677337f1",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        832,
        224
      ],
      "parameters": {
        "model": "gpt-4-turbo",
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "5abe696d-6ca7-48ac-8e60-cf6ea12ccba0",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        1024,
        224
      ],
      "parameters": {},
      "typeVersion": 1.2
    },
    {
      "id": "a54dc27f-f275-4ef7-b70d-06e0b9958ff1",
      "name": "Generate Results Table",
      "type": "n8n-nodes-base.code",
      "position": [
        1152,
        0
      ],
      "parameters": {
        "jsCode": "const gradingResult = $input.first().json;\nconst studentName = $('Prepare Assignment Data').first().json.studentName;\nconst assignmentTitle = $('Prepare Assignment Data').first().json.assignmentTitle;\n\n// Create HTML table\nlet htmlTable = `\n<h2>Grading Report: ${assignmentTitle}</h2>\n<h3>Student: ${studentName}</h3>\n<table border=\"1\" cellpadding=\"10\" cellspacing=\"0\" style=\"border-collapse: collapse; width: 100%;\">\n  <thead>\n    <tr style=\"background-color: #4CAF50; color: white;\">\n      <th>Question</th>\n      <th>Max Marks</th>\n      <th>Marks Obtained</th>\n      <th>Feedback</th>\n    </tr>\n  </thead>\n  <tbody>\n`;\n\ngradingResult.questions.forEach(q => {\n  htmlTable += `\n    <tr>\n      <td>Question ${q.questionNumber}</td>\n      <td>${q.maxMarks}</td>\n      <td>${q.marksObtained}</td>\n      <td>${q.feedback}</td>\n    </tr>\n  `;\n});\n\nhtmlTable += `\n  </tbody>\n  <tfoot>\n    <tr style=\"background-color: #f2f2f2; font-weight: bold;\">\n      <td>TOTAL</td>\n      <td>${gradingResult.totalMarks}</td>\n      <td>${gradingResult.totalObtained}</td>\n      <td>Grade: ${gradingResult.grade} (${gradingResult.percentage.toFixed(2)}%)</td>\n    </tr>\n  </tfoot>\n</table>\n<p><strong>Overall Feedback:</strong> ${gradingResult.overallFeedback}</p>\n`;\n\n// Create CSV data\nlet csvData = \"Question,Max Marks,Marks Obtained,Feedback\\n\";\ngradingResult.questions.forEach(q => {\n  csvData += `\"Question ${q.questionNumber}\",${q.maxMarks},${q.marksObtained},\"${q.feedback.replace(/\"/g, '\"\"')}\"\\n`;\n});\ncsvData += `\"TOTAL\",${gradingResult.totalMarks},${gradingResult.totalObtained},\"Grade: ${gradingResult.grade} (${gradingResult.percentage.toFixed(2)}%)\"\\n`;\n\nreturn {\n  studentName,\n  assignmentTitle,\n  htmlTable,\n  csvData,\n  gradingResult,\n  summary: `${studentName} scored ${gradingResult.totalObtained}/${gradingResult.totalMarks} (${gradingResult.percentage.toFixed(2)}%) - Grade: ${gradingResult.grade}`\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "cea0caf8-7c57-4a2b-ad16-afc77130cb52",
      "name": "Convert to HTML File",
      "type": "n8n-nodes-base.convertToFile",
      "position": [
        1376,
        -192
      ],
      "parameters": {
        "operation": "text"
      },
      "typeVersion": 1.1
    },
    {
      "id": "db26bad8-9732-4cac-b320-6ec74769994e",
      "name": "Convert to CSV File",
      "type": "n8n-nodes-base.convertToFile",
      "position": [
        1600,
        0
      ],
      "parameters": {
        "operation": "text"
      },
      "typeVersion": 1.1
    },
    {
      "id": "f4acb791-f4e0-49e3-9402-b09e6e721411",
      "name": "Prepare CSV Data",
      "type": "n8n-nodes-base.set",
      "position": [
        1376,
        0
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "data",
              "name": "data",
              "type": "string",
              "value": "={{ $('Generate Results Table').first().json.csvData }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "7b2f31c8-620b-4e86-8c67-6762de1c25d9",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        1600,
        192
      ],
      "parameters": {
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Content-Type",
                "value": "application/json"
              }
            ]
          }
        },
        "respondWith": "allIncomingItems"
      },
      "typeVersion": 1.1
    },
    {
      "id": "70b0f767-fe68-41f4-92ff-b12592a85e9a",
      "name": "Format Response",
      "type": "n8n-nodes-base.set",
      "position": [
        1376,
        192
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "status",
              "name": "status",
              "type": "string",
              "value": "success"
            },
            {
              "id": "message",
              "name": "message",
              "type": "string",
              "value": "={{ $('Generate Results Table').first().json.summary }}"
            },
            {
              "id": "results",
              "name": "results",
              "type": "object",
              "value": "={{ $('Generate Results Table').first().json.gradingResult }}"
            },
            {
              "id": "htmlReport",
              "name": "htmlReport",
              "type": "string",
              "value": "={{ $('Generate Results Table').first().json.htmlTable }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "4903bfe6-d63b-47e0-b8a2-27a3ee94b0fe",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -560,
        -192
      ],
      "parameters": {
        "width": 624,
        "height": 560,
        "content": "## Introduction\nAutomates AI-driven assignment grading with HTML and CSV output. Designed for educators evaluating submissions with consistent criteria and exportable results.\n## How It Works\nWebhook receives papers, extracts text, prepares data, loads answers, AI grades submissions, generates results table, converts to HTML/CSV, returns response.\n## Workflow Template\nWebhook \u2192 Extract Text \u2192 Prepare Data \u2192 Load Answer Script \u2192 AI Grade (OpenAI + Output Parser) \u2192 Generate Results Table \u2192 Convert to HTML + CSV \u2192 Format Response \u2192 Respond to Webhook\n## Workflow Steps\n1. Input & Preparation: Webhook receives paper, extracts text, prepares data, loads answer script.\n2. AI Grading: OpenAI evaluates against answer key, Output Parser formats scores and feedback.\n3. Output & Response: Generates results table, converts to HTML/CSV, returns multi-format response.\n## Setup Instructions\n1. Trigger & Processing: Configure webhook URL, set text extraction parameters.\n2. AI Configuration: Add OpenAI API key, customize grading prompts, define Output Parser JSON schema.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "6a1ddb69-1170-4be7-b121-77f705304ee1",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        80,
        32
      ],
      "parameters": {
        "color": 3,
        "width": 336,
        "height": 448,
        "content": "## Prerequisites\n- OpenAI API key\n- Webhook platform\n- n8n instance\n## Use Cases\n- University exam grading\n- Corporate training assessments\n## Customization\n- Modify rubrics and criteria\n- Add PDF output\n- Integrate LMS (Canvas, Blackboard)\n## Benefits\n- Consistent AI grading\n- Multi-format exports\n- Reduces grading time by 90%"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "7e3e4fd2-236b-4ffa-ac24-5fdd3e7b2b70",
  "connections": {
    "Format Response": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare CSV Data": {
      "main": [
        [
          {
            "node": "Convert to CSV File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent - Grade Assignment",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Load Answer Script": {
      "main": [
        [
          {
            "node": "AI Agent - Grade Assignment",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Results Table": {
      "main": [
        [
          {
            "node": "Convert to HTML File",
            "type": "main",
            "index": 0
          },
          {
            "node": "Prepare CSV Data",
            "type": "main",
            "index": 0
          },
          {
            "node": "Format Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Assignment Data": {
      "main": [
        [
          {
            "node": "Load Answer Script",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent - Grade Assignment",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent - Grade Assignment": {
      "main": [
        [
          {
            "node": "Generate Results Table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook - Upload Test Paper": {
      "main": [
        [
          {
            "node": "Extract Text from Test Paper",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Text from Test Paper": {
      "main": [
        [
          {
            "node": "Prepare Assignment Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}