How to Deploy Genkit Apps on Cloudflare Workers

Iniciado por joomlamz, Ontem às 19:15

Respostas: 0   |   Visualizações: 3

Tópico anterior - Tópico seguinte

0 Membros e 2 Visitantes estão a ver este tópico.


                     How to Deploy Genkit Apps on Cloudflare Workers
               




Tópico:
                     How to Deploy Genkit Apps on Cloudflare Workers
               
Categoria: Tutoriais | FreeCodeCamp Premium
Idioma Principal: Português (Conteúdo de Tecnologia)

Conteúdo do Tutorial / Guia Passo a Passo:
-------------------------------------------------------------------------
Genkit is an open-source framework built by Google you can use to build AI-powered applications. The framework gives you structured output and tool calling, RAG, streaming, and generative UI. It also gives you tools around your AI application for tracing and evaluation, plus a user interface to test and observe the application.

Genkit has support for many model providers through an
ai.generate()API, which means you can swap Gemini for OpenAI or Claude without rewriting your application.

Cloudflare Workers is a serverless platform that runs your code close to your users. Workers run your JavaScript app in V8 isolates. These are lightweight sandboxes that start in milliseconds, so you don't have any cold start problems like you would've had in cloud serverless environments.

In this tutorial, you'll build an AI application called Explainer. You'll send it a software engineering topic, and it'll explains the topic to you as if you were a junior engineer.

Genkit applications break on Workers out of the box, so in this tutorial you'll understand why and also learn how to fix it.

What We'll Cover:

• Prerequisites

• Why Genkit Breaks on Cloudflare Workers
• Restrictions onevalandnew Function()

• The Error from Genkit's Reflection Server Express Module

• How to Set Up the Starter Repository

• How to Build the Explainer Flow
• Swap in a Different Provider

• Configure the Genkit Runtime

• Stop the Express Crash

• Add the HTTP Endpoint

• How to Test the Worker Locally

• How to Run the App with the Genkit Dev UI

• How to Deploy to Cloudflare Workers

• How to Set Up Groq as a Fallback Provider

• Troubleshooting Common Errors

• Streaming Responses with Genkit on Cloudflare Workers

• Limitations of Genkit on Cloudflare Workers

• Conclusion

• References

Prerequisites

To follow along with this tutorial, you need:

• Working knowledge of TypeScript

• Basic knowledge of Genkit (flows and
ai.generate())

• A Cloudflare account

• A Gemini API key (a Groq key is optional, for the fallback section)

•
pnpminstalled

•
curlor Postman for testing

Why Genkit Breaks on Cloudflare Workers

JavaScript doesn't run by itself. Think of JavaScript as a car. Can it move? Yes, but it needs a solid surface on which to move. Similarly, JavaScript needs an environment to run in, and that environment is a program that provides the APIs (or support) that your JavaScript code executes.

The browser is an example of a runtime environment (with
fetchand the DOM APIs), while Node.js is another (with
fsand
netAPIs). Bun and Deno are still others.

Cloudflare Workers uses its own runtime (workerd) built on V8, the same engine that powers Chrome tabs. It gives you
fetch,
crypto, and timers, but it's not Node.js. Building a JavaScript application on Workers that assumes a full Node.js environment can fail.

Genkit relies on two capabilities that the Workers runtime deliberately restricts.

Restrictions on
evaland
new Function()[/b]

Workers forbids generating code from strings. Both
eval()and
new Function()throw this error in workerd, Cloudflare's runtime:

EvalError: Code generation from strings disallowed for this context

Genkit uses
new Function()in two places:

• When its schema validator

... [O tutorial continua no link abaixo] ...


Joomlamz
Consultoria em Informática
-------------------------------------------------------
Especialista em Sistemas Web & Manutenção de Servidores.
A desenvolver o novo AplPortal com suporte a PHP 8.
Precisa de ajuda profissional? Contacte-me.

Tags: