Skip to content

Commit c37fc9c

Browse files
Vision tutorial
1 parent 203a984 commit c37fc9c

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
---
2+
updated: 2025-01-17
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 } from "~/components";
12+
13+
## 1: 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+
## 2: 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+
## 3: 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+
3. Navigate to your application directory
61+
Change into the project directory:
62+
63+
```bash
64+
cd llama-vision-tutorial
65+
```
66+
67+
4. Project structure
68+
Your `llama-vision-tutorial` directory will include:
69+
- A "Hello World" Worker at `src/index.ts`.
70+
- A `wrangler.toml` configuration file for managing deployment settings.
71+
72+
## 4: Write the Worker code
73+
74+
Edit the `src/index.ts` (or `index.js` if you're 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+
## 5: Bind Workers AI to your Worker
102+
103+
1. Open `wrangler.toml` and add the following configuration:
104+
105+
```toml
106+
[env]
107+
[ai]
108+
binding="AI"
109+
model = "@cf/meta/llama-3.2-11b-vision-instruct"
110+
```
111+
112+
2. Save the file.
113+
114+
## 6: Deploy the Worker
115+
116+
Run the following command to deploy your Worker:
117+
118+
```bash
119+
wrangler deploy
120+
```
121+
122+
## 7: Test Your Worker
123+
124+
1. After deployment, you will receive a unique URL for your Worker (e.g., `https://llama-vision-tutorial.<your-subdomain>.workers.dev`).
125+
2. Use a tool like `curl` or Postman to send a request to your Worker:
126+
127+
```bash
128+
curl -X POST https://llama-vision-tutorial.<your-subdomain>.workers.dev \
129+
-d '{ "image": "BASE64_ENCODED_IMAGE" }'
130+
```
131+
132+
Replace `BASE64_ENCODED_IMAGE` with an actual base64-encoded image string.
133+
134+
## 8: Verify the Response
135+
136+
The response will include the model's output, such as a description or answer to your prompt based on the image provided.
137+
138+
Example response:
139+
140+
```json
141+
{
142+
"result": "This is a golden retriever sitting in a grassy park."
143+
}
144+
```

0 commit comments

Comments
 (0)