Skip to content

Commit 1f6dae3

Browse files
authored
chore: make playground example fallback to WorkersAI when OpenAI key is not set (#796)
* chore: make playground example fallback to WorkersAI when OpenAI key is not set * fix
1 parent 3824385 commit 1f6dae3

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

examples/playground/env.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable */
2-
// Generated by Wrangler by running `wrangler types env.d.ts --include-runtime false` (hash: f103d0d6947e92f53f1c4ba8d54acaa6)
2+
// Generated by Wrangler by running `wrangler types env.d.ts --include-runtime false` (hash: 58d9bb5230a737a6ca0946ef9228fa8c)
33
declare namespace Cloudflare {
44
interface GlobalProps {
55
mainModule: typeof import("./src/server");
@@ -11,6 +11,7 @@ declare namespace Cloudflare {
1111
Stateful: DurableObjectNamespace<import("./src/server").Stateful>;
1212
Chat: DurableObjectNamespace<import("./src/server").Chat>;
1313
Rpc: DurableObjectNamespace<import("./src/server").Rpc>;
14+
AI: Ai;
1415
ASSETS: Fetcher;
1516
}
1617
}

examples/playground/src/model.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
import { createWorkersAI } from "workers-ai-provider";
12
import { openai } from "@ai-sdk/openai";
3+
import type { LanguageModelV3 } from "@ai-sdk/provider";
4+
import { env } from "cloudflare:workers";
25

3-
export const model = openai("gpt-4o");
6+
export const model: LanguageModelV3 = (() => {
7+
if (env.OPENAI_API_KEY) {
8+
return openai("gpt-4");
9+
} else {
10+
const workersai = createWorkersAI({ binding: env.AI });
11+
return workersai("@cf/meta/llama-2-7b-chat-int8");
12+
}
13+
})();

examples/playground/wrangler.jsonc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
"nodejs_compat",
88
"nodejs_compat_populate_process_env"
99
],
10+
"ai": {
11+
"binding": "AI",
12+
"remote": true
13+
},
1014
"assets": {
1115
"binding": "ASSETS"
1216
},

0 commit comments

Comments
 (0)