Skip to content

Commit 74620c4

Browse files
authored
docs: modifying the AI provider
1 parent 72778ce commit 74620c4

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,53 @@ Tools can be configured in two ways:
127127
1. With an `execute` function for automatic execution
128128
2. Without an `execute` function, requiring confirmation and using the `executions` object to handle the confirmed action
129129

130+
### Use a different AI model provider
131+
132+
The starting [`server.ts`](https://github.com/cloudflare/agents-starter/blob/main/src/server.ts) implementation uses the [`ai-sdk`](https://sdk.vercel.ai/docs/introduction) and the [OpenAI provider](https://sdk.vercel.ai/providers/ai-sdk-providers/openai), but you can use any AI model provider by:
133+
134+
1. Installing an alternative AI provider for the `ai-sdk`, such as the [`workers-ai-provider`](https://sdk.vercel.ai/providers/community-providers/cloudflare-workers-ai) or [`anthropic`](https://sdk.vercel.ai/providers/ai-sdk-providers/anthropic) provider:
135+
2. Replacing the AI SDK with the [OpenAI SDK](https://github.com/openai/openai-node)
136+
3. Using the Cloudflare [Workers AI + AI Gateway](https://developers.cloudflare.com/ai-gateway/providers/workersai/#workers-binding) binding API directly
137+
138+
For example, to use the [`workers-ai-provider`](https://sdk.vercel.ai/providers/community-providers/cloudflare-workers-ai), install the package:
139+
140+
```sh
141+
npm install workers-ai-provider
142+
```
143+
144+
Add an `ai` binding to `wrangler.jsonc`:
145+
146+
```jsonc
147+
// rest of file
148+
"ai": {
149+
"binding": "AI"
150+
}
151+
// rest of file
152+
```
153+
154+
Replace the `@ai-sdk/openai` import and usage with the `workers-ai-provider`:
155+
156+
```diff
157+
// server.ts
158+
// Change the imports
159+
- import { createOpenAI } from "@ai-sdk/openai";
160+
+ import { createWorkersAI } from 'workers-ai-provider';
161+
162+
// Create a Workers AI instance
163+
- const openai = createOpenAI({
164+
- apiKey: this.env.OPENAI_API_KEY,
165+
- });
166+
+ const workersai = createWorkersAI({ binding: env.AI });
167+
168+
// Use it when calling the streamText method (or other methods)
169+
// from the ai-sdk
170+
- const result = streamText({
171+
- model: openai("gpt-4o-2024-11-20"),
172+
+ const model = workersai("@cf/deepseek-ai/deepseek-r1-distill-qwen-32b")
173+
```
174+
175+
Commit your changes and then run the `agents-starter` as per the rest of this README.
176+
130177
### Modifying the UI
131178

132179
The chat interface is built with React and can be customized in `app.tsx`:

0 commit comments

Comments
 (0)