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
# Cloudflare Workers AI + LaunchDarkly: Random Joke Example
2
2
3
-
This example demonstrates using LaunchDarkly AI Configs with Cloudflare Workers AI to generate jokes dynamically.
3
+
This example shows how to use LaunchDarkly AI Configs with Cloudflare Workers AI to generate a random joke when you curl the endpoint with a user ID.
4
4
5
5
## Prerequisites
6
6
@@ -72,77 +72,33 @@ LD_CLIENT_ID = "LD_CLIENT_ID"
72
72
binding = "AI"
73
73
```
74
74
75
-
### 4. Create LaunchDarkly AI Config
75
+
### 4. Create LaunchDarkly AI Config (Random Joke)
76
76
77
-
In your LaunchDarkly dashboard, create an AI Config that will control your Cloudflare Workers AI model. Follow the [official LaunchDarkly documentation](https://launchdarkly.com/docs/home/ai-configs/create) for creating AI Configs.
77
+
Create an AI Config that the worker will use to generate a random joke.
78
78
79
-
#### Step 1: Add a Custom Model for Cloudflare Workers AI
79
+
1) In LaunchDarkly, go to **AI Configs → Create AI Config**
80
+
-**Key**: `random-joke` (required — the worker uses this key)
81
+
- Click **Create AI Config**
80
82
81
-
Since Cloudflare Workers AI models are not yet built into LaunchDarkly's model list, you'll need to add a custom model first.
83
+
2) Choose a Cloudflare model
84
+
- You can use any model from Cloudflare's list. If it's not pre-listed in LaunchDarkly, add a custom model:
85
+
- Go to **Project settings → AI model configs → Add custom model**
86
+
-**Model ID**: paste a Workers AI model ID (e.g. `@cf/meta/llama-3.1-8b-instruct-fast`)
-**Model name**: Enter a descriptive name like `Llama 3.1 8B Instruct Fast`
93
-
-**Model ID**: Enter the Cloudflare model ID, e.g., `@cf/meta/llama-3.1-8b-instruct-fast`
94
-
-**Model type**: Select **Chat**
95
-
-**Provider**: Select **Custom** or enter `Cloudflare Workers AI`
96
-
- Click **Save**
94
+
4) Add messages (your prompt)
95
+
- System: `You are a witty comedian who tells a single short joke.`
96
+
- User: `Tell a random joke.`
97
97
98
-
Refer to the [LaunchDarkly documentation for creating custom models](https://launchdarkly.com/docs/home/ai-configs/create-model-config#complete-the-add-custom-model-dialog) for more details.
98
+
5) Targeting
99
+
- Enable targeting for your environment and serve the variation to all users.
- Enter message content: `You are a funny comedian who tells short jokes.`
131
-
- Click **+ Add another message**
132
-
- Select message role: **user**
133
-
- Enter message content: `Tell me a {{joke_type}} joke{{topic_section}}.`
134
-
- The `{{joke_type}}` and `{{topic_section}}` variables are provided at runtime by the example
135
-
- Click **Review and save**
136
-
137
-
Refer to the [LaunchDarkly documentation for creating variations](https://launchdarkly.com/docs/home/ai-configs/create-variation) for more details.
138
-
139
-
#### Step 3: Configure Targeting
140
-
141
-
**Set up targeting rules**
142
-
- In your AI Config, go to the **Targeting** tab
143
-
- Enable targeting for your environment
144
-
- Set the default rule to serve your variation to all users
145
-
- Click **Review and save**
101
+
Refer to: [Cloudflare AI Models](https://developers.cloudflare.com/workers-ai/models/) for the full model list, and [LD AI Config docs](https://launchdarkly.com/docs/home/ai-configs/create) for configuration details.
"joke": "Why do programmers prefer dark mode? Because light attracts bugs!",
@@ -187,39 +132,24 @@ Expected response:
187
132
188
133
## How It Works
189
134
190
-
1.**Initialize Clients**: Creates LaunchDarkly and AI clients
191
-
2.**Get AI Config**: Retrieves `joke-ai-config` from LaunchDarkly with variables `{ joke_type, topic_section }`
192
-
3.**Variable Interpolation**: Fills `{{joke_type}}` and `{{topic_section}}` in messages
193
-
4.**Call AI Model**: Map to Workers AI via `config.toWorkersAI(env.AI)` and run with metrics using `await config.tracker.trackWorkersAIMetrics(() => env.AI.run(wc.model, wc))`
0 commit comments