{
  "name": "IDS AI - Chat Handler",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "ids-ai-chat",
        "responseMode": "responseNode",
        "options": {
          "allowedOrigins": "*"
        }
      },
      "id": "webhook-trigger",
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Exercise Detection Function\nconst EXERCISE_KEYWORDS = [\n  'exercise', 'homework', 'assignment', 'quiz', 'exam',\n  'test questions', 'examination', 'worksheet', 'practice problems',\n  'problem set', 'take-home', 'final exam', 'midterm'\n];\n\nconst message = $input.first().json.body.message || '';\nconst context = $input.first().json.body.context || '';\nconst combinedText = (message + ' ' + context).toLowerCase();\n\n// Check for exercise keywords\nlet isExercise = false;\nlet reasons = [];\n\nfor (const keyword of EXERCISE_KEYWORDS) {\n  if (combinedText.includes(keyword)) {\n    isExercise = true;\n    reasons.push(`Contains keyword: ${keyword}`);\n  }\n}\n\n// Check for point patterns\nif (/\\d+\\s*(points?|pts?|marks?)/i.test(combinedText)) {\n  isExercise = true;\n  reasons.push('Contains point/mark values');\n}\n\n// Check for numbered questions\nif (/question\\s*\\d+|problem\\s*\\d+/i.test(combinedText)) {\n  isExercise = true;\n  reasons.push('Contains numbered questions');\n}\n\nreturn [{\n  json: {\n    ...$input.first().json,\n    isExercise,\n    reasons,\n    message,\n    context,\n    conversationId: $input.first().json.body.conversationId || 'new'\n  }\n}];"
      },
      "id": "exercise-filter",
      "name": "Exercise Filter",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.isExercise }}",
              "value2": true
            }
          ]
        }
      },
      "id": "if-exercise",
      "name": "Is Exercise?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        660,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"response\": \"I'm sorry, but I cannot help with exercise, homework, quiz, or exam content.\\n\\nThis policy is in place to:\\n- Encourage independent learning and problem-solving\\n- Support academic integrity\\n- Help you develop critical thinking skills\\n\\nIf you have questions about concepts or need help understanding topics, I'd be happy to assist with that instead!\",\n  \"conversationId\": \"{{ $json.conversationId }}\",\n  \"isBlocked\": true,\n  \"blockReason\": \"Exercise content detected\",\n  \"reasons\": {{ JSON.stringify($json.reasons) }}\n}",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Content-Type",
                "value": "application/json"
              }
            ]
          }
        }
      },
      "id": "reject-exercise",
      "name": "Reject Exercise",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        880,
        180
      ]
    },
    {
      "parameters": {
        "model": "llama3.2:latest",
        "messages": {
          "messageValues": [
            {
              "role": "system",
              "content": "You are IDS AI, an intelligent document assistant. You are helpful, accurate, and concise. You can help users with questions about documents, general knowledge, and various topics. Always be polite and professional.\n\nIMPORTANT: You must NEVER help with exercises, homework, quizzes, exams, or any academic assessment content. If you detect any such content, politely decline and explain why."
            },
            {
              "role": "user",
              "content": "={{ $json.context ? 'Context from uploaded document:\\n' + $json.context + '\\n\\nUser question: ' + $json.message : $json.message }}"
            }
          ]
        },
        "options": {
          "temperature": 0.7,
          "maxTokens": 2048
        }
      },
      "id": "ollama-chat",
      "name": "Ollama Chat",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [
        880,
        420
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"response\": \"{{ $json.text || $json.message?.content || 'No response generated' }}\",\n  \"conversationId\": \"{{ $node['Exercise Filter'].json.conversationId }}\",\n  \"isBlocked\": false\n}",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Content-Type",
                "value": "application/json"
              }
            ]
          }
        }
      },
      "id": "send-response",
      "name": "Send Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1100,
        420
      ]
    }
  ],
  "connections": {
    "Webhook Trigger": {
      "main": [
        [
          {
            "node": "Exercise Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Exercise Filter": {
      "main": [
        [
          {
            "node": "Is Exercise?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Exercise?": {
      "main": [
        [
          {
            "node": "Reject Exercise",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Ollama Chat",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ollama Chat": {
      "main": [
        [
          {
            "node": "Send Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "IDS AI"
    }
  ],
  "triggerCount": 0,
  "updatedAt": "2024-12-17T00:00:00.000Z",
  "versionId": "1"
}