Workers KV

Workers KV is commonly used as an easy to implement cache. In our case though, we will use Workers KV to decouple configuration from the code.

Task

Use KV to store the system prompt that defines the AI’s personality, making it easy to experiment with different personalities without the need to re-deploy your Worker.

Why

  • KV provides a simple key-value storage solution for Cloudflare Workers.
  • KV is well suited for read-heavy workloads with only occasional writes
  • KV provides eventual consistency, meaning that reads may not always reflect the most recent writes immediately. This allows for faster performance and simpler development. If you need strong consistency, use Durable Objects instead.

Steps

1. Create KV namespace

Let's start by creating a new namespace called CONFIG.

wrangler kv:namespace create "CONFIG"
...
🌀 Creating namespace with title "support-chatbot-CONFIG"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
{ binding = "CONFIG", id = "..." }

Add the binding details of this KV namespace to your wrangler.toml.

Unlike with the AI binding in previous chapter, your project can use multiple KV namespaces and therefore the bindings are an array. Just like with the AI binding, there are two ways to express this, pick one and add it to your wrangler.toml:

Option A: Array of tables

[[kv_namespaces]]
binding = "CONFIG"
id = "..."

Option B: Inline array of tables (more compact)

kv_namespaces = [
   { binding = "CONFIG", id = "..." }
]

2. Add key-value pair

Now let's add our system prompt as a key-value pair to the namespace we just created. Feel free to experiment and devise your own system prompt.

wrangler kv:key put systemPrompt "You are a friendly assistant that is accurate, highly organized and above all else succinct, you are able to distil complex topics into just a sentence or two" --binding CONFIG --local

The --local flag instructs Wrangler to only update the local development instance of that namespace. To make sure you have the value available once you deploy your Worker, re-run the same command without the --local flag.

wrangler kv:key put systemPrompt "You are a friendly assistant that is accurate, highly organized and above all else succinct, you are able to distil complex topics into just a sentence or two" --binding CONFIG

Lastly, add the binding to the TypeScript Env interface in worker-configuration.d.ts:

interface Env {
  AI: any;
🟩  CONFIG: KVNamespace;
}

3. Use KV in a Worker

To retrieve this value inside the Worker, you'd then use something like await env.CONFIG.get('systemPrompt'), which could look like this:

⚙️ line-start=14 ⚙️
🟥const systemPrompt = 'You are a friendly assistant that always responds in a rhyme';
🟩const systemPrompt = (await env.CONFIG.get('systemPrompt')) ?? `You are a friendly assistant that always responds in a rhyme`

chatPost.ts

And that's it!

4. Test

Restart your local Worker and start a chat. You should notice the chatbot no longer responds in rhyme.

Then, with your Worker still running, try updating the prompt and then continuing a chat, without restarting the Worker:

wrangler kv:key put systemPrompt "You are an unfriendly assistant that always responds negatively to any request and refuses to help" --binding CONFIG --local

You should see your chatbot's personality change immediately:

Change the prompt back:

wrangler kv:key put systemPrompt "You are a friendly assistant that is accurate, highly organized and above all else succinct, you are able to distil complex topics into just a sentence or two" --binding CONFIG --local

5. Challenge (optional)

Remember how we limited chatbot's memory recall window to the last 6 messages? Make this configurable via KV, so you can easily modify the window.