Skip to content

Commit ccee4e1

Browse files
committed
Merge branch 'production' into aig-vercel-changelog
2 parents 93b8246 + 33a5230 commit ccee4e1

File tree

44 files changed

+1259
-418
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1259
-418
lines changed

public/_redirects

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@
9393
/ai/ /use-cases/ai/ 301
9494

9595
# AI Gateway
96-
9796
/ai-gateway/get-started/configuring-settings/ /ai-gateway/get-started/ 301
9897
/ai-gateway/get-started/connecting-applications/ /ai-gateway/get-started/ 301
9998
/ai-gateway/get-started/creating-gateway/ /ai-gateway/get-started/ 301
10099
/ai-gateway/pricing/ /ai-gateway/reference/pricing/ 301
101100
/ai-gateway/observability/evaluations/ /ai-gateway/evaluations/ 301
102101
/ai-gateway/observability/evaluations/set-up-evaluations/ /ai-gateway/evaluations//set-up-evaluations/ 301
103102
/ai-gateway/integration/vercel-ai-sdk/ /ai-gateway/integrations/vercel-ai-sdk/ 301
103+
/ai-gateway/integration/aig-workers-ai-binding/ /ai-gateway/integrations/aig-workers-ai-binding/ 301
104104
/ai-gateway/integration/ /ai-gateway/integrations/ 301
105105

106106
# analytics
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
title: Workers AI
3+
pcx_content_type: tutorial
4+
updated: 2024-10-17
5+
---
6+
7+
import { Render, PackageManagers } from "~/components";
8+
9+
This guide will walk you through setting up and deploying a Workers AI project. You will use [Workers](/workers/), an AI Gateway binding, and a large language model (LLM), to deploy your first AI-powered application on the Cloudflare global network.
10+
11+
## Prerequisites
12+
13+
<Render file="prereqs" product="workers" />
14+
15+
## 1. Create a Worker Project
16+
17+
You will create a new Worker project using the create-Cloudflare CLI (C3). C3 is a command-line tool designed to help you set up and deploy new applications to Cloudflare.
18+
19+
Create a new project named `hello-ai` by running:
20+
21+
<PackageManagers type="create" pkg="cloudflare@latest" args={"hello-ai"} />
22+
23+
Running `npm create cloudflare@latest` will prompt you to install the create-cloudflare package and lead you through setup. C3 will also install [Wrangler](/workers/wrangler/), the Cloudflare Developer Platform CLI.
24+
25+
<Render
26+
file="c3-post-run-steps"
27+
product="workers"
28+
params={{
29+
category: "hello-world",
30+
type: "Hello World Worker",
31+
lang: "TypeScript",
32+
}}
33+
/>
34+
35+
This will create a new `hello-ai` directory. Your new `hello-ai` directory will include:
36+
37+
- A "Hello World" Worker at `src/index.ts`.
38+
- A `wrangler.toml` configuration file.
39+
40+
Go to your application directory:
41+
42+
```bash
43+
cd hello-ai
44+
```
45+
46+
## 2. Connect your Worker to Workers AI
47+
48+
You must create an AI binding for your Worker to connect to Workers AI. Bindings allow your Workers to interact with resources, like Workers AI, on the Cloudflare Developer Platform.
49+
50+
To bind Workers AI to your Worker, add the following to the end of your `wrangler.toml` file:
51+
52+
```toml title="wrangler.toml"
53+
[ai]
54+
binding = "AI"
55+
```
56+
57+
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/).
58+
59+
You will need to have your `gateway id` for the next step. You can learn [how to create an AI Gateway in this tutorial](/ai-gateway/get-started/).
60+
61+
## 3. Run an inference task containing AI Gateway in your Worker
62+
63+
You are now ready to run an inference task in your Worker. In this case, you will use an LLM, [`llama-3.1-8b-instruct-fast`](/workers-ai/models/llama-3.1-8b-instruct-fast/), to answer a question. Your gateway ID is found on the dashboard.
64+
65+
Update the `index.ts` file in your `hello-ai` application directory with the following code:
66+
67+
```typescript title="src/index.ts" {78-81}
68+
export interface Env {
69+
// If you set another name in wrangler.toml as the value for 'binding',
70+
// replace "AI" with the variable name you defined.
71+
AI: Ai;
72+
}
73+
74+
export default {
75+
async fetch(request, env): Promise<Response> {
76+
// Specify the gateway label and other options here
77+
const response = await env.AI.run("@cf/meta/llama-3.1-8b-instruct-fast", {
78+
prompt: "What is the origin of the phrase Hello, World",
79+
gateway: {
80+
id: "GATEWAYID", // Use your gateway label here
81+
skipCache: true, // Optional: Skip cache if needed
82+
},
83+
});
84+
85+
// Return the AI response as a JSON object
86+
return new Response(JSON.stringify(response), {
87+
headers: { "Content-Type": "application/json" },
88+
});
89+
},
90+
} satisfies ExportedHandler<Env>;
91+
```
92+
93+
Up to this point, you have created an AI binding for your Worker and configured your Worker to be able to execute the Llama 3.1 model. You can now test your project locally before you deploy globally.
94+
95+
## 4. Develop locally with Wrangler
96+
97+
While in your project directory, test Workers AI locally by running [`wrangler dev`](/workers/wrangler/commands/#dev):
98+
99+
```bash
100+
npx wrangler dev
101+
```
102+
103+
<Render file="ai-local-usage-charges" product="workers" />
104+
105+
You will be prompted to log in after you run `wrangler dev`. When you run `npx wrangler dev`, Wrangler will give you a URL (most likely `localhost:8787`) to review your Worker. After you go to the URL Wrangler provides, you will see a message that resembles the following example:
106+
107+
````json
108+
{
109+
"response": "A fascinating question!\n\nThe phrase \"Hello, World!\" originates from a simple computer program written in the early days of programming. It is often attributed to Brian Kernighan, a Canadian computer scientist and a pioneer in the field of computer programming.\n\nIn the early 1970s, Kernighan, along with his colleague Dennis Ritchie, were working on the C programming language. They wanted to create a simple program that would output a message to the screen to demonstrate the basic structure of a program. They chose the phrase \"Hello, World!\" because it was a simple and recognizable message that would illustrate how a program could print text to the screen.\n\nThe exact code was written in the 5th edition of Kernighan and Ritchie's book \"The C Programming Language,\" published in 1988. The code, literally known as \"Hello, World!\" is as follows:\n\n```
110+
main()
111+
{
112+
printf(\"Hello, World!\");
113+
}
114+
```\n\nThis code is still often used as a starting point for learning programming languages, as it demonstrates how to output a simple message to the console.\n\nThe phrase \"Hello, World!\" has since become a catch-all phrase to indicate the start of a new program or a small test program, and is widely used in computer science and programming education.\n\nSincerely, I'm glad I could help clarify the origin of this iconic phrase for you!"
115+
}
116+
````
117+
118+
## 5. Deploy your AI Worker
119+
120+
Before deploying your AI Worker globally, log in with your Cloudflare account by running:
121+
122+
```bash
123+
npx wrangler login
124+
```
125+
126+
You will be directed to a web page asking you to log in to the Cloudflare dashboard. After you have logged in, you will be asked if Wrangler can make changes to your Cloudflare account. Scroll down and select **Allow** to continue.
127+
128+
Finally, deploy your Worker to make your project accessible on the Internet. To deploy your Worker, run:
129+
130+
```bash
131+
npx wrangler deploy
132+
```
133+
134+
Once deployed, your Worker will be available at a URL like:
135+
136+
```bash
137+
https://hello-ai.<YOUR_SUBDOMAIN>.workers.dev
138+
```
139+
140+
Your Worker will be deployed to your custom [`workers.dev`](/workers/configuration/routing/workers-dev/) subdomain. You can now visit the URL to run your AI Worker.
141+
142+
By completing this tutorial, you have created a Worker, connected it to Workers AI through an AI Gateway binding, and successfully ran an inference task using the Llama 3.1 model.

src/content/docs/cloudflare-one/applications/non-http/infrastructure-apps.mdx

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,13 @@ Access for Infrastructure currently only supports [SSH](/cloudflare-one/connecti
4747

4848
<Render file="access/add-infrastructure-app" />
4949

50-
## 3. Add a policy
51-
52-
<Render file="access/add-infrastructure-policy" />
53-
54-
### Selectors
55-
56-
The following [Access policy selectors](/cloudflare-one/policies/access/#selectors) are available for securing infrastructure applications:
57-
58-
- Email
59-
- Emails ending in
60-
- SAML group
61-
- Country
62-
- Authentication method
63-
- Device posture
64-
- Entra group, GitHub organization, Google Workspace group, Okta group
65-
66-
## 4. Configure the server
50+
## 3. Configure the server
6751

6852
Certain protocols require configuring the server to trust connections through Access for Infrastructure. For more information, refer to the protocol-specific tutorial:
6953

7054
- [SSH](/cloudflare-one/connections/connect-networks/use-cases/ssh/ssh-infrastructure-access/#7-configure-ssh-server)
7155

72-
## Connect as a user
56+
## 4. Connect as a user
7357

7458
Users connect to the target's IP address as if they were on your private network, using their preferred client software. The user must be logged into WARP on their device, but no other system configuration is required. You can optionally configure a [private DNS resolver](/cloudflare-one/policies/gateway/resolver-policies/) to allow connections to the target's private hostname.
7559

@@ -116,3 +100,15 @@ warp-cli target list
116100
## Revoke a user's session
117101

118102
To revoke a user's access to all infrastructure targets, you can either [revoke the user from Zero Trust](/cloudflare-one/identity/users/session-management/#per-user) or revoke their device. Cloudflare does not currently support revoking a user's session for a specific target.
103+
104+
## Infrastructure policy selectors
105+
106+
The following [Access policy selectors](/cloudflare-one/policies/access/#selectors) are available for securing infrastructure applications:
107+
108+
- Email
109+
- Emails ending in
110+
- SAML group
111+
- Country
112+
- Authentication method
113+
- Device posture
114+
- Entra group, GitHub organization, Google Workspace group, Okta group

0 commit comments

Comments
 (0)