DEV Community

Olatunji Ayodele Abidemi
Olatunji Ayodele Abidemi

Posted on

1

Building a serverless AI App on Cloudflare Workers

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
// Parse the incoming request to get the input data
const data = await request.json();

// Here you would typically call an AI model with the input data
// For example, using Cloudflare's Open Models (replace with actual model endpoint and API key)
const aiModelUrl = 'https://api.cloudflare.com/client/v4/models/YOUR_MODEL_ID';
const aiResponse = await fetch(aiModelUrl, {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({ input: data.input })
});

// Get the AI response and format it as needed
const aiResult = await aiResponse.json();

// Return the AI result as the response from the Worker
return new Response(JSON.stringify(aiResult), {
headers: { 'content-type': 'application/json' },
});
}

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (1)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more