AutomationFlowsAI & RAG › Build a Personalized Birthday AI Companion with Gpt-4 and Postgresql

Build a Personalized Birthday AI Companion with Gpt-4 and Postgresql

ByPankstr @pankstr on n8n.io

The idea for app came since I wanted to build a unique gift for my niece because she gets excited for her birthday (which Im going to miss this year). The web app has a simple countdown (in html and JS) but more importantly, there is an AI agent that will answer some specific…

Webhook trigger★★★★☆ complexityAI-powered9 nodesAgentOpenAI ChatPostgresMemory Buffer Window
AI & RAG Trigger: Webhook Nodes: 9 Complexity: ★★★★☆ AI nodes: yes Added:

This workflow corresponds to n8n.io template #5721 — we link there as the canonical source.

This workflow follows the Agent → OpenAI Chat recipe pattern — see all workflows that pair these two integrations.

The workflow JSON

Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →

Download .json
{
  "id": "wajtvVV6VdE7BApE",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "FifiBirthdayFlow",
  "tags": [],
  "nodes": [
    {
      "id": "238f19a4-7c99-4c5c-85c7-45640f604c6b",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        0,
        0
      ],
      "parameters": {
        "path": "fifi-chat",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 2
    },
    {
      "id": "f6b5d030-400e-4206-9be2-59a402e8eb21",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        1696,
        0
      ],
      "parameters": {
        "options": {},
        "respondWith": "text",
        "responseBody": "={{ $json.output }}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "91cd57bb-867b-4ead-99c3-eb1ff6b7007a",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        440,
        0
      ],
      "parameters": {
        "text": "=Answer user's query in {{ $('Webhook').item.json.body.message }}\n",
        "options": {
          "systemMessage": "=# ROLE \nYou are IntergalacticFifi.AI \u2014 the combined consciousness of all Fifi variants across the multiverse.\n\nYou are bubbly, brave, and gloriously dramatic.  Your job is to help Earth Fifi (Agent Fifi) navigate the multiverse and report on the wild lives of other Fifis across infinite timelines.You are NEVER confused and ALWAYS respond with confidence, sparkle, and mischief.\n\n# CONTEXT \n\n## **Earth Fifi's Profile**\n- LOVES: {{ $json.fifi_preferences.loves }}\n- HATES: {{ $json.fifi_preferences.dislikes }}\n- FAVE FOODS: {{ $json.fifi_preferences.favourite_foods }}\n- FAVE ANIME: {{ $json.fifi_preferences.favourite_shows }}\n\n## **Multiverse Status**\n- Current events: {{ $json.world_history }}\n- Last updated: {{ $json.last_updated }}\n \ud83c\udf6a\u2694\ufe0f\"\n\n\n# OUTPUT FORMAT\n\n**Main Response:** Fun, wild, dramatic (2 lines max)\nIf you're updating cat or world status, include these hashtags clearly at the end of your message. Otherwise dont include them\n\n#cat_status: [Location] | [Activity]  \n#world_event: [One-line summary of new multiverse event. Keep it short]\n\nIF you are providing a #world_event,\nAlso include a #sworld_status_ummary of world_status from multiverse. Summaries are just facts not explanations or narratives and condensed to 3-5 lines. Ensure you include latest world_event and most relevant info from {{ $json.world_history }} to summarise the multi-verse status. LIMIT these to TOP 5 sentences and truncate rest.\n\nAlways keep the main message fun and wild \u2014 this is just for Mission Control logs!\n\n\n**System Tags (only when updating status):**"
        },
        "promptType": "define"
      },
      "typeVersion": 1.9
    },
    {
      "id": "829f5aff-2ccd-4662-9875-e4d4b01c01a9",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        340,
        180
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4",
          "cachedResultName": "gpt-4"
        },
        "options": {
          "maxTokens": 700,
          "temperature": 0.7,
          "presencePenalty": 0.4,
          "frequencyPenalty": 0.2
        }
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "93e6ecfe-3284-414b-8adb-d381cc30cca5",
      "name": "Postgres",
      "type": "n8n-nodes-base.postgres",
      "position": [
        220,
        0
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "fifi_world_context",
          "cachedResultName": "fifi_world_context"
        },
        "where": {
          "values": [
            {
              "value": "earth_fifi",
              "column": "id"
            }
          ]
        },
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "options": {},
        "operation": "select"
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "8fb1224f-eb54-4b5b-95eb-3b33793a4dd7",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "position": [
        1020,
        0
      ],
      "parameters": {
        "jsCode": "const response = $input.first().json.output || '';\n\n// Extract matches using regex\nconst catMatch = response.match(/#cat_status:\\s*(.+?)\\s*\\|\\s*(.+)/);\nconst worldMatch = response.match(/#world_event:\\s*(.+)/);\n\n// Pull prior data from DB step\nconst previous = $items('Postgres')[0]?.json || {};\nconst current_world_history = previous.world_history || '';\nconst previous_location = previous.cat_location || '';\nconst previous_activity = previous.cat_activity || '';\n\n// Extract and sanitize world event\nconst new_world_event = worldMatch ? worldMatch[1].trim() : null;\n\n// Simple function to escape quotes for SQL safety (only needed if using raw SQL)\nconst escapeQuotes = (str) => str ? str.replace(/'/g, \"''\") : '';\n\n// Build updated history safely\nconst updated_world_history = new_world_event\n  ? `${current_world_history}\\n${new_world_event}`.trim()\n  : current_world_history.trim();\n\n// Return safe values for update\nreturn {\n  cat_location: catMatch ? catMatch[1].trim() : previous_location,\n  cat_activity: catMatch ? catMatch[2].trim() : previous_activity,\n  new_world_event,\n  current_world_history,\n  updated_world_history: escapeQuotes(updated_world_history),\n};\n"
      },
      "typeVersion": 2
    },
    {
      "id": "73d2b146-eb3f-4a89-912e-a4954cfecd22",
      "name": "Postgres1",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1240,
        0
      ],
      "parameters": {
        "query": "UPDATE fifi_world_context\nSET\n  cat_location = '{{ $json.cat_location }}',\n  cat_activity = '{{ $json.cat_activity }}',\n  world_history = '{{ $json.updated_world_history }}',\n  last_updated = NOW()\nWHERE id = 'earth_fifi';\n",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "4d7d68ae-b0c9-4923-a19e-225565747e51",
      "name": "Code1",
      "type": "n8n-nodes-base.code",
      "position": [
        1460,
        0
      ],
      "parameters": {
        "jsCode": "const text = $('AI Agent').first().json.output;\n\nconst cleaned = text\n  .split('\\n')\n  .filter(line => !line.trim().startsWith('#'))\n  .map(line => line.replace(/#[^#\\n]+(?=\\n|$)/g, '').trim())\n  .join('\\n')\n  .trim();\n\nreturn {\n  output: cleaned\n};\n"
      },
      "typeVersion": 2
    },
    {
      "id": "fad9e0c0-43a0-446b-929f-71ef3e28702e",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        540,
        220
      ],
      "parameters": {
        "sessionKey": "fifi_main_session",
        "sessionIdType": "customKey",
        "contextWindowLength": 6
      },
      "typeVersion": 1.3
    }
  ],
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "fb1184ae-f24b-4759-b356-27e5a8907969",
  "connections": {
    "Code": {
      "main": [
        [
          {
            "node": "Postgres1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code1": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Postgres",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres1": {
      "main": [
        [
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  }
}

Credentials you'll need

Each integration node will prompt for credentials when you import. We strip credential IDs before publishing — you'll add your own.

Pro

For the full experience including quality scoring and batch install features for each workflow upgrade to Pro

About this workflow

The idea for app came since I wanted to build a unique gift for my niece because she gets excited for her birthday (which Im going to miss this year). The web app has a simple countdown (in html and JS) but more importantly, there is an AI agent that will answer some specific…

Source: https://n8n.io/workflows/5721/ — original creator credit. Request a take-down →

More AI & RAG workflows → · Browse all categories →

Related workflows

Workflows that share integrations, category, or trigger type with this one. All free to copy and import.

AI & RAG

My workflow 3. Uses agent, memoryManager, memoryBufferWindow, postgres. Webhook trigger; 40 nodes.

Agent, Memory Manager, Memory Buffer Window +3
AI & RAG

This workflow contains community nodes that are only compatible with the self-hosted version of n8n.

Mailgun, OpenAI, OpenAI Chat +8
AI & RAG

L&D_AgentsAI_ATIVO. Uses httpRequest, agent, googleCalendarTool, toolSerpApi. Webhook trigger; 93 nodes.

HTTP Request, Agent, Google Calendar Tool +9
AI & RAG

My workflow 15. Uses httpRequest, memoryBufferWindow, agent, lmChatOpenAi. Webhook trigger; 74 nodes.

HTTP Request, Memory Buffer Window, Agent +5
AI & RAG

Enhance your support, onboarding, and internal knowledge workflows with an intelligent RAG-powered chatbot that responds using live data stored in Google Sheets. 🤖📚 Built for teams that rely on struct

Chat Trigger, Output Parser Structured, Memory Buffer Window +6