GitHub Repository

You can find the project source code on GitHub.

This guide provides detailed, step-by-step instructions on how to use Upstash RAG Chat with Cloudflare Workers. You can also explore our Cloudflare Workers - Hono example for detailed, end-to-end examples and best practices.

Project Setup

Create a new Cloudflare Worker and install @upstash/rag-chat package.

npm create cloudflare@latest -- my-first-worker
cd my-first-worker
npm install @upstash/rag-chat

Setup Upstash Redis

Create a Redis database using Upstash Console or Upstash CLI and copy the UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN into your .dev.vars file.

.dev.vars
UPSTASH_REDIS_REST_URL=<YOUR_URL>
UPSTASH_REDIS_REST_TOKEN=<YOUR_TOKEN>

Setup Upstash Vector

Create a Vector index using Upstash Console or Upstash CLI and copy the UPSTASH_VECTOR_REST_URL and UPSTASH_VECTOR_REST_TOKEN into your .dev.vars file.

.dev.vars
UPSTASH_VECTOR_REST_URL=<YOUR_URL>
UPSTASH_VECTOR_REST_TOKEN=<YOUR_TOKEN>

Setup QStash LLM

Navigate to QStash Console and copy the QSTASH_TOKEN into your .dev.vars file.

.dev.vars
QSTASH_TOKEN=<YOUR_TOKEN>

Setup Cloudflare Worker

Note that cache must be set to false in the Index constructor for Cloudflare Workers as seen in the example below.

Update /src/index.ts:

/src/index.ts
import { RAGChat, upstash } from "@upstash/rag-chat";
import { Index } from "@upstash/vector";
import { Redis } from "@upstash/redis/cloudflare";

export interface Env {
  UPSTASH_REDIS_REST_TOKEN: string;
  UPSTASH_REDIS_REST_URL: string;
  UPSTASH_VECTOR_REST_TOKEN: string;
  UPSTASH_VECTOR_REST_URL: string;
  QSTASH_TOKEN: string;
}

export default {
  async fetch(request, env, ctx): Promise<Response> {
    const ragChat = new RAGChat({
      redis: new Redis({
		token: env.UPSTASH_REDIS_REST_TOKEN,
		url: env.UPSTASH_REDIS_REST_URL
	  }),
      vector: new Index({
		token: env.UPSTASH_VECTOR_REST_TOKEN,
		url: env.UPSTASH_VECTOR_REST_URL,
		cache: false
	  }),
      model: upstash("meta-llama/Meta-Llama-3-8B-Instruct", {
		apiKey: env.QSTASH_TOKEN
	  }),
	});

    const response = await ragChat.chat("What is the speed of light?");
    return new Response(response.output);
  },
} satisfies ExportedHandler<Env>;

Run

Run the Cloudflare Worker locally:

npx wrangler dev

Visit http://localhost:8787

Deploy

For deployment, use the wrangler CLI to securely set environment variables. Run the following command for each secret:

npx wrangler secret put SECRET_NAME

Replace SECRET_NAME with the actual name of each environment variable (e.g., UPSTASH_REDIS_REST_URL).

Deploy the Cloudflare Worker:

npx wrangler deploy