Serve a RAG chat backend for Next.js using OpenAI, Redis, and Pinecone

Go to Workflow
0 views
Built by Oneclick AI Squad Oneclick AI Squad
Created on September 17, 2026

Description

Quick overview
This workflow exposes a secure webhook API for a Next.js app to run a RAG chat agent using OpenAI, a Pinecone vector store, Redis caching, and Postgres logging, and it optionally calls back into Next.js to trigger ISR revalidation when grounded answers indicate public content should be refreshed.

How it works
Receives a POST request on a header-authenticated webhook endpoint from a Next.js Server Action or Route Handler.
Validates the request payload (query, conversationId, tenantId, and allowed namespace) and returns a structured 400 response if any checks fail.
Builds a deterministic cache key and checks Redis to return a cached answer immediately for identical repeated queries.
On a cache miss, generates OpenAI embeddings and retrieves the top relevant passages from a Pinecone vector index scoped to the request namespace.
Uses an OpenAI-powered agent with conversation window memory and a vector-search tool to produce a structured JSON answer with citations and a groundedness/confidence score.
Validates and normalizes the agent’s JSON output, stores the result in Redis with a TTL, and logs the conversation turn to Postgres.
Responds to Next.js with the final answer payload, and if the answer is grounded, posts a non-blocking callback to Next.js /api/revalidate to revalidate an ISR tag for that namespace.

Setup
Create and connect credentials for OpenAI, Redis, and Postgres, and configure the webhook’s Header Auth (x-api-key) so only your Next.js server can call it.
Set up a Pinecone index and connect Pinecone credentials, then select the index in the vector store node and ensure your documents are already embedded and stored in the namespaces you plan to use.
Update the configuration values in the workflow (allowedNamespaces, default namespace, embedding/chat models, cache TTL, and max query length) to match your app’s requirements.
Implement a Next.js Route Handler at /api/revalidate that validates the shared secret and calls revalidateTag/revalidatePath, then set nextjsRevalidateUrl and nextjsRevalidateSecret in the workflow to match it.
Ensure your Postgres database contains a rag_conversation_turns table (or update the insert target) with columns matching the logged fields (conversation, query, answer, citations, groundedness, confidence, latency, cacheHit, createdAt).

Nodes Used (10)

AI Agent
@n8n/n8n-nodes-langchain.agent
Code
n8n-nodes-base.code
Embeddings OpenAI
@n8n/n8n-nodes-langchain.embeddingsOpenAi
HTTP Request
n8n-nodes-base.httpRequest
OpenAI Chat Model
@n8n/n8n-nodes-langchain.lmChatOpenAi
Pinecone Vector Store
@n8n/n8n-nodes-langchain.vectorStorePinecone
Postgres
n8n-nodes-base.postgres
Redis
n8n-nodes-base.redis
Simple Memory
@n8n/n8n-nodes-langchain.memoryBufferWindow
Vector Store Question Answer Tool
@n8n/n8n-nodes-langchain.toolVectorStore