π Build a local RAG chatbot with Ollama, Qwen, BGE-M3 and Postgres PGVector
β‘ 693 views Β· π Internal Wiki & Knowledge Base
Description
Build a fully local RAG chatbot using Ollama that works without tool calling β ideal for smaller open-source models like Qwen that donβt support native function calls. This template lets you run a private, self-hosted AI assistant with retrieval-augmented generation using only your own hardware.
How it works
- A Webhook receives the userβs chat message
- A small classifier LLM (Qwen 7B) analyzes the input and decides: is this small talk, or a real question that needs the knowledge base?
- For small talk, a dedicated AI agent responds conversationally with chat memory
- For real questions, the classifier generates focused sub-queries, which are sent through a loop-based RAG pipeline:
- Each sub-query is embedded using BGE-M3 and matched against a Postgres PGVector store
- Results are filtered by a relevance score threshold (>0.4)
- Chunks are aggregated and deduplicated across all sub-queries
- An Answer Generator agent (Qwen 14B) produces a sourced answer using a strict 3-step format: short answer β sources β follow-up question
- Both paths use Postgres-backed chat memory for multi-turn conversations
- A post-processing step removes
<think>tags that some reasoning models produce
Set up steps
- Install Ollama and pull the required models:
ollama pull qwen2.5:7b(classifier + small talk)ollama pull qwen3:14b(answer generation)ollama pull bge-m3(embeddings)
- Set up PostgreSQL with the
pgvectorextension enabled - Create your vector store β ingest your documents into the PGVector store using BGE-M3 embeddings (you can use n8nβs built-in document loaders for this)
- Configure credentials in n8n:
- Ollama connection (default:
http://localhost:11434) - PostgreSQL connection for both chat memory and vector store
- Ollama connection (default:
- Customize the webhook path and connect it to your frontend or API client
- Optional: Adjust the relevance score threshold, swap models for larger/smaller ones, or modify the system prompts to match your use case
π Nodes Used
Webhook, Filter, AI Agent, Basic LLM Chain, Ollama Chat Model, Embeddings Ollama
π₯ Import
Download workflow.json and import into n8n:
Workflow menu β Import from File