sarvam-ai-sdk 0.3.1 → 0.3.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 +2 -2
- package/dist/index.cjs +42 -19
- package/dist/index.js +42 -19
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
<a href="https://github.com/
|
|
2
|
-
<img alt="cover" src="https://github.com/
|
|
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
3
|
</a>
|
|
4
4
|
|
|
5
5
|
# AI SDK - Sarvam Provider
|
package/dist/index.cjs
CHANGED
|
@@ -41,6 +41,20 @@ const sarvamFailedResponseHandler = (0, __ai_sdk_provider_utils.createJsonErrorR
|
|
|
41
41
|
|
|
42
42
|
//#endregion
|
|
43
43
|
//#region src/chat/convert-to-chat-messages.ts
|
|
44
|
+
function getToolResultContent(output) {
|
|
45
|
+
switch (output.type) {
|
|
46
|
+
case "text":
|
|
47
|
+
case "error-text": return output.value;
|
|
48
|
+
case "json":
|
|
49
|
+
case "error-json":
|
|
50
|
+
case "content": return JSON.stringify(output.value);
|
|
51
|
+
case "execution-denied": return output.reason ?? "Tool execution was denied.";
|
|
52
|
+
default: {
|
|
53
|
+
const _exhaustiveCheck = output;
|
|
54
|
+
return JSON.stringify(_exhaustiveCheck);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
44
58
|
function convertToChatMessages(prompt) {
|
|
45
59
|
const messages = [];
|
|
46
60
|
for (const message of prompt) switch (message.role) {
|
|
@@ -112,7 +126,7 @@ function convertToChatMessages(prompt) {
|
|
|
112
126
|
for (const part of message.content) if (part.type === "tool-result") messages.push({
|
|
113
127
|
role: "tool",
|
|
114
128
|
tool_call_id: part.toolCallId,
|
|
115
|
-
content:
|
|
129
|
+
content: getToolResultContent(part.output)
|
|
116
130
|
});
|
|
117
131
|
break;
|
|
118
132
|
default: {
|
|
@@ -367,18 +381,16 @@ var SarvamChatLanguageModel = class {
|
|
|
367
381
|
type: "reasoning",
|
|
368
382
|
text: choice.message.reasoning_content
|
|
369
383
|
});
|
|
370
|
-
if (choice.message.tool_calls && choice.message.tool_calls.length > 0) for (const toolCall of choice.message.tool_calls) {
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
});
|
|
381
|
-
}
|
|
384
|
+
if (choice.message.tool_calls && choice.message.tool_calls.length > 0) for (const toolCall of choice.message.tool_calls) if (isJSON) content.push({
|
|
385
|
+
type: "text",
|
|
386
|
+
text: toolCall.function.arguments
|
|
387
|
+
});
|
|
388
|
+
else content.push({
|
|
389
|
+
type: "tool-call",
|
|
390
|
+
toolCallId: toolCall.id ?? (this.config.generateId ?? __ai_sdk_provider_utils.generateId)(),
|
|
391
|
+
toolName: toolCall.function.name,
|
|
392
|
+
input: toolCall.function.arguments
|
|
393
|
+
});
|
|
382
394
|
return {
|
|
383
395
|
content,
|
|
384
396
|
finishReason: {
|
|
@@ -539,12 +551,21 @@ var SarvamChatLanguageModel = class {
|
|
|
539
551
|
};
|
|
540
552
|
const toolCall$1 = toolCalls[index];
|
|
541
553
|
if (toolCall$1.name != null && toolCall$1.arguments != null) {
|
|
554
|
+
controller.enqueue({
|
|
555
|
+
type: "tool-input-start",
|
|
556
|
+
id: toolCall$1.id,
|
|
557
|
+
toolName: toolCall$1.name
|
|
558
|
+
});
|
|
542
559
|
if (toolCall$1.arguments.length > 0) controller.enqueue({
|
|
543
560
|
type: "tool-input-delta",
|
|
544
561
|
id: toolCall$1.id,
|
|
545
562
|
delta: toolCall$1.arguments
|
|
546
563
|
});
|
|
547
564
|
if ((0, __ai_sdk_provider_utils.isParsableJson)(toolCall$1.arguments)) {
|
|
565
|
+
controller.enqueue({
|
|
566
|
+
type: "tool-input-end",
|
|
567
|
+
id: toolCall$1.id
|
|
568
|
+
});
|
|
548
569
|
controller.enqueue({
|
|
549
570
|
type: "tool-call",
|
|
550
571
|
toolCallId: toolCall$1.id,
|
|
@@ -565,6 +586,10 @@ var SarvamChatLanguageModel = class {
|
|
|
565
586
|
delta: toolCallDelta.function.arguments ?? ""
|
|
566
587
|
});
|
|
567
588
|
if (toolCall.name != null && toolCall.arguments != null && (0, __ai_sdk_provider_utils.isParsableJson)(toolCall.arguments)) {
|
|
589
|
+
controller.enqueue({
|
|
590
|
+
type: "tool-input-end",
|
|
591
|
+
id: toolCall.id
|
|
592
|
+
});
|
|
568
593
|
controller.enqueue({
|
|
569
594
|
type: "tool-call",
|
|
570
595
|
toolCallId: toolCall.id,
|
|
@@ -1047,8 +1072,7 @@ var SarvamTranslationModel = class {
|
|
|
1047
1072
|
schema: translationSettingsSchema
|
|
1048
1073
|
});
|
|
1049
1074
|
if (!sarvamOptions) throw new Error("Translation Settings is not provided");
|
|
1050
|
-
const from =
|
|
1051
|
-
const to = sarvamOptions.to;
|
|
1075
|
+
const { from = "auto", to,...restOptions } = sarvamOptions;
|
|
1052
1076
|
if (from === to) throw new Error("Source and target languages code must be different.");
|
|
1053
1077
|
if (this.modelId === "sarvam-translate:v1") {
|
|
1054
1078
|
if ((sarvamOptions.mode ?? "formal") !== "formal") throw new Error("Sarvam 'sarvam-translate:v1' only support mode formal.");
|
|
@@ -1058,7 +1082,7 @@ var SarvamTranslationModel = class {
|
|
|
1058
1082
|
args: {
|
|
1059
1083
|
input: convertPromptToInput(prompt),
|
|
1060
1084
|
model: this.modelId,
|
|
1061
|
-
...
|
|
1085
|
+
...restOptions,
|
|
1062
1086
|
source_language_code: from,
|
|
1063
1087
|
target_language_code: to
|
|
1064
1088
|
},
|
|
@@ -1164,8 +1188,7 @@ var SarvamTransliterateModel = class {
|
|
|
1164
1188
|
schema: transliterateSettingsSchema
|
|
1165
1189
|
});
|
|
1166
1190
|
if (!sarvamOptions) throw new Error("Transliterate Settings is not provided");
|
|
1167
|
-
const from =
|
|
1168
|
-
const to = sarvamOptions.to;
|
|
1191
|
+
const { from = "auto", to,...restOptions } = sarvamOptions;
|
|
1169
1192
|
if (from !== "auto") {
|
|
1170
1193
|
if (to !== "en-IN" && from !== "en-IN") {
|
|
1171
1194
|
if (to !== from) throw new Error("Sarvam doesn't support Indic-Indic Transliteration yet");
|
|
@@ -1174,7 +1197,7 @@ var SarvamTransliterateModel = class {
|
|
|
1174
1197
|
return {
|
|
1175
1198
|
args: {
|
|
1176
1199
|
input: convertPromptToInput(prompt),
|
|
1177
|
-
...
|
|
1200
|
+
...restOptions,
|
|
1178
1201
|
source_language_code: from,
|
|
1179
1202
|
target_language_code: to,
|
|
1180
1203
|
spoken_form_numerals_language: sarvamOptions.spoken_form ? sarvamOptions.spoken_form_numerals_language ?? "english" : void 0
|
package/dist/index.js
CHANGED
|
@@ -15,6 +15,20 @@ const sarvamFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
15
15
|
|
|
16
16
|
//#endregion
|
|
17
17
|
//#region src/chat/convert-to-chat-messages.ts
|
|
18
|
+
function getToolResultContent(output) {
|
|
19
|
+
switch (output.type) {
|
|
20
|
+
case "text":
|
|
21
|
+
case "error-text": return output.value;
|
|
22
|
+
case "json":
|
|
23
|
+
case "error-json":
|
|
24
|
+
case "content": return JSON.stringify(output.value);
|
|
25
|
+
case "execution-denied": return output.reason ?? "Tool execution was denied.";
|
|
26
|
+
default: {
|
|
27
|
+
const _exhaustiveCheck = output;
|
|
28
|
+
return JSON.stringify(_exhaustiveCheck);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
18
32
|
function convertToChatMessages(prompt) {
|
|
19
33
|
const messages = [];
|
|
20
34
|
for (const message of prompt) switch (message.role) {
|
|
@@ -86,7 +100,7 @@ function convertToChatMessages(prompt) {
|
|
|
86
100
|
for (const part of message.content) if (part.type === "tool-result") messages.push({
|
|
87
101
|
role: "tool",
|
|
88
102
|
tool_call_id: part.toolCallId,
|
|
89
|
-
content:
|
|
103
|
+
content: getToolResultContent(part.output)
|
|
90
104
|
});
|
|
91
105
|
break;
|
|
92
106
|
default: {
|
|
@@ -341,18 +355,16 @@ var SarvamChatLanguageModel = class {
|
|
|
341
355
|
type: "reasoning",
|
|
342
356
|
text: choice.message.reasoning_content
|
|
343
357
|
});
|
|
344
|
-
if (choice.message.tool_calls && choice.message.tool_calls.length > 0) for (const toolCall of choice.message.tool_calls) {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
});
|
|
355
|
-
}
|
|
358
|
+
if (choice.message.tool_calls && choice.message.tool_calls.length > 0) for (const toolCall of choice.message.tool_calls) if (isJSON) content.push({
|
|
359
|
+
type: "text",
|
|
360
|
+
text: toolCall.function.arguments
|
|
361
|
+
});
|
|
362
|
+
else content.push({
|
|
363
|
+
type: "tool-call",
|
|
364
|
+
toolCallId: toolCall.id ?? (this.config.generateId ?? generateId)(),
|
|
365
|
+
toolName: toolCall.function.name,
|
|
366
|
+
input: toolCall.function.arguments
|
|
367
|
+
});
|
|
356
368
|
return {
|
|
357
369
|
content,
|
|
358
370
|
finishReason: {
|
|
@@ -513,12 +525,21 @@ var SarvamChatLanguageModel = class {
|
|
|
513
525
|
};
|
|
514
526
|
const toolCall$1 = toolCalls[index];
|
|
515
527
|
if (toolCall$1.name != null && toolCall$1.arguments != null) {
|
|
528
|
+
controller.enqueue({
|
|
529
|
+
type: "tool-input-start",
|
|
530
|
+
id: toolCall$1.id,
|
|
531
|
+
toolName: toolCall$1.name
|
|
532
|
+
});
|
|
516
533
|
if (toolCall$1.arguments.length > 0) controller.enqueue({
|
|
517
534
|
type: "tool-input-delta",
|
|
518
535
|
id: toolCall$1.id,
|
|
519
536
|
delta: toolCall$1.arguments
|
|
520
537
|
});
|
|
521
538
|
if (isParsableJson(toolCall$1.arguments)) {
|
|
539
|
+
controller.enqueue({
|
|
540
|
+
type: "tool-input-end",
|
|
541
|
+
id: toolCall$1.id
|
|
542
|
+
});
|
|
522
543
|
controller.enqueue({
|
|
523
544
|
type: "tool-call",
|
|
524
545
|
toolCallId: toolCall$1.id,
|
|
@@ -539,6 +560,10 @@ var SarvamChatLanguageModel = class {
|
|
|
539
560
|
delta: toolCallDelta.function.arguments ?? ""
|
|
540
561
|
});
|
|
541
562
|
if (toolCall.name != null && toolCall.arguments != null && isParsableJson(toolCall.arguments)) {
|
|
563
|
+
controller.enqueue({
|
|
564
|
+
type: "tool-input-end",
|
|
565
|
+
id: toolCall.id
|
|
566
|
+
});
|
|
542
567
|
controller.enqueue({
|
|
543
568
|
type: "tool-call",
|
|
544
569
|
toolCallId: toolCall.id,
|
|
@@ -1021,8 +1046,7 @@ var SarvamTranslationModel = class {
|
|
|
1021
1046
|
schema: translationSettingsSchema
|
|
1022
1047
|
});
|
|
1023
1048
|
if (!sarvamOptions) throw new Error("Translation Settings is not provided");
|
|
1024
|
-
const from =
|
|
1025
|
-
const to = sarvamOptions.to;
|
|
1049
|
+
const { from = "auto", to,...restOptions } = sarvamOptions;
|
|
1026
1050
|
if (from === to) throw new Error("Source and target languages code must be different.");
|
|
1027
1051
|
if (this.modelId === "sarvam-translate:v1") {
|
|
1028
1052
|
if ((sarvamOptions.mode ?? "formal") !== "formal") throw new Error("Sarvam 'sarvam-translate:v1' only support mode formal.");
|
|
@@ -1032,7 +1056,7 @@ var SarvamTranslationModel = class {
|
|
|
1032
1056
|
args: {
|
|
1033
1057
|
input: convertPromptToInput(prompt),
|
|
1034
1058
|
model: this.modelId,
|
|
1035
|
-
...
|
|
1059
|
+
...restOptions,
|
|
1036
1060
|
source_language_code: from,
|
|
1037
1061
|
target_language_code: to
|
|
1038
1062
|
},
|
|
@@ -1138,8 +1162,7 @@ var SarvamTransliterateModel = class {
|
|
|
1138
1162
|
schema: transliterateSettingsSchema
|
|
1139
1163
|
});
|
|
1140
1164
|
if (!sarvamOptions) throw new Error("Transliterate Settings is not provided");
|
|
1141
|
-
const from =
|
|
1142
|
-
const to = sarvamOptions.to;
|
|
1165
|
+
const { from = "auto", to,...restOptions } = sarvamOptions;
|
|
1143
1166
|
if (from !== "auto") {
|
|
1144
1167
|
if (to !== "en-IN" && from !== "en-IN") {
|
|
1145
1168
|
if (to !== from) throw new Error("Sarvam doesn't support Indic-Indic Transliteration yet");
|
|
@@ -1148,7 +1171,7 @@ var SarvamTransliterateModel = class {
|
|
|
1148
1171
|
return {
|
|
1149
1172
|
args: {
|
|
1150
1173
|
input: convertPromptToInput(prompt),
|
|
1151
|
-
...
|
|
1174
|
+
...restOptions,
|
|
1152
1175
|
source_language_code: from,
|
|
1153
1176
|
target_language_code: to,
|
|
1154
1177
|
spoken_form_numerals_language: sarvamOptions.spoken_form ? sarvamOptions.spoken_form_numerals_language ?? "english" : void 0
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "sarvam-ai-sdk",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"description": "Sarvam provider support for Vercel's AI-SDK",
|
|
5
|
-
"version": "0.3.
|
|
5
|
+
"version": "0.3.4",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
7
7
|
"module": "./dist/index.js",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"build:watch": "tsdown --watch",
|
|
15
15
|
"type-check": "tsc --noEmit",
|
|
16
16
|
"check": "biome check",
|
|
17
|
-
"test": "
|
|
17
|
+
"test": "bun test"
|
|
18
18
|
},
|
|
19
19
|
"exports": {
|
|
20
20
|
"./package.json": "./package.json",
|