JSPM

  • Created
  • Published
  • Downloads 10673
  • Score
    100M100P100Q176970F

Workers AI Provider for the vercel AI SDK

Package Exports

  • workers-ai-provider
  • workers-ai-provider/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (workers-ai-provider) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

workers-ai-provider ⬡ ⤫ ▴

A custom provider that enables Workers AI's models for the Vercel AI SDK.

[!CAUTION] This project is in its experimental early stages and is not recommended for production use.

Install

npm install workers-ai-provider

Usage

First setup an AI binding in wrangler.toml:

# ...
[ai]
binding = "AI"
# ...

Then in your Worker, import the factory function and create a new AI provider:

// index.ts
import { createWorkersAI } from "workers-ai-provider";
import * as ai from "ai";

export default {
  fetch(req: Request, env: Env) {
    const workersai = createWorkersAI({ binding: env.AI });
    // Use the AI provider to interact with the Vercel AI SDK
    // Here, we generate a chat stream based on a prompt
    const response = ai.streamText({
      model: workersai("@cf/meta/llama-2-7b-chat-int8"),
      messages: [
        {
          role: "user",
          content: "Write an essay about hello world",
        },
      ],
    });

    return new Response(txt.text);
  },
};

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.