Skip to content
GitHubXDiscord

AI

The AI binding lets you run machine learning models using Cloudflare Workers AI directly from your Workers.

Create a basic AI binding for text generation:

import { Worker, Ai } from "alchemy/cloudflare";
const ai = new Ai();
await Worker("ai-worker", {
name: "ai-worker",
entrypoint: "./src/worker.ts",
bindings: {
AI: ai,
},
});

Use AI models for text generation tasks. See the text generation models documentation for available options.

import { Worker, Ai } from "alchemy/cloudflare";
const ai = new Ai();
await Worker("text-generator", {
name: "text-generator",
entrypoint: "./src/text.ts",
bindings: {
AI: ai,
},
});

Worker implementation:

src/text.ts
export default {
async fetch(request: Request, env: { AI: Ai }): Promise<Response> {
const response = await env.AI.run("@cf/meta/llama-3.1-8b-instruct", {
prompt: "What is the capital of France?",
});
return new Response(JSON.stringify(response));
},
};

Generate embeddings for semantic search and similarity. See the text embeddings models documentation for available options.

import { Worker, Ai } from "alchemy/cloudflare";
const ai = new Ai();
await Worker("embeddings-worker", {
name: "embeddings-worker",
entrypoint: "./src/embeddings.ts",
bindings: {
AI: ai,
},
});

Worker implementation:

src/embeddings.ts
export default {
async fetch(request: Request, env: { AI: Ai }): Promise<Response> {
const response = await env.AI.run("@cf/baai/bge-base-en-v1.5", {
text: ["Hello world", "Goodbye world"],
});
return new Response(JSON.stringify(response));
},
};

Classify images using computer vision models. See the image classification models documentation for available options.

import { Worker, Ai } from "alchemy/cloudflare";
const ai = new Ai();
await Worker("image-classifier", {
name: "image-classifier",
entrypoint: "./src/image.ts",
bindings: {
AI: ai,
},
});

Worker implementation:

src/image.ts
export default {
async fetch(request: Request, env: { AI: Ai }): Promise<Response> {
const imageArrayBuffer = await request.arrayBuffer();
const response = await env.AI.run("@cf/microsoft/resnet-50", {
image: imageArrayBuffer,
});
return new Response(JSON.stringify(response));
},
};

Combine AI binding with AI Gateway for observability and control. Learn more about AI Gateway integration.

import { Worker, Ai, AiGateway } from "alchemy/cloudflare";
const aiGateway = await AiGateway("my-gateway", {
name: "my-ai-gateway",
});
const ai = new Ai();
await Worker("gateway-ai-worker", {
name: "gateway-ai-worker",
entrypoint: "./src/gateway.ts",
bindings: {
AI: ai,
GATEWAY: aiGateway,
},
});

Worker implementation with gateway:

src/gateway.ts
export default {
async fetch(request: Request, env: { AI: Ai }): Promise<Response> {
const response = await env.AI.run(
"@cf/meta/llama-3.1-8b-instruct",
{
prompt: "Explain quantum computing",
},
{
gateway: {
id: "my-ai-gateway",
cacheKey: "quantum-explanation",
cacheTtl: 3600,
},
}
);
return new Response(JSON.stringify(response));
},
};

Workers AI supports 50+ open-source models across different categories including text generation, embeddings, image classification, and more.

The AI binding provides several methods in your Worker runtime. See the Workers AI API reference for complete details.

Run inference on a specific model:

const response = await env.AI.run(
"@cf/meta/llama-3.1-8b-instruct",
{
prompt: "Your prompt here",
},
{
gateway: { id: "my-gateway" },
}
);

List available models:

const models = await env.AI.models();

Get AI Gateway instance:

const gateway = env.AI.gateway("my-gateway-id");

For full type safety, you can specify model types:

import { Worker, Ai, type } from "alchemy/cloudflare";
interface MyAiModels {
"@cf/meta/llama-3.1-8b-instruct": {
inputs: { prompt: string };
postProcessedOutputs: { response: string };
};
}
const ai = new Ai<MyAiModels>();
import { Worker, Ai } from "alchemy/cloudflare";
const ai = new Ai();
await Worker("my-worker", {
name: "my-worker",
entrypoint: "./src/worker.ts",
bindings: {
AI: ai,
},
});