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