Skip to content

Commit bef38e0

Browse files
Workers AI Vision tutorial (#19755)
* Vision tutorial * minor edits * Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx Co-authored-by: Jun Lee <[email protected]> * Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx Co-authored-by: Jun Lee <[email protected]> * Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx Co-authored-by: Jun Lee <[email protected]> * Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx Co-authored-by: Jun Lee <[email protected]> * Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx Co-authored-by: Jun Lee <[email protected]> * Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx Co-authored-by: Jun Lee <[email protected]> * Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx Co-authored-by: Jun Lee <[email protected]> * Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx Co-authored-by: Jun Lee <[email protected]> * Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx Co-authored-by: Jun Lee <[email protected]> * Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx Co-authored-by: Jun Lee <[email protected]> --------- Co-authored-by: Jun Lee <[email protected]>
1 parent 1d16bbe commit bef38e0

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
updated: 2025-02-05
3+
difficulty: Beginner
4+
content_type: 📝 Tutorial
5+
pcx_content_type: tutorial
6+
title: Llama 3.2 11B Vision Instruct model on Cloudflare Workers AI
7+
tags:
8+
- AI
9+
---
10+
11+
import { Details, Render, PackageManagers, WranglerConfig } from "~/components";
12+
13+
## Prerequisites
14+
15+
Before you begin, ensure you have the following:
16+
17+
1. A [Cloudflare account](https://dash.cloudflare.com/sign-up) with Workers and Workers AI enabled.
18+
2. Your `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_AUTH_TOKEN`.
19+
- You can generate an API token in your Cloudflare dashboard under API Tokens.
20+
3. Node.js installed for working with Cloudflare Workers (optional but recommended).
21+
22+
## 1. Agree to Meta's license
23+
24+
The first time you use the [Llama 3.2 11B Vision Instruct](/workers-ai/models/llama-3.2-11b-vision-instruct) model, you need to agree to Meta's License and Acceptable Use Policy.
25+
26+
```bash title="curl"
27+
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run/@cf/meta/llama-3.2-11b-vision-instruct \
28+
-X POST \
29+
-H "Authorization: Bearer $CLOUDFLARE_AUTH_TOKEN" \
30+
-d '{ "prompt": "agree" }'
31+
```
32+
33+
Replace `$CLOUDFLARE_ACCOUNT_ID` and `$CLOUDFLARE_AUTH_TOKEN` with your actual account ID and token.
34+
35+
## 2. Set up your Cloudflare Worker
36+
37+
1. Create a Worker Project
38+
You will create a new Worker project using the `create-cloudflare` CLI (`C3`). This tool simplifies setting up and deploying new applications to Cloudflare.
39+
40+
Run the following command in your terminal:
41+
42+
<PackageManagers
43+
type="create"
44+
pkg="cloudflare@latest"
45+
args={"llama-vision-tutorial"}
46+
/>
47+
48+
<Render
49+
file="c3-post-run-steps"
50+
product="workers"
51+
params={{
52+
category: "hello-world",
53+
type: "Hello World Worker",
54+
lang: "JavaScript",
55+
}}
56+
/>
57+
58+
After completing the setup, a new directory called `llama-vision-tutorial` will be created.
59+
60+
2. Navigate to your application directory
61+
Change into the project directory:
62+
63+
```bash
64+
cd llama-vision-tutorial
65+
```
66+
67+
3. Project structure
68+
Your `llama-vision-tutorial` directory will include:
69+
- A "Hello World" Worker at `src/index.ts`.
70+
- A `wrangler.json` configuration file for managing deployment settings.
71+
72+
## 3. Write the Worker code
73+
74+
Edit the `src/index.ts` (or `index.js` if you are not using TypeScript) file and replace the content with the following code:
75+
76+
```javascript
77+
export interface Env {
78+
AI: Ai;
79+
}
80+
81+
export default {
82+
async fetch(request, env): Promise<Response> {
83+
const messages = [
84+
{ role: "system", content: "You are a helpful assistant." },
85+
{ role: "user", content: "Describe the image I'm providing." },
86+
];
87+
88+
// Replace this with your image data encoded as base64 or a URL
89+
const imageBase64 = "data:image/png;base64,IMAGE_DATA_HERE";
90+
91+
const response = await env.AI.run("@cf/meta/llama-3.2-11b-vision-instruct", {
92+
messages,
93+
image: imageBase64,
94+
});
95+
96+
return Response.json(response);
97+
},
98+
} satisfies ExportedHandler<Env>;
99+
```
100+
101+
## 4. Bind Workers AI to your Worker
102+
103+
1. Open `wrangler.toml / wrangler.json` and add the following configuration:
104+
105+
<WranglerConfig>
106+
```toml
107+
[env]
108+
[ai]
109+
binding="AI"
110+
model = "@cf/meta/llama-3.2-11b-vision-instruct"
111+
```
112+
</WranglerConfig>
113+
114+
2. Save the file.
115+
116+
## 5. Deploy the Worker
117+
118+
Run the following command to deploy your Worker:
119+
120+
```bash
121+
wrangler deploy
122+
```
123+
124+
## 6. Test Your Worker
125+
126+
1. After deployment, you will receive a unique URL for your Worker (e.g., `https://llama-vision-tutorial.<your-subdomain>.workers.dev`).
127+
2. Use a tool like `curl` or Postman to send a request to your Worker:
128+
129+
```bash
130+
curl -X POST https://llama-vision-tutorial.<your-subdomain>.workers.dev \
131+
-d '{ "image": "BASE64_ENCODED_IMAGE" }'
132+
```
133+
134+
Replace `BASE64_ENCODED_IMAGE` with an actual base64-encoded image string.
135+
136+
## 7. Verify the response
137+
138+
The response will include the output from the model, such as a description or answer to your prompt based on the image provided.
139+
140+
Example response:
141+
142+
```json
143+
{
144+
"result": "This is a golden retriever sitting in a grassy park."
145+
}
146+
```

0 commit comments

Comments
 (0)