-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Workers AI Vision tutorial #19755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Workers AI Vision tutorial #19755
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c37fc9c
Vision tutorial
daisyfaithauma 7e5224e
minor edits
daisyfaithauma 3a6181d
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma b8c4469
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma 064586a
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma c8a7392
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma a0e8c19
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma 5fbd9d0
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma eb96a9a
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma c971208
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma 969eaf1
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma 4455d0c
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma 7ce101b
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma 7cbcfec
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma a6ccc79
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma 8ca7807
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma e26368e
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma 06fe290
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma ca3d0cf
Update src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
daisyfaithauma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
src/content/docs/workers-ai/tutorials/llama-vision-tutorial.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| --- | ||
| updated: 2025-02-05 | ||
| difficulty: Beginner | ||
| content_type: 📝 Tutorial | ||
| pcx_content_type: tutorial | ||
| title: Llama 3.2 11B Vision Instruct model on Cloudflare Workers AI | ||
| tags: | ||
| - AI | ||
| --- | ||
|
|
||
| import { Details, Render, PackageManagers } from "~/components"; | ||
|
|
||
| ## 1: Prerequisites | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Before you begin, ensure you have the following: | ||
|
|
||
| 1. A [Cloudflare account](https://dash.cloudflare.com/sign-up) with Workers and Workers AI enabled. | ||
| 2. Your `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_AUTH_TOKEN`. | ||
| - You can generate an API token in your Cloudflare dashboard under API Tokens. | ||
| 3. Node.js installed for working with Cloudflare Workers (optional but recommended). | ||
|
|
||
| ## 2: Agree to Meta's license | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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. | ||
|
|
||
| ```bash title="curl" | ||
| curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run/@cf/meta/llama-3.2-11b-vision-instruct \ | ||
| -X POST \ | ||
| -H "Authorization: Bearer $CLOUDFLARE_AUTH_TOKEN" \ | ||
| -d '{ "prompt": "agree" }' | ||
| ``` | ||
|
|
||
| Replace `$CLOUDFLARE_ACCOUNT_ID` and `$CLOUDFLARE_AUTH_TOKEN` with your actual account ID and token. | ||
|
|
||
| ## 3: Set up your Cloudflare Worker | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 1. Create a Worker Project | ||
| You will create a new Worker project using the `create-cloudflare` CLI (`C3`). This tool simplifies setting up and deploying new applications to Cloudflare. | ||
|
|
||
| Run the following command in your terminal: | ||
|
|
||
| <PackageManagers | ||
| type="create" | ||
| pkg="cloudflare@latest" | ||
| args={"llama-vision-tutorial"} | ||
| /> | ||
|
|
||
| <Render | ||
| file="c3-post-run-steps" | ||
| product="workers" | ||
| params={{ | ||
| category: "hello-world", | ||
| type: "Hello World Worker", | ||
| lang: "JavaScript", | ||
| }} | ||
| /> | ||
|
|
||
| After completing the setup, a new directory called `llama-vision-tutorial` will be created. | ||
|
|
||
| 3. Navigate to your application directory | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Change into the project directory: | ||
|
|
||
| ```bash | ||
| cd llama-vision-tutorial | ||
| ``` | ||
|
|
||
| 4. Project structure | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Your `llama-vision-tutorial` directory will include: | ||
| - A "Hello World" Worker at `src/index.ts`. | ||
| - A `wrangler.toml` configuration file for managing deployment settings. | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## 4: Write the Worker code | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Edit the `src/index.ts` (or `index.js` if you're not using TypeScript) file and replace the content with the following code: | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```javascript | ||
| export interface Env { | ||
| AI: Ai; | ||
| } | ||
|
|
||
| export default { | ||
| async fetch(request, env): Promise<Response> { | ||
| const messages = [ | ||
| { role: "system", content: "You are a helpful assistant." }, | ||
| { role: "user", content: "Describe the image I'm providing." }, | ||
| ]; | ||
|
|
||
| // Replace this with your image data encoded as base64 or a URL | ||
| const imageBase64 = "data:image/png;base64,IMAGE_DATA_HERE"; | ||
|
|
||
| const response = await env.AI.run("@cf/meta/llama-3.2-11b-vision-instruct", { | ||
| messages, | ||
| image: imageBase64, | ||
| }); | ||
|
|
||
| return Response.json(response); | ||
| }, | ||
| } satisfies ExportedHandler<Env>; | ||
| ``` | ||
|
|
||
| ## 5: Bind Workers AI to your Worker | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 1. Open `wrangler.toml` and add the following configuration: | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
daisyfaithauma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ```toml | ||
| [env] | ||
| [ai] | ||
| binding="AI" | ||
| model = "@cf/meta/llama-3.2-11b-vision-instruct" | ||
| ``` | ||
|
|
||
daisyfaithauma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 2. Save the file. | ||
|
|
||
| ## 6: Deploy the Worker | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Run the following command to deploy your Worker: | ||
|
|
||
| ```bash | ||
| wrangler deploy | ||
| ``` | ||
|
|
||
| ## 7: Test Your Worker | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 1. After deployment, you will receive a unique URL for your Worker (e.g., `https://llama-vision-tutorial.<your-subdomain>.workers.dev`). | ||
| 2. Use a tool like `curl` or Postman to send a request to your Worker: | ||
|
|
||
| ```bash | ||
| curl -X POST https://llama-vision-tutorial.<your-subdomain>.workers.dev \ | ||
| -d '{ "image": "BASE64_ENCODED_IMAGE" }' | ||
| ``` | ||
|
|
||
| Replace `BASE64_ENCODED_IMAGE` with an actual base64-encoded image string. | ||
|
|
||
| ## 8: Verify the Response | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| The response will include the model's output, such as a description or answer to your prompt based on the image provided. | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Example response: | ||
|
|
||
| ```json | ||
| { | ||
| "result": "This is a golden retriever sitting in a grassy park." | ||
| } | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.