sarvam-ai-sdk 0.1.3 → 0.1.5-beta
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 +246 -248
- package/dist/index.d.mts +464 -373
- package/dist/index.d.ts +464 -373
- package/dist/index.js +1337 -1552
- package/dist/index.mjs +1317 -1574
- package/package.json +22 -19
- package/CHANGELOG.md +0 -7
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -1,248 +1,246 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
>
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
import {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
import {
|
|
49
|
-
import {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
import {
|
|
65
|
-
import {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
import {
|
|
78
|
-
import {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
import {
|
|
93
|
-
import {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
import {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
"
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
import {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
to: "ml-IN",
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
import {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
import {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}),
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
## Generate JSON object
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
Please check out the **[Sarvam provider documentation](https://v4.ai-sdk.dev/providers/ai-sdk-providers/sarvam)** and **[Sarvam API documentation](https://docs.sarvam.ai)** for more information.
|
|
1
|
+
# Package
|
|
2
|
+
# AI SDK - Sarvam Provider
|
|
3
|
+
|
|
4
|
+
The **[Sarvam provider](https://v4.ai-sdk.dev/providers/ai-sdk-providers/sarvam)** for the [AI SDK](https://v4.ai-sdk.dev/docs)
|
|
5
|
+
contains language model support for the Sarvam chat completion, Text-to-Speech and Speech-to-Text APIs.
|
|
6
|
+
|
|
7
|
+
## Setup
|
|
8
|
+
|
|
9
|
+
The **[Sarvam](http://sarvam.ai)** provider is available in the `sarvam-ai-sdk` module. You can install it with
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm i sarvam-ai-sdk ai@4
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
> [!WARNING]
|
|
16
|
+
> This package only works with Vercel AI-SDK v4, not latest v6. Make sure to install `ai@4` in your project.
|
|
17
|
+
|
|
18
|
+
## Provider Instance
|
|
19
|
+
|
|
20
|
+
You can import the default provider instance `sarvam` from `sarvam-ai-sdk`:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { sarvam } from 'sarvam-ai-sdk';
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Create `.env` file with API key from **[Sarvam Dashboard](https://dashboard.sarvam.ai/)**
|
|
27
|
+
```bash
|
|
28
|
+
SARVAM_API_KEY="your_api_key"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Example
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { sarvam } from 'sarvam-ai-sdk';
|
|
35
|
+
import { generateText } from 'ai';
|
|
36
|
+
|
|
37
|
+
const { text } = await generateText({
|
|
38
|
+
model: sarvam("sarvam-30b"),
|
|
39
|
+
prompt: "Translate this to malayalam: 'Keep cooking, guys'",
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
console.log(text); // പാചകം തുടരൂ, സുഹൃത്തുക്കളേ
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Text-to-Speech
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { sarvam } from "sarvam-ai-sdk";
|
|
49
|
+
import { experimental_generateSpeech as generateSpeech } from "ai";
|
|
50
|
+
import { writeFile } from "fs/promises";
|
|
51
|
+
|
|
52
|
+
const { audio } = await generateSpeech({
|
|
53
|
+
model: sarvam.speech("bulbul:v3", "ml-IN"),
|
|
54
|
+
text: "പാചകം തുടരൂ, സുഹൃത്തുക്കളേ",
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const audioBuffer = Buffer.from(audio.base64, "base64")
|
|
58
|
+
await writeFile("./src/transcript-test.wav", audioBuffer);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Speech-to-Text
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { sarvam } from "sarvam-ai-sdk";
|
|
65
|
+
import { experimental_transcribe as transcribe } from "ai";
|
|
66
|
+
import { readFile } from "fs/promises";
|
|
67
|
+
|
|
68
|
+
const { text } = await transcribe({
|
|
69
|
+
model: sarvam.transcription("saarika:v2.5", "ml-IN"),
|
|
70
|
+
audio: await readFile("./src/transcript-test.wav"),
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
console.log(text); // പാചകം തുടരും സുഹൃത്തുക്കളെ
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { sarvam } from "sarvam-ai-sdk";
|
|
78
|
+
import { experimental_transcribe as transcribe } from "ai";
|
|
79
|
+
import { readFile } from "fs/promises";
|
|
80
|
+
|
|
81
|
+
const { text } = await transcribe({
|
|
82
|
+
model: sarvam.transcription("saaras:v3", "en-IN"),
|
|
83
|
+
audio: await readFile("./src/transcript-test.wav"),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
console.log(text); // Pachakam thudaroo, suhruthukkale.
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Speech-to-Text-Translate
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { sarvam } from "sarvam-ai-sdk";
|
|
93
|
+
import { experimental_transcribe as transcribe } from "ai";
|
|
94
|
+
import { readFile } from "fs/promises";
|
|
95
|
+
|
|
96
|
+
const result = await transcribe({
|
|
97
|
+
model: sarvam.speechTranslation("saaras:v2.5"),
|
|
98
|
+
audio: await readFile("./src/transcript-test.wav"),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
console.log(result.text); // Cooking continues, my friends
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Translation
|
|
105
|
+
|
|
106
|
+
> NB: Only transliterates `prompt` and `role:user` messages, not `role:system` not `role:assistant`.
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
import { sarvam } from "sarvam-ai-sdk";
|
|
110
|
+
import { generateText } from "ai";
|
|
111
|
+
|
|
112
|
+
const result = await generateText({
|
|
113
|
+
model: sarvam.translation("mayura:v1", {
|
|
114
|
+
"from": "ml-IN",
|
|
115
|
+
"to": "en-IN",
|
|
116
|
+
}),
|
|
117
|
+
prompt: "ഇതൊക്കെ ശ്രദ്ധിക്കണ്ടേ അംബാനെ?",
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
console.log(result.text); // Shouldn't we be careful about this, Ambane?
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Transliterate
|
|
124
|
+
|
|
125
|
+
> NB: Only transliterates `prompt` and `role:user` messages, not `role:system` not `role:assistant`.
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
import { sarvam } from "sarvam-ai-sdk";
|
|
129
|
+
import { generateText } from "ai";
|
|
130
|
+
|
|
131
|
+
const result = await generateText({
|
|
132
|
+
model: sarvam.transliterate({
|
|
133
|
+
to: "ml-IN",
|
|
134
|
+
from: "en-IN", // optional
|
|
135
|
+
}),
|
|
136
|
+
prompt: "eda mone, happy alle?",
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
console.log(result.text); // എടാ മോനെ, ഹാപ്പി അല്ലേ?
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Language Identification
|
|
143
|
+
|
|
144
|
+
> NB: Only identifies `prompt` and `role:user` messages, not `role:system` not `role:assistant`.
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
import { sarvam } from "sarvam-ai-sdk";
|
|
148
|
+
import { generateText } from "ai";
|
|
149
|
+
|
|
150
|
+
const result = await generateText({
|
|
151
|
+
model: sarvam.languageIdentification(),
|
|
152
|
+
prompt: "ബുദ്ധിയാണ് സാറേ ഇവൻ്റെ മെയിൻ",
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
console.log(result.text); // ml-IN
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Tool Calling
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
import { z } from "zod";
|
|
162
|
+
import { generateText, tool } from "ai";
|
|
163
|
+
import { sarvam } from "sarvam-ai-sdk";
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
const result = await generateText({
|
|
167
|
+
model: sarvam("sarvam-30b"),
|
|
168
|
+
tools: {
|
|
169
|
+
weather: tool({
|
|
170
|
+
description: "Get the weather in a location",
|
|
171
|
+
parameters: z.object({
|
|
172
|
+
location: z.string().describe("The location to get the weather for"),
|
|
173
|
+
}),
|
|
174
|
+
execute: async ({ location }) => ({
|
|
175
|
+
location,
|
|
176
|
+
temperature: 72 + Math.floor(Math.random() * 21) - 10,
|
|
177
|
+
}),
|
|
178
|
+
}),
|
|
179
|
+
},
|
|
180
|
+
system: "Your are a helpful AI",
|
|
181
|
+
prompt: "കൊച്ചിയിലെ കാലാവസ്ഥ എന്താണ്?",
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
console.log(result.toolResults);
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
> [!WARNING]
|
|
188
|
+
> Old `sarvam-m` models isn't trained on native tool calling feature (aka JSON mode). So we recommend using latest models.
|
|
189
|
+
|
|
190
|
+
## Generate JSON object
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
import { z } from "zod";
|
|
194
|
+
import { sarvam } from "sarvam-ai-sdk";
|
|
195
|
+
import { generateObject } from 'ai';
|
|
196
|
+
|
|
197
|
+
const { object } = await generateObject({
|
|
198
|
+
model: sarvam("sarvam-30b"),
|
|
199
|
+
schema: z.object({
|
|
200
|
+
recipe: z.object({
|
|
201
|
+
name: z.string(),
|
|
202
|
+
ingredients: z.array(z.string()),
|
|
203
|
+
steps: z.array(z.string()),
|
|
204
|
+
}),
|
|
205
|
+
}),
|
|
206
|
+
prompt: 'Generate a South Indian recipe, in Malayalam',
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
console.log(object);
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
> [!WARNING]
|
|
213
|
+
> Old `sarvam-m` models isn't trained on native JSON object generation. So we recommend using latest models.
|
|
214
|
+
|
|
215
|
+
## All APIs
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
import { sarvam } from "sarvam-ai-sdk";
|
|
219
|
+
|
|
220
|
+
// Text-to-Text + Chat Completion
|
|
221
|
+
sarvam("sarvam-105b");
|
|
222
|
+
sarvam.languageModel("sarvam-30b");
|
|
223
|
+
|
|
224
|
+
// Text-to-Text + Transliteration
|
|
225
|
+
sarvam.transliterate({ to: "ml-IN", from: "en-IN" });
|
|
226
|
+
|
|
227
|
+
// Text-to-Text + Translation
|
|
228
|
+
sarvam.translation("mayura:v1", { from: "en-IN", to: "ml-IN" });
|
|
229
|
+
|
|
230
|
+
// Text-to-Text + Language identification
|
|
231
|
+
sarvam.languageIdentification();
|
|
232
|
+
|
|
233
|
+
// Text-to-Speech
|
|
234
|
+
sarvam.speech("bulbul:v3", "ml-IN");
|
|
235
|
+
|
|
236
|
+
// Speech-to-Text + Transcribe to same language
|
|
237
|
+
sarvam.transcription("saarika:v2.5");
|
|
238
|
+
|
|
239
|
+
// Speech-to-Text + Translate to English
|
|
240
|
+
sarvam.speechTranslation("saaras:v3");
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
## Documentation
|
|
245
|
+
|
|
246
|
+
Please check out the **[Sarvam provider documentation](https://v4.ai-sdk.dev/providers/ai-sdk-providers/sarvam)** and **[Sarvam API documentation](https://docs.sarvam.ai)** for more information.
|