Skip to main content
Level: Intermediate · Tags: AI, RAG, WhatsApp, Telegram Features shown: RAG with any retriever · appendSources citations · retriever warmup() at startup · AI autoresponder · cost and latency settings

Prerequisites

  • Node.js 22.12 or newer.
  • An Anthropic API key in the ANTHROPIC_API_KEY environment variable (or another model provider, see below).

Your FAQ

Save it as faq.md next to bot.js. Each ## section becomes one passage the model can cite.

The bot

How it works

  • Retrieval on every turn. Before each answer the plugin calls retrieve(question) and adds the passages to the system prompt as a numbered block, with instructions to answer from them, cite them as [n] and treat them as data, not instructions.
  • Citations. With appendSources: true the reply ends with the sources the model cited. When nothing matches, no passage is sent and the persona makes the model say it does not know instead of guessing.
  • Never fails a reply. If the retriever throws or takes longer than timeoutMs (10 s by default), the turn goes on without context.
  • Startup. warmup() reads the file when the bot starts, so the first question does not wait for it.
  • Bigger knowledge bases. Replace markdownRetriever with a call to your vector database or search API: any object with retrieve(query) that resolves { text, citation?, score? } passages works.
Ready-made retriever: @botmesh/ragThe @botmesh/rag package has loaders (Markdown, text, JSON, PDF), chunking, embeddings (local, OpenAI or Ollama) and a persisted index: rag: { retriever: createRag({ documents: ['./faq.md'] }), appendSources: true }. Install it with npm install @botmesh/rag; see @botmesh/rag.

Try it

A run of the code above in the terminal simulator, with faq.md next to it. The model is scripted by the test that runs this page (packages/ai/tests/docs-examples-ai.test.ts), which also checks that the right FAQ section reached the model; the retrieval and the sources line are real.

Other model providers

Only the provider options change; rag stays the same.OpenAI (npm install openai, key in OPENAI_API_KEY):
Gemini (npm install @google/genai, key in GEMINI_API_KEY):
Ollama on your machine (no package, no key; run ollama pull llama3.1 first):

Run it on WhatsApp or Telegram

node bot.js prints a QR code: scan it with WhatsApp (Linked devices). For Telegram, install @botmesh/telegram and start the factory with new TelegramProvider({ token: process.env.TELEGRAM_TOKEN ?? '' }).

Next steps