{
  "name": "OpenAI Chatbot with Redis Memory",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "chat",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "a1b2c3d4-1111-4000-8000-000000000001",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "set-msg",
              "name": "message",
              "value": "={{ $json.body.message }}",
              "type": "string"
            },
            {
              "id": "set-session",
              "name": "sessionId",
              "value": "={{ $json.body.sessionId }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "a1b2c3d4-1111-4000-8000-000000000002",
      "name": "Extract Message",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "operation": "get",
        "key": "={{ 'chat_history:' + $json.sessionId }}",
        "options": {}
      },
      "id": "a1b2c3d4-1111-4000-8000-000000000003",
      "name": "Redis Get History",
      "type": "n8n-nodes-base.redis",
      "typeVersion": 1,
      "position": [
        750,
        300
      ],
      "credentials": {
        "redis": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "message",
        "model": "gpt-4o",
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are a helpful AI assistant. You have memory of past conversations with this user. Be concise, accurate, and friendly. If you don't know something, say so honestly."
            },
            {
              "role": "user",
              "content": "={{ 'Previous conversation:\\n' + ($json.data || 'No previous history') + '\\n\\nUser message: ' + $('Extract Message').item.json.message }}"
            }
          ]
        },
        "options": {
          "temperature": 0.7,
          "maxTokens": 1024
        }
      },
      "id": "a1b2c3d4-1111-4000-8000-000000000004",
      "name": "OpenAI Chat",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.6,
      "position": [
        1000,
        300
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "set",
        "key": "={{ 'chat_history:' + $('Extract Message').item.json.sessionId }}",
        "value": "={{ ($('Redis Get History').item.json.data || '') + '\\nUser: ' + $('Extract Message').item.json.message + '\\nAssistant: ' + $json.text }}",
        "expire": true,
        "ttl": 3600,
        "options": {}
      },
      "id": "a1b2c3d4-1111-4000-8000-000000000005",
      "name": "Redis Save Response",
      "type": "n8n-nodes-base.redis",
      "typeVersion": 1,
      "position": [
        1250,
        300
      ],
      "credentials": {
        "redis": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "options": {
          "responseCode": 200
        },
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ response: $('OpenAI Chat').item.json.text, sessionId: $('Extract Message').item.json.sessionId }) }}"
      },
      "id": "a1b2c3d4-1111-4000-8000-000000000006",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1500,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Extract Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Message": {
      "main": [
        [
          {
            "node": "Redis Get History",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Redis Get History": {
      "main": [
        [
          {
            "node": "OpenAI Chat",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat": {
      "main": [
        [
          {
            "node": "Redis Save Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Redis Save Response": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null
}