This site is not affiliated with or endorsed by Cloudflare, Inc. It showcases deployable experiments across most Cloudflare products.
AI & Machine Learning

Sentiment Analyzer

Analyze text sentiment with Workers AI at the edge - positive/negative/neutral classification API for apps and agents

Classify text sentiment as positive or negative using Workers AI and DistilBERT. Returns a label and confidence score for any short text input.

Features

  • Classify text as positive or negative with confidence scores
  • Workers AI DistilBERT model at the edge
  • Single GET endpoint with a text query parameter
  • No external ML API keys required

API Reference

GET /sentiment

Analyzes the sentiment of the provided text.

Prop

Type

Example Request

curl "https://your-worker.workers.dev/sentiment?text=This%20pizza%20is%20great!"

Success Response

text string

The original input text

label string

Sentiment label (e.g. POSITIVE or NEGATIVE)

score number

Confidence score for the predicted label (0–1)

{
  "text": "This pizza is great!",
  "label": "POSITIVE",
  "score": 0.9998
}

Error Response

{
  "error": "Missing or invalid query parameter: text",
  "code": "INVALID_TEXT"
}

Error Codes

  • 400 - Missing or invalid text (INVALID_TEXT)
  • 502 - Workers AI model run failed (AI_ERROR)

Use Cases

  • Score customer feedback and reviews at the edge
  • Filter or route messages by sentiment in real time
  • Learn Workers AI classification models in a stateless Worker
  • Build moderation or analytics pipelines without external ML APIs

Limitations

  • Workers AI is subject to usage limits by plan
  • Input text length is capped
  • Accuracy varies on very short, sarcastic, or multilingual text

Use in your project

Copy these files into an existing Worker. Prefer Deployment to try the full experiment first. Source: apps/experiments/sentiment-analyzer.

DependenciesNoneBindingsWorkers AI (AI binding)PlatformWorkers AI
import type { Env } from "../types/env";import { AI_MODEL } from "../constants/defaults";type SentimentLabel = {  label?: string;  score?: number;};export type SentimentResult = {  label: string;  score: number;};export async function analyzeSentiment(env: Env, text: string): Promise<SentimentResult> {  const out = (await env.AI.run(AI_MODEL, { text })) as SentimentLabel[];  if (!Array.isArray(out) || out.length === 0) {    throw new Error("Sentiment model returned invalid output");  }  const best = out.reduce(    (prev, curr) => ((curr.score ?? 0) > (prev.score ?? 0) ? curr : prev),    out[0]  );  if (!best.label || best.score === undefined) {    throw new Error("Sentiment model returned incomplete labels");  }  return { label: best.label, score: best.score };}

Deployment

Click the deploy button

Deploy to Cloudflare Workers

Deploy

Enable the Workers AI binding (AI) in your Worker settings. The deploy button configures this automatically via wrangler.json. Requires a Cloudflare account with Workers AI enabled.

Test your deployment

curl "https://your-worker.workers.dev/sentiment?text=This%20pizza%20is%20great!"

Local Development

cd apps/experiments/sentiment-analyzer
npm install
npm run dev

Test locally:

curl "http://localhost:8787/sentiment?text=This%20pizza%20is%20great!"

Cloudflare Features Used

  • Workers - Edge compute runtime
  • Workers AI - @cf/huggingface/distilbert-sst-2-int8 sentiment classification model

On this page