sarvam-ai-sdk 0.0.5 → 0.0.6

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
@@ -71,7 +71,7 @@ console.log(text); // പാചകം തുടരും സുഹൃത്ത
71
71
 
72
72
  ## Translation
73
73
 
74
- > Only transliterates `prompt` and `role:user` messages, not `system` not `assistant`.
74
+ > NB: Only transliterates `prompt` and `role:user` messages, not `system` not `assistant`.
75
75
 
76
76
  ```ts
77
77
  import { sarvam } from "sarvam-ai-sdk";
@@ -90,7 +90,7 @@ console.log(result.text); // Shouldn't we be careful about this, Ambane?
90
90
 
91
91
  ## Transliterate
92
92
 
93
- > Only transliterates `prompt` and `role:user` messages, not `system` not `assistant`.
93
+ > NB: Only transliterates `prompt` and `role:user` messages, not `system` not `assistant`.
94
94
 
95
95
  ```ts
96
96
  import { sarvam } from "sarvam-ai-sdk";
@@ -107,6 +107,22 @@ const result = await generateText({
107
107
  console.log(result.text); // എടാ മോനെ, ഹാപ്പി അല്ലേ?
108
108
  ```
109
109
 
110
+ ## Language Identification
111
+
112
+ > NB: Only identifies `prompt` and `role:user` messages, not `system` not `assistant`.
113
+
114
+ ```ts
115
+ import { sarvam } from "sarvam-ai-sdk";
116
+ import { generateText } from "ai";
117
+
118
+ const result = await generateText({
119
+ model: sarvam.languageIdentification(),
120
+ prompt: "ബുദ്ധിയാണ് സാറേ ഇവൻ്റെ മെയിൻ",
121
+ });
122
+
123
+ console.log(result.text); // ml-IN
124
+ ```
125
+
110
126
  ## Tool Calling
111
127
 
112
128
  > [!WARNING]
@@ -120,7 +136,7 @@ import { sarvam } from "sarvam-ai-sdk";
120
136
 
121
137
  const result = await generateText({
122
138
  model: sarvam("sarvam-m", {
123
- simulateToolCalling: true, // ⚠️ important
139
+ simulate: "tool-calling" // ⚠️ important
124
140
  }),
125
141
  tools: {
126
142
  weather: tool({
@@ -140,6 +156,32 @@ const result = await generateText({
140
156
 
141
157
  console.log(result.toolResults);
142
158
  ```
159
+ ## Generate JSON object
160
+
161
+ > [!WARNING]
162
+ > Latest `sarvam-m` model isn't trained on native JSON object generation. So we simulate this with prompt engineering technique.
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-m", {
171
+ simulate: "json-object" // ⚠️ important
172
+ }),
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
+ ```
143
185
 
144
186
  ## Documentation
145
187
 
package/dist/index.cjs CHANGED
@@ -55,7 +55,7 @@ __export(index_exports, {
55
55
  module.exports = __toCommonJS(index_exports);
56
56
 
57
57
  // src/sarvam-provider.ts
58
- var import_provider_utils9 = require("@ai-sdk/provider-utils");
58
+ var import_provider_utils10 = require("@ai-sdk/provider-utils");
59
59
 
60
60
  // src/sarvam-chat-language-model.ts
61
61
  var import_provider3 = require("@ai-sdk/provider");
@@ -300,24 +300,29 @@ const myChoice: YourToolChoices = {
300
300
  }`;
301
301
  return text;
302
302
  };
303
- var extractToolCallData = (text) => {
303
+ var extractToolCallData = (jsonObject) => {
304
+ const toolFunction = jsonObject;
305
+ if (!("toolName" in toolFunction)) return;
306
+ if (!("toolData" in toolFunction)) return;
307
+ return {
308
+ args: JSON.stringify(toolFunction.toolData),
309
+ toolCallId: (0, import_provider_utils3.generateId)(),
310
+ toolCallType: "function",
311
+ toolName: toolFunction.toolName
312
+ };
313
+ };
314
+ var parseJSON = (text) => {
304
315
  const jsonRegex = /\{(?:[^{}]*|\{[^{}]*\})*\}/g;
305
316
  const jsonMatches = text.match(jsonRegex);
306
317
  if (jsonMatches && jsonMatches[0]) {
307
318
  try {
308
- const toolFunction = JSON.parse(jsonMatches[0]);
309
- if (!("toolName" in toolFunction)) return;
310
- if (!("toolData" in toolFunction)) return;
311
- return {
312
- args: JSON.stringify(toolFunction.toolData),
313
- toolCallId: (0, import_provider_utils3.generateId)(),
314
- toolCallType: "function",
315
- toolName: toolFunction.toolName
316
- };
319
+ const jsonObject = JSON.parse(jsonMatches[0]);
320
+ return jsonObject;
317
321
  } catch (error) {
318
322
  }
319
323
  }
320
324
  };
325
+ var simulateJsonSchema = () => "If user doen't specify, make sure to translate json data content into pure English.";
321
326
 
322
327
  // src/sarvam-chat-language-model.ts
323
328
  var SarvamChatLanguageModel = class {
@@ -351,7 +356,18 @@ var SarvamChatLanguageModel = class {
351
356
  providerMetadata
352
357
  }) {
353
358
  const type = mode.type;
359
+ const simulate = this.settings.simulate;
360
+ if (type === "object-json" && simulate === "tool-calling")
361
+ throw new Error('Use { simulate: "json-object" } with generateObject()');
362
+ if (type === "regular" && simulate === "json-object")
363
+ throw new Error('Use { simulate: "tool-calling" } with generateText()');
354
364
  const warnings = [];
365
+ if (stream) {
366
+ warnings.push({
367
+ type: "other",
368
+ message: "Streaming is still experimental for Sarvam"
369
+ });
370
+ }
355
371
  if (topK != null) {
356
372
  warnings.push({
357
373
  type: "unsupported-setting",
@@ -372,7 +388,7 @@ var SarvamChatLanguageModel = class {
372
388
  reasoningFormat: import_zod2.z.enum(["parsed", "raw", "hidden"]).nullish()
373
389
  })
374
390
  });
375
- const baseArgs = (prompt2, fakeToolSystemPrompt) => ({
391
+ const baseArgs = (prompt2, extraSystemPrompt) => ({
376
392
  // model id:
377
393
  model: this.modelId,
378
394
  // model specific settings:
@@ -394,16 +410,16 @@ var SarvamChatLanguageModel = class {
394
410
  // provider options:
395
411
  reasoning_format: sarvamOptions == null ? void 0 : sarvamOptions.reasoningFormat,
396
412
  // messages:
397
- messages: convertToSarvamChatMessages(prompt2, fakeToolSystemPrompt)
413
+ messages: convertToSarvamChatMessages(prompt2, extraSystemPrompt)
398
414
  });
399
415
  switch (type) {
400
416
  case "regular": {
401
417
  const { tools, tool_choice, toolWarnings } = prepareTools({
402
418
  mode
403
419
  });
404
- const fakeSystemPrompt = tools && this.settings.simulateToolCalling ? await simulateToolCalling(tools) : void 0;
420
+ const extraSystemPrompt = tools && simulate === "tool-calling" ? await simulateToolCalling(tools) : void 0;
405
421
  return {
406
- args: __spreadProps(__spreadValues({}, baseArgs(prompt, fakeSystemPrompt)), {
422
+ args: __spreadProps(__spreadValues({}, baseArgs(prompt, extraSystemPrompt)), {
407
423
  tools,
408
424
  tool_choice
409
425
  }),
@@ -411,8 +427,9 @@ var SarvamChatLanguageModel = class {
411
427
  };
412
428
  }
413
429
  case "object-json": {
430
+ const extraSystemPrompt = simulate === "json-object" ? simulateJsonSchema() : void 0;
414
431
  return {
415
- args: __spreadProps(__spreadValues({}, baseArgs(prompt)), {
432
+ args: __spreadProps(__spreadValues({}, baseArgs(prompt, extraSystemPrompt)), {
416
433
  response_format: (
417
434
  // json object response format is not supported for streaming:
418
435
  stream === false ? { type: "json_object" } : void 0
@@ -484,12 +501,24 @@ var SarvamChatLanguageModel = class {
484
501
  args: toolCall.function.arguments
485
502
  };
486
503
  });
487
- if (this.settings.simulateToolCalling) {
488
- if (text && text.length !== 0 && (!toolCalls || (toolCalls == null ? void 0 : toolCalls.length) === 0)) {
489
- const newTools = extractToolCallData(text);
490
- if (newTools) {
491
- toolCalls = [newTools];
492
- text = void 0;
504
+ if (this.settings.simulate === "tool-calling") {
505
+ if (text && text.length !== 0) {
506
+ const jsonObject = parseJSON(text);
507
+ if (jsonObject) {
508
+ const newTools = extractToolCallData(jsonObject);
509
+ if (newTools) {
510
+ toolCalls = [newTools];
511
+ text = void 0;
512
+ }
513
+ }
514
+ }
515
+ }
516
+ if (this.settings.simulate === "json-object") {
517
+ if (text && text.length !== 0) {
518
+ const jsonObject = parseJSON(text);
519
+ if (jsonObject) {
520
+ const newTools = extractToolCallData(jsonObject);
521
+ text = JSON.stringify(jsonObject);
493
522
  }
494
523
  }
495
524
  }
@@ -1064,6 +1093,18 @@ var SarvamLanguageCodeSchema = import_zod7.z.enum([
1064
1093
  "en-IN",
1065
1094
  "gu-IN"
1066
1095
  ]);
1096
+ var SarvamScriptCodeSchema = import_zod7.z.enum([
1097
+ "Latn",
1098
+ "Deva",
1099
+ "Beng",
1100
+ "Gujr",
1101
+ "Knda",
1102
+ "Mlym",
1103
+ "Orya",
1104
+ "Guru",
1105
+ "Taml",
1106
+ "Telu"
1107
+ ]);
1067
1108
 
1068
1109
  // src/sarvam-translation-model.ts
1069
1110
  var SarvamTranslationModel = class {
@@ -1267,11 +1308,98 @@ var sarvamTransliterateResponseSchema = import_zod9.z.object({
1267
1308
  request_id: import_zod9.z.string().nullish()
1268
1309
  });
1269
1310
 
1311
+ // src/sarvam-lid-model.ts
1312
+ var import_provider_utils9 = require("@ai-sdk/provider-utils");
1313
+ var import_zod10 = require("zod");
1314
+ var SarvamLidModel = class {
1315
+ constructor(config) {
1316
+ this.specificationVersion = "v1";
1317
+ this.supportsStructuredOutputs = false;
1318
+ this.defaultObjectGenerationMode = "json";
1319
+ this.modelId = "unknown";
1320
+ this.config = config;
1321
+ }
1322
+ get provider() {
1323
+ return this.config.provider;
1324
+ }
1325
+ get supportsImageUrls() {
1326
+ return false;
1327
+ }
1328
+ getArgs({
1329
+ mode,
1330
+ prompt
1331
+ }) {
1332
+ const type = mode.type;
1333
+ const warnings = [];
1334
+ if (type !== "regular") {
1335
+ const _exhaustiveCheck = type;
1336
+ throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
1337
+ }
1338
+ const messages = convertToSarvamChatMessages(prompt);
1339
+ return {
1340
+ messages,
1341
+ args: {
1342
+ input: messages.filter((m) => m.role === "user").map((m) => m.content).join("\n")
1343
+ },
1344
+ warnings
1345
+ };
1346
+ }
1347
+ async doGenerate(options) {
1348
+ var _b;
1349
+ const { args, warnings, messages } = this.getArgs(__spreadProps(__spreadValues({}, options), {
1350
+ stream: false
1351
+ }));
1352
+ const body = JSON.stringify(args);
1353
+ const {
1354
+ responseHeaders,
1355
+ value: response,
1356
+ rawValue: rawResponse
1357
+ } = await (0, import_provider_utils9.postJsonToApi)({
1358
+ url: this.config.url({
1359
+ path: "/text-lid"
1360
+ }),
1361
+ headers: (0, import_provider_utils9.combineHeaders)(this.config.headers(), options.headers),
1362
+ body: args,
1363
+ failedResponseHandler: sarvamFailedResponseHandler,
1364
+ successfulResponseHandler: (0, import_provider_utils9.createJsonResponseHandler)(
1365
+ sarvamLidResponseSchema
1366
+ ),
1367
+ abortSignal: options.abortSignal,
1368
+ fetch: this.config.fetch
1369
+ });
1370
+ const _a = args, { input: rawPrompt } = _a, rawSettings = __objRest(_a, ["input"]);
1371
+ const text = (_b = response.language_code) != null ? _b : void 0;
1372
+ return {
1373
+ text,
1374
+ toolCalls: void 0,
1375
+ reasoning: void 0,
1376
+ finishReason: "unknown",
1377
+ usage: {
1378
+ promptTokens: NaN,
1379
+ completionTokens: NaN
1380
+ },
1381
+ rawCall: { rawPrompt, rawSettings },
1382
+ rawResponse: { headers: responseHeaders, body: rawResponse },
1383
+ response: void 0,
1384
+ warnings,
1385
+ request: { body }
1386
+ };
1387
+ }
1388
+ async doStream(options) {
1389
+ throw new Error("Language Identification feature doesn't streaming yet");
1390
+ }
1391
+ };
1392
+ var sarvamLidResponseSchema = import_zod10.z.object({
1393
+ script_code: SarvamScriptCodeSchema.nullish(),
1394
+ language_code: SarvamLanguageCodeSchema.nullable(),
1395
+ request_id: import_zod10.z.string().nullish()
1396
+ });
1397
+
1270
1398
  // src/sarvam-provider.ts
1271
1399
  function createSarvam(options = {}) {
1272
1400
  var _a;
1273
- const baseURL = (_a = (0, import_provider_utils9.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://api.sarvam.ai";
1274
- const ApiKey = (0, import_provider_utils9.loadApiKey)({
1401
+ const baseURL = (_a = (0, import_provider_utils10.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://api.sarvam.ai";
1402
+ const ApiKey = (0, import_provider_utils10.loadApiKey)({
1275
1403
  apiKey: options.apiKey,
1276
1404
  environmentVariableName: "SARVAM_API_KEY",
1277
1405
  description: "Sarvam"
@@ -1334,6 +1462,14 @@ function createSarvam(options = {}) {
1334
1462
  fetch: options.fetch
1335
1463
  }
1336
1464
  );
1465
+ const createLidModel = () => new SarvamLidModel(
1466
+ {
1467
+ provider: "sarvam.lid",
1468
+ url: ({ path }) => `${baseURL}${path}`,
1469
+ headers: getHeaders,
1470
+ fetch: options.fetch
1471
+ }
1472
+ );
1337
1473
  const provider = (modelId, settings) => createLanguageModel(modelId, settings);
1338
1474
  provider.languageModel = createLanguageModel;
1339
1475
  provider.chat = createChatModel;
@@ -1341,6 +1477,7 @@ function createSarvam(options = {}) {
1341
1477
  provider.speech = createSpeechModel;
1342
1478
  provider.transliterate = createTransliterateModel;
1343
1479
  provider.translation = createTranslationModel;
1480
+ provider.languageIdentification = createLidModel;
1344
1481
  return provider;
1345
1482
  }
1346
1483
  var sarvam = createSarvam();