Créer une API compatible OpenAI pour les agents/workflows N8N

Avancé

Ceci est uncontenant 21 nœuds.Utilise principalement des nœuds comme If, N8n, Set, Code, Webhook. Créer un point de terminaison d'API OpenAI compatible universel pour plusieurs workflows IA

Prérequis
  • Point de terminaison HTTP Webhook (généré automatiquement par n8n)
  • Peut nécessiter les informations d'identification d'authentification de l'API cible
  • Clé API OpenAI

Catégorie

-
Aperçu du workflow
Visualisation des connexions entre les nœuds, avec support du zoom et du déplacement
Exporter le workflow
Copiez la configuration JSON suivante dans n8n pour importer et utiliser ce workflow
{
  "id": "CcsSozcaZmwgTZRJ",
  "meta": {
    "instanceId": "d2b7bf62ebeaf79f36ba97778f61f82b716fe9a9dd9efd59e42986e968c3c6ce",
    "templateId": "4217",
    "templateCredsSetupCompleted": true
  },
  "name": "Create OpenAI-Compatible API For N8n Agents/Workflows",
  "tags": [],
  "nodes": [
    {
      "id": "f8831a38-92ef-44e5-99dd-0f410e9c0883",
      "name": "Agrégation",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        1424,
        992
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData"
      },
      "typeVersion": 1
    },
    {
      "id": "3c36a5c8-28da-4656-aa9d-8593926b3e77",
      "name": "Réponse des modèles",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        1600,
        992
      ],
      "parameters": {
        "options": {},
        "respondWith": "json",
        "responseBody": "={{\n({\n  \"object\": \"list\",\n  \"data\":  $json.data.map(item => ({\n    \"id\": item.path,\n    \"object\": \"model\",\n    \"created\": Math.floor($now / 1000),\n    \"owned_by\": \"system\"\n  }))\n})\n}}"
      },
      "executeOnce": false,
      "typeVersion": 1.2
    },
    {
      "id": "c28b03cb-bd1b-41d5-ba1b-486a099afb92",
      "name": "GET modèles",
      "type": "n8n-nodes-base.webhook",
      "position": [
        896,
        992
      ],
      "webhookId": "f88b9992-b7b9-4ab5-829c-7b89e9cc4775",
      "parameters": {
        "path": "youragents/models",
        "options": {},
        "responseMode": "responseNode"
      },
      "typeVersion": 2
    },
    {
      "id": "855c24f5-0758-4344-b780-523383b51de1",
      "name": "Note adhésive2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        848,
        800
      ],
      "parameters": {
        "color": 7,
        "width": 1016,
        "height": 452,
        "content": "## 1. Listing All Available Models \n[Read more about the Webhook Trigger node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.webhook/)\n\nOur first endpoint is for listing all models. We will get all n8n workflows with the tag 'aimodel' and then map the response to be OpenAI /models compatible"
      },
      "typeVersion": 1
    },
    {
      "id": "d5f0446d-2e59-47ee-a442-f66490d8759d",
      "name": "Note adhésive3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2000,
        784
      ],
      "parameters": {
        "color": 7,
        "width": 1448,
        "height": 588,
        "content": "## 2. Request a Chat Completion \n[Read more about the HTTP Request node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.httprequest)\n\nOur second endpoint is for executing a chat completion. Note that depending on if you have connected with an agent, the response expected is a \"stream\" and returned data needs to be of type `text/plain`."
      },
      "typeVersion": 1
    },
    {
      "id": "cb4e2823-21af-418e-9f46-9c367932aef4",
      "name": "Obtenir plusieurs workflows",
      "type": "n8n-nodes-base.n8n",
      "position": [
        1088,
        992
      ],
      "parameters": {
        "filters": {
          "tags": "aimodel"
        },
        "requestOptions": {}
      },
      "typeVersion": 1
    },
    {
      "id": "f1c9a7d9-9958-4e3f-bd2d-22fb77028cd8",
      "name": "Modifier les champs",
      "type": "n8n-nodes-base.set",
      "position": [
        1264,
        992
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "b03926a4-4ad9-4d87-8188-989fa5158a07",
              "name": "name",
              "type": "string",
              "value": "={{ $json.name }}"
            },
            {
              "id": "88bd31bd-005d-4734-bffa-4fd3b8487166",
              "name": "id",
              "type": "string",
              "value": "={{ $json.id }}"
            },
            {
              "id": "7fbabd24-8dea-495e-96fe-da08790f971c",
              "name": "path",
              "type": "string",
              "value": "={{ $json.nodes.find(node => node.parameters?.path).parameters.path }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "fe6a6983-4b80-4b54-92f2-ffe7235d0c2e",
      "name": "POST ChatCompletions",
      "type": "n8n-nodes-base.webhook",
      "position": [
        2096,
        1072
      ],
      "webhookId": "e8c56164-1825-4ac4-9c23-d209f4907458",
      "parameters": {
        "path": "youragents/chat/completions",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode",
        "authentication": "headerAuth"
      },
      "typeVersion": 2
    },
    {
      "id": "b296aba2-a74a-47fb-8461-c1d17badab28",
      "name": "Remapper vers le schéma de réponse API",
      "type": "n8n-nodes-base.code",
      "position": [
        2352,
        1072
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "function tranformContent(content) {\n  return [].concat(content).map(content => {\n    if (typeof content === 'string') {\n      return { type: \"input_text\", text: content };\n    }\n    return {\n      type: getInputType(content.type),\n      [content.type]: content[content.type].url\n    }\n  })\n};\n\nfunction getInputType(type) {\n  if (type === 'image_url') return 'input_image';\n  if (type === 'file_url') return 'input_file';\n  return 'input_text';\n}\n\nconst input = $input.item.json.body.messages.map(message => ({\n  role: message.role,\n  content: tranformContent(message.content)\n}));\n\nreturn { input };"
      },
      "typeVersion": 2
    },
    {
      "id": "f2a859c0-c7d4-49cc-baf6-b711700cef46",
      "name": "Formater la réponse de complétion",
      "type": "n8n-nodes-base.code",
      "position": [
        3040,
        1200
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "return {\n  \"id\": $('POST ChatCompletions').item.json.headers['cf-ray'],\n  \"object\": \"chat.completion\",\n  \"created\": Math.floor($now / 1000),\n  \"model\":$('POST ChatCompletions').item.json.body.model,\n  \"choices\":  [{\n      index: 0,\n      finish_reason: \"stop\",\n      \"message\": {\n        \"content\": $('Call workflow webhook1').item.json.output,\n        \"refusal\": null,\n        \"role\": \"assistant\"\n      }\n    }],\n  \"usage\": {\n    \"completion_tokens\": null,\n    \"completion_tokens_details\": null,\n    \"prompt_tokens\": null,\n    \"prompt_tokens_details\": null,\n    \"total_tokens\": null\n  },\n  \"system_fingerprint\": $('POST ChatCompletions').item.json.headers['cf-ray']\n}"
      },
      "typeVersion": 2
    },
    {
      "id": "9312f313-97f5-4ff7-9bbd-65d0ec3c6c2d",
      "name": "Réponse JSON",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        3200,
        1200
      ],
      "parameters": {
        "options": {},
        "respondWith": "json",
        "responseBody": "={{ $json }}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "68572df2-ea64-4520-9b67-3d60a2059992",
      "name": "Réponse texte",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        3200,
        976
      ],
      "parameters": {
        "options": {},
        "respondWith": "text",
        "responseBody": "={{ $json.data }}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "1a2c1e1b-d5ea-4da6-ae86-081a700ca80d",
      "name": "Formater la réponse en flux",
      "type": "n8n-nodes-base.code",
      "position": [
        3024,
        976
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const chunk = {\n  id: $('POST ChatCompletions').item.json.headers['cf-ray'],\n  object:\"chat.completion.chunk\",\n  created: Math.floor($now / 1000),\n  model: $('POST ChatCompletions').item.json.body.model,\n  system_fingerprint: $('POST ChatCompletions').item.json.headers['cf-ray'],\n  choices: [{\n    index: 0,\n     delta: { content: $('Call workflow webhook').item.json.output }\n  }],\n  \"usage\": {\n    \"completion_tokens\": null,\n    \"completion_tokens_details\": null,\n    \"prompt_tokens\": null,\n    \"prompt_tokens_details\": null,\n    \"total_tokens\": null\n  }\n};\n\nconst data = [\n  `data: ${JSON.stringify(chunk)}`,\n  `data: [DONE]`\n].join('\\n\\n');\n\nreturn { data };"
      },
      "typeVersion": 2
    },
    {
      "id": "5e832bb8-c2a2-453b-9aef-b08862c659fc",
      "name": "Est-ce un flux ?",
      "type": "n8n-nodes-base.if",
      "position": [
        2576,
        1072
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "213702bf-d5c2-4a8a-b5c8-e55f804e4496",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $('POST ChatCompletions').first().json.body.stream }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "790bb7d3-7b5d-417f-9d31-2f01d6ac89f3",
      "name": "Appeler le workflow webhook",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        2816,
        976
      ],
      "parameters": {
        "url": "=https://n8n.lucidusfortis.com/webhook/{{ $('POST ChatCompletions').first().json.body.model }}",
        "method": "POST",
        "options": {},
        "jsonBody": "={{\n{\n  model: $('POST ChatCompletions').first().json.body.model,\n  stream: $('POST ChatCompletions').first().json.body.stream,\n  chatInput: $json.input.toJsonString(),\n  sessionId: $('POST ChatCompletions').item.json.headers['cf-ray'],\n  fromLLM: true\n}\n}}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "n8nApi"
      },
      "typeVersion": 4.2
    },
    {
      "id": "4d0d9dfa-2df0-49dd-a60c-1164347c85c5",
      "name": "Appeler le webhook du workflow1",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        2816,
        1200
      ],
      "parameters": {
        "url": "=https://n8n.lucidusfortis.com/webhook/{{ $('POST ChatCompletions').first().json.body.model }}",
        "method": "POST",
        "options": {},
        "jsonBody": "={{\n{\n  model: $('POST ChatCompletions').item.json.body.model,\n  stream: false,\n  chatInput: $json.input.toJsonString(),\n  sessionId: $('POST ChatCompletions').item.json.headers['cf-ray'],\n  fromLLM: true\n}\n}}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "n8nApi"
      },
      "typeVersion": 4.2
    },
    {
      "id": "d8728083-673c-479a-b804-6bbad1fa1581",
      "name": "Webhooks n8n",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1056,
        1824
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "youragentname",
          "cachedResultName": "youragentname"
        },
        "options": {}
      },
      "typeVersion": 1.2
    },
    {
      "id": "f96c8bae-1c75-4e01-bd41-bfc0b047d0f7",
      "name": "Quand un message de chat est reçu",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        928,
        1632
      ],
      "webhookId": "6ec7783c-8f47-4fdf-9838-7846b1da4bfa",
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "51aa0e26-8cb5-4561-9d9c-a5e9e306fecb",
      "name": "Note adhésive1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        864,
        1392
      ],
      "parameters": {
        "color": 7,
        "width": 680,
        "height": 580,
        "content": "## 3. Create a New Custom OpenAI Credential\n[Learn more about OpenAI Credentials](https://docs.n8n.io/integrations/builtin/credentials/openai/)\n\nTo chat with n8n workflow, the approach is to mimic an openAI compatible API connected through the OpenAI model subnode. The first step is to setup a new OpenAI credential so that we can change the Base URL."
      },
      "typeVersion": 1
    },
    {
      "id": "5280f412-126a-45ca-8d3e-8fe460d966d7",
      "name": "Propulsé par les modèles de workflow n8n",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1120,
        1632
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 2.2
    },
    {
      "id": "9ecab0cf-c376-4ab1-81ce-896e1766b734",
      "name": "Mémoire simple",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        1184,
        1840
      ],
      "parameters": {},
      "typeVersion": 1.3
    }
  ],
  "active": true,
  "pinData": {},
  "settings": {},
  "versionId": "682200cb-34fb-4bf3-9239-0a435e1ec12a",
  "connections": {
    "f8831a38-92ef-44e5-99dd-0f410e9c0883": {
      "main": [
        [
          {
            "node": "3c36a5c8-28da-4656-aa9d-8593926b3e77",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "c28b03cb-bd1b-41d5-ba1b-486a099afb92": {
      "main": [
        [
          {
            "node": "cb4e2823-21af-418e-9f46-9c367932aef4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "5e832bb8-c2a2-453b-9aef-b08862c659fc": {
      "main": [
        [
          {
            "node": "790bb7d3-7b5d-417f-9d31-2f01d6ac89f3",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "4d0d9dfa-2df0-49dd-a60c-1164347c85c5",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "f1c9a7d9-9958-4e3f-bd2d-22fb77028cd8": {
      "main": [
        [
          {
            "node": "f8831a38-92ef-44e5-99dd-0f410e9c0883",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "d8728083-673c-479a-b804-6bbad1fa1581": {
      "ai_languageModel": [
        [
          {
            "node": "5280f412-126a-45ca-8d3e-8fe460d966d7",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "9ecab0cf-c376-4ab1-81ce-896e1766b734": {
      "ai_memory": [
        [
          {
            "node": "5280f412-126a-45ca-8d3e-8fe460d966d7",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "3c36a5c8-28da-4656-aa9d-8593926b3e77": {
      "main": [
        []
      ]
    },
    "cb4e2823-21af-418e-9f46-9c367932aef4": {
      "main": [
        [
          {
            "node": "f1c9a7d9-9958-4e3f-bd2d-22fb77028cd8",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "fe6a6983-4b80-4b54-92f2-ffe7235d0c2e": {
      "main": [
        [
          {
            "node": "b296aba2-a74a-47fb-8461-c1d17badab28",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "790bb7d3-7b5d-417f-9d31-2f01d6ac89f3": {
      "main": [
        [
          {
            "node": "1a2c1e1b-d5ea-4da6-ae86-081a700ca80d",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "4d0d9dfa-2df0-49dd-a60c-1164347c85c5": {
      "main": [
        [
          {
            "node": "f2a859c0-c7d4-49cc-baf6-b711700cef46",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "1a2c1e1b-d5ea-4da6-ae86-081a700ca80d": {
      "main": [
        [
          {
            "node": "68572df2-ea64-4520-9b67-3d60a2059992",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "f2a859c0-c7d4-49cc-baf6-b711700cef46": {
      "main": [
        [
          {
            "node": "9312f313-97f5-4ff7-9bbd-65d0ec3c6c2d",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "f96c8bae-1c75-4e01-bd41-bfc0b047d0f7": {
      "main": [
        [
          {
            "node": "5280f412-126a-45ca-8d3e-8fe460d966d7",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "b296aba2-a74a-47fb-8461-c1d17badab28": {
      "main": [
        [
          {
            "node": "5e832bb8-c2a2-453b-9aef-b08862c659fc",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "5280f412-126a-45ca-8d3e-8fe460d966d7": {
      "main": [
        []
      ]
    }
  }
}
Foire aux questions

Comment utiliser ce workflow ?

Copiez le code de configuration JSON ci-dessus, créez un nouveau workflow dans votre instance n8n et sélectionnez "Importer depuis le JSON", collez la configuration et modifiez les paramètres d'authentification selon vos besoins.

Dans quelles scénarios ce workflow est-il adapté ?

Avancé

Est-ce payant ?

Ce workflow est entièrement gratuit et peut être utilisé directement. Veuillez noter que les services tiers utilisés dans le workflow (comme l'API OpenAI) peuvent nécessiter un paiement de votre part.

Informations sur le workflow
Niveau de difficulté
Avancé
Nombre de nœuds21
Catégorie-
Types de nœuds13
Description de la difficulté

Adapté aux utilisateurs avancés, avec des workflows complexes contenant 16+ nœuds

Auteur
Dele Odufuye

Dele Odufuye

@deleodufuye

Award winning IT pro. Serial entrepreneur. Mentor. Investor. Ex-MD/VP Cavista Holdings. Builds tech solutions for business problems. Founder, Lucidus Fortis.

Liens externes
Voir sur n8n.io

Partager ce workflow

Catégories

Catégories: 34