|
| 1 | +--- |
| 2 | +pcx_content_type: configuration |
| 3 | +title: AI SDK |
| 4 | +sidebar: |
| 5 | + order: 3 |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +Workers AI can be used with the [AI SDK](https://sdk.vercel.ai/) for JavaScript and TypeScript codebases. |
| 10 | + |
| 11 | +## Setup |
| 12 | + |
| 13 | +Install the [`workers-ai-provider` provider](https://sdk.vercel.ai/providers/community-providers/cloudflare-workers-ai): |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install workers-ai-provider |
| 17 | +``` |
| 18 | + |
| 19 | +Then, add an AI binding in your Workers project `wrangler.toml` file: |
| 20 | + |
| 21 | +```toml |
| 22 | +[ai] |
| 23 | +binding = "AI" |
| 24 | +``` |
| 25 | + |
| 26 | +## Models |
| 27 | + |
| 28 | +The AI SDK can be configured to work with [any AI model](/workers-ai/models/). |
| 29 | + |
| 30 | +```js |
| 31 | +import { createWorkersAI } from 'workers-ai-provider'; |
| 32 | + |
| 33 | +const workersai = createWorkersAI({ binding: env.AI }); |
| 34 | + |
| 35 | +// Choose any model: https://developers.cloudflare.com/workers-ai/models/ |
| 36 | +const model = workersai('@cf/meta/llama-3.1-8b-instruct', {}); |
| 37 | +``` |
| 38 | + |
| 39 | +## Generate Text |
| 40 | + |
| 41 | +Once you have selected your model, you can generate text from a given prompt. |
| 42 | + |
| 43 | +```js |
| 44 | +import { createWorkersAI } from 'workers-ai-provider'; |
| 45 | +import { generateText } from 'ai'; |
| 46 | + |
| 47 | +type Env = { |
| 48 | + AI: Ai; |
| 49 | +}; |
| 50 | + |
| 51 | +export default { |
| 52 | + async fetch(_: Request, env: Env) { |
| 53 | + const workersai = createWorkersAI({ binding: env.AI }); |
| 54 | + const result = await generateText({ |
| 55 | + model: workersai('@cf/meta/llama-2-7b-chat-int8'), |
| 56 | + prompt: 'Write a 50-word essay about hello world.', |
| 57 | + }); |
| 58 | + |
| 59 | + return new Response(result.text); |
| 60 | + }, |
| 61 | +}; |
| 62 | +``` |
| 63 | + |
| 64 | +## Stream Text |
| 65 | + |
| 66 | +For longer responses, consider streaming responses to provide as the generation completes. |
| 67 | + |
| 68 | +```js |
| 69 | +import { createWorkersAI } from 'workers-ai-provider'; |
| 70 | +import { streamText } from 'ai'; |
| 71 | + |
| 72 | +type Env = { |
| 73 | + AI: Ai; |
| 74 | +}; |
| 75 | + |
| 76 | +export default { |
| 77 | + async fetch(_: Request, env: Env) { |
| 78 | + const workersai = createWorkersAI({ binding: env.AI }); |
| 79 | + const result = streamText({ |
| 80 | + model: workersai('@cf/meta/llama-2-7b-chat-int8'), |
| 81 | + prompt: 'Write a 50-word essay about hello world.', |
| 82 | + }); |
| 83 | + |
| 84 | + return result.toTextStreamResponse({ |
| 85 | + headers: { |
| 86 | + // add these headers to ensure that the |
| 87 | + // response is chunked and streamed |
| 88 | + 'Content-Type': 'text/x-unknown', |
| 89 | + 'content-encoding': 'identity', |
| 90 | + 'transfer-encoding': 'chunked', |
| 91 | + }, |
| 92 | + }); |
| 93 | + }, |
| 94 | +}; |
| 95 | +``` |
| 96 | + |
| 97 | +## Generate Structured Objects |
| 98 | + |
| 99 | +You can provide a Zod schema to generate a structured JSON response. |
| 100 | + |
| 101 | +```js |
| 102 | +import { createWorkersAI } from 'workers-ai-provider'; |
| 103 | +import { generateObject } from 'ai'; |
| 104 | +import { z } from 'zod'; |
| 105 | + |
| 106 | +type Env = { |
| 107 | + AI: Ai; |
| 108 | +}; |
| 109 | + |
| 110 | +export default { |
| 111 | + async fetch(_: Request, env: Env) { |
| 112 | + const workersai = createWorkersAI({ binding: env.AI }); |
| 113 | + const result = await generateObject({ |
| 114 | + model: workersai('@cf/meta/llama-3.1-8b-instruct'), |
| 115 | + prompt: 'Generate a Lasagna recipe', |
| 116 | + schema: z.object({ |
| 117 | + recipe: z.object({ |
| 118 | + ingredients: z.array(z.string()), |
| 119 | + description: z.string(), |
| 120 | + }), |
| 121 | + }), |
| 122 | + }); |
| 123 | + |
| 124 | + return Response.json(result.object); |
| 125 | + }, |
| 126 | +}; |
| 127 | +``` |
0 commit comments