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 tutorial explains how to use [Cloudflare AI Gateway](/ai-gateway/) and Zero Trust to create a functional and secure AI agent wrapper. Cloudflare Zero Trust admins can enforce [Gateway](/cloudflare-one/policies/gateway/) controls on how their users interact with AI agents, including executing AI agents in an isolated browser with [Browser Isolation](/cloudflare-one/policies/browser-isolation/), enforcing [Data Loss Prevention](/cloudflare-one/policies/data-loss-prevention/) profiles to prevent sensitive data exfiltration, and scanning content to avoid answers from AI agents that violate internal corporate guidelines. Creating an AI agent wrapper is also an effective way to enforce tenant control if you have an enterprise plan of a specific AI provider, such as ChatGPT Enterprise.
10
+
This tutorial explains how to use [Cloudflare AI Gateway](/ai-gateway/) and Zero Trust to create a functional and secure AI agent wrapper. Cloudflare Zero Trust admins can protect access to the wrapper with [Cloudflare Access](/cloudflare-one/policies/access/). Additionally, you can enforce [Gateway](/cloudflare-one/policies/gateway/) controls on how your users interact with AI agents, including executing AI agents in an isolated browser with [Browser Isolation](/cloudflare-one/policies/browser-isolation/), enforcing [Data Loss Prevention](/cloudflare-one/policies/data-loss-prevention/) profiles to prevent sensitive data exfiltration, and scanning content to avoid answers from AI agents that violate internal corporate guidelines. Creating an AI agent wrapper is also an effective way to enforce tenant control if you have an enterprise plan of a specific AI provider, such as ChatGPT Enterprise.
11
11
12
12
This tutorial uses ChatGPT as an example AI agent.
13
13
@@ -26,7 +26,7 @@ First, create an AI gateway to control an AI app.
26
26
5. Select **Create**.
27
27
6. Configure your desired options for the gateway.
28
28
7.[Connect your AI provider](/ai-gateway/get-started/#connect-application) to proxy queries to your AI agent of choice using your AI gateway.
29
-
8. Turn on [Authenticated Gateway](/ai-gateway/configuration/authentication/). The Authenticated Gateway feature ensures your AI gateway can only be called securely by enforcing a token in the form of a request header `cf-aig-authorization`.
29
+
8.(Optional) Turn on [Authenticated Gateway](/ai-gateway/configuration/authentication/). The Authenticated Gateway feature ensures your AI gateway can only be called securely by enforcing a token in the form of a request header `cf-aig-authorization`.
30
30
1. Go to **AI** > **AI Gateway**.
31
31
2. Select your AI gateway, then go to **Settings**.
32
32
3. Turn on **Authenticated Gateway**, then choose **Confirm**.
@@ -35,7 +35,7 @@ First, create an AI gateway to control an AI app.
35
35
36
36
For more information, refer to [Getting started with AI Gateway](/ai-gateway/get-started/).
37
37
38
-
###2. (Optional) Use Guardrails to block unsafe or inappropriate content
38
+
## 2. (Optional) Use Guardrails to block unsafe or inappropriate content
39
39
40
40
[Guardrails](/ai-gateway/guardrails/) is an built-in AI Gateway security feature that allows Cloudflare to identify unsafe or inappropriate content in prompts and responses based on selected categories.
41
41
@@ -45,13 +45,15 @@ For more information, refer to [Getting started with AI Gateway](/ai-gateway/get
45
45
4. Turn on Guardrails.
46
46
5. Select **Change** to configure the categories you would like to filter for both prompts and responses.
47
47
48
-
## 3. Create a Worker to serve a wrapper
48
+
## 3. Build a Worker to serve the wrapper
49
+
50
+
### 1. Create the Worker
49
51
50
52
In order to build the Worker, you will need to choose if you want to build it locally using [Wrangler](/workers/wrangler/install-and-update/) or remotely using the [dashboard](https://dash.cloudflare.com/).
51
53
52
54
<Tabs> <TabItemlabel="Wrangler">
53
55
54
-
1.Log in to your Cloudlare account:
56
+
1.In a terminal, log in to your Cloudflare account:
55
57
56
58
```bash
57
59
wrangler login
@@ -76,33 +78,34 @@ In order to build the Worker, you will need to choose if you want to build it lo
76
78
# Add any environment variables here
77
79
```
78
80
79
-
4. Add your AI provider API Key as a secret:
81
+
4. Add your AI provider's API key as a [secret](/workers/configuration/secrets/):
80
82
81
83
```bash
82
-
wrangler secret put OPENAI_API_KEY
84
+
wrangler secret put <OPENAI_API_KEY>
83
85
```
84
86
85
-
5. The Worker can now be built using the `index.js` file that was created by **Wrangler**.
87
+
You can now build the Worker using the `index.js` file created by Wrangler.
86
88
87
89
</TabItem> <TabItemlabel="Dashboard">
88
90
89
-
1. Log into the [Cloudflare dashboard](https://dash.cloudflare.com/) and select your account.
91
+
1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com/) and select your account.
90
92
2. Go to **Workers & Pages** > **Workers & Pages**.
91
93
3. Select **Create**.
92
-
4. Select **Workers** and the **Hello world** template.
93
-
5. Select **Deploy**.
94
-
6. Go to your Worker, select the **Settings** tab.
95
-
7. Within the **Variables and Secrets** section, select **Add**.
96
-
8. Choose **Secret** as the type, give your variable a name such as `OPENAI_API_KEY`, and past the value of your AI provider API key in the **Value** field.
97
-
9. The Worker can now be built by using the online code editor by clicking the **Edit code** icon at the top-right.
94
+
4. In **Workers**, choose the **Hello world** template.
95
+
5. Name your worker, then select **Deploy**.
96
+
6. Select your Worker, then go to the **Settings** tab.
97
+
7. Go to **Variables and Secrets**, then select **Add**.
98
+
8. Choose _Secret_ as the type, name your secret (for example, `OPENAI_API_KEY`), and enter the value of your AI provider's API key in **Value**.
99
+
100
+
You can now build the Worker using the online code editor by selecting **Edit code** on your Worker page.
98
101
99
102
</TabItem> </Tabs>
100
103
101
-
### Build the Worker
104
+
### 2. Build the Worker
102
105
103
-
The following is an example starter Worker that serves a simple front-end to allow a user to interact with an AI provider behind **AI Gateway**. For this example, OpenAI was used as the provider:
106
+
The following is an example starter Worker that serves a simple front-end to allow a user to interact with an AI provider behind AI Gateway. This example uses OpenAI as its AI provider:
104
107
105
-
<Detailsheader="Example Worker">
108
+
<Detailsheader="Example AI wrapper Worker">
106
109
107
110
```javascript
108
111
exportdefault {
@@ -340,17 +343,15 @@ const HTML = `<!DOCTYPE html>
340
343
341
344
</Details>
342
345
343
-
Notice that the **Account ID** and the **Gateway ID** need to be replaced in the AI Gateway endpoint. You can add these as environment variables or Secrets. If you chose to use the **Authenticated Gateway** option when creating your AI Gateway, make sure to also add your token as a Secret and pass its value through to the AI Gateway in the form of the `cf-aig-authorization` header.
344
-
345
-
This Worker can serve as a starting point and extra functionality can be built on top.
346
+
Note that the account ID and gateway ID need to be replaced in the AI Gateway endpoint. You can add these as [environment variables](/workers/configuration/environment-variables/) or [secrets](/workers/configuration/secrets/) in Workers. If you chose to use Authenticated Gateway when creating your AI gateway, make sure to also add your token as a secret and pass its value to the AI gateway in the `cf-aig-authorization` header.
346
347
347
-
### Publish the Worker
348
+
### 3. Publish the Worker
348
349
349
-
Once the Worker is code is completed, it is almost ready to be published. We now need to make the Worker addressable via a hostname that can be controlled by Cloudlare Access.
350
+
Once the Worker code is complete, you need to make the Worker addressable using a hostname controllable by Cloudflare Access.
350
351
351
352
<Tabs> <TabItemlabel="Wrangler">
352
353
353
-
Edit the `wrangler.toml` configuration file and add the following information to ensure that the worker is only accessible via the custom hostname.
354
+
Edit the `wrangler.toml` configuration file and add the following information to ensure that the worker is only accessible using the custom hostname:
@@ -377,19 +378,19 @@ In order to ensure that the worker is only accessible via the custom hostname, d
377
378
378
379
2. Go to **Workers & Pages** > **Workers & Pages**.
379
380
3. Select your Worker.
380
-
4.Select the**Settings** tab.
381
-
5. Within the **Domains & Routes** section, select **Add**.
382
-
6. Choose **Custom domain (Recommended)** (**Route** is also a viable option).
383
-
7.Add your desired custom domain name.
381
+
4.Go to**Settings**.
382
+
5. Within **Domains & Routes**, select **Add**.
383
+
6. Choose **Custom domain**.
384
+
7.Enter your desired custom domain name.
384
385
8. Select **Add domain**.
385
386
386
-
The Worker now sits behind an addressable public hostname. Make sure to disable both your **workers.dev** and **Preview URLs** so that the Worker can only be accessed via its **Custom domain**.
387
+
The Worker is now behind an addressable public hostname. Make sure to turn off both **workers.dev** and **Preview URLs** so that the Worker can only be accessed with its custom domain.
387
388
388
389
</TabItem> </Tabs>
389
390
390
-
## Secure the AI Agent Wrapper using Access
391
+
## 4. Secure the wrapper with Access
391
392
392
-
We need to secure our AI Agent Wrapper to ensure that only trusted users can access it. We will use [**Access**](https://developers.cloudflare.com/cloudflare-one/policies/access/) to achieve this.
393
+
To secure the AI agent wrapper to ensure that only trusted users can access it:
393
394
394
395
1. In [Zero Trust](https://one.dash.cloudflare.com/), go to **Access** > **Applications**.
0 commit comments