Andi Ashari

Tech Voyager & Digital Visionary

Mastering CloudFlare Worker AI: Your Ultimate Guide to Free, Open-Source AI Models

Mastering CloudFlare Worker AI: Your Ultimate Guide to Free, Open-Source AI Models

Welcome to the ultimate guide designed to help developers of all skill levels leverage the power of CloudFlare’s Worker AI. This guide is your comprehensive resource for utilizing CloudFlare’s innovative AI capabilities, whether you’re a seasoned pro or just starting out with machine learning.

Introduction to CloudFlare Worker AI

CloudFlare Worker AI is a groundbreaking platform enabling developers to run machine learning models on Cloudflare’s global network, supported by serverless GPUs. It’s crafted to be accessible, with minimal machine learning knowledge required to get started.

Prerequisites

  • A Cloudflare account
  • Basic understanding of REST APIs
  • Familiarity with programming languages like JavaScript (Node.js) or Python

Step-by-Step Guide to Using the API

Generating an Access Token

To interact with Cloudflare’s AI models, start by creating an API token through these steps:

  1. Log into your Cloudflare dashboard.
  2. Navigate to My Profile > API Tokens.
  3. Use the direct shortcut for quicker access to API Tokens.
  4. Click Create Token.
  5. Select Workers AI (Beta) for AI applications.
  6. Proceed to Continue to summary and confirm with Create token.

Important: Your token will be shown only once. Make sure to copy and store it securely.

Using the API

Engage with Cloudflare’s diverse machine learning models through the API:

  • Locate your account ID on the Cloudflare dashboard URL: https://dash.cloudflare.com/{ACCOUNTID}.
  • Explore Cloudflare’s model range.
  • Example: Using the text generation model @cf/meta/llama-2-7b-chat-fp16.

CURL Example

curl https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/llama-2-7b-chat-int8 \
-X POST \
-H "Authorization: Bearer {APITOKEN}" \
-d '{ "messages": [{ "role": "system", "content": "Your name is Brainy Buddy, a friendly assistant" }, { "role": "user", "content": "Hai! how are you?" }]}'

Replace {ACCOUNT_ID} and {APITOKEN} with your details to get a response.

Python Example

For Python users, interact with the API like so:

import requests

url = 'https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/llama-2-7b-chat-int8'
headers = {
    'Authorization': 'Bearer {APITOKEN}',
    'Content-Type': 'application/json'
}
data = {
    "messages": [
        {"role": "system", "content": "Your name is Brainy Buddy, a friendly assistant"},
        {"role": "user", "content": "Hai! how are you?"}
    ]
}

response = requests.post(url, headers=headers, json=data)
print(response.json())

Understanding ‘Role’ and ‘Content’

  • System Role: Sets the AI’s context.
  • User Role: Represents the human interacting with the AI.
  • Content: The actual message text.

These elements enable coherent interactions with the AI model.

Use Case: Text Categorization

Text categorization with Cloudflare Worker AI classifies texts into predefined categories, useful for organizing content like news articles.

Setting Up the Categorization Model

Define the AI’s role for categorizing articles into categories such as politics, economy, or health.

Example Payload

{
  "messages": [
    {
      "role": "system",
      "content": "You are skilled at categorizing articles. When a user provides an article, categorize it into one of the following categories: politics, economy, or health."
    },
    {
      "role": "user",
      "content": "[Insert Article Here]\ncategory:"
    }
  ]
}

Example Use Case Scenario

  1. System Role: AI is set to categorize articles.
  2. User Role: Inputs an article about a political development.
  3. AI Response: Categorizes the article as ‘politics’.

Python Code for Article Categorization

import requests

url = 'https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/llama-2-7b-chat-int8'
headers = {
    'Authorization': f'Bearer {APITOKEN}',
    'Content-Type': 'application/json'
}
article_text = "Your article text goes here."
data = {
    "messages": [
        { "role": "system", "content": "Categorize this article into one of the following categories: politics, economy, or health." },
        { "role": "user", "content":

 article_text + "\n\nCategory: " }
    ]
}

response = requests.post(url, headers=headers, json=data)
print(response.json())

Expanding Your Horizons with Cloudflare Worker AI

Cloudflare Worker AI offers a suite of AI models for various applications, from text generation to image classification. Explore these models to integrate AI into your projects seamlessly.

Accessing Detailed Documentation

For comprehensive details, including functionalities and implementation, visit the Cloudflare Worker AI documentation.