{
  "name": "Agente IA con ChromaDB + Ollama + API Empresa",
  "nodes": [
    {
      "parameters": {},
      "id": "webhook",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "path": "ask-ai",
      "method": "POST",
      "responseMode": "lastNode",
      "respondWithOptions": "responseNode"
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "http://localhost:8000/api/v1/collections/{{ $env.CHROMA_COLLECTION_ID }}/query",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "{\n  \"query_texts\": [\"={{ $('Webhook').item.json.question }}\"],\n  \"n_results\": 3\n}"
      },
      "id": "chroma-query",
      "name": "Buscar en ChromaDB",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "http://localhost:11434/api/generate",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "{\n  \"model\": \"qwen2:7b\",\n  \"prompt\": \"Contexto:\\n{{ $json.documents[0][0] }}\\n\\nPregunta: {{ $('Webhook').item.json.question }}\\n\\nInstrucci\u00f3n: Responde la pregunta usando SOLO el contexto. Si necesitas consultar la API de la empresa, responde EXACTAMENTE: 'API_NECESARIA:nombre_endpoint'.\",\n  \"stream\": false\n}"
      },
      "id": "ollama-initial",
      "name": "Ollama - Respuesta Inicial",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [],
          "string": [
            {
              "value1": "={{ $json.response.trim() }}",
              "operation": "contains",
              "value2": "API_NECESARIA:"
            }
          ]
        }
      },
      "id": "if-api-needed",
      "name": "\u00bfNecesita API?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const response = $input.item.json.response.trim();\nconst match = response.match(/API_NECESARIA:(\\w+)/);\nif (!match) {\n  return { error: \"No se pudo extraer el endpoint\" };\n}\n\nconst endpoint = match[1];\nlet apiUrl = \"\";\n\nswitch(endpoint) {\n  case \"consulta_saldo\":\n    apiUrl = \"http://localhost:3000/api/consulta-saldo\";\n    break;\n  case \"estado_pedido\":\n    apiUrl = \"http://localhost:3000/api/estado-pedido\";\n    break;\n  default:\n    throw new Error(\"Endpoint no soportado: \" + endpoint);\n}\n\nreturn {\n  ...$input.item.json,\n  apiUrl,\n  endpoint,\n  body: {\n    clienteId: \"123\"\n  }\n};"
      },
      "id": "extract-endpoint",
      "name": "Extraer Endpoint",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        1250,
        200
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "={{ $json.apiUrl }}",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "={{ JSON.stringify($json.body) }}"
      },
      "id": "call-company-api",
      "name": "Llamar API Empresa",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1450,
        200
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "http://localhost:11434/api/generate",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "{\n  \"model\": \"qwen2:7b\",\n  \"prompt\": \"Datos de la API: {{ JSON.stringify($json.body) }}\\n\\nInstrucci\u00f3n: Usa estos datos para responder claramente la pregunta original: {{ $('Webhook').item.json.question }}. S\u00e9 conciso y amable.\",\n  \"stream\": false\n}"
      },
      "id": "ollama-final",
      "name": "Ollama - Respuesta Final",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1650,
        200
      ]
    },
    {
      "parameters": {
        "responseCode": 200,
        "responseData": "={{ $json.response }}",
        "responseHeaders": "{\n  \"Content-Type\": \"application/json\"\n}",
        "options": {}
      },
      "id": "webhook-response",
      "name": "Responder al Cliente",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1850,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Buscar en ChromaDB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buscar en ChromaDB": {
      "main": [
        [
          {
            "node": "Ollama - Respuesta Inicial",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ollama - Respuesta Inicial": {
      "main": [
        [
          {
            "node": "\u00bfNecesita API?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\u00bfNecesita API?": {
      "main": [
        [
          {
            "node": "Responder al Cliente",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extraer Endpoint",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extraer Endpoint": {
      "main": [
        [
          {
            "node": "Llamar API Empresa",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Llamar API Empresa": {
      "main": [
        [
          {
            "node": "Ollama - Respuesta Final",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ollama - Respuesta Final": {
      "main": [
        [
          {
            "node": "Responder al Cliente",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null
}