⛅️ ✨ workers-ai-provider ✨ ⛅️
A custom provider that enables Workers AI's models for the Vercel AI SDK.
Install
npm install workers-ai-provider
Usage
First, setup an AI binding in wrangler.toml
in your Workers project:
[ai]
binding = "AI"
Then in your Worker, import the factory function and create a new AI provider:
import { createWorkersAI } from "workers-ai-provider";
import { streamText } from "ai";
type Env = {
AI: Ai;
};
export default {
async fetch(req: Request, env: Env) {
const workersai = createWorkersAI({ binding: env.AI });
const text = await streamText({
model: workersai("@cf/meta/llama-2-7b-chat-int8"),
messages: [
{
role: "user",
content: "Write an essay about hello world",
},
],
});
return text.toTextStreamResponse({
headers: {
"Content-Type": "text/x-unknown",
"content-encoding": "identity",
"transfer-encoding": "chunked",
},
});
},
};
For more info, refer to the documentation of the Vercel AI SDK.
Credits
Based on work by Dhravya Shah and the Workers AI team at Cloudflare.