Get started with Workers
This guide will instruct you through setting up and deploying your first Workers AI project. You will use Workers, a Workers AI binding, and a large language model (LLM) to deploy your first AI-powered application on the Cloudflare global network.
Prerequisites
- Sign up for a Cloudflare account.
- Install
npm
. - Install
Node.js
.
Get started in the dashboard
This guide uses the command line. To instead create your Workers AI application using the Cloudflare dashboard:
- Log in to the Cloudflare dashboard and select your account.
- Select Workers & Pages > Create application.
- Under Create using a template, select LLM App. After you select your template, an AI binding will be created for you in the dashboard.
- Review the pregenerated code and select Deploy.
- Preview your Worker at its provided
workers.dev
subdomain.
1. Create a Worker project
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.
Create a new project named hello-ai
by running:
$ npm create cloudflare@latest
$ yarn create cloudflare
Running npm create cloudflare@latest
will prompt you to install the create-cloudflare
package, and lead you through setup. C3 will also install Wrangler, the Cloudflare Developer Platform CLI.
When setting up your hello-ai
Worker, answer the setup questions as follows:
- Enter
hello-ai
for the directory to create in. - Choose
"Hello World" Worker
for the type of application. - Select
yes
to using TypeScript. - Select
yes
to using Git. - Select
no
to deploying.
This will create a new hello-ai
directory. Your new hello-ai
directory will include:
- A
"Hello World"
Worker atsrc/index.ts
. - A
wrangler.toml
configuration file.
Go to your application directory:
$ cd hello-ai
2. Connect your Worker to Workers AI
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.
To bind Workers AI to your Worker, add the following to the end of your wrangler.toml
file:
wrangler.toml[ai]
binding = "AI"
Your binding is available in your Worker code on env.AI
.
You can also bind Workers AI to a Pages Function. For more information, refer to Functions Bindings.
3. Install the Workers AI SDK
The Workers AI SDK makes Workers AI APIs available for use in your code. To import the Workers AI SDK, run the following command in your application directory:
$ npm install --save-dev @cloudflare/ai
$ yarn add --dev @cloudflare/ai
4. Run an inference task in your Worker
You are now ready to run an inference task in your Worker. In this case, you will use an LLM, llama-2-7b-chat-int8
, to answer a question.
Update the index.ts
file in your hello-ai
application directory with the following code:
"src/index.ts"import { Ai } from '@cloudflare/ai'
export interface Env { // If you set another name in wrangler.toml as the value for 'binding', // replace "AI" with the variable name you defined. AI: any;
}
export default { async fetch(request: Request, env: Env) { const ai = new Ai(env.AI);
const response = await ai.run('@cf/meta/llama-2-7b-chat-int8', { prompt: "What is the origin of the phrase Hello, World" } );
return new Response(JSON.stringify(response)); },
};
Up to this point, you have created an AI binding for your Worker and configured your Worker to be able to execute the Llama 2 model. You can now test your project locally before you deploy globally.
5. Develop locally with Wrangler
While in your project directory, test Workers AI locally by running wrangler dev
:
$ npx wrangler dev
You will be prompted to log in after you run the 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, a message will render that resembles the following example:
{ "result": { "response": "Hello, World first appeared in 1974 at Bell Labs when Brian Kernighan included it in the C programming language example. It became widely used as a basic test program due to simplicity and clarity. It represents an inviting greeting from a program to the world." }
}
6. Deploy your AI Worker
Before deploying your AI Worker globally, log in with your Cloudflare account by running:
$ npx wrangler login
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.
Finally, deploy your Worker to make your project accessible on the Internet. To deploy your Worker, run:
$ npx wrangler deploy
# Outputs: https://hello-ai.<YOUR_SUBDOMAIN>.workers.dev
Your Worker will be deployed to your custom workers.dev
subdomain. You can now visit the URL to run your AI Worker.
By finishing this tutorial, you have created a Worker, connected it to Workers AI through an AI binding, and ran an inference task from the Llama 2 model.
Related resources
- Cloudflare Developers community on Discord - Submit feature requests, report bugs, and share your feedback directly with the Cloudflare team by joining the Cloudflare Discord server.
- Models - Browse the Workers AI models catalog.