### Example D1 Read Replication Setup Source: https://developers.cloudflare.com/d1/best-practices/read-replication This example demonstrates how to configure a D1 database for read replication. It involves creating a read-only replica of the primary database. ```bash wrangler d1 replicate ``` -------------------------------- ### Agent Initialization with onStart Source: https://developers.cloudflare.com/agents/runtime/lifecycle/agent-class Implement `onStart` to run initialization logic every time a Durable Object starts up, before any fetch or RPC calls. This example shows initializing SQL access. ```typescript class MyServer extends Server { onStart() { // Some initialization logic that you wish // to run every time the DO is started up. const sql = this.ctx.storage.sql; sql.exec(`...`); } } ``` -------------------------------- ### Create a Target and Get Live View URL Source: https://developers.cloudflare.com/agents/tools/browser Example of creating a new browser target and obtaining a Live View URL for it. ```APIDOC ## Create Target and Get Live View URL ```typescript async () => { const { targetId } = await cdp.send({ method: "Target.createTarget", params: { url: "https://example.com/login" }, }); const { url } = await cdp.getLiveViewUrl({ targetId, mode: "tab" }); return { needsHumanLogin: url }; }; ``` ``` -------------------------------- ### Kimi-2.7B Code Generation with System Prompt Source: https://developers.cloudflare.com/workers-ai/models/kimi-k2.7-code This example demonstrates using a system prompt with the Kimi-2.7B model to guide code generation. This can help in setting the context or desired output format. ```javascript import { Ai } from "@cloudflare/ai"; const ai = new Ai(env.AI); const response = await ai.run( "@cf/meta/llama-2-7b-chat-fp16", { system: "You are a helpful assistant that generates code.", prompt: "Write a JavaScript function to reverse a string.", } ); console.log(response.response); ``` -------------------------------- ### Start a D1 session from previous context (bookmark) Source: https://developers.cloudflare.com/d1/best-practices/read-replication Create a new session that starts with a database version at least as up-to-date as a provided bookmark by passing the `bookmark` parameter to `withSession()`. This ensures the new session adheres to the context of a previous session. The bookmark can be retrieved from a previous session and stored for future use, for example, in an HTTP header. ```typescript // retrieve bookmark from previous session stored in HTTP headerconst bookmark = request.headers.get('x-d1-bookmark') ?? 'first-unconstrained'; const session = env.DB.withSession(bookmark) const result = await session .prepare(`SELECT * FROM Customers WHERE CompanyName = 'Bs Beverages'`) .run() // store bookmark for a future sessionresponse.headers.set('x-d1-bookmark', session.getBookmark() ?? "") ``` -------------------------------- ### Agent with Default State and State Change Reaction Source: https://developers.cloudflare.com/agents/runtime/lifecycle/state This example demonstrates defining default state and reacting to state changes in an agent. It includes logic to start a game when enough players join. ```typescript import { Agent } from "agents"; type GameState = { players: string[]; score: number; status: "waiting" | "playing" | "finished"; }; export class GameAgent extends Agent { // Default state for new agents initialState: GameState = { players: [], score: 0, status: "waiting", }; // React to state changes onStateChanged(state: GameState, source: Connection | "server") { if (source !== "server" && state.players.length >= 2) { // Client added a player, start the game this.setState({ ...state, status: "playing" }); } } addPlayer(name: string) { this.setState({ ...this.state, players: [...this.state.players, name], }); } } ``` -------------------------------- ### Create API Token (Example) Source: https://developers.cloudflare.com/fundamentals/api/get-started/create-token This example demonstrates how to create an API token. Ensure you have the necessary permissions and replace placeholders with your actual values. ```bash curl "https://api.cloudflare.com/client/v4/user/tokens/create" \ --header "Content-Type: application/json" \ --data '{ "name": "My token name", "policies": [ { "permission": { "zone": { "id": "YOUR_ZONE_ID", "name": "*" }, "scope": null, "effect": "allow" }, "resources": { " zone": { "id": "YOUR_ZONE_ID", "name": "*" }, " account": { "id": "YOUR_ACCOUNT_ID", "name": "*" } } } } ], "lease_time_in_minutes": 1440 }' ``` -------------------------------- ### Create a new Worker project with pnpm Source: https://developers.cloudflare.com/workers-ai/get-started/workers-wrangler Use pnpm to create a new Worker project named 'hello-ai'. This command installs the `create-cloudflare` package and guides you through project setup. ```bash pnpm create cloudflare@latest hello-ai ``` -------------------------------- ### Agent onStart Lifecycle Method with Props Source: https://developers.cloudflare.com/agents/runtime/communication/routing Demonstrates how to receive and use props within an agent's `onStart` method for initialization. This example shows how to access `userId` and `config` passed during agent instantiation. ```javascript class MyAgent extends Agent { userId; config; async onStart(props) { this.userId = props?.userId; this.config = props?.config; }} ``` ```typescript class MyAgent extends Agent { private userId?: string; private config?: { maxRetries: number }; async onStart(props?: { userId: string; config: { maxRetries: number } }) { this.userId = props?.userId; this.config = props?.config; }} ``` -------------------------------- ### Create a new Worker project with yarn Source: https://developers.cloudflare.com/workers-ai/get-started/workers-wrangler Use yarn to create a new Worker project named 'hello-ai'. This command installs the `create-cloudflare` package and guides you through project setup. ```bash yarn create cloudflare hello-ai ``` -------------------------------- ### Create a new Worker project with npm Source: https://developers.cloudflare.com/workers-ai/get-started/workers-wrangler Use npm to create a new Worker project named 'hello-ai'. This command installs the `create-cloudflare` package and guides you through project setup. ```bash npm create cloudflare@latest -- hello-ai ``` -------------------------------- ### Navigate to Project Directory Source: https://developers.cloudflare.com/agents/examples/slack-agent Change into the newly created project directory. ```bash cd my-slack-agent ``` -------------------------------- ### Create and Run Cloudflare Agents Starter Source: https://developers.cloudflare.com/workers-ai/llms-full.txt Use this command to set up a starter project for Cloudflare Agents. It includes streaming AI chat, server-side and client-side tools, and task scheduling. No API keys are required as it defaults to Workers AI. ```bash npx create-cloudflare@latest --template cloudflare/agents-startercd agents-starter && npm installnpm run dev ``` -------------------------------- ### KV Get Operation (TypeScript SDK) Source: https://developers.cloudflare.com/kv Example of how to get a value for a given key from a KV namespace using the Cloudflare TypeScript SDK. ```typescript const value = await client.kv.namespaces.values.get('', 'KEY', { account_id: '', }); ``` -------------------------------- ### Initialize Cloudflare Agents Starter Project Source: https://developers.cloudflare.com/workers-ai/llms-full.txt Use this command to quickly set up a new Cloudflare Agents project using the provided starter template. It includes streaming AI chat, server-side and client-side tools, and more. ```bash npx create-cloudflare@latest --template cloudflare/agents-starter cd agents-starter npm install npm run dev ``` -------------------------------- ### Get Value from KV Namespace (TypeScript) Source: https://developers.cloudflare.com/kv/get-started Use the get() method to retrieve the value associated with a specific key from your KV namespace. This example fetches the value for 'user_2'. ```typescript let value = await env.USERS_NOTIFICATION_CONFIG.get("user_2"); ``` -------------------------------- ### List Keys with Prefix Source: https://developers.cloudflare.com/kv/concepts/how-kv-works Example of listing keys in a KV namespace that start with a specific prefix. ```javascript const list = await env.MY_KV_NAMESPACE.list({ "prefix": "user_" }); ``` -------------------------------- ### Initialize a new project non-interactively Source: https://developers.cloudflare.com/kv/get-started Create a basic 'Hello World' project non-interactively using CI=true environment variable. ```bash CI=true npm create cloudflare@latest kv-tutorial --type=simple --git --ts --deploy=false ``` -------------------------------- ### Basic Wrangler Configuration Source: https://developers.cloudflare.com/workers/wrangler A minimal `wrangler.toml` file to get started with a Cloudflare Worker project. ```toml [env.production] name = "my-worker-production" route = "https://my-worker.example.com/*" kv_namespaces = ["MY_KV_NAMESPACE"] [env.staging] name = "my-worker-staging" route = "https://staging.my-worker.example.com/*" kv_namespaces = ["STAGING_KV_NAMESPACE"] ``` -------------------------------- ### Create an API Token using Node.js Source: https://developers.cloudflare.com/fundamentals/api/get-started/create-token This example demonstrates how to create an API token using the Cloudflare Node.js SDK. Make sure to install the SDK first (`npm install @cloudflare/api-connect`). ```javascript import { createApiToken } from "@cloudflare/api-connect"; async function main() { const token = await createApiToken({ api_key: "YOUR_API_KEY", email: "YOUR_EMAIL", policy: { வதால்: [ { id: "zone", value: "*", edit: true } ], permissions: { zone: { read: true, edit: true } } } }); console.log(token); } main(); ``` -------------------------------- ### Navigate to the project directory Source: https://developers.cloudflare.com/workers-ai/get-started/workers-wrangler Change your current directory to the newly created 'hello-ai' project folder. ```bash cd hello-ai ``` -------------------------------- ### Create a new Worker project with C3 Source: https://developers.cloudflare.com/workers/get-started/guide Use C3 to scaffold a new Worker project. Select 'Hello World example', 'Worker only', 'JavaScript', and 'Yes' for Git, then 'No' for initial deployment. ```bash npm create cloudflare@latest -- my-first-worker ``` ```bash yarn create cloudflare my-first-worker ``` ```bash pnpm create cloudflare@latest my-first-worker ``` -------------------------------- ### KV Namespace Example: Storing User Preferences Source: https://developers.cloudflare.com/kv/concepts/kv-namespaces This example demonstrates how to store and retrieve user preferences using a KV namespace. It includes putting and getting data, and handling potential errors. ```javascript export default { async fetch(request, env) { const url = new URL(request.url); const userId = url.searchParams.get("userId"); const preference = url.searchParams.get("preference"); if (!userId) { return new Response("userId is required", { status: 400 }); } const userKey = `user:${userId}`; if (request.method === "POST") { if (!preference) { return new Response("preference is required", { status: 400 }); } await env.USER_PREFERENCES.put(userKey, preference); return new Response("Preference saved"); } else if (request.method === "GET") { const savedPreference = await env.USER_PREFERENCES.get(userKey); if (savedPreference === null) { return new Response("Preference not found", { status: 404 }); } return new Response(savedPreference); } return new Response("Method not allowed", { status: 405 }); }, }; ``` -------------------------------- ### SQL API Example: Get All Data Source: https://developers.cloudflare.com/durable-objects/best-practices/access-durable-objects-storage This example demonstrates how to retrieve all data from a table using the SQL API within a Durable Object. It uses `state.storage.get()` to fetch data and `JSON.parse()` to deserialize it. ```typescript async fetch(request: Request): Promise { const url = new URL(request.url); if (url.pathname === "/items") { const items = await this.storage.get>("items"); return Response.json(items || []); } // ... rest of the fetch logic } ``` -------------------------------- ### Set up .env file for API Credentials Source: https://developers.cloudflare.com/workers-ai/llms-full.txt Example of how to configure your Cloudflare API token and Account ID in a .env file for authentication. ```bash CLOUDFLARE_API_TOKEN="YOUR-TOKEN"CLOUDFLARE_ACCOUNT_ID="YOUR-ACCOUNT-ID" ``` -------------------------------- ### Few-Shot Prompting with Workers AI Source: https://developers.cloudflare.com/workers-ai/guides/prompting This example shows how to use few-shot prompting to guide the AI model. By providing examples of desired input/output pairs, you can improve the quality and relevance of the generated text. ```javascript import { Ai } from "@cloudflare/ai"; export default { async fetch(request, env) { const ai = new Ai(env.AI); const prompt = ` Translate the following English text to French: English: Hello world French: Bonjour le monde English: How are you? French: Comment allez-vous? English: What is your name? French:`; const response = await ai.run( "@cf/meta/llama-2-7b-chat-int8", { prompt: prompt, } ); return new Response(JSON.stringify(response)); }, }; ``` -------------------------------- ### Initialize Quick Action Tools Source: https://developers.cloudflare.com/agents/tools/browser Create Quick Action tools for interactive, multi-step automation. These tools require only the 'browser' binding and do not need a Worker Loader or sandbox. ```javascript import { createQuickActionTools } from "agents/browser/ai"; const tools = createQuickActionTools({ browser: this.env.BROWSER });// browser_markdown, browser_extract, browser_links, browser_scrape ``` ```typescript import { createQuickActionTools } from "agents/browser/ai"; const tools = createQuickActionTools({ browser: this.env.BROWSER });// browser_markdown, browser_extract, browser_links, browser_scrape ``` -------------------------------- ### Basic Fetch Request Source: https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch A simple example of making a GET request to a URL using the fetch API. ```javascript const response = await fetch('https://example.com'); const data = await response.json(); console.log(data); ``` -------------------------------- ### Agent Initialization with onStart Source: https://developers.cloudflare.com/agents/runtime/communication/websockets The onStart method is called once when the agent begins, before any connections are made. Use it to initialize resources and load persistent data. ```javascript export class MyAgent extends Agent { async onStart() { // Initialize resources console.log(`Agent ${this.name} starting...`); // Load data from storage const savedData = this.sql`SELECT * FROM cache`; for (const row of savedData) { // Rebuild in-memory state from persistent storage } } onConnect(connection) { // By the time connections arrive, onStart has completed } } ``` -------------------------------- ### Start Development Server Source: https://developers.cloudflare.com/agents/examples/slack-agent Command to start the local development server for the Slack agent. ```bash npm run dev ``` -------------------------------- ### Durable Object: Get Unique Number Example Source: https://blog.cloudflare.com/durable-objects-easy-fast-correct-choose-three This example demonstrates a common pattern for incrementing a counter within a Durable Object. Previously, this code was prone to race conditions and slow performance, but has been corrected by runtime changes. ```javascript async function getUniqueNumber() { let val = await this.storage.get("counter"); await this.storage.put("counter", val + 1); return val; } ``` -------------------------------- ### Get the current status of a given Access policy test Source: https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/query Retrieves the status of a previously started Access policy test. ```APIDOC ## GET /accounts/{account_identifier}/access/apps/{app_id}/policy_tests/{test_id} ### Description Retrieves the status of a previously started Access policy test. ### Method GET ### Endpoint /accounts/{account_identifier}/access/apps/{app_id}/policy_tests/{test_id} ### Parameters #### Path Parameters - **account_identifier** (string) - Required - Your Cloudflare account ID - **app_id** (string) - Required - The ID of the Access application - **test_id** (string) - Required - The ID of the policy test ``` -------------------------------- ### Setup Cloudflare Pipelines Source: https://developers.cloudflare.com/pipelines Run this Wrangler command to set up your first Cloudflare Pipeline. ```bash npx wrangler pipelines setup ``` -------------------------------- ### Text Generation with Llama-2-7b-chat-fp16 (Custom Prompt) Source: https://developers.cloudflare.com/workers-ai-notebooks/cloudflare-workers-ai.ipynb Custom prompt example for Llama-2-7b-chat-fp16, demonstrating how to guide the model's output. ```javascript const prompt = { "prompt": "Write a haiku about a rainy day.", }; const resp = await ai.run("@cf/meta/llama-2-7b-chat-fp16", prompt); ``` -------------------------------- ### Install Browser Tools Source: https://developers.cloudflare.com/agents/examples/browser-agent Install the browser tools package using npm or yarn. This is the first step to using the browser agent. ```bash npm install @cloudflare/browsertools yarn add @cloudflare/browsertools ``` -------------------------------- ### Custom Build Tool Command Source: https://developers.cloudflare.com/workers/wrangler/configuration Example command to run a custom build tool for a specific environment, demonstrating how a framework might initiate a build process. ```bash > my-tool build --env=staging ``` -------------------------------- ### Uploading an Image for AI Processing Source: https://developers.cloudflare.com/workers-ai-notebooks/cloudflare-workers-ai.ipynb Example of how to get an image file from a binding (e.g., R2) for use with AI models like CLIP. ```javascript const image = await DEMO_PAGES_BINDING.get("image.jpg"); const imageBuffer = await image.arrayBuffer(); ``` -------------------------------- ### Navigate to Project Directory Source: https://developers.cloudflare.com/workers-ai/llms-full.txt Change the current directory to the newly created 'rag-ai-tutorial' project folder. This is necessary to run subsequent commands within the project context. ```bash cd rag-ai-tutorial ``` -------------------------------- ### Embedding Generation with OpenAI Ada Source: https://developers.cloudflare.com/workers-ai-notebooks/cloudflare-workers-ai.ipynb Generate embeddings for text using the OpenAI Ada model. This example shows how to get text embeddings. ```javascript import { Ai } from "@cloudflare/ai"; export default { async fetch(request, env, ctx) { const ai = new Ai(env.AI); const prompt = { "text": "This is a test sentence.", }; const resp = await ai.run("@cf/openai/text-embedding-ada-002", prompt); return new Response(JSON.stringify(resp)); }, }; ``` -------------------------------- ### Setup Session with Compaction - JavaScript Source: https://developers.cloudflare.com/agents/runtime/lifecycle/sessions Configure a session with memory context and define a custom compaction function. The session will automatically compact after a specified number of tokens. ```javascript import { createCompactFunction } from "agents/experimental/memory/utils/compaction-helpers"; const session = Session.create(this) .withContext("memory", { maxTokens: 1100 }) .onCompaction( createCompactFunction({ summarize: (prompt) => generateText({ model: myModel, prompt }).then((r) => r.text), protectHead: 3, tailTokenBudget: 20000, minTailMessages: 2, tokenCounter: async (messages) => estimateWithYourTokenizer({ messages }), }), ) .compactAfter(100_000); ``` -------------------------------- ### Create a new Cloudflare Worker project (npm) Source: https://developers.cloudflare.com/d1/get-started Use this command to create a new D1 tutorial project with npm. Select 'Hello World example', 'Worker only', 'TypeScript', 'Yes' for git, and 'No' for deployment. ```bash npm create cloudflare@latest -- d1-tutorial ``` -------------------------------- ### Embedding Generation with Sentence Transformer Source: https://developers.cloudflare.com/workers-ai-notebooks/cloudflare-workers-ai.ipynb Generate embeddings for text using the sentence transformer model. This example shows how to get text embeddings. ```javascript import { Ai } from "@cloudflare/ai"; export default { async fetch(request, env, ctx) { const ai = new Ai(env.AI); const prompt = { "text": "This is a test sentence.", }; const resp = await ai.run("@cf/sentence-transformers/all-MiniLM-L6-v2", prompt); return new Response(JSON.stringify(resp)); }, }; ``` -------------------------------- ### Model Parameters and Options Source: https://developers.cloudflare.com/workers-ai/models/qwen3-30b-a3b-fp8 Illustrates how to configure model parameters such as temperature, top_p, and max_tokens for the Qwen3-30B-A3B-FP8 model. ```javascript import { Ai } from "@cloudflare/ai"; export default { async fetch(request, env) { const ai = new Ai(env.AI); const prompt = "Generate a list of creative blog post ideas about AI."; const response = await ai.run( "@cf/qwen/qwen3-30b-a3b-fp8", { prompt: prompt, temperature: 0.7, top_p: 0.9, max_tokens: 150, } ); return new Response(JSON.stringify(response), { headers: { "content-type": "application/json;charset=UTF-8", }, }); }, }; ``` -------------------------------- ### Embedding Generation with Text Embedding Source: https://developers.cloudflare.com/workers-ai-notebooks/cloudflare-workers-ai.ipynb Generate embeddings for text using the text embedding model. This example shows how to get text embeddings. ```javascript import { Ai } from "@cloudflare/ai"; export default { async fetch(request, env, ctx) { const ai = new Ai(env.AI); const prompt = { "text": "This is a test sentence.", }; const resp = await ai.run("@cf/baai/bge-small-en-v1.5", prompt); return new Response(JSON.stringify(resp)); }, }; ``` -------------------------------- ### Voice Agent Options Example Source: https://developers.cloudflare.com/agents/examples/voice-agent Example of options that can be passed during Voice Agent initialization, such as model and voice. ```javascript const options = { model: 'warp-synthesizer-v1', voice: 'alloy', }; const agent = new VoiceAgent(options); ``` -------------------------------- ### Basic Scheduled Handler Source: https://developers.cloudflare.com/workers/runtime-apis/handlers/scheduled A minimal example of a Scheduled Handler that logs a message when a cron trigger fires. This is the most basic setup for a scheduled task. ```typescript export default { async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext) { console.log('Cron pattern matched. Event triggered.'); } }; ``` -------------------------------- ### Using Public LoRAs with Workers AI Source: https://developers.cloudflare.com/workers-ai/fine-tunes/public-loras This example demonstrates how to use a public LoRA with the Workers AI SDK. Ensure you have the SDK installed and configured. ```javascript import { Ai } from "@cloudflare/ai"; export default { async fetch(request, env) { const ai = new Ai(env.AI); // Example: Using a public LoRA for image generation const response = await ai.generateImage( "stable-diffusion-xl-base-1.0", "A majestic cat wearing a crown, digital art", { // Specify the LoRA to use lora: "/mnt/loras/my-lora-model", // Other generation options can be passed here width: 1024, height: 1024, } ); return new Response(response.data[0].url, { headers: { "content-type": "image/png", }, }); }, }; ``` -------------------------------- ### Initialize and use GPT-OSS-20B Source: https://developers.cloudflare.com/workers-ai/models/gpt-oss-20b This snippet shows how to initialize the GPT-OSS-20B model and use it to generate text based on a prompt. It demonstrates a basic text generation workflow. ```javascript import { Ai } from "@cloudflare/ai"; export default { async fetch(request, env, ctx) { const ai = new Ai(env.AI); const prompt = "The future of AI is"; const response = await ai.run( "@cf/openai/gpt-oss-20b", { prompt: prompt, } ); return new Response(JSON.stringify(response)); }, }; ``` -------------------------------- ### Custom Cache Key Example Source: https://developers.cloudflare.com/ai-gateway/features/caching Demonstrates how to use a custom cache key based on the prompt and model. This allows for more granular control over caching. ```javascript import { AiGateway } from "@cloudflare/ai-gateway"; const gateway = new AiGateway({ apiToken: "YOUR_API_TOKEN", cache: { ttl: 3600, key: (prompt, model) => `${model}-${prompt.substring(0, 50)}`, // Custom cache key }, }); async function getResponse() { const response = await gateway.complete({ model: "@cf/meta/llama-2-7b-chat-int8", prompt: "What is the capital of France?", }); return response; } ``` -------------------------------- ### Direct API Call for Text Embeddings Source: https://developers.cloudflare.com/workers-ai-notebooks/cloudflare-workers-ai.ipynb This example demonstrates a direct API call to get text embeddings. The output is a vector representation of the input text. ```bash curl -X POST "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/ai/run/@cf/baai/bge-small-en-v1.5" \ -H "Authorization: Bearer $AI_TOKEN" \ -H "Content-Type: application/json" \ --data '{"text": "What is the weather like today?"}' ``` -------------------------------- ### Running Phi-2 with specific parameters Source: https://developers.cloudflare.com/workers-ai/models/phi-2 This example shows how to run the Phi-2 model with specific parameters to control the generation process, such as `max_tokens` and `temperature`. ```javascript import { Ai } from "@cloudflare/ai"; export default { async fetch(request, env, ctx) { const ai = new Ai(env.AI); const prompt = "Write a short story about a robot learning to paint."; const response = await ai.run( "@cf/microsoft/phi-2", { prompt: prompt, max_tokens: 500, temperature: 0.7, } ); return new Response(JSON.stringify(response)); }, }; ``` -------------------------------- ### Image Classification with CLIP Source: https://developers.cloudflare.com/workers-ai-notebooks/cloudflare-workers-ai.ipynb Classify an image using the CLIP model. This example demonstrates sending an image and a list of class names to get classification results. ```javascript addEventListener("fetch", (event) => { event.respondWith(handleRequest(event.request)); }); async function handleRequest(request) { const url = new URL(request.url); const imageUrl = url.searchParams.get("imageUrl"); const classNames = url.searchParams.get("classNames"); if (!imageUrl || !classNames) { return new Response("Please provide imageUrl and classNames parameters.", { status: 400, }); } const response = await fetch( "https://api.cloudflare.com/client/v4/accounts/" + ACCOUNT_ID + "/ai/run/@cf/openai/clip-vit-base-patch32", { method: "POST", headers: { "Authorization": `Bearer ${AI_TOKEN}`, "Content-Type": "application/json", }, body: JSON.stringify({ image_url: imageUrl, class_prompt: classNames, }), } ); const data = await response.json(); return new Response(JSON.stringify(data), { headers: { "content-type": "application/json", }, }); } ``` -------------------------------- ### Run GLM-4.7-Flash without Streaming in TypeScript Source: https://developers.cloudflare.com/workers-ai/models/glm-4.7-flash This example shows how to get a non-streaming response from the GLM-4.7-Flash model within a Worker. It requires the `Ai` binding to be configured. ```typescript export interface Env { AI: Ai;} export default { async fetch(request, env): Promise { const messages = [ { role: "system", content: "You are a friendly assistant" }, { role: "user", content: "What is the origin of the phrase Hello, World", }, ]; const response = await env.AI.run("@cf/zai-org/glm-4.7-flash", { messages }); return Response.json(response); } } satisfies ExportedHandler; ``` -------------------------------- ### Basic Think Agent Server (JavaScript) Source: https://developers.cloudflare.com/agents/harnesses/think A minimal JavaScript example demonstrating how to set up a Think agent. It defines a custom agent class extending Think and configures the Workers AI model. ```javascript import { Think } from "@cloudflare/think"; import { createWorkersAI } from "workers-ai-provider"; import { routeAgentRequest } from "agents"; export class MyAgent extends Think { getModel() { return createWorkersAI({ binding: this.env.AI })( "@cf/moonshotai/kimi-k2.6", ); } } export default { async fetch(request, env) { return ( (await routeAgentRequest(request, env)) || new Response("Not found", { status: 404 }) ); }, }; ``` -------------------------------- ### Write and Read KV Data (TypeScript) Source: https://developers.cloudflare.com/kv/get-started This TypeScript code performs the same KV put() and get() operations as the JavaScript example, with explicit type definitions for the environment. ```typescript export interface Env { USERS_NOTIFICATION_CONFIG: KVNamespace; } export default { async fetch(request, env, ctx): Promise { try { await env.USERS_NOTIFICATION_CONFIG.put("user_2", "disabled"); const value = await env.USERS_NOTIFICATION_CONFIG.get("user_2"); if (value === null) { return new Response("Value not found", { status: 404 }); } return new Response(value); } catch (err) { console.error(`KV returned error:`, err); const errorMessage = err instanceof Error ? err.message : "An unknown error occurred when accessing KV storage"; return new Response(errorMessage, { status: 500, headers: { "Content-Type": "text/plain" }, }); } }, } satisfies ExportedHandler; ``` -------------------------------- ### Basic Scheduled Event Handler Source: https://developers.cloudflare.com/workers/runtime-apis/handlers/scheduled A simple example of a scheduled event handler that logs a message when triggered. This is the most basic setup for a cron job in Cloudflare Workers. ```javascript export default { async scheduled (event, env, ctx) { console.log("The scheduled event has run!"); } }; ``` -------------------------------- ### Llama 3.2 1B Instruct with generation parameters Source: https://developers.cloudflare.com/workers-ai/models/llama-3.2-1b-instruct This example demonstrates how to use the Llama 3.2 1B Instruct model with advanced generation parameters such as temperature, top_p, and max_tokens. ```javascript const response = await ai.run(model, { prompt: "Write a short story about a robot learning to paint.", temperature: 0.7, top_p: 0.9, max_tokens: 500, }); console.log(response.response); ``` -------------------------------- ### Call a Model with System Prompt Source: https://developers.cloudflare.com/ai-gateway/get-started Enhance model responses by providing a system prompt that guides the AI's behavior. This example sets a persona for the AI. ```javascript async function callModelWithSystemPrompt() { const response = await ai.run( "@cf/meta/llama-2-13b-chat-fp16", { system: "You are a helpful assistant.", prompt: "What is the weather like today?", } ); console.log(response); } ``` -------------------------------- ### Creating a Session using Direct Constructor Source: https://developers.cloudflare.com/agents/runtime/lifecycle/sessions Illustrates creating a session using the direct constructor for full control over providers, including custom context configurations. ```APIDOC ## Creating a Session using Direct Constructor For full control over providers: ### JavaScript Example ```javascript import { Session, AgentSessionProvider, AgentContextProvider, } from "agents/experimental/memory/session"; const session = new Session(new AgentSessionProvider(this), { context: [ { label: "memory", description: "Notes", maxTokens: 500, provider: new AgentContextProvider(this, "memory"), }, { label: "soul", provider: { get: async () => "You are helpful." } }, ], }); ``` ### TypeScript Example ```typescript import { Session, AgentSessionProvider, AgentContextProvider, } from "agents/experimental/memory/session"; const session = new Session(new AgentSessionProvider(this), { context: [ { label: "memory", description: "Notes", maxTokens: 500, provider: new AgentContextProvider(this, "memory"), }, { label: "soul", provider: { get: async () => "You are helpful." } }, ], }); ``` ``` -------------------------------- ### Basic Function Calling Example Source: https://developers.cloudflare.com/workers-ai/features/function-calling/embedded This snippet demonstrates a basic implementation of function calling. It defines a tool schema and then uses it in a prompt to get a function call from the model. ```typescript import { Ai } from "@cloudflare/ai"; export default { async fetch(request: Request, env: Env): Promise { const ai = new Ai(env.AI); const toolSchema = { "name": "get_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "The temperature unit to use. Infer this from the users location." } }, "required": ["location"] } }; const messages = [ { role: "user", content: "What's the weather in Boston?" } ]; const response = await ai.chat.completions.create({ model: "@cf/meta/llama-2-7b-chat-fp16", messages: messages, tools: [toolSchema], tool_choice: "auto", }); const responseMessage = response.choices[0].message; if (responseMessage.tool_calls) { // The model wants to call a tool const toolCall = responseMessage.tool_calls[0].function; console.log("Tool Call:", toolCall); // You would then call the tool and send the result back to the model // For example: // const toolArgs = JSON.parse(toolCall.arguments); // const toolResult = await get_weather(toolArgs.location, toolArgs.unit); // messages.push(responseMessage); // messages.push({ // tool_call_id: response.choices[0].message.tool_calls[0].id, // role: "tool", // content: toolResult, // }); } return new Response(JSON.stringify(response.choices[0].message.content)); } } ``` -------------------------------- ### Initialize Hono and Basic GET Route Source: https://developers.cloudflare.com/workers-ai/llms-full.txt Import and initialize Hono, then set up a basic GET route at the root path that uses the AI binding to run a model. ```javascript import { Hono } from "hono"; const app = new Hono(); app.get("/", async (c) => { const answer = await c.env.AI.run("@cf/meta/llama-3-8b-instruct", { messages: [{ role: "user", content: `What is the square root of 9?` }], }); return c.json(answer); }); export default app; ```