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