Skip to content

Commit f4e07c8

Browse files
LLama tutorial
1 parent edd92ed commit f4e07c8

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
updated: 2025-02-04
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+
<PackageManagers
42+
type="create"
43+
pkg="cloudflare@latest"
44+
args={"llama-vision-tutorial"}
45+
/>
46+
47+
<Render
48+
file="c3-post-run-steps"
49+
product="workers"
50+
params={{
51+
category: "hello-world",
52+
type: "Hello World Worker",
53+
lang: "JavaScript",
54+
}}
55+
/>
56+
57+
After completing the setup, a new directory called `llama-vision-tutorial` will be created.
58+
59+
3. Navigate to your application directory
60+
Change into the project directory:
61+
62+
```bash
63+
cd llama-vision-tutorial
64+
```
65+
66+
4. Project structure
67+
Your `llama-vision-tutorial` directory will include:
68+
- A "Hello World" Worker at `src/index.ts`.
69+
- A `wrangler.toml` configuration file for managing deployment settings.
70+
71+
## 4: Write the Worker code
72+
73+
Edit the `src/index.ts` (or `index.js` if you're not using TypeScript) file and replace the content with the following code:
74+
75+
```javascript
76+
export interface Env {
77+
AI: Ai;
78+
}
79+
80+
export default {
81+
async fetch(request, env): Promise<Response> {
82+
const messages = [
83+
{ role: "system", content: "You are a helpful assistant." },
84+
{ role: "user", content: "Describe the image I'm providing." },
85+
];
86+
87+
// Replace this with your image data encoded as base64 or a URL
88+
const imageBase64 = "data:image/png;base64,IMAGE_DATA_HERE";
89+
90+
const response = await env.AI.run("@cf/meta/llama-3.2-11b-vision-instruct", {
91+
messages,
92+
image: imageBase64,
93+
});
94+
95+
return Response.json(response);
96+
},
97+
} satisfies ExportedHandler<Env>;
98+
```
99+
100+
## 5: Bind Workers AI to your Worker
101+
102+
1. Open `wrangler.toml` and add the following configuration:
103+
104+
```toml
105+
[env]
106+
[ai]
107+
binding="AI"
108+
model = "@cf/meta/llama-3.2-11b-vision-instruct"
109+
```
110+
111+
2. Save the file.
112+
113+
## 6: Deploy the Worker
114+
115+
Run the following command to deploy your Worker:
116+
117+
```bash
118+
wrangler deploy
119+
```
120+
121+
## 7: Test Your Worker
122+
123+
1. After deployment, you will receive a unique URL for your Worker (e.g., `https://llama-vision-tutorial.<your-subdomain>.workers.dev`).
124+
2. Use a tool like `curl` or Postman to send a request to your Worker:
125+
126+
```bash
127+
curl -X POST https://llama-vision-tutorial.<your-subdomain>.workers.dev \
128+
-d '{ "image": "BASE64_ENCODED_IMAGE" }'
129+
```
130+
131+
Replace `BASE64_ENCODED_IMAGE` with an actual base64-encoded image string.
132+
133+
## 8: Verify the Response
134+
135+
The response will include the model's output, such as a description or answer to your prompt based on the image provided.
136+
137+
Example response:
138+
139+
```json
140+
{
141+
"result": "This is a golden retriever sitting in a grassy park."
142+
}
143+
```

0 commit comments

Comments
 (0)