workers-ai-provider 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -1
- package/dist/index.d.ts +94 -8
- package/dist/index.js +365 -167
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/autorag-chat-language-model.ts +172 -0
- package/src/autorag-chat-settings.ts +14 -0
- package/src/convert-to-workersai-chat-messages.ts +8 -18
- package/src/index.ts +74 -1
- package/src/streaming.ts +37 -0
- package/src/utils.ts +93 -1
- package/src/workers-ai-embedding-model.ts +87 -0
- package/src/workersai-chat-language-model.ts +5 -128
- package/src/workersai-models.ts +5 -0
package/README.md
CHANGED
@@ -19,10 +19,12 @@ binding = "AI"
|
|
19
19
|
# ...
|
20
20
|
```
|
21
21
|
|
22
|
+
### Using Workers AI
|
23
|
+
|
22
24
|
Then in your Worker, import the factory function and create a new AI provider:
|
23
25
|
|
24
26
|
```ts
|
25
|
-
import { createWorkersAI } from "
|
27
|
+
import { createWorkersAI } from "workers-ai-provider";
|
26
28
|
import { streamText } from "ai";
|
27
29
|
|
28
30
|
type Env = {
|
@@ -76,6 +78,45 @@ const text = await streamText({
|
|
76
78
|
});
|
77
79
|
```
|
78
80
|
|
81
|
+
### Using AutoRAG
|
82
|
+
|
83
|
+
The provider now supports [Cloudflare's AutoRAG](https://developers.cloudflare.com/autorag/), allowing you to prompt your AutoRAG models directly from the Vercel AI SDK. Here's how to use it in your Worker:
|
84
|
+
|
85
|
+
```ts
|
86
|
+
import { createAutoRAG } from "workers-ai-provider";
|
87
|
+
import { streamText } from "ai";
|
88
|
+
|
89
|
+
type Env = {
|
90
|
+
AI: Ai;
|
91
|
+
};
|
92
|
+
|
93
|
+
export default {
|
94
|
+
async fetch(req: Request, env: Env) {
|
95
|
+
const autorag = createAutoRAG({ binding: env.AI.autorag('my-rag-name') });
|
96
|
+
|
97
|
+
const text = await streamText({
|
98
|
+
model: autorag("@cf/meta/llama-3.3-70b-instruct-fp8-fast"),
|
99
|
+
messages: [
|
100
|
+
{
|
101
|
+
role: "user",
|
102
|
+
content: "How to setup AI Gateway?",
|
103
|
+
},
|
104
|
+
],
|
105
|
+
});
|
106
|
+
|
107
|
+
return text.toTextStreamResponse({
|
108
|
+
headers: {
|
109
|
+
// add these headers to ensure that the
|
110
|
+
// response is chunked and streamed
|
111
|
+
"Content-Type": "text/x-unknown",
|
112
|
+
"content-encoding": "identity",
|
113
|
+
"transfer-encoding": "chunked",
|
114
|
+
},
|
115
|
+
});
|
116
|
+
},
|
117
|
+
};
|
118
|
+
```
|
119
|
+
|
79
120
|
For more info, refer to the documentation of the [Vercel AI SDK](https://sdk.vercel.ai/).
|
80
121
|
|
81
122
|
### Credits
|
package/dist/index.d.ts
CHANGED
@@ -1,20 +1,15 @@
|
|
1
|
-
import { LanguageModelV1, ImageModelV1 } from '@ai-sdk/provider';
|
1
|
+
import { LanguageModelV1, EmbeddingModelV1, ImageModelV1 } from '@ai-sdk/provider';
|
2
2
|
|
3
3
|
type StringLike = string | {
|
4
4
|
toString(): string;
|
5
5
|
};
|
6
6
|
|
7
|
-
type
|
7
|
+
type AutoRAGChatSettings = {
|
8
8
|
/**
|
9
9
|
* Whether to inject a safety prompt before all conversations.
|
10
10
|
* Defaults to `false`.
|
11
11
|
*/
|
12
12
|
safePrompt?: boolean;
|
13
|
-
/**
|
14
|
-
* Optionally set Cloudflare AI Gateway options.
|
15
|
-
* @deprecated
|
16
|
-
*/
|
17
|
-
gateway?: GatewayOptions;
|
18
13
|
} & {
|
19
14
|
/**
|
20
15
|
* Passthrough settings that are provided directly to the run function.
|
@@ -27,10 +22,84 @@ type WorkersAIChatSettings = {
|
|
27
22
|
*/
|
28
23
|
type TextGenerationModels = Exclude<value2key<AiModels, BaseAiTextGeneration>, value2key<AiModels, BaseAiTextToImage>>;
|
29
24
|
type ImageGenerationModels = value2key<AiModels, BaseAiTextToImage>;
|
25
|
+
/**
|
26
|
+
* The names of the BaseAiTextToEmbeddings models.
|
27
|
+
*/
|
28
|
+
type EmbeddingModels = value2key<AiModels, BaseAiTextEmbeddings>;
|
30
29
|
type value2key<T, V> = {
|
31
30
|
[K in keyof T]: T[K] extends V ? K : never;
|
32
31
|
}[keyof T];
|
33
32
|
|
33
|
+
type AutoRAGChatConfig = {
|
34
|
+
provider: string;
|
35
|
+
binding: AutoRAG;
|
36
|
+
gateway?: GatewayOptions;
|
37
|
+
};
|
38
|
+
declare class AutoRAGChatLanguageModel implements LanguageModelV1 {
|
39
|
+
readonly specificationVersion = "v1";
|
40
|
+
readonly defaultObjectGenerationMode = "json";
|
41
|
+
readonly modelId: TextGenerationModels;
|
42
|
+
readonly settings: AutoRAGChatSettings;
|
43
|
+
private readonly config;
|
44
|
+
constructor(modelId: TextGenerationModels, settings: AutoRAGChatSettings, config: AutoRAGChatConfig);
|
45
|
+
get provider(): string;
|
46
|
+
private getArgs;
|
47
|
+
doGenerate(options: Parameters<LanguageModelV1["doGenerate"]>[0]): Promise<Awaited<ReturnType<LanguageModelV1["doGenerate"]>>>;
|
48
|
+
doStream(options: Parameters<LanguageModelV1["doStream"]>[0]): Promise<Awaited<ReturnType<LanguageModelV1["doStream"]>>>;
|
49
|
+
}
|
50
|
+
|
51
|
+
type WorkersAIEmbeddingConfig = {
|
52
|
+
provider: string;
|
53
|
+
binding: Ai;
|
54
|
+
gateway?: GatewayOptions;
|
55
|
+
};
|
56
|
+
type WorkersAIEmbeddingSettings = {
|
57
|
+
gateway?: GatewayOptions;
|
58
|
+
maxEmbeddingsPerCall?: number;
|
59
|
+
supportsParallelCalls?: boolean;
|
60
|
+
} & {
|
61
|
+
/**
|
62
|
+
* Arbitrary provider-specific options forwarded unmodified.
|
63
|
+
*/
|
64
|
+
[key: string]: StringLike;
|
65
|
+
};
|
66
|
+
declare class WorkersAIEmbeddingModel implements EmbeddingModelV1<string> {
|
67
|
+
/**
|
68
|
+
* Semantic version of the {@link EmbeddingModelV1} specification implemented
|
69
|
+
* by this class. It never changes.
|
70
|
+
*/
|
71
|
+
readonly specificationVersion = "v1";
|
72
|
+
readonly modelId: EmbeddingModels;
|
73
|
+
private readonly config;
|
74
|
+
private readonly settings;
|
75
|
+
/**
|
76
|
+
* Provider name exposed for diagnostics and error reporting.
|
77
|
+
*/
|
78
|
+
get provider(): string;
|
79
|
+
get maxEmbeddingsPerCall(): number;
|
80
|
+
get supportsParallelCalls(): boolean;
|
81
|
+
constructor(modelId: EmbeddingModels, settings: WorkersAIEmbeddingSettings, config: WorkersAIEmbeddingConfig);
|
82
|
+
doEmbed({ values, }: Parameters<EmbeddingModelV1<string>["doEmbed"]>[0]): Promise<Awaited<ReturnType<EmbeddingModelV1<string>["doEmbed"]>>>;
|
83
|
+
}
|
84
|
+
|
85
|
+
type WorkersAIChatSettings = {
|
86
|
+
/**
|
87
|
+
* Whether to inject a safety prompt before all conversations.
|
88
|
+
* Defaults to `false`.
|
89
|
+
*/
|
90
|
+
safePrompt?: boolean;
|
91
|
+
/**
|
92
|
+
* Optionally set Cloudflare AI Gateway options.
|
93
|
+
* @deprecated
|
94
|
+
*/
|
95
|
+
gateway?: GatewayOptions;
|
96
|
+
} & {
|
97
|
+
/**
|
98
|
+
* Passthrough settings that are provided directly to the run function.
|
99
|
+
*/
|
100
|
+
[key: string]: StringLike;
|
101
|
+
};
|
102
|
+
|
34
103
|
type WorkersAIChatConfig = {
|
35
104
|
provider: string;
|
36
105
|
binding: Ai;
|
@@ -102,6 +171,9 @@ interface WorkersAI {
|
|
102
171
|
* Creates a model for text generation.
|
103
172
|
**/
|
104
173
|
chat(modelId: TextGenerationModels, settings?: WorkersAIChatSettings): WorkersAIChatLanguageModel;
|
174
|
+
embedding(modelId: EmbeddingModels, settings?: WorkersAIEmbeddingSettings): WorkersAIEmbeddingModel;
|
175
|
+
textEmbedding(modelId: EmbeddingModels, settings?: WorkersAIEmbeddingSettings): WorkersAIEmbeddingModel;
|
176
|
+
textEmbeddingModel(modelId: EmbeddingModels, settings?: WorkersAIEmbeddingSettings): WorkersAIEmbeddingModel;
|
105
177
|
/**
|
106
178
|
* Creates a model for image generation.
|
107
179
|
**/
|
@@ -111,5 +183,19 @@ interface WorkersAI {
|
|
111
183
|
* Create a Workers AI provider instance.
|
112
184
|
*/
|
113
185
|
declare function createWorkersAI(options: WorkersAISettings): WorkersAI;
|
186
|
+
type AutoRAGSettings = {
|
187
|
+
binding: AutoRAG;
|
188
|
+
};
|
189
|
+
interface AutoRAGProvider {
|
190
|
+
(options?: AutoRAGChatSettings): AutoRAGChatLanguageModel;
|
191
|
+
/**
|
192
|
+
* Creates a model for text generation.
|
193
|
+
**/
|
194
|
+
chat(settings?: AutoRAGChatSettings): AutoRAGChatLanguageModel;
|
195
|
+
}
|
196
|
+
/**
|
197
|
+
* Create a Workers AI provider instance.
|
198
|
+
*/
|
199
|
+
declare function createAutoRAG(options: AutoRAGSettings): AutoRAGProvider;
|
114
200
|
|
115
|
-
export { type WorkersAI, type WorkersAISettings, createWorkersAI };
|
201
|
+
export { type AutoRAGProvider, type AutoRAGSettings, type WorkersAI, type WorkersAISettings, createAutoRAG, createWorkersAI };
|