See llms.txt for all machine-readable content.

Back to Templates

Answer FAQ questions with citations using OpenAI embeddings and GPT-4o-mini

Quick overview

This workflow receives a question via webhook (or manual test), embeds it alongside in-workflow FAQ entries using an OpenAI-compatible embeddings API, and answers with an OpenAI-compatible chat model only when a similarity threshold is met, returning a refusal otherwise and enforcing valid FAQ citations.

How it works

  1. Receives a POST request on the n8n webhook (or runs via a manual test question) containing a JSON body with a question field.
  2. Loads workflow configuration (models, base URL, threshold, refusal text) and prepares the FAQ entries and an embeddings request payload.
  3. Calls an OpenAI-compatible /embeddings endpoint to embed the question and all FAQ entries.
  4. Calculates cosine similarity in code, selects the top matches above the configured score threshold, and stops early with a fixed refusal if nothing qualifies.
  5. If there are qualifying matches, sends the question plus the retrieved FAQ context to an OpenAI-compatible /chat/completions endpoint with instructions to answer only from the provided entries and include a final Source: FAQ-xx line.
  6. Verifies that the response includes citations and that every cited FAQ ID was retrieved, replacing the output with the fixed refusal if the citation rules are violated.
  7. Returns a JSON response to the webhook containing the question, answer, status, scores, and the retrieved/cited source IDs.

Setup

  1. Add an HTTP Header Auth credential (for example Authorization: Bearer <API_KEY>) and select it for both HTTP Request calls to the embeddings and chat endpoints.
  2. In the Config node, set llm_base_url, chat_model, embedding_model, score_threshold, top_k, and customize refusal_text for your policy.
  3. Replace the sample FAQ strings with your own entries, keeping one entry per paragraph and starting each with a unique ID like [FAQ-01].
  4. If using the webhook in production, copy the webhook URL and send {"question":"..."} as a POST request to /webhook/faq-answer from your app or form.

Additional info

Tested on n8n 2.48.8 with Ollama qwen2.5:7b and bge-m3 on 15 questions (10 in scope, 5 out of scope). Retrieval and the refusal gate run in Code nodes, with no vector database; the score threshold must be re-tuned per embedding model. The FAQ is synthetic.