|
| 1 | +--- |
| 2 | +pcx_content_type: get-started |
| 3 | +title: Using Batch API via Workers |
| 4 | +sidebar: |
| 5 | + order: 2 |
| 6 | +--- |
| 7 | + |
| 8 | +import { Render, PackageManagers, WranglerConfig, CURL } from "~/components"; |
| 9 | + |
| 10 | +If you want to skip the steps and get started quickly, click the button below: |
| 11 | + |
| 12 | +[](https://deploy.workers.cloudflare.com/?url=https://github.com/craigsdennis/batch-please-workers-ai) |
| 13 | + |
| 14 | +This will create a repository in your GitHub account and deploy a ready-to-use Worker that demonstrates how to use Cloudflare's Async Batch API. The template includes preconfigured AI bindings, and examples for sending and retrieving batch requests with and without external references. Once deployed, you can visit the live Worker and start experimenting with the Batch API immediately. |
| 15 | + |
| 16 | +## 1. Prerequisites and setup |
| 17 | + |
| 18 | +<Render file="prereqs" product="workers" /> |
| 19 | + |
| 20 | +## 2. Creating Your Cloudflare Worker project |
| 21 | + |
| 22 | +Open your terminal and run the following command: |
| 23 | + |
| 24 | +Create a new Worker project named `batch-api` by running: |
| 25 | + |
| 26 | +<PackageManagers type="create" pkg="cloudflare@latest" args={"batch-api"} /> |
| 27 | + |
| 28 | +<Render |
| 29 | + file="c3-post-run-steps" |
| 30 | + product="workers" |
| 31 | + params={{ |
| 32 | + category: "hello-world", |
| 33 | + type: "Worker only", |
| 34 | + lang: "TypeScript", |
| 35 | + }} |
| 36 | +/> |
| 37 | + |
| 38 | +This will create a new `batch-api` directory. Your new `batch-api` directory will include: |
| 39 | + |
| 40 | +- A `"Hello World"` [Worker](/workers/get-started/guide/#3-write-code) at `src/index.ts`. |
| 41 | +- A [`wrangler.jsonc`](/workers/wrangler/configuration/) configuration file. |
| 42 | + |
| 43 | +Go to your application directory: |
| 44 | + |
| 45 | +```sh |
| 46 | +cd batch-api |
| 47 | +``` |
| 48 | + |
| 49 | +## 3. Configure wrangler |
| 50 | + |
| 51 | +You must create an AI binding for your Worker to connect to Workers AI. [Bindings](/workers/runtime-apis/bindings/) allow your Workers to interact with resources, like Workers AI, on the Cloudflare Developer Platform. |
| 52 | + |
| 53 | +To bind Workers AI to your Worker, add the following to the end of your Wrangler file: |
| 54 | + |
| 55 | +<WranglerConfig> |
| 56 | + |
| 57 | +```toml |
| 58 | +[ai] |
| 59 | +binding = "AI" |
| 60 | +``` |
| 61 | + |
| 62 | +</WranglerConfig> |
| 63 | + |
| 64 | +Your binding is [available in your Worker code](/workers/reference/migrate-to-module-workers/#bindings-in-es-modules-format) on [`env.AI`](/workers/runtime-apis/handlers/fetch/). |
| 65 | + |
| 66 | +## 4. How to use the Batch API |
| 67 | + |
| 68 | +### 1. Sending a Batch request |
| 69 | + |
| 70 | +Send your initial batch inference request by composing a JSON payload containing an array of individual inference requests. |
| 71 | + |
| 72 | +:::note[Note] |
| 73 | + |
| 74 | +Ensure that the total payload is under 10 MB. |
| 75 | + |
| 76 | +::: |
| 77 | + |
| 78 | +```typescript title="src/index.js" |
| 79 | +interface AIRequest { |
| 80 | + prompt: string; |
| 81 | + temperature: number; |
| 82 | + max_tokens: number; |
| 83 | +} |
| 84 | + |
| 85 | +const resp = env.AI.run( |
| 86 | + "@cf/meta/ray-llama-3.3-70b-instruct-fp8-fast", |
| 87 | + { |
| 88 | + requests: [ |
| 89 | + { |
| 90 | + prompt: "tell me a joke", |
| 91 | + temperature: 0.5, |
| 92 | + max_tokens: 100, |
| 93 | + }, |
| 94 | + { |
| 95 | + prompt: "write an email from user to provider.", |
| 96 | + temperature: 0.6, |
| 97 | + max_tokens: 101, |
| 98 | + }, |
| 99 | + { |
| 100 | + prompt: "tell me a joke about llamas", |
| 101 | + temperature: 0.7, |
| 102 | + max_tokens: 102, |
| 103 | + }, |
| 104 | + ] as AIRequest[], |
| 105 | + }, |
| 106 | + { queueRequest: true }, |
| 107 | +); |
| 108 | +``` |
| 109 | + |
| 110 | +#### Expected Response |
| 111 | + |
| 112 | +After sending your batch request, you will receive a response similar to: |
| 113 | + |
| 114 | +```json |
| 115 | +{ |
| 116 | + "status": "queued", |
| 117 | + "request_id": "000-000-000", |
| 118 | + "model": "@cf/meta/ray-llama-3.3-70b-instruct-fp8-fast" |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +- **`status`**: Indicates that your request is queued. |
| 123 | +- **`request_id`**: A unique identifier for the batch request. |
| 124 | +- **`model`**: The model used for the batch inference. |
| 125 | + |
| 126 | +### 2. Polling the Batch Request Status |
| 127 | + |
| 128 | +Once your batch request is queued, use the `request_id` to poll for its status. During processing, the API returns a status "queued" or "running" indicating that the request is still in the queue or being processed. |
| 129 | + |
| 130 | +```javascript title=example |
| 131 | +// Polling the status of the batch request using the request_id |
| 132 | +const status = env.AI.run("@cf/meta/ray-llama-3.3-70b-instruct-fp8-fast", { |
| 133 | + request_id: "000-000-000", |
| 134 | +}); |
| 135 | +``` |
| 136 | + |
| 137 | +```json output |
| 138 | +{ |
| 139 | + "status": "queued", |
| 140 | + "request_id": "000-000-000" |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +### 3. Retrieving the Batch Inference results |
| 145 | + |
| 146 | +When the inference is complete, the API returns a final HTTP status code of `200` along with an array of responses. Each response object corresponds to an individual input prompt, identified by an `id` that maps to the index of the prompt in your original request. |
| 147 | + |
| 148 | +```json title=Example complete response |
| 149 | +{ |
| 150 | + "responses": [ |
| 151 | + { |
| 152 | + "id": 2, |
| 153 | + "result": { |
| 154 | + "result": { |
| 155 | + "response": "\nHere's one:\n\nWhy did the llama refuse to play poker?\n\nBecause he always got fleeced!\n\n(Sorry, it's a bit of a woolly joke, but I hope it made you smile!)" |
| 156 | + } |
| 157 | + }, |
| 158 | + "success": true |
| 159 | + }, |
| 160 | + { |
| 161 | + "id": 0, |
| 162 | + "result": { |
| 163 | + "result": { |
| 164 | + "response": ", please!\nHere's one:\n\nWhat do you call a fake noodle?\n\n(wait for it...)\n\nAn impasta!\n\nHope that made you laugh! Do you want to hear another one? \n#joke #humor #funny #laugh #smile #noodle #impasta #pastajoke\nHow was that? Do you want another one? I have a million of them!\n\nHere's another one:\n\nWhat do you call a can opener that doesn't work?\n\n(wait" |
| 165 | + } |
| 166 | + }, |
| 167 | + "success": true |
| 168 | + }, |
| 169 | + { |
| 170 | + "id": 1, |
| 171 | + "result": { |
| 172 | + "result": { |
| 173 | + "response": " The user is asking for a refund for a service that was not provided.\nHere is an example of an email that a user might send to a provider requesting a refund for a service that was not provided:\nSubject: Request for Refund for Undelivered Service\n\nDear [Provider's Name],\n\nI am writing to request a refund for the [service name] that I was supposed to receive from your company on [date]. Unfortunately, the service was not provided as agreed upon, and I have not" |
| 174 | + } |
| 175 | + }, |
| 176 | + "success": true |
| 177 | + } |
| 178 | + ], |
| 179 | + "usage": { |
| 180 | + "prompt_tokens": 22, |
| 181 | + "completion_tokens": 243, |
| 182 | + "total_tokens": 265 |
| 183 | + } |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +- **`responses`**: An array of response objects. Each object includes: |
| 188 | + - **`id`**: The index of the corresponding prompt. |
| 189 | + - **`result`**: The inference output, which may be nested depending on your implementation. |
| 190 | + - **`success`**: A Boolean flag indicating if the request was processed successfully. |
| 191 | +- **`usage`**: Contains token usage details for the batch request. |
| 192 | + |
| 193 | +## 6. Implementing the Batch API in your Worker |
| 194 | + |
| 195 | +Below is a sample TypeScript Worker that receives a batch of inference requests, sends them to a batch-enabled AI model, and returns the results. |
| 196 | + |
| 197 | +```ts title="src/index.js" |
| 198 | +export interface Env { |
| 199 | + AI: { |
| 200 | + run: (model: string, payload: any, options: any) => Promise<any>; |
| 201 | + }; |
| 202 | +} |
| 203 | + |
| 204 | +export default { |
| 205 | + async fetch(request: Request, env: Env): Promise<Response> { |
| 206 | + // Only allow POST requests |
| 207 | + if (request.method !== "POST") { |
| 208 | + return new Response("Method Not Allowed", { status: 405 }); |
| 209 | + } |
| 210 | + |
| 211 | + try { |
| 212 | + // Parse the incoming JSON payload |
| 213 | + const data = await request.json(); |
| 214 | + |
| 215 | + // Validate that we have a 'requests' array in the payload |
| 216 | + if (!data.requests || !Array.isArray(data.requests)) { |
| 217 | + return new Response( |
| 218 | + JSON.stringify({ |
| 219 | + error: "Missing or invalid 'requests' array in request payload.", |
| 220 | + }), |
| 221 | + { status: 400, headers: { "Content-Type": "application/json" } }, |
| 222 | + ); |
| 223 | + } |
| 224 | + |
| 225 | + // Send the batch request to the AI model via the AI binding |
| 226 | + // Replace "@cf/meta/ray-llama-3.3-70b-instruct-fp8-fast" with your desired batch-enabled model if needed. |
| 227 | + const batchResponse = await env.AI.run( |
| 228 | + "@cf/meta/ray-llama-3.3-70b-instruct-fp8-fast", |
| 229 | + { |
| 230 | + requests: data.requests, |
| 231 | + }, |
| 232 | + { queueRequest: true }, |
| 233 | + ); |
| 234 | + |
| 235 | + // Return the response from the AI API |
| 236 | + return new Response(JSON.stringify(batchResponse), { |
| 237 | + status: 200, |
| 238 | + headers: { "Content-Type": "application/json" }, |
| 239 | + }); |
| 240 | + } catch (error: any) { |
| 241 | + // Log the error if needed and return a 500 response |
| 242 | + return new Response( |
| 243 | + JSON.stringify({ |
| 244 | + error: error?.toString() || "An unknown error occurred.", |
| 245 | + }), |
| 246 | + { status: 500, headers: { "Content-Type": "application/json" } }, |
| 247 | + ); |
| 248 | + } |
| 249 | + }, |
| 250 | +}; |
| 251 | +``` |
| 252 | + |
| 253 | +### How it works: |
| 254 | + |
| 255 | +- **Receiving the Batch request:** |
| 256 | + The Worker expects a `POST` request with a `JSON` payload containing an array called `requests`. Each prompt is an individual inference request. |
| 257 | + |
| 258 | +- **Processing the request:** |
| 259 | + The code validates the payload and uses the AI binding (`env.AI.run()`) to send the batch request to a designated model (such as, `@cf/meta/ray-llama-3.3-70b-instruct-fp8-fast`). |
| 260 | + |
| 261 | +- **Returning the results:** |
| 262 | + Once processed, the AI API returns the batch responses. These responses include an array where each object has an `id` (matching the prompt index) and the corresponding inference result. |
| 263 | + |
| 264 | +## 7. Deployment |
| 265 | + |
| 266 | +After completing your changes, deploy your Worker with the following command: |
| 267 | + |
| 268 | +```sh |
| 269 | +npm run deploy |
| 270 | +``` |
0 commit comments