Building a RAG Chatbot with Next.js, Python and pgvector

2026-10-30

Architecture overview

Next.js provides the user interface and authenticated application shell. A Python service handles ingestion, retrieval and answer generation. PostgreSQL stores application metadata, permissions and document state; pgvector enables vector similarity queries inside the same database.

This stack is useful when your team already operates PostgreSQL and wants to avoid an extra search service early on. It is not automatically the best option for every scale or ranking requirement.

Ingestion pipeline

Accept documents through an authenticated backend, store the original file securely, parse text and record source metadata. Split content into chunks that preserve headings and document identifiers. Generate embeddings and write chunks with document version, tenant and access metadata.

Use background jobs for parsing and embedding. Make ingestion idempotent and handle updates and deletion. A changed document should not leave stale searchable fragments behind.

Retrieval flow

Authenticate the user before retrieval. Build database filters for tenant and document permissions, then run similarity or hybrid search. Rerank when evaluation shows a benefit. Send only authorized evidence to the model and ask it to cite the retrieved sources.

Do not rely on the language model to decide which tenant a chunk belongs to. Permission filtering must happen in trusted application code or database policies.

Answering and UX

Stream the answer to the Next.js interface when it improves perceived latency, but keep citations and source identifiers attached to the response. Show when the system lacks evidence instead of fabricating a confident answer.

Include useful error states for unavailable documents, expired sessions, rate limits and model failures. Log request identifiers rather than sensitive full payloads by default.

Evaluation before launch

Create a small dataset of real questions with expected supporting documents and acceptable answers. Measure retrieval recall, citation correctness, groundedness and latency. Include adversarial cases such as irrelevant documents and attempts to override instructions inside source text.

Evaluate changes to chunk size, embedding model, retrieval parameters and prompts independently. A more complex pipeline is worthwhile only when it improves the agreed metrics.

Production checklist

Add migrations, backups, least-privilege database roles, secret management, rate limits, ingestion monitoring, retry policies, tracing and a safe rollback process. Test that one user's documents cannot be retrieved by another user.

If you need a RAG chatbot integrated with an existing product, I can help scope the smallest secure implementation and the tests needed before launch.

Need a developer for the long run?

I can start now and work with you for months or years, not hand off after two weeks. Send me a message and we will talk through your project, requirements and next steps.

Ask about experience or services