UNPKG

sarvam-ai-sdk

Version:

Sarvam provider support for Vercel's AI-SDK

513 lines (511 loc) 16.7 kB
import { FetchFunction } from "@ai-sdk/provider-utils"; import { LanguageModelV4, SpeechModelV4, TranscriptionModelV4 } from "@ai-sdk/provider"; import { z } from "zod"; //#region src/config.d.ts /** * Specifies the language in BCP-47 format. */ type SarvamLanguageCode = z.infer<typeof SarvamLanguageCodeSchema>; declare const SarvamLanguageCodeSchema: z.ZodEnum<{ "hi-IN": "hi-IN"; "bn-IN": "bn-IN"; "kn-IN": "kn-IN"; "ml-IN": "ml-IN"; "mr-IN": "mr-IN"; "od-IN": "od-IN"; "pa-IN": "pa-IN"; "ta-IN": "ta-IN"; "te-IN": "te-IN"; "en-IN": "en-IN"; "gu-IN": "gu-IN"; }>; type MoreSarvamLanguageCode = z.infer<typeof MoreSarvamLanguageCodeSchema>; declare const MoreSarvamLanguageCodeSchema: z.ZodEnum<{ "as-IN": "as-IN"; "ur-IN": "ur-IN"; "ne-IN": "ne-IN"; "kok-IN": "kok-IN"; "ks-IN": "ks-IN"; "sd-IN": "sd-IN"; "sa-IN": "sa-IN"; "sat-IN": "sat-IN"; "mni-IN": "mni-IN"; "brx-IN": "brx-IN"; "mai-IN": "mai-IN"; "doi-IN": "doi-IN"; }>; interface SarvamProviderSettings { /** * URL for the Sarvam API calls. * @default https://api.sarvam.ai */ baseURL?: string; /** * API key for authenticating requests. * @default process.env.SARVAM_API_KEY */ apiKey?: string; /** * Custom headers to include in the requests. * @default * Authorization: `Bearer ${process.env.SARVAM_API_KEY}`, * "api-subscription-key": process.env.SARVAM_API_KEY */ headers?: Record<string, string>; /** * Custom fetch implementation. You can use it as a middleware to intercept requests, * or to provide a custom fetch implementation for e.g. testing. */ fetch?: FetchFunction; } //#endregion //#region src/chat/settings.d.ts /** * @description Production models * @see https://docs.sarvam.ai/api-reference-docs/chat/chat-completions */ type ChatModelId = "sarvam-30b" | "sarvam-105b" | (string & {}); type ChatSettings = { /** * The effort to use for reasoning. * * Can be disabled by explicitly setting to "none". * * @default "medium" */ reasoning_effort?: "none" | "low" | "medium" | "high"; /** * If set to true, the model response will be wiki grounded. */ wiki_grounding?: boolean; /** * How many chat completion choices to generate for each input message. * * Note that you will be charged based on the number of generated tokens across all of the choices. * Keep `n` as `1` to minimize costs. */ n?: number; /** * Enables structured outputs, with or without a specified JSON schema. * * Early & Experimental, Sarvam model might not perform well. * * @example * true: JSON is generated with response_format * false: JSON is generated through tool calling argument * * @default false */ experimental_json_mode?: boolean; }; //#endregion //#region src/stt/transcription-settings.d.ts /** * - `saaras:v3`: State-of-the-art model with 23-language support and flexible output formats. * Supports multiple modes via the mode parameter: transcribe, translate, verbatim, translit, codemix. */ type TranscriptionModelId = "saaras:v3" | (string & {}); type TranscriptionSettings<T extends TranscriptionModelId = TranscriptionModelId> = { /** * Mode of operation. * * @default "transcribe" * * @description * - `transcribe`: Standard transcription in the original language, `output`: Text in source language * - `translate`: Transcribe and translate to English, `output`: English text * - `verbatim`: Word-for-word transcription including filler words and repetitions, `output`: Verbatim text in source language * - `translit`: Transcribe and transliterate to Roman script, `output`: Romanized text * - `codemix`: Transcribe code-mixed speech (e.g., Hindi-English) naturally, `output`: Code-mixed text */ mode?: "transcribe" | "translate" | "verbatim" | "translit" | "codemix"; /** * Chunk-level timestamp support. * Provides start and end times for each segment of text. * Useful for subtitle alignment and audio navigation. */ with_timestamps?: boolean; }; //#endregion //#region src/tts/speech-settings.d.ts /** * Specifies the speech generation model to use. * * - `bulbul:v3`: Latest model with improved quality, 30+ voices, and temperature control */ type SpeechModelId = "bulbul:v3" | (string & {}); declare const SpeakerSchema: z.ZodEnum<{ shubh: "shubh"; aditya: "aditya"; rahul: "rahul"; rohan: "rohan"; amit: "amit"; dev: "dev"; ratan: "ratan"; varun: "varun"; manan: "manan"; sumit: "sumit"; kabir: "kabir"; aayan: "aayan"; ashutosh: "ashutosh"; advait: "advait"; anand: "anand"; tarun: "tarun"; sunny: "sunny"; mani: "mani"; gokul: "gokul"; vijay: "vijay"; mohit: "mohit"; rehan: "rehan"; soham: "soham"; ritu: "ritu"; priya: "priya"; neha: "neha"; pooja: "pooja"; simran: "simran"; kavya: "kavya"; ishita: "ishita"; shreya: "shreya"; roopa: "roopa"; amelia: "amelia"; sophia: "sophia"; tanya: "tanya"; shruti: "shruti"; suhani: "suhani"; kavitha: "kavitha"; rupali: "rupali"; }>; declare const outputAudioCodecSchema: z.ZodEnum<{ mp3: "mp3"; linear16: "linear16"; mulaw: "mulaw"; alaw: "alaw"; opus: "opus"; flac: "flac"; aac: "aac"; wav: "wav"; }>; declare const speechOptionsSchema: z.ZodObject<{ speaker: z.ZodOptional<z.ZodEnum<{ shubh: "shubh"; aditya: "aditya"; rahul: "rahul"; rohan: "rohan"; amit: "amit"; dev: "dev"; ratan: "ratan"; varun: "varun"; manan: "manan"; sumit: "sumit"; kabir: "kabir"; aayan: "aayan"; ashutosh: "ashutosh"; advait: "advait"; anand: "anand"; tarun: "tarun"; sunny: "sunny"; mani: "mani"; gokul: "gokul"; vijay: "vijay"; mohit: "mohit"; rehan: "rehan"; soham: "soham"; ritu: "ritu"; priya: "priya"; neha: "neha"; pooja: "pooja"; simran: "simran"; kavya: "kavya"; ishita: "ishita"; shreya: "shreya"; roopa: "roopa"; amelia: "amelia"; sophia: "sophia"; tanya: "tanya"; shruti: "shruti"; suhani: "suhani"; kavitha: "kavitha"; rupali: "rupali"; }>>; pace: z.ZodOptional<z.ZodNumber>; speech_sample_rate: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<8000>, z.ZodLiteral<16000>, z.ZodLiteral<22050>, z.ZodLiteral<24000>, z.ZodLiteral<32000>, z.ZodLiteral<44100>, z.ZodLiteral<48000>]>>; output_audio_codec: z.ZodOptional<z.ZodEnum<{ mp3: "mp3"; linear16: "linear16"; mulaw: "mulaw"; alaw: "alaw"; opus: "opus"; flac: "flac"; aac: "aac"; wav: "wav"; }>>; temperature: z.ZodOptional<z.ZodNumber>; dict_id: z.ZodOptional<z.ZodString>; }, z.core.$strip>; /** * Configuration settings for Sarvam Text-to-Speech API (bulbul:v3). */ type SpeechSettings<T extends SpeechModelId = SpeechModelId> = { /** * The speaker voice to be used for the output audio. * * @default "shubh" */ speaker?: z.infer<typeof SpeakerSchema>; /** * Controls the speed of the audio. Range: `0.5 - 2.0` * * @default 1.0 * @example 0.5 (Slower speech) * @example 2.0 (Faster speech) */ pace?: number; /** * Specifies the sample rate of the output audio. * * @default 24000 */ speech_sample_rate?: z.infer<(typeof speechOptionsSchema)["shape"]["speech_sample_rate"]>; /** * Specifies the audio codec for the output audio file. * Different codecs offer various compression and quality characteristics. */ output_audio_codec?: z.infer<typeof outputAudioCodecSchema>; /** * Temperature controls how much randomness and expressiveness the TTS model uses while generating speech. * Lower values produce more stable and consistent output, * while higher values sound more expressive but may introduce artifacts or errors. * * Range: `0.01 - 2` * @default 0.6 */ temperature?: number; /** * The ID of a pronunciation dictionary to apply during synthesis. * When provided, matching words in the input text will be replaced with their custom pronunciations before generating speech. */ dict_id?: string; }; //#endregion //#region src/ttt/translation-settings.d.ts /** * Specifies the translation model to use. * * - `mayura:v1`: Supports 12 languages with all modes, output scripts, and automatic language detection. * - `sarvam-translate:v1`: Supports all 22 scheduled languages of India, formal mode only */ type TranslationModelId = "mayura:v1" | "sarvam-translate:v1" | (string & {}); type TranslationSettings<T extends TranslationModelId = TranslationModelId> = { /** * The language code of the input text. This specifies the source language for transliteration. * * @default "auto" * `mayura:v1` supports automatic language detection using ‘auto’ as the source language code. */ from?: SarvamLanguageCode | (T extends "mayura:v1" ? "auto" : never) | (T extends "sarvam-translate:v1" ? MoreSarvamLanguageCode : never); /** * The language code of the transliteration text. This specifies the target language for transliteration. */ to: SarvamLanguageCode | (T extends "sarvam-translate:v1" ? MoreSarvamLanguageCode : never); /** * If `international` format is selected, we use regular numerals (0-9). For example: मेरा phone number है: 9840950950 * * If `native` format is selected, we use language-specific native numerals, like: मेरा phone number है: ९८४०९५०९५० * * @default "international" */ numerals_format?: "native" | "international"; /** * Specifies the gender of the speaker for better translations. * This feature is only supported for code-mixed translation models. * * @example Input: "मैंने कहा कि मैं आऊंगा।" Output (male): "I said that I will come." Output (female): "I said that I will come." */ speaker_gender?: "Male" | "Female"; /** * Specifies the tone or style of the translation. * * @example Input: "आप कैसे हैं?" Output (formal): "How are you?" Output (modern-colloquial): "What's up?" Output (classic-colloquial): "How art thou?" Output (code-mixed): "How are you, bhai?" * @default "formal" */ mode?: "formal" | (T extends "mayura:v1" ? "modern-colloquial" | "classic-colloquial" | "code-mixed" : never); /** * Enables custom preprocessing of the input text, which can result in better translations. * * @default false */ enable_preprocessing?: boolean; /** * Controls the transliteration style applied to the output text. * * @example Input: "Your EMI of Rs. 3000 is pending." Output (roman): "aapka Rs. 3000 ka EMI pending hai." Output (fully-native): "आपका रु. 3000 का ई.एम.ऐ. पेंडिंग है।" Output (spoken-form-in-native): "आपका थ्री थाउजेंड रूपीस का ईएमअइ पेंडिंग है।" * @default null */ output_script?: "roman" | "fully-native" | "spoken-form-in-native"; }; //#endregion //#region src/ttt/transliterate-settings.d.ts type TransliterateSettings<S extends boolean = true, T extends SarvamLanguageCode = SarvamLanguageCode, F extends SarvamLanguageCode | "auto" = "auto" | "en-IN" | (S extends true ? SarvamLanguageCode : T extends "en-IN" ? SarvamLanguageCode : T)> = { /** * The language code of the input text. This specifies the source language for transliteration. * * @default "auto" */ from?: F; /** * The language code of the transliteration text. This specifies the target language for transliteration. */ to: T; /** * If `international` format is selected, we use regular numerals (0-9). For example: मेरा phone number है: 9840950950 * * If `native` format is selected, we use language-specific native numerals, like: मेरा phone number है: ९८४०९५०९५० * * @default "international" */ numerals_format?: "native" | "international"; /** * Converts text into a natural spoken form when True. Note: No effect if output language is en-IN. * @example Input: मुझे कल 9:30am को appointment है Output: मुझे कल सुबह साढ़े नौ बजे को अपॉइंटमेंट है * @default false */ spoken_form?: boolean; /** * only works when `spoken_form` is true * * If `english`, Numbers in the text will be spoken in English. * * If `native`, Numbers in the text will be spoken in the native language. * @example Input: “मेरे पास ₹200 है” Output: “मेरे पास टू हन्डर्ड रूपीस है” (If english format is selecte) “मेरे पास दो सौ रुपये है” (If native format is selected) * @default "native" */ spoken_form_numerals_language?: "english" | "native"; }; //#endregion //#region src/type.d.ts type SarvamProvider = { /** * Creates a model for text generation. * * @example * const { text } = await generateText({ * model: sarvam("sarvam-30b"), * prompt: "Translate this to malayalam: 'Keep cooking, guys'", * }); */ (modelId: ChatModelId, settings?: ChatSettings): LanguageModelV4; /** * Creates an Sarvam chat model for text generation. * * @example * const { text } = await generateText({ * model: sarvam.languageModel("sarvam-30b"), * prompt: "Translate this to malayalam: 'Keep cooking, guys'", * }); */ languageModel(modelId: ChatModelId, settings?: ChatSettings): LanguageModelV4; /** * Creates a Sarvam model for chat. * * @example * const { text } = await generateText({ * model: sarvam.chat("sarvam-30b"), * prompt: "Translate this to malayalam: 'Keep cooking, guys'", * }); */ chat(modelId: ChatModelId, settings?: ChatSettings): LanguageModelV4; /** * Creates a Sarvam model for transcription. * * @example * const { text } = await transcribe({ * model: sarvam.transcription("saaras:v4"), * audio: await readFile("./audio.wav"), * }); */ transcription<T extends TranscriptionModelId>(modelId: T, /** * Audio source language code. * Use "unknown" for automatic language detection. * * @default "unknown" */ languageCode?: SarvamLanguageCode | MoreSarvamLanguageCode | "unknown", settings?: TranscriptionSettings<T>): TranscriptionModelV4; /** * Creates a Sarvam model for speech. * @example * const { audio } = await generateSpeech({ * model: sarvam.speech("bulbul:v4", "ml-IN"), * text: "പാചകം തുടരൂ, സുഹൃത്തുക്കളേ", * }); * * await writeFile("./audio.wav", Buffer.from(audio.base64, "base64")); */ speech<T extends SpeechModelId>(modelId: T, languageCode: SarvamLanguageCode, settings?: SpeechSettings<T>): SpeechModelV4; /** * Creates an Sarvam model for transliterate. * * @example * const { text } = await generateText({ * model: sarvam.transliterate({ * to: "ml-IN", * from: "en-IN", // Optional * }), * prompt: "eda mone, happy alle?", * }); */ transliterate<T extends SarvamLanguageCode>(settings: TransliterateSettings<false, T>): LanguageModelV4; /** * Creates an Sarvam model for translation. * * @example * const { text } = await generateText({ * model: sarvam.translation("mayura:v1", { * to: "en-IN", * from: "ml-IN", // Optional * }), * prompt: "ഇതൊക്കെ ശ്രദ്ധിക്കണ്ടേ അംബാനെ?", * }); */ translation<T extends TranslationModelId>(model: T, settings: TranslationSettings<T>): LanguageModelV4; /** * Creates an Sarvam model for language identification. * * @example * const { text } = await generateText({ * model: sarvam.languageIdentification(), * prompt: "ബുദ്ധിയാണ് സാറേ ഇവൻ്റെ മെയിൻ", * }); */ languageIdentification(): LanguageModelV4; }; //#endregion //#region src/provider.d.ts /** * Create an Sarvam provider instance. */ declare function createSarvam(options?: SarvamProviderSettings): SarvamProvider; /** * Default Sarvam provider instance. */ declare const sarvam: SarvamProvider; //#endregion export { type SarvamProvider, type SarvamProviderSettings, createSarvam, sarvam };