https://github.com/RafalWilinski/cloudflare-rag Skip to content Navigation Menu Toggle navigation Sign in * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + GitHub Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions By size + Enterprise + Teams + Startups By industry + Healthcare + Financial services + Manufacturing By use case + CI/CD & Automation + DevOps + DevSecOps * Resources Topics + AI + DevOps + Security + Software Development + View all Explore + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Enterprise + Enterprise platform AI-powered developer platform Available add-ons + Advanced Security Enterprise-grade security features + GitHub Copilot Enterprise-grade AI features + Premium Support Enterprise-grade 24/7 support * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up Reseting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} RafalWilinski / cloudflare-rag Public * Notifications You must be signed in to change notification settings * Fork 13 * Star 154 Fullstack "Chat with your PDFs" RAG (Retrieval Augmented Generation) app built fully on Cloudflare rwilinski.ai 154 stars 13 forks Branches Tags Activity Star Notifications You must be signed in to change notification settings * Code * Issues 4 * Pull requests 0 * Actions * Projects 0 * Security * Insights Additional navigation options * Code * Issues * Pull requests * Actions * Projects * Security * Insights RafalWilinski/cloudflare-rag This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main BranchesTags Go to file Code Folders and files Last Last Name Name commit commit message date Latest commit History 56 Commits app app assets assets drizzle drizzle functions functions public public .env.template .env.template .eslintrc.cjs .eslintrc.cjs .gitignore .gitignore README.md README.md components.json components.json drizzle.config.ts drizzle.config.ts load-context.ts load-context.ts package-lock.json package-lock.json package.json package.json pnpm-lock.yaml pnpm-lock.yaml postcss.config.js postcss.config.js remix.config.js remix.config.js schema.ts schema.ts setup.sh setup.sh tailwind.config.ts tailwind.config.ts tsconfig.json tsconfig.json vite.config.ts vite.config.ts worker-configuration.d.ts worker-configuration.d.ts wrangler.toml wrangler.toml View all files Repository files navigation * README Fullstack Cloudflare RAG This is a fullstack example of how to build a RAG (Retrieval Augmented Generation) app with Cloudflare. It uses Cloudflare Workers, Pages, D1, KV, R2, AI Gateway and Workers AI. cloudflare_rag_demo.mp4 Demo Deploy to Cloudflare Workers Features: * Every interaction is streamed to the UI using Server-Sent Events * Hybrid RAG using Full-Text Search on D1 and Vector Search on Vectorize * Switchable between various providers (OpenAI, Groq, Anthropic) using AI Gateway with fallbacks * Per-IP Rate limiting using Cloudflare's KV * OCR is running inside Cloudflare Worker using unpdf * Smart Placement automatically places your workloads in an optimal location that minimizes latency and speeds up your applications Development Make sure you have Node, pnpm and wrangler CLI installed. Install dependencies: pnpm install # or npm install Deploy necessary primitives: ./setup.sh Then, in wrangler.toml, set the d1_databases.database_id to your D1 database id and kv_namespaces.rate_limiter to your rate limiter KV namespace id. Then, create a .dev.vars file with your API keys: CLOUDFLARE_ACCOUNT_ID=your-cloudflare-account-id # Required GROQ_API_KEY=your-groq-api-key # Optional OPENAI_API_KEY=your-openai-api-key # Optional ANTHROPIC_API_KEY=your-anthropic-api-key # Optional If you don't have these keys, /api/stream will fallback to Workers AI . Run the dev server: npm run dev And access the app at http://localhost:5173/. Deployment Having the necessary primitives setup, first setup secrets: npx wrangler secret put CLOUDFLARE_ACCOUNT_ID npx wrangler secret put GROQ_API_KEY npx wrangler secret put OPENAI_API_KEY npx wrangler secret put ANTHROPIC_API_KEY Then, deploy your app to Cloudflare Pages: npm run deploy Hybrid Search RAG Hybrid Search RAG This project uses a combination of classical Full Text Search (sparse) against Cloudflare D1 and Hybrid Search with embeddings against Vectorize (dense) to provide the best of both worlds providing the most applicable context to the LLM. The way it works is this: 1. We take user input and we rewrite it to 5 different queries using an LLM 2. We run each of these queries against our both datastores - D1 database using BM25 for full-text search and Vectorize for dense retrieval 3. We take the results from both datastores and we merge them together using Reciprocal Rank Fusion which provides us with a single list of results 4. We then take the top 10 results from this list and we pass them to the LLM to generate a response License This project is licensed under the terms of the MIT License. Consulting If you need help in building AI applications, please reach out to me on Twitter or via my website. Happy to help! About Fullstack "Chat with your PDFs" RAG (Retrieval Augmented Generation) app built fully on Cloudflare rwilinski.ai Topics cloudflare rag llm chatgpt Resources Readme Activity Stars 154 stars Watchers 3 watching Forks 13 forks Report repository Languages * TypeScript 94.4% * JavaScript 2.5% * CSS 2.3% * Shell 0.8% Footer (c) 2024 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact * Manage cookies * Do not share my personal information You can't perform that action at this time.