Back to Templates

Chat-based financial analysis of P&L and balance sheets with GPT-4 & PostgreSQL

Created by

Created by: Zain Ali || zain104
Zain Ali

Last update

Last update 3 months ago

Share


๐Ÿงพ Whoโ€™s it for

This workflow is designed for finance teams, accountants, and data analysts ๐Ÿ“Š who want to interact with financial data from two PostgreSQL databases โ€” one containing Profit & Loss data and another containing Balance Sheet data โ€” using natural language chat.
Itโ€™s perfect for those who need quick, AI-powered insights with the correct database automatically selected based on the question.


โš™๏ธ How it works / What it does

  1. Chat Trigger ๐Ÿ’ฌ โ€“ Starts the workflow when a chat message is received.
  2. AI Agent ๐Ÿค– โ€“ Processes the userโ€™s question and decides:
    • Profit & Loss DB โ†’ If the question is about revenue, costs, expenses, or profit.
    • Balance Sheet DB โ†’ If the question is about assets, liabilities, or equity.
  3. PostgreSQL Query Nodes ๐Ÿ—„๏ธ โ€“
    • P_L_Reports queries the financial_agent_pl_reports table.
    • Balance_Sheets queries the financial_agent_balancesheets table.
  4. AI Model (OpenAI) ๐Ÿง  โ€“ Uses gpt-4.1-nano to interpret results and provide an easy-to-read answer.
  5. Memory Buffer ๐Ÿ“ โ€“ Keeps recent conversation context for a smoother chat experience.
  6. Table Output ๐Ÿ“‹ โ€“ Always formats the results as a clean, readable table with two decimal precision.

๐Ÿ› ๏ธ How to set up

  1. Prepare Your Databases

    • Feed your Profit & Loss and Balance Sheet data into PostgreSQL.
    • Ensure the correct table structures are used:
      • financial_agent_pl_reports โ†’ P&L data.
      • financial_agent_balancesheets โ†’ Balance Sheet data.
  2. Configure the PostgreSQL Nodes

    • Add connection credentials for both databases.
    • Link P_L_Reports and Balance_Sheets nodes to the correct tables.
  3. Set Up the AI Agent

    • Paste the provided system message into the AI Agent node (already configured in your workflow).
  4. Connect the Nodes

    • Ensure Chat Trigger โ†’ AI Agent โ†’ DB Nodes โ†’ AI Model connections match your workflow.
  5. Deploy

    • Save and activate the workflow.
    • Start sending finance-related queries to test.

๐Ÿ“‹ Requirements

  • n8n (latest version recommended)
  • PostgreSQL databases with:
    • financial_agent_pl_reports table (P&L data).
    • financial_agent_balancesheets table (Balance Sheet data).
  • OpenAI API credentials with access to gpt-4.1-nano.
  • Active Webhook/Chat Trigger for receiving queries.

๐ŸŽจ How to customize

  • Expand AI Instructions ๐Ÿ—’๏ธ โ€“ Add more rules in the system message for different data sources or formatting styles.
  • Change AI Model ๐Ÿง  โ€“ Switch to a different OpenAI model for faster or more accurate results.
  • Add More Databases ๐Ÿ—„๏ธ โ€“ Connect extra financial datasets, e.g., cash flow, sales analytics.
  • Enhance Table Styling ๐Ÿ“Š โ€“ Use Markdown or HTML formatting for richer outputs.
  • Refine Query Logic ๐Ÿ” โ€“ Modify filtering logic to better match your reporting needs.