sarvam-ai-sdk 0.3.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,245 +1,238 @@
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("saarika:v2.5", "ml-IN"),
73
- audio: await readFile("./src/transcript-test.wav"),
74
- });
75
-
76
- console.log(text); // പാചകം തുടരും സുഹൃത്തുക്കളെ
77
- ```
78
-
79
- ```ts
80
- import { sarvam } from "sarvam-ai-sdk";
81
- import { experimental_transcribe as transcribe } from "ai";
82
- import { readFile } from "fs/promises";
83
-
84
- const { text } = await transcribe({
85
- model: sarvam.transcription("saaras:v3", "en-IN"),
86
- audio: await readFile("./src/transcript-test.wav"),
87
- });
88
-
89
- console.log(text); // Pachakam thudaroo, suhruthukkale.
90
- ```
91
-
92
- ## Speech-to-Text-Translate
93
-
94
- ```ts
95
- import { sarvam } from "sarvam-ai-sdk";
96
- import { experimental_transcribe as transcribe } from "ai";
97
- import { readFile } from "fs/promises";
98
-
99
- const result = await transcribe({
100
- model: sarvam.speechTranslation("saaras:v2.5"),
101
- audio: await readFile("./src/transcript-test.wav"),
102
- });
103
-
104
- console.log(result.text); // Cooking continues, my friends
105
- ```
106
-
107
- ## Translation
108
-
109
- > NB: Only transliterates `prompt` and `role:user` messages, not `role:system` not `role:assistant`.
110
-
111
- ```ts
112
- import { sarvam } from "sarvam-ai-sdk";
113
- import { generateText } from "ai";
114
-
115
- const result = await generateText({
116
- model: sarvam.translation("mayura:v1", {
117
- "from": "ml-IN",
118
- "to": "en-IN",
119
- }),
120
- prompt: "ഇതൊക്കെ ശ്രദ്ധിക്കണ്ടേ അംബാനെ?",
121
- });
122
-
123
- console.log(result.text); // Shouldn't we be careful about this, Ambane?
124
- ```
125
-
126
- ## Transliterate
127
-
128
- > NB: Only transliterates `prompt` and `role:user` messages, not `role:system` not `role:assistant`.
129
-
130
- ```ts
131
- import { sarvam } from "sarvam-ai-sdk";
132
- import { generateText } from "ai";
133
-
134
- const result = await generateText({
135
- model: sarvam.transliterate({
136
- to: "ml-IN",
137
- from: "en-IN", // optional
138
- }),
139
- prompt: "eda mone, happy alle?",
140
- });
141
-
142
- console.log(result.text); // എടാ മോനെ, ഹാപ്പി അല്ലേ?
143
- ```
144
-
145
- ## Language Identification
146
-
147
- > NB: Only identifies `prompt` and `role:user` messages, not `role:system` not `role:assistant`.
148
-
149
- ```ts
150
- import { sarvam } from "sarvam-ai-sdk";
151
- import { generateText } from "ai";
152
-
153
- const result = await generateText({
154
- model: sarvam.languageIdentification(),
155
- prompt: "ബുദ്ധിയാണ് സാറേ ഇവൻ്റെ മെയിൻ",
156
- });
157
-
158
- console.log(result.text); // ml-IN
159
- ```
160
-
161
- ## Tool Calling
162
-
163
- ```ts
164
- import { z } from "zod";
165
- import { generateText, tool } from "ai";
166
- import { sarvam } from "sarvam-ai-sdk";
167
-
168
-
169
- const result = await generateText({
170
- model: sarvam("sarvam-30b"),
171
- tools: {
172
- weather: tool({
173
- description: "Get the weather in a location",
174
- inputSchema: z.object({
175
- location: z.string(),
176
- }),
177
- execute: async ({ location }) => ({
178
- location,
179
- temperature: 72 + Math.floor(Math.random() * 21) - 10,
180
- }),
181
- }),
182
- },
183
- system: "Your are a helpful AI",
184
- prompt: "കൊച്ചിയിലെ കാലാവസ്ഥ എന്താണ്?",
185
- });
186
-
187
- console.log(result.toolResults);
188
- ```
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
- schemaName: "Recipe",
200
- schemaDescription: "A recipe with a name, ingredients and steps",
201
- schema: z.object({
202
- recipe: z.object({
203
- name: z.string(),
204
- ingredients: z.array(z.string()),
205
- steps: z.array(z.string()),
206
- }),
207
- }),
208
- prompt: 'Generate a South Indian recipe, in Malayalam',
209
- });
210
-
211
- console.log(object);
212
- ```
213
-
214
- ## All APIs
215
-
216
- ```ts
217
- import { sarvam } from "sarvam-ai-sdk";
218
-
219
- // Text-to-Text + Chat Completion
220
- sarvam("sarvam-105b");
221
- sarvam.languageModel("sarvam-30b");
222
-
223
- // Text-to-Text + Transliteration
224
- sarvam.transliterate({ to: "ml-IN", from: "en-IN" });
225
-
226
- // Text-to-Text + Translation
227
- sarvam.translation("mayura:v1", { from: "en-IN", to: "ml-IN" });
228
-
229
- // Text-to-Text + Language identification
230
- sarvam.languageIdentification();
231
-
232
- // Text-to-Speech
233
- sarvam.speech("bulbul:v3", "ml-IN");
234
-
235
- // Speech-to-Text + Transcribe to same language
236
- sarvam.transcription("saarika:v2.5");
237
-
238
- // Speech-to-Text + Translate to English
239
- sarvam.speechTranslation("saaras:v3");
240
- ```
241
-
242
-
243
- ## Documentation
244
-
245
- 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
+ ## 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.