/ Docs

Cloudflare Workers

1. Go to cloudflare.com and log in.

2. In your dashboard go to Workers & Pages and then Workers & Pages.

3. Click Create button, find Start from a template and pick LLM App.

4. Name your worker however you want and click Deploy.

5. Continue to the project.

6. In Deployments tab find the edit code button, should be top right.

7. Paste the code below in the editor.

8. Click Deploy go back to the dashboard.

9. In the Settings tab under Domains & Routes copy the active worker domain ( should be smth like name.email.workers.dev ) and paste it under /settings into AI Worker.

10. That's it, you can now play around with different models and redeploy any change to the worker that you want.

The model can be selected in /settings , speed and results will depend on the size of the model, you can find the full list of models here.

AI Worker Code

const corsHeaders = {
  'Access-Control-Allow-Headers': '*',
  'Access-Control-Allow-Methods': 'GET, OPTIONS', 
  'Access-Control-Allow-Origin': 'https://khofly.com', // Replace if you need with appropriate domain 
};

export default {
  async fetch(request, env) {
    const { method } = request;

    const url = new URL(request.url); // Get the URL of the request
    const prompt = url.searchParams.get('prompt'); // Retrieve the 'prompt' query parameter
    const model = url.searchParams.get('model'); // Retrieve the 'model' query parameter

    const source_lang = url.searchParams.get('source_lang'); // For translate
    const target_lang = url.searchParams.get('target_lang'); // For translate

    if (request.method === "OPTIONS") {
      return new Response("OK", {
        headers: corsHeaders
      });
    }

    if (method !== "GET") {
      return new Response('Method not allowed', { 
        status: 405, 
        headers: corsHeaders 
      });
    }

    if (!prompt) return new Response('Prompt is missing', { status: 400 });

    try {
      // Different body for different models
      const body = model.includes("m2m100") ? {
        text: prompt,
        source_lang: source_lang,
        target_lang: target_lang
      } : {
        prompt: prompt
      };

      let response = await env.AI.run(model, body);
      return Response.json(response, {
        headers: corsHeaders
      });
    } catch (error) {
      // Handle error
      return new Response('Error: ' + error?.message, { 
        status: 500, 
        headers: corsHeaders 
      });
    }
  }
};

Read more