Minimal RAG application on Cloudflare: Worker (TypeScript) + Workers AI (LLM) + D1 (documents) + KV (conversation memory) + React frontend.
- LLM: Workers AI (
@cf/meta/llama-3.1-8b-instruct) - Workflow/coordination: Cloudflare Worker (
worker/src/rag-worker.ts) - User input: React chat UI (
frontend/src) - Memory/state: KV (
CONVERSATIONS) for conversation history
- API (Worker): https://rag-worker.tchan-efa.workers.dev
- Frontend (Pages): https://819dfebf.rag-llm-aqj.pages.dev
This app returns sources when matching content exists in D1. If you fork this repo, load your documents into D1 using the scripts in migrations/.
# Insert a single test document (production D1)
wrangler d1 execute DB --command "INSERT INTO documents (id, content, source, metadata) VALUES ('test1','Reinforcement learning is a type of machine learning where an agent learns by interacting with an environment and receiving rewards.','test.md','{\"page\":1}');"
# Verify document count
wrangler d1 execute DB --command "SELECT COUNT(*) AS total FROM documents;"cloudflare-rag/
├── worker/ # Cloudflare Worker API
│ ├── src/ # RAG pipeline
│ └── wrangler.toml # Bindings (AI, D1, KV, Vectorize)
├── frontend/ # React chat UI
└── migrations/ # Import scripts
- Repository name must start with
cf_ai_ - Include
PROMPTS.mdat repo root with prompts used - Include this README with clear run instructions