{
  "name": "My workflow 2",
  "nodes": [
    {
      "parameters": {
        "jsCode": "// Get input\nconst data = $input.item.json || {};\n\n// Normalize values\nfunction normalize(v) {\n  if (v === null || v === undefined) return \"\";\n  const s = String(v).trim();\n  if (s === \"\") return \"\";\n  const lower = s.toLowerCase();\n  if ([\"empty\", \"null\", \"undefined\"].includes(lower)) return \"\";\n  return s;\n}\n\n// Robust search with trimming and aliases\nfunction findValue(root, keyExact, aliases = []) {\n  const lowerKey = keyExact.trim().toLowerCase();\n\n  // Search root keys\n  for (const k of Object.keys(root)) {\n    if (k.trim().toLowerCase() === lowerKey) return root[k];\n  }\n\n  // Search in grades if exists\n  if (root.grades) {\n    for (const k of Object.keys(root.grades)) {\n      if (k.trim().toLowerCase() === lowerKey) return root.grades[k];\n    }\n  }\n\n  // Search aliases\n  for (const alias of aliases) {\n    const a = alias.trim().toLowerCase();\n    for (const k of Object.keys(root)) {\n      if (k.trim().toLowerCase().includes(a)) return root[k];\n    }\n    if (root.grades) {\n      for (const k of Object.keys(root.grades)) {\n        if (k.trim().toLowerCase().includes(a)) return root.grades[k];\n      }\n    }\n  }\n\n  return undefined;\n}\n\n// === Grades ===\nconst rawMath = findValue(data, \"MATH\", [\"math\"]);\nconst rawPhysics = findValue(data, \"PHYSICS\", [\"physic\", \"pc\"]);\nconst rawScience = findValue(data, \"SCIENCE\", [\"sci\", \"svt\", \"bio\"]);\nconst rawEnglish = findValue(data, \"ENGLISH\", [\"eng\", \"anglais\"]);\nconst rawOtherLang = findValue(data, \"OTHER Language\", [\"other language\", \"lang\", \"language\"]);\n\n// === Grade system ===\nconst gradeSystem =\n  data[\"GRADE SYSTEM\"] ?? data[\"Grade System\"] ?? data.gradeSystem ?? \"\";\n\n// === Hobbies ===\nlet hobbies = data.hobbies ?? data[\"HOBBYS ( optional )\"] ?? data.hobby ?? data.Hobby ?? [];\nif (!Array.isArray(hobbies)) hobbies = normalize(hobbies) ? [normalize(hobbies)] : [];\n\n// === Accomplishments ===\nlet accomplishments =\n  data.accomplishments ?? data[\"Accomplishements ( MUNS - HACKATHONS .... ) ( optional )\"] ?? data.achievements ?? [];\nif (!Array.isArray(accomplishments)) accomplishments = normalize(accomplishments) ? [normalize(accomplishments)] : [];\n\n// === COUNTRY (uppercase key) ===\nconst COUNTRY = normalize(findValue(data, \"COUNTRY\"));\n\n// === Student output ===\nconst student = {\n  gradeSystem,\n  grades: {\n    math: normalize(rawMath),\n    physics: normalize(rawPhysics),\n    science: normalize(rawScience),\n    english: normalize(rawEnglish),\n    otherLanguage: normalize(rawOtherLang),\n  },\n  hobbies,\n  accomplishments,\n  submittedAt: data.submittedAt || \"\",\n};\n\n// === Return ONE item with both student and COUNTRY ===\nreturn [\n  {\n    json: {\n      student,\n      COUNTRY\n    }\n  }\n];\n\n\n\n\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        768,
        992
      ],
      "id": "93091ab0-1b48-429a-98ca-286f8d190dfc",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        1312,
        1216
      ],
      "id": "c45b5af6-c714-42c1-adac-baff89b95d21",
      "name": "Google Gemini Chat Model",
      "credentials": {
        "googlePalmApi": {
          "id": "YvB1Djct4C595B0k",
          "name": "Google Gemini(PaLM) Api account"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Tu es un conseiller d’orientation. Ta mission est de recommander *trois filières adaptées au profil de l’étudiant, en te basant UNIQUEMENT sur les données ci-dessous. La réponse doit absolument inclure le **nom exact de chaque filière* et une *justification brève en une ligne* pour chacune.  \n\n──────────────────────────────\nDONNÉES ÉTUDIANT  \n{{ $json.STUDENT }}\n──────────────────────────────\n\nRÉPONSE ATTENDUE (format strict, trois filières) :\n\n### 🎓 Filières recommandées\n1. Nom complet de la filière — Compatibilité : X%  \n   Justification brève (1 ligne)\n\n2. Nom complet de la filière — Compatibilité : X%  \n   Justification brève (1 ligne)\n\n3. Nom complet de la filière — Compatibilité : X%  \n   Justification brève (1 ligne)",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [
        1408,
        1008
      ],
      "id": "e707ba8f-e688-4e18-adfb-f75efeecb0fb",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "AIzaSyA85yJzbonoMi2KpzosQmW6oPD1AEAY6J4"
      },
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        1488,
        1200
      ],
      "id": "14695960-7809-48a1-8626-06b6e2077b9a",
      "name": "Simple Memory"
    },
    {
      "parameters": {
        "content": "## FORMULAIRE A REMPLIRE  \n remplire ce formulaire permettra a lagent de bien identifier l orientation \n",
        "color": 2
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        416,
        832
      ],
      "typeVersion": 1,
      "id": "d71bce63-e2a5-4a3e-85cf-e9229763db79",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "content": "## JAVA CODE \n ce code permet de reunir tous les sorties su formulaire ( donnes ex : note math ... )",
        "color": 6
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        800,
        864
      ],
      "typeVersion": 1,
      "id": "b5884fd5-8e17-4b41-a10b-e820098f9ec4",
      "name": "Sticky Note1"
    },
    {
      "parameters": {
        "content": "## Agent Ai \ncet agenty va identifier la filliere correspondante en fonction des notes et des interets de l etudiant ",
        "color": 3
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1344,
        864
      ],
      "typeVersion": 1,
      "id": "1206d66f-f281-4e80-bb9a-694b7c4e3fd8",
      "name": "Sticky Note2"
    },
    {
      "parameters": {
        "formTitle": "GRADES",
        "formDescription": "smth",
        "formFields": {
          "values": [
            {
              "fieldLabel": "GRADE SYSTEM",
              "fieldType": "dropdown",
              "fieldOptions": {
                "values": [
                  {
                    "option": "Out of 20 "
                  },
                  {
                    "option": "Letter grades "
                  },
                  {
                    "option": "Classes "
                  },
                  {
                    "option": "Out of 6 "
                  },
                  {
                    "option": "Out of 10 "
                  },
                  {
                    "option": "Out of 5 "
                  },
                  {
                    "option": "Percentage"
                  }
                ]
              }
            },
            {
              "fieldLabel": "MATH",
              "requiredField": true
            },
            {
              "fieldLabel": "PHYSICS ",
              "requiredField": true
            },
            {
              "fieldLabel": "SCIENCE ",
              "requiredField": true
            },
            {
              "fieldLabel": "ENGLISH",
              "requiredField": true
            },
            {
              "fieldLabel": "OTHER Language ",
              "requiredField": true
            },
            {
              "fieldLabel": "OTHER LANGUAGE GRADE ",
              "requiredField": true
            },
            {
              "fieldLabel": "HOBBYS ( optional )"
            },
            {
              "fieldLabel": "Accomplishements ( MUNS - HACKATHONS  .... ) ( optional ) "
            },
            {
              "fieldLabel": "COUNTRY ",
              "fieldType": "textarea",
              "requiredField": true
            },
            {
              "fieldLabel": "SAT OR TOEFL ( OPTIONAL ) "
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.formTrigger",
      "typeVersion": 2.3,
      "position": [
        496,
        992
      ],
      "id": "b4c9258a-6fac-4582-9d2d-84b9e328798d",
      "name": "On form submission1",
      "webhookId": "a3e59d4f-a703-49b9-8254-a5a200c609f7"
    },
    {
      "parameters": {
        "mode": "retrieve-as-tool",
        "toolDescription": "STORE",
        "memoryKey": {
          "__rl": true,
          "mode": "list",
          "value": "vector_store_key"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.vectorStoreInMemory",
      "typeVersion": 1.3,
      "position": [
        1648,
        1184
      ],
      "id": "719cdebe-8c33-4f95-aacb-76c27065a700",
      "name": "Simple Vector Store"
    },
    {
      "parameters": {},
      "type": "@n8n/n8n-nodes-langchain.embeddingsGoogleGemini",
      "typeVersion": 1,
      "position": [
        1664,
        1312
      ],
      "id": "8d5ffcdd-84d7-4d5f-97cc-e1cb7d676800",
      "name": "Embeddings Google Gemini",
      "credentials": {
        "googlePalmApi": {
          "id": "YvB1Djct4C595B0k",
          "name": "Google Gemini(PaLM) Api account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Récupère le texte depuis n'importe quelle clé\nlet text = \"\";\nfor (const key of Object.keys(items[0].json)) {\n  if (typeof items[0].json[key] === \"string\" && items[0].json[key].includes(\"1\")) {\n    text = items[0].json[key];\n    break;\n  }\n}\n\n// Si pas de texte trouvé → retourne un item pour éviter No output data\nif (!text) {\n  return [{\n    json: {\n      error: \"Texte non trouvé dans l'input\",\n      keys_disponibles: Object.keys(items[0].json)\n    }\n  }];\n}\n\n// Remplace les \"\\n\" littéraux par de vrais retours à la ligne\ntext = text.replace(/\\\\n/g, \"\\n\");\n\n// Découpe sur \"\\n1\", \"\\n2\", \"\\n3\" (numéro suivi d'un point ou non)\nlet parts = text.split(/\\n\\d+/);\n\n// Nettoyage : retire les espaces vides\nparts = parts.map(p => p.trim()).filter(p => p.length > 0);\n\n// Retourne chaque partie comme item séparé\nreturn parts.map((p, i) => ({\n  json: {\n    index: i + 1,\n    texte_partie: p\n  }\n}));\n\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1808,
        992
      ],
      "id": "603e02e7-7d69-46c0-badb-96160fbf535d",
      "name": "Code in JavaScript1"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "b7fd1bf0-25d5-436a-8158-18ede4dd25e3",
              "name": "STUDENT",
              "value": "={{ $json.student }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1200,
        1040
      ],
      "id": "c8086b01-d0ba-4db4-8715-0d70d49c63de",
      "name": "Edit Fields"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "9a59d05c-5030-4e48-9b62-f4315b0df2d0",
              "name": "COUNTRY ",
              "value": "={{ $json.COUNTRY }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2000,
        816
      ],
      "id": "63c045c9-94ef-49e4-a445-057c76a74711",
      "name": "Edit Fields1"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "37bacedd-9c1c-4ffd-8edb-72822d600c2a",
              "name": "COUNTRY WHERE TO STUDY ",
              "value": "={{ $json['COUNTRY '] }}",
              "type": "string"
            },
            {
              "id": "6d8082ef-4de7-4746-86f3-d65265a7840a",
              "name": "FILIERE",
              "value": "={{ $('AI Agent').item.json.output }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2112,
        1024
      ],
      "id": "5c14df84-7be4-4916-8fdd-c75ff47cdb01",
      "name": "Edit Fields2"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=SAY THE BEST UNIVERSITIES IN THIS COUNTRY :  {{ $json['COUNTRY WHERE TO STUDY '] }} TO STUDY THESE SUBJECTS :{{ $json.FILIERE }} . JUST TELL ME 3 UNIVERSITIES IN FRANCE THAT TEACHES THESE SUBJEConly type the 3 universities without talking ",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [
        2528,
        1024
      ],
      "id": "dc5b2a1c-b0a2-4ced-ab09-c5ff52477e7d",
      "name": "AI Agent1"
    },
    {
      "parameters": {
        "modelName": "models/gemini-flash-lite-latest",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        2400,
        1232
      ],
      "id": "5dd3b95d-ae7e-458d-949f-1a0244c99f40",
      "name": "Google Gemini Chat Model1",
      "credentials": {
        "googlePalmApi": {
          "id": "YvB1Djct4C595B0k",
          "name": "Google Gemini(PaLM) Api account"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "LAST NODES"
      },
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        2544,
        1232
      ],
      "id": "628803b9-0f7d-4795-9e7b-a273b72e8db0",
      "name": "Simple Memory1"
    },
    {
      "parameters": {
        "mode": "retrieve-as-tool",
        "toolDescription": "GOOD",
        "memoryKey": {
          "__rl": true,
          "mode": "list",
          "value": "vector_store_key"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.vectorStoreInMemory",
      "typeVersion": 1.3,
      "position": [
        2752,
        1152
      ],
      "id": "6ab64fb8-7fa6-488a-925b-298ade7fc357",
      "name": "Simple Vector Store1"
    },
    {
      "parameters": {},
      "type": "@n8n/n8n-nodes-langchain.embeddingsGoogleGemini",
      "typeVersion": 1,
      "position": [
        2816,
        1328
      ],
      "id": "d3e6fb2e-6bf0-455b-9188-5f11ebbc1494",
      "name": "Embeddings Google Gemini1",
      "credentials": {
        "googlePalmApi": {
          "id": "YvB1Djct4C595B0k",
          "name": "Google Gemini(PaLM) Api account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Get FILIERE from current input\nconst filiere = $input.all().find(i => i.json[\"FILIERE\"])\n  ?.json[\"FILIERE\"] ?? \"NOT SET\";\n\n// Get COUNTRY from the other node by name\nconst country = $items(\"Edit Fields2\").find(i => i.json[\"COUNTRY WHERE TO STUDY \"])\n  ?.json[\"COUNTRY WHERE TO STUDY \"] ?? \"NOT SET\";\n\nreturn [\n  {\n    json: {\n      \"COUNTRY WHERE TO STUDY \": country,\n      \"FILIERE\": filiere\n    }\n  }\n];\n\n\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2320,
        1024
      ],
      "id": "1d1697ae-cae0-44e8-a97a-f54e2a3bd532",
      "name": "Code in JavaScript2"
    },
    {
      "parameters": {
        "jsCode": "const input = $input.all()[0]?.json?.output;\nconst universities = input.split(\"\\n\");\nconst output = universities.map((university, index) => {\n  return { json: { university }, pairedItem: index };\n});\nreturn output;\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2880,
        1040
      ],
      "id": "112d30eb-f023-47e5-8bcc-02e4ca03a23a",
      "name": "Code in JavaScript3"
    }
  ],
  "pinData": {},
  "connections": {
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "Edit Fields1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "On form submission1": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Code in JavaScript1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Vector Store": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings Google Gemini": {
      "ai_embedding": [
        [
          {
            "node": "Simple Vector Store",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript1": {
      "main": [
        [
          {
            "node": "Edit Fields2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields1": {
      "main": [
        [
          {
            "node": "Edit Fields2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields2": {
      "main": [
        [
          {
            "node": "Code in JavaScript2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent1",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory1": {
      "ai_memory": [
        [
          {
            "node": "AI Agent1",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Simple Vector Store1": {
      "ai_tool": [
        [
          {
            "node": "AI Agent1",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings Google Gemini1": {
      "ai_embedding": [
        [
          {
            "node": "Simple Vector Store1",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript2": {
      "main": [
        [
          {
            "node": "AI Agent1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent1": {
      "main": [
        [
          {
            "node": "Code in JavaScript3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript3": {
      "main": [
        []
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "68f6aada-0d83-4cfd-b7c5-b26779410178",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "de5ab2b6191ebcea11a52e8b3877d54f289f09c4c6f82875c42c575b3162a126"
  },
  "id": "6LlVEEuBKy63GgNU",
  "tags": []
}