@ai-sdk/baseten 1.0.18 → 1.0.19
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/CHANGELOG.md +6 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/docs/170-baseten.mdx +248 -0
- package/package.json +6 -2
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -34,7 +34,7 @@ var import_v4 = require("zod/v4");
|
|
|
34
34
|
var import_performance_client = require("@basetenlabs/performance-client");
|
|
35
35
|
|
|
36
36
|
// src/version.ts
|
|
37
|
-
var VERSION = true ? "1.0.
|
|
37
|
+
var VERSION = true ? "1.0.19" : "0.0.0-test";
|
|
38
38
|
|
|
39
39
|
// src/baseten-provider.ts
|
|
40
40
|
var basetenErrorSchema = import_v4.z.object({
|
package/dist/index.mjs
CHANGED
|
@@ -15,7 +15,7 @@ import { z } from "zod/v4";
|
|
|
15
15
|
import { PerformanceClient } from "@basetenlabs/performance-client";
|
|
16
16
|
|
|
17
17
|
// src/version.ts
|
|
18
|
-
var VERSION = true ? "1.0.
|
|
18
|
+
var VERSION = true ? "1.0.19" : "0.0.0-test";
|
|
19
19
|
|
|
20
20
|
// src/baseten-provider.ts
|
|
21
21
|
var basetenErrorSchema = z.object({
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Baseten
|
|
3
|
+
description: Learn how to use Baseten models with the AI SDK.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Baseten Provider
|
|
7
|
+
|
|
8
|
+
[Baseten](https://baseten.co/) is an inference platform for serving frontier, enterprise-grade opensource AI models via their [API](https://docs.baseten.co/overview).
|
|
9
|
+
|
|
10
|
+
## Setup
|
|
11
|
+
|
|
12
|
+
The Baseten provider is available via the `@ai-sdk/baseten` module. You can install it with
|
|
13
|
+
|
|
14
|
+
<Tabs items={['pnpm', 'npm', 'yarn']}>
|
|
15
|
+
<Tab>
|
|
16
|
+
<Snippet text="pnpm add @ai-sdk/baseten" dark />
|
|
17
|
+
</Tab>
|
|
18
|
+
<Tab>
|
|
19
|
+
<Snippet text="npm install @ai-sdk/baseten" dark />
|
|
20
|
+
</Tab>
|
|
21
|
+
<Tab>
|
|
22
|
+
<Snippet text="yarn add @ai-sdk/baseten" dark />
|
|
23
|
+
</Tab>
|
|
24
|
+
</Tabs>
|
|
25
|
+
|
|
26
|
+
## Provider Instance
|
|
27
|
+
|
|
28
|
+
You can import the default provider instance `baseten` from `@ai-sdk/baseten`:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { baseten } from '@ai-sdk/baseten';
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
If you need a customized setup, you can import `createBaseten` from `@ai-sdk/baseten`
|
|
35
|
+
and create a provider instance with your settings:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { createBaseten } from '@ai-sdk/baseten';
|
|
39
|
+
|
|
40
|
+
const baseten = createBaseten({
|
|
41
|
+
apiKey: process.env.BASETEN_API_KEY ?? '',
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
You can use the following optional settings to customize the Baseten provider instance:
|
|
46
|
+
|
|
47
|
+
- **baseURL** _string_
|
|
48
|
+
|
|
49
|
+
Use a different URL prefix for API calls, e.g. to use proxy servers.
|
|
50
|
+
The default prefix is `https://inference.baseten.co/v1`.
|
|
51
|
+
|
|
52
|
+
- **apiKey** _string_
|
|
53
|
+
|
|
54
|
+
API key that is being sent using the `Authorization` header. It defaults to
|
|
55
|
+
the `BASETEN_API_KEY` environment variable. It is recommended you set the environment variable using `export` so you do not need to include the field everytime.
|
|
56
|
+
You can grab your Baseten API Key [here](https://app.baseten.co/settings/api_keys)
|
|
57
|
+
|
|
58
|
+
- **modelURL** _string_
|
|
59
|
+
|
|
60
|
+
Custom model URL for specific models (chat or embeddings). If not provided,
|
|
61
|
+
the default Model APIs will be used.
|
|
62
|
+
|
|
63
|
+
- **headers** _Record<string,string>_
|
|
64
|
+
|
|
65
|
+
Custom headers to include in the requests.
|
|
66
|
+
|
|
67
|
+
- **fetch** _(input: RequestInfo, init?: RequestInit) => Promise<Response>_
|
|
68
|
+
|
|
69
|
+
Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.
|
|
70
|
+
|
|
71
|
+
## Model APIs
|
|
72
|
+
|
|
73
|
+
You can select [Baseten models](https://www.baseten.co/products/model-apis/) using a provider instance.
|
|
74
|
+
The first argument is the model id, e.g. `'moonshotai/Kimi-K2-Instruct-0905'`: The complete supported models under Model APIs can be found [here](https://docs.baseten.co/development/model-apis/overview#supported-models).
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const model = baseten('moonshotai/Kimi-K2-Instruct-0905');
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Example
|
|
81
|
+
|
|
82
|
+
You can use Baseten language models to generate text with the `generateText` function:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { baseten } from '@ai-sdk/baseten';
|
|
86
|
+
import { generateText } from 'ai';
|
|
87
|
+
|
|
88
|
+
const { text } = await generateText({
|
|
89
|
+
model: baseten('moonshotai/Kimi-K2-Instruct-0905'),
|
|
90
|
+
prompt: 'What is the meaning of life? Answer in one sentence.',
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Baseten language models can also be used in the `streamText` function
|
|
95
|
+
(see [AI SDK Core](/docs/ai-sdk-core)).
|
|
96
|
+
|
|
97
|
+
## Dedicated Models
|
|
98
|
+
|
|
99
|
+
Baseten supports dedicated model URLs for both chat and embedding models. You have to specify a `modelURL` when creating the provider:
|
|
100
|
+
|
|
101
|
+
### OpenAI-Compatible Endpoints (`/sync/v1`)
|
|
102
|
+
|
|
103
|
+
For models deployed with Baseten's OpenAI-compatible endpoints:
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import { createBaseten } from '@ai-sdk/baseten';
|
|
107
|
+
|
|
108
|
+
const baseten = createBaseten({
|
|
109
|
+
modelURL: 'https://model-{MODEL_ID}.api.baseten.co/sync/v1',
|
|
110
|
+
});
|
|
111
|
+
// No modelId is needed because we specified modelURL
|
|
112
|
+
const model = baseten();
|
|
113
|
+
const { text } = await generateText({
|
|
114
|
+
model: model,
|
|
115
|
+
prompt: 'Say hello from a Baseten chat model!',
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### `/predict` Endpoints
|
|
120
|
+
|
|
121
|
+
`/predict` endpoints are currently NOT supported for chat models. You must use `/sync/v1` endpoints for chat functionality.
|
|
122
|
+
|
|
123
|
+
## Embedding Models
|
|
124
|
+
|
|
125
|
+
You can create models that call the Baseten embeddings API using the `.embeddingModel()` factory method. The Baseten provider uses the high-performance `@basetenlabs/performance-client` for optimal embedding performance.
|
|
126
|
+
|
|
127
|
+
<Note>
|
|
128
|
+
**Important:** Embedding models require a dedicated deployment with a custom
|
|
129
|
+
`modelURL`. Unlike chat models, embeddings cannot use Baseten's default Model
|
|
130
|
+
APIs and must specify a dedicated model endpoint.
|
|
131
|
+
</Note>
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
import { createBaseten } from '@ai-sdk/baseten';
|
|
135
|
+
import { embed, embedMany } from 'ai';
|
|
136
|
+
|
|
137
|
+
const baseten = createBaseten({
|
|
138
|
+
modelURL: 'https://model-{MODEL_ID}.api.baseten.co/sync',
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const embeddingModel = baseten.embeddingModel();
|
|
142
|
+
|
|
143
|
+
// Single embedding
|
|
144
|
+
const { embedding } = await embed({
|
|
145
|
+
model: embeddingModel,
|
|
146
|
+
value: 'sunny day at the beach',
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// Batch embeddings
|
|
150
|
+
const { embeddings } = await embedMany({
|
|
151
|
+
model: embeddingModel,
|
|
152
|
+
values: [
|
|
153
|
+
'sunny day at the beach',
|
|
154
|
+
'rainy afternoon in the city',
|
|
155
|
+
'snowy mountain peak',
|
|
156
|
+
],
|
|
157
|
+
});
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Endpoint Support for Embeddings
|
|
161
|
+
|
|
162
|
+
**Supported:**
|
|
163
|
+
|
|
164
|
+
- `/sync` endpoints (Performance Client automatically adds `/v1/embeddings`)
|
|
165
|
+
- `/sync/v1` endpoints (automatically strips `/v1` before passing to Performance Client)
|
|
166
|
+
|
|
167
|
+
**Not Supported:**
|
|
168
|
+
|
|
169
|
+
- `/predict` endpoints (not compatible with Performance Client)
|
|
170
|
+
|
|
171
|
+
### Performance Features
|
|
172
|
+
|
|
173
|
+
The embedding implementation includes:
|
|
174
|
+
|
|
175
|
+
- **High-performance client**: Uses `@basetenlabs/performance-client` for optimal performance
|
|
176
|
+
- **Automatic batching**: Efficiently handles multiple texts in a single request
|
|
177
|
+
- **Connection reuse**: Performance Client is created once and reused for all requests
|
|
178
|
+
- **Built-in retries**: Automatic retry logic for failed requests
|
|
179
|
+
|
|
180
|
+
## Error Handling
|
|
181
|
+
|
|
182
|
+
The Baseten provider includes built-in error handling for common API errors:
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
import { baseten } from '@ai-sdk/baseten';
|
|
186
|
+
import { generateText } from 'ai';
|
|
187
|
+
|
|
188
|
+
try {
|
|
189
|
+
const { text } = await generateText({
|
|
190
|
+
model: baseten('moonshotai/Kimi-K2-Instruct-0905'),
|
|
191
|
+
prompt: 'Hello, world!',
|
|
192
|
+
});
|
|
193
|
+
} catch (error) {
|
|
194
|
+
console.error('Baseten API error:', error.message);
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Common Error Scenarios
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
// Embeddings require a modelURL
|
|
202
|
+
try {
|
|
203
|
+
baseten.embeddingModel();
|
|
204
|
+
} catch (error) {
|
|
205
|
+
// Error: "No model URL provided for embeddings. Please set modelURL option for embeddings."
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// /predict endpoints are not supported for chat models
|
|
209
|
+
try {
|
|
210
|
+
const baseten = createBaseten({
|
|
211
|
+
modelURL:
|
|
212
|
+
'https://model-{MODEL_ID}.api.baseten.co/environments/production/predict',
|
|
213
|
+
});
|
|
214
|
+
baseten(); // This will throw an error
|
|
215
|
+
} catch (error) {
|
|
216
|
+
// Error: "Not supported. You must use a /sync/v1 endpoint for chat models."
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// /sync/v1 endpoints are now supported for embeddings
|
|
220
|
+
const baseten = createBaseten({
|
|
221
|
+
modelURL:
|
|
222
|
+
'https://model-{MODEL_ID}.api.baseten.co/environments/production/sync/v1',
|
|
223
|
+
});
|
|
224
|
+
const embeddingModel = baseten.embeddingModel(); // This works fine!
|
|
225
|
+
|
|
226
|
+
// /predict endpoints are not supported for embeddings
|
|
227
|
+
try {
|
|
228
|
+
const baseten = createBaseten({
|
|
229
|
+
modelURL:
|
|
230
|
+
'https://model-{MODEL_ID}.api.baseten.co/environments/production/predict',
|
|
231
|
+
});
|
|
232
|
+
baseten.embeddingModel(); // This will throw an error
|
|
233
|
+
} catch (error) {
|
|
234
|
+
// Error: "Not supported. You must use a /sync or /sync/v1 endpoint for embeddings."
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Image models are not supported
|
|
238
|
+
try {
|
|
239
|
+
baseten.imageModel('test-model');
|
|
240
|
+
} catch (error) {
|
|
241
|
+
// Error: NoSuchModelError for imageModel
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
<Note>
|
|
246
|
+
For more information about Baseten models and deployment options, see the
|
|
247
|
+
[Baseten documentation](https://docs.baseten.co/).
|
|
248
|
+
</Note>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/baseten",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.19",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -8,10 +8,14 @@
|
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
9
|
"files": [
|
|
10
10
|
"dist/**/*",
|
|
11
|
+
"docs/**/*",
|
|
11
12
|
"src",
|
|
12
13
|
"CHANGELOG.md",
|
|
13
14
|
"README.md"
|
|
14
15
|
],
|
|
16
|
+
"directories": {
|
|
17
|
+
"doc": "./docs"
|
|
18
|
+
},
|
|
15
19
|
"exports": {
|
|
16
20
|
"./package.json": "./package.json",
|
|
17
21
|
".": {
|
|
@@ -56,7 +60,7 @@
|
|
|
56
60
|
"scripts": {
|
|
57
61
|
"build": "pnpm clean && tsup --tsconfig tsconfig.build.json",
|
|
58
62
|
"build:watch": "pnpm clean && tsup --watch",
|
|
59
|
-
"clean": "rm -rf dist *.tsbuildinfo",
|
|
63
|
+
"clean": "rm -rf dist docs *.tsbuildinfo",
|
|
60
64
|
"lint": "eslint \"./**/*.ts*\"",
|
|
61
65
|
"type-check": "tsc --build",
|
|
62
66
|
"prettier-check": "prettier --check \"./**/*.ts*\"",
|