sarvam-ai-sdk 0.1.3 → 0.1.4

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,245 @@
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
+ # 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
+ ```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
+ parameters: z.object({
171
+ location: z.string().describe("The location to get the weather for"),
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
+ > [!WARNING]
187
+ > Old `sarvam-m` models isn't trained on native tool calling feature (aka JSON mode). So we recommend using latest models.
188
+
189
+ ## Generate JSON object
190
+
191
+ ```ts
192
+ import { z } from "zod";
193
+ import { sarvam } from "sarvam-ai-sdk";
194
+ import { generateObject } from 'ai';
195
+
196
+ const { object } = await generateObject({
197
+ model: sarvam("sarvam-30b"),
198
+ schema: z.object({
199
+ recipe: z.object({
200
+ name: z.string(),
201
+ ingredients: z.array(z.string()),
202
+ steps: z.array(z.string()),
203
+ }),
204
+ }),
205
+ prompt: 'Generate a South Indian recipe, in Malayalam',
206
+ });
207
+
208
+ console.log(object);
209
+ ```
210
+
211
+ > [!WARNING]
212
+ > Old `sarvam-m` models isn't trained on native JSON object generation. So we recommend using latest models.
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({ from: "en-IN", to: "ml-IN" });
225
+
226
+ // Text-to-Text + Translation
227
+ sarvam.translation({ 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://v4.ai-sdk.dev/providers/ai-sdk-providers/sarvam)** and **[Sarvam API documentation](https://docs.sarvam.ai)** for more information.
package/dist/index.d.mts CHANGED
@@ -3,34 +3,10 @@ import { FetchFunction } from '@ai-sdk/provider-utils';
3
3
  import { z } from 'zod';
4
4
 
5
5
  /**
6
- * @description Product models
6
+ * @description Production models
7
7
  */
8
- type SarvamChatModelId = "sarvam-30b" | "sarvam-30b-16k" | "sarvam-105b" | "sarvam-105b-32k" | SarvamChatLegacyModelId | (string & {});
9
- /**
10
- * @description Legacy models
11
- * @deprecated
12
- */
13
- type SarvamChatLegacyModelId = "sarvam-m";
8
+ type SarvamChatModelId = "sarvam-30b" | "sarvam-30b-16k" | "sarvam-105b" | "sarvam-105b-32k" | (string & {});
14
9
  interface SarvamChatSettings {
15
- /**
16
- * Whether to simulate artificial tool calling or JSON object generation, because Sarvam Models doen't support native Tool Calling or JSON Schmea.
17
- * @default undefined
18
- * @example
19
- await generateText({
20
- model: sarvam("sarvam-m", {
21
- simulate: "tool-calling"
22
- })
23
- tools: {...}
24
- })
25
-
26
- await generateObject({
27
- model: sarvam("sarvam-m", {
28
- simulate: "json-object"
29
- })
30
- schema: {...}
31
- })
32
- */
33
- simulate?: "tool-calling" | "json-object";
34
10
  /**
35
11
  * Whether to enable parallel function calling during tool use.
36
12
  * @default true
package/dist/index.d.ts CHANGED
@@ -3,34 +3,10 @@ import { FetchFunction } from '@ai-sdk/provider-utils';
3
3
  import { z } from 'zod';
4
4
 
5
5
  /**
6
- * @description Product models
6
+ * @description Production models
7
7
  */
8
- type SarvamChatModelId = "sarvam-30b" | "sarvam-30b-16k" | "sarvam-105b" | "sarvam-105b-32k" | SarvamChatLegacyModelId | (string & {});
9
- /**
10
- * @description Legacy models
11
- * @deprecated
12
- */
13
- type SarvamChatLegacyModelId = "sarvam-m";
8
+ type SarvamChatModelId = "sarvam-30b" | "sarvam-30b-16k" | "sarvam-105b" | "sarvam-105b-32k" | (string & {});
14
9
  interface SarvamChatSettings {
15
- /**
16
- * Whether to simulate artificial tool calling or JSON object generation, because Sarvam Models doen't support native Tool Calling or JSON Schmea.
17
- * @default undefined
18
- * @example
19
- await generateText({
20
- model: sarvam("sarvam-m", {
21
- simulate: "tool-calling"
22
- })
23
- tools: {...}
24
- })
25
-
26
- await generateObject({
27
- model: sarvam("sarvam-m", {
28
- simulate: "json-object"
29
- })
30
- schema: {...}
31
- })
32
- */
33
- simulate?: "tool-calling" | "json-object";
34
10
  /**
35
11
  * Whether to enable parallel function calling during tool use.
36
12
  * @default true