Automation library · Documents and files

Build an OpenAI RAG system with Postgres

Started by another system calling it, and works across Postgres. It puts OpenAI behind a retrieval step, so answers are drawn from documents you supply rather than from the model alone. 20 steps, 2 branches: a working afternoon to understand properly.

MediumCalls a model: OpenAIRetrieval

20 stepsMedium: a working afternoon
2Branches: places the flow splits
0Loops or waits
1Service it connects to

The flow

Starts it 4Model 8Logic 4Service 4

The steps in the order they run, from what starts it.

  1. 1Webhook TriggerWebhookStarts it
  2. 2Text SplitterText Splitter Recursive Character Text SplitterModel
  3. 3OpenAI EmbeddingsEmbeddings Open AiModel
  4. 4OpenAI Chat ModelLm Chat Open AiModel
  5. 5Workflow ConfigurationSetLogic
  6. 6Document LoaderDocument Default Data LoaderModel
  7. 7Store Embeddings in PGVectorVector Store PGVectorModel
  8. 8Retrieve Relevant ChunksVector Store PGVectorModel
  9. 9Answer Query with ContextAgentModel
  10. 10Route by ActionSwitchLogic
  11. 11Log Upload to CachePostgresService
  12. 12Answer questions with a vector storeTool Vector StoreModel
  13. 13Save to Query CachePostgresService
  14. 14Extract Text from DocumentExtract From FileService
  15. 15Check Query CachePostgresService
  16. 16Respond Upload SuccessRespond To WebhookStarts it
  17. 17Respond with AnswerRespond To WebhookStarts it
  18. 18Cache Hit or MissSwitchLogic
  19. 19Format Cached ResponseSetLogic
  20. 20Respond with Cached AnswerRespond To WebhookStarts it

How it's built

Starts on
Another system calling it
Steps
20, with 2 branches
Size
Medium: a working afternoon
Connects to
Postgres
Model
OpenAI
Retrieval
Answers are drawn from documents you supply, not from the model alone

Questions

What does it need to run?

An n8n account, plus your own Postgres account, and a key for OpenAI. You connect your own accounts after importing it; none are included.

What starts it?

It starts on another system calling it.

Does it use AI?

Yes. It calls OpenAI. Its answers draw on documents you give it, not on the model alone. Model calls are billed by the provider, not by n8n.

How long does it take to set up?

Plan an afternoon. 20 steps is enough that you should read each one before running it.

How do I import it?

Copy the workflow, open a new workflow in n8n and paste. The steps appear on the canvas, ready for your own accounts.

Before you run it

Nothing here has been tested by us. It is somebody's working automation, read and described from its own steps. Treat the connections it wants as a list of accounts it will touch, and read the 20 steps before you point it at anything that matters.