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