Skip to content

Commit 368f038

Browse files
docs: Add AI SDK for Workers AI (#19067)
* docs: Add AI SDK * Update src/content/docs/workers-ai/configuration/ai-sdk.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> --------- Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com>
1 parent f0281f4 commit 368f038

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
```

src/content/docs/workers-ai/get-started/rest-api.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,4 @@ By completing this guide, you have created a Cloudflare account (if you did not
7272
## Related resources
7373

7474
- [Models](/workers-ai/models/) - Browse the Workers AI models catalog.
75+
- [AI SDK](/workers-ai/configuration/ai-sdk) - Learn how to integrate with an AI model.

src/content/docs/workers-ai/get-started/workers-wrangler.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,4 @@ By finishing this tutorial, you have created a Worker, connected it to Workers A
141141

142142
- [Cloudflare Developers community on Discord](https://discord.cloudflare.com) - Submit feature requests, report bugs, and share your feedback directly with the Cloudflare team by joining the Cloudflare Discord server.
143143
- [Models](/workers-ai/models/) - Browse the Workers AI models catalog.
144+
- [AI SDK](/workers-ai/configuration/ai-sdk) - Learn how to integrate with an AI model.

0 commit comments

Comments
 (0)