sarvam-ai-provider
Version:
The **Sarvam AI SDK Provider** is a library developed to integrate with the Vercel AI SDK. This library brings Speech to Text (STT) capabilities to your applications, allowing for seamless interaction with audio and text data.
194 lines (189 loc) • 6.82 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
createSarvam: () => createSarvam,
sarvam: () => sarvam
});
module.exports = __toCommonJS(src_exports);
// src/sarvam-transcription-model.ts
var import_provider_utils2 = require("@ai-sdk/provider-utils");
var import_zod2 = require("zod");
// src/sarvam-error.ts
var import_zod = require("zod");
var import_provider_utils = require("@ai-sdk/provider-utils");
var SarvamErrorDataSchema = import_zod.z.object({
error: import_zod.z.object({
message: import_zod.z.string(),
code: import_zod.z.number()
})
});
var sarvamFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
errorSchema: SarvamErrorDataSchema,
errorToMessage: (data) => data.error.message
});
// src/sarvam-transcription-model.ts
var sarvamProviderOptionsSchema = import_zod2.z.object({
language_code: import_zod2.z.string(),
with_timestamps: import_zod2.z.boolean().nullish().default(false),
/**
* Enables speaker diarization, which identifies and separates different speakers in the audio.
* When set to true, the API will provide speaker-specific segments in the response.
* Note: This parameter is currently in Beta mode.
*/
with_diarization: import_zod2.z.boolean().nullish().default(false),
/**
* Number of speakers to be detected in the audio.
* This is used when with_diarization is set to true.
* Can be null.
*/
num_speakers: import_zod2.z.number().int().nullish()
});
var SarvamTranscriptionModel = class {
constructor(modelId, config) {
this.modelId = modelId;
this.config = config;
this.specificationVersion = "v1";
}
get provider() {
return this.config.provider;
}
getArgs({
audio,
mediaType,
providerOptions
}) {
const warnings = [];
const sarvamOptions = (0, import_provider_utils2.parseProviderOptions)({
provider: "sarvam",
providerOptions,
schema: sarvamProviderOptionsSchema
});
const formData = new FormData();
const blob = audio instanceof Blob ? audio : new Blob([audio], { type: mediaType });
formData.append("file", blob);
formData.append("model", this.modelId);
if (sarvamOptions) {
formData.append("language_code", sarvamOptions.language_code);
formData.append(
"with_timestamps",
sarvamOptions.with_timestamps ? "true" : "false"
);
formData.append(
"with_diarization",
sarvamOptions.with_diarization ? "true" : "false"
);
if (sarvamOptions.num_speakers !== null && sarvamOptions.num_speakers !== void 0) {
formData.append("num_speakers", sarvamOptions.num_speakers.toString());
}
}
return {
formData,
warnings
};
}
async doGenerate(options) {
var _a, _b, _c, _d, _e;
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
const { formData, warnings } = this.getArgs(options);
const {
value: response,
responseHeaders,
rawValue: rawResponse
} = await (0, import_provider_utils2.postFormDataToApi)({
url: this.config.url({
path: "/speech-to-text",
modelId: this.modelId
}),
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
formData,
failedResponseHandler: sarvamFailedResponseHandler,
successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
sarvamTranscriptionResponseSchema
),
abortSignal: options.abortSignal,
fetch: this.config.fetch
});
return {
text: response.transcript,
segments: response.timestamps ? response.timestamps.words.map((word, index) => ({
text: word,
startSecond: response.timestamps.start_time_seconds[index],
endSecond: response.timestamps.end_time_seconds[index]
})) : [],
language: response.language_code ? response.language_code : void 0,
durationInSeconds: (_e = (_d = response.timestamps) == null ? void 0 : _d.end_time_seconds[response.timestamps.end_time_seconds.length - 1]) != null ? _e : void 0,
warnings,
response: {
timestamp: currentDate,
modelId: this.modelId,
headers: responseHeaders,
body: rawResponse
}
};
}
};
var sarvamTranscriptionResponseSchema = import_zod2.z.object({
request_id: import_zod2.z.string().nullable(),
transcript: import_zod2.z.string(),
language_code: import_zod2.z.string().nullable(),
timestamps: import_zod2.z.object({
end_time_seconds: import_zod2.z.array(import_zod2.z.number()),
start_time_seconds: import_zod2.z.array(import_zod2.z.number()),
words: import_zod2.z.array(import_zod2.z.string())
}).optional(),
diarized_transcript: import_zod2.z.object({
entries: import_zod2.z.array(
import_zod2.z.object({
end_time_seconds: import_zod2.z.array(import_zod2.z.number()),
start_time_seconds: import_zod2.z.array(import_zod2.z.number()),
transcript: import_zod2.z.string(),
speaker_id: import_zod2.z.string()
})
)
}).optional()
});
// src/sarvam-provider.ts
function createSarvam(options = {}) {
const getHeaders = () => ({
...options.headers
});
const createTranscriptionModel = (modelId) => new SarvamTranscriptionModel(modelId, {
provider: `sarvam.transcription`,
url: ({ path }) => `https://api.sarvam.ai${path}`,
headers: getHeaders,
fetch: options.fetch
});
const provider = function(modelId) {
return {
transcription: createTranscriptionModel(modelId)
};
};
provider.transcription = createTranscriptionModel;
provider.transcriptionModel = createTranscriptionModel;
return provider;
}
var sarvam = createSarvam();
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createSarvam,
sarvam
});
//# sourceMappingURL=index.js.map