This is a simple polyfill for Cloudflare Workers AI in wrangler dev --local
mode. It doesn't actually run the models locally—it just reaches out to Cloudflare via their APIs. I think this is a reasonable compromise for local development while we wait.
I've only implemented and tested this for embeddings models, other models shouldn't be too hard but are left as an exercise for the reader. If you do implement them let me know and I'll update the gist :)
To polyfill it, I use wrangler dev --define IS_LOCAL:true
in local mode, and then have something like:
import { Ai as RemoteAi } from "@cloudflare/ai";
const Ai = IS_LOCAL ? new LocalAi(env.AI_GATEWAY_URL, env.AI_API_KEY) : new RemoteAi(env.AI);
If you're reading this in the future, I built this in October 2023 and it will likely become obsolete pretty soon. Brendan & the Miniflare team are pretty fast at adding stuff to local mode!