Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit 6868be7

Browse files
Fixes gateway option to work with streaming. (#26)
* Fix: Cloudflare AI Gateway options does not work with doStream Feature: Cloudflare AI Gateway options should be set when creating * Feature: more options for models * Fix: gateway options not enable * spacing --------- Co-authored-by: jiang-zhexin <162887873+jiang-zhexin@users.noreply.github.com>
1 parent aa3e2d4 commit 6868be7

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

.changeset/warm-snails-pump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"workers-ai-provider": patch
3+
---
4+
5+
configures AI Gateway to work with streamText

packages/ai-provider/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ binding = "AI"
2929
```
3030
**/
3131
binding: Ai;
32+
/**
33+
* Optionally set Cloudflare AI Gateway options.
34+
*/
35+
gateway?: GatewayOptions;
3236
}
3337

3438
/**
@@ -42,6 +46,7 @@ export function createWorkersAI(options: WorkersAISettings): WorkersAI {
4246
new WorkersAIChatLanguageModel(modelId, settings, {
4347
provider: "workersai.chat",
4448
binding: options.binding,
49+
gateway: options.gateway,
4550
});
4651

4752
const provider = function (

packages/ai-provider/src/workersai-chat-language-model.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { events } from "fetch-event-stream";
1414
type WorkersAIChatConfig = {
1515
provider: string;
1616
binding: Ai;
17+
gateway?: GatewayOptions;
1718
};
1819

1920
export class WorkersAIChatLanguageModel implements LanguageModelV1 {
@@ -138,11 +139,12 @@ export class WorkersAIChatLanguageModel implements LanguageModelV1 {
138139
args.model,
139140
{
140141
messages: args.messages,
142+
max_tokens: args.max_tokens,
143+
temperature: args.temperature,
141144
tools: args.tools,
145+
top_p: args.top_p,
142146
},
143-
{
144-
gateway: this.settings.gateway,
145-
}
147+
{ gateway: this.config.gateway ?? this.settings.gateway }
146148
);
147149

148150
if (output instanceof ReadableStream) {
@@ -173,11 +175,18 @@ export class WorkersAIChatLanguageModel implements LanguageModelV1 {
173175
): Promise<Awaited<ReturnType<LanguageModelV1["doStream"]>>> {
174176
const { args, warnings } = this.getArgs(options);
175177

176-
const response = await this.config.binding.run(args.model, {
177-
messages: args.messages,
178-
stream: true,
179-
tools: args.tools,
180-
});
178+
const response = await this.config.binding.run(
179+
args.model,
180+
{
181+
messages: args.messages,
182+
max_tokens: args.max_tokens,
183+
stream: true,
184+
temperature: args.temperature,
185+
tools: args.tools,
186+
top_p: args.top_p,
187+
},
188+
{ gateway: this.config.gateway ?? this.settings.gateway }
189+
);
181190

182191
if (!(response instanceof ReadableStream)) {
183192
throw new Error("This shouldn't happen");

packages/ai-provider/src/workersai-chat-settings.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface WorkersAIChatSettings {
77
safePrompt?: boolean;
88
/**
99
* Optionally set Cloudflare AI Gateway options.
10+
* @deprecated
1011
*/
11-
gateway?: GatewayOptions
12+
gateway?: GatewayOptions;
1213
}

0 commit comments

Comments
 (0)