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 CHANGED
@@ -1,248 +1,246 @@
1
- # AI SDK - Sarvam Provider
2
-
3
- The **[Sarvam provider](https://v4.ai-sdk.dev/providers/ai-sdk-providers/sarvam)** for the [AI SDK](https://v4.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
12
- ```
13
-
14
- > [!WARNING]
15
- > This package only works with Vercel AI-SDK v4, not latest v6. Make sure to install `ai@4` 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 `system` not `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({
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 `system` not `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
- from: "en-IN",
133
- to: "ml-IN",
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 `system` not `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
- > [!WARNING]
160
- > Latest `sarvam` models isn't trained on native tool calling feature (aka JSON mode). So we simulate this with prompt engineering technique.
161
-
162
- ```ts
163
- import { z } from "zod";
164
- import { generateText, tool } from "ai";
165
- import { sarvam } from "sarvam-ai-sdk";
166
-
167
-
168
- const result = await generateText({
169
- model: sarvam("sarvam-30b", {
170
- simulate: "tool-calling" // ⚠️ important
171
- }),
172
- tools: {
173
- weather: tool({
174
- description: "Get the weather in a location",
175
- parameters: z.object({
176
- location: z.string().describe("The location to get the weather for"),
177
- }),
178
- execute: async ({ location }) => ({
179
- location,
180
- temperature: 72 + Math.floor(Math.random() * 21) - 10,
181
- }),
182
- }),
183
- },
184
- system: "Your are a helpful AI",
185
- prompt: "കൊച്ചിയിലെ കാലാവസ്ഥ എന്താണ്?",
186
- });
187
-
188
- console.log(result.toolResults);
189
- ```
190
- ## Generate JSON object
191
-
192
- > [!WARNING]
193
- > Latest `sarvam` models isn't trained on native JSON object generation. So we simulate this with prompt engineering technique.
194
-
195
- ```ts
196
- import { z } from "zod";
197
- import { sarvam } from "sarvam-ai-sdk";
198
- import { generateObject } from 'ai';
199
-
200
- const { object } = await generateObject({
201
- model: sarvam("sarvam-30b", {
202
- simulate: "json-object" // ⚠️ important
203
- }),
204
- schema: z.object({
205
- recipe: z.object({
206
- name: z.string(),
207
- ingredients: z.array(z.string()),
208
- steps: z.array(z.string()),
209
- }),
210
- }),
211
- prompt: 'Generate a South Indian recipe, in Malayalam',
212
- });
213
-
214
- console.log(object);
215
- ```
216
-
217
- ## All APIs
218
-
219
- ```ts
220
- import { sarvam } from "sarvam-ai-sdk";
221
-
222
- // Text-to-Text + Chat Completion
223
- sarvam("sarvam-105b");
224
- sarvam.languageModel("sarvam-30b");
225
-
226
- // Text-to-Text + Transliteration
227
- sarvam.transliterate({ from: "en-IN", to: "ml-IN" });
228
-
229
- // Text-to-Text + Translation
230
- sarvam.translation({ from: "en-IN", to: "ml-IN" });
231
-
232
- // Text-to-Text + Language identification
233
- sarvam.languageIdentification();
234
-
235
- // Text-to-Speech
236
- sarvam.speech("bulbul:v3", "ml-IN");
237
-
238
- // Speech-to-Text + Transcribe to same language
239
- sarvam.transcription("saarika:v2.5");
240
-
241
- // Speech-to-Text + Translate to English
242
- sarvam.speechTranslation("saaras:v3");
243
- ```
244
-
245
-
246
- ## Documentation
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.