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
This guide provides an overview of how to use the latest Cloudflare Workers AI Gateway binding methods. You will learn how to set up an AI Gateway binding, access new methods, and integrate them into your Workers.
10
10
11
-
## Prerequisites
12
-
13
-
- Install and use the `@cloudflare/workers-types` library, version `4.20250124.3` or above.
14
-
15
11
## 1. Add an AI Binding to your Worker
16
12
17
13
To connect your Worker to Workers AI, add the following to your [Wrangler configuration file](/workers/wrangler/configuration/):
@@ -29,18 +25,24 @@ binding = "AI"
29
25
30
26
This configuration sets up the AI binding accessible in your Worker code as `env.AI`.
Additionally, you can access the latest request log ID with:
@@ -64,12 +66,12 @@ Once you have the gateway instance, you can use the following methods:
64
66
The `patchLog` method allows you to send feedback, score, and metadata for a specific log ID. All object properties are optional, so you can include any combination of the parameters:
65
67
66
68
```typescript
67
-
gateway.patchLog('my-log-id', {
68
-
feedback: 1,
69
-
score: 100,
70
-
metadata: {
71
-
user: "123"
72
-
}
69
+
gateway.patchLog("my-log-id", {
70
+
feedback: 1,
71
+
score: 100,
72
+
metadata: {
73
+
user: "123",
74
+
},
73
75
});
74
76
```
75
77
@@ -78,7 +80,7 @@ gateway.patchLog('my-log-id', {
78
80
79
81
### 3.2. `getLog`: Read Log Details
80
82
81
-
The `getLog` method retrieves details of a specific log ID. It returns an object of type `Promise<AiGatewayLog>`. You can import the `AiGatewayLog` type from the `@cloudflare/workers-types` library.
83
+
The `getLog` method retrieves details of a specific log ID. It returns an object of type `Promise<AiGatewayLog>`. If this type is missing, ensure you have run [`wrangler types`](/workers/languages/typescript/#generate-types).
@@ -145,14 +150,14 @@ Refer to the [Universal endpoint documentation](/ai-gateway/providers/universal/
145
150
146
151
```typescript
147
152
const resp =awaitgateway.run({
148
-
provider: "workers-ai",
149
-
endpoint: "@cf/meta/llama-3.1-8b-instruct",
150
-
headers: {
151
-
authorization: "Bearer my-api-token"
152
-
},
153
-
query: {
154
-
prompt: "tell me a joke"
155
-
}
153
+
provider: "workers-ai",
154
+
endpoint: "@cf/meta/llama-3.1-8b-instruct",
155
+
headers: {
156
+
authorization: "Bearer my-api-token",
157
+
},
158
+
query: {
159
+
prompt: "tell me a joke",
160
+
},
156
161
});
157
162
```
158
163
@@ -168,4 +173,4 @@ With these AI Gateway binding methods, you can now:
168
173
- Get gateway URLs for direct API access with `getUrl`, making it easy to integrate with popular AI SDKs.
169
174
- Execute universal requests to any AI Gateway provider with `run`.
170
175
171
-
These methods offer greater flexibility and control over your AI integrations, empowering you to build more sophisticated applications on the Cloudflare Workers platform.
176
+
These methods offer greater flexibility and control over your AI integrations, empowering you to build more sophisticated applications on the Cloudflare Workers platform.
0 commit comments