sarvam-ai-sdk
Version:
Sarvam AI Provider support for AI-SDK
313 lines (303 loc) • 11.3 kB
TypeScript
import { LanguageModelV1, TranscriptionModelV1, SpeechModelV1 } from '@ai-sdk/provider';
import { FetchFunction } from '@ai-sdk/provider-utils';
import { z } from 'zod';
type SarvamChatModelId = "sarvam-m" | (string & {});
interface SarvamChatSettings {
/**
* Whether to simulate artificial tool calling or JSON object generation, because Sarvam Models doen't support native Tool Calling or JSON Schmea.
* @default undefined
* @example
await generateText({
model: sarvam("sarvam-m", {
simulate: "tool-calling"
})
tools: {...}
})
await generateObject({
model: sarvam("sarvam-m", {
simulate: "json-object"
})
schema: {...}
})
*/
simulate?: "tool-calling" | "json-object";
/**
* Whether to enable parallel function calling during tool use.
* @default true
*/
parallelToolCalls?: boolean;
/**
* A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.
*/
user?: string;
/**
* Automatically download images and pass the image as data to the model. Sarvam supports image URLs for public models, so this is only needed for private models or when the images are not publicly accessible.
* @default false
*/
downloadImages?: boolean;
}
type SarvamLanguageCode = z.infer<typeof SarvamLanguageCodeSchema>;
declare const SarvamLanguageCodeSchema: z.ZodEnum<["hi-IN", "bn-IN", "kn-IN", "ml-IN", "mr-IN", "od-IN", "pa-IN", "ta-IN", "te-IN", "en-IN", "gu-IN"]>;
type SarvamSpeechModelId = "bulbul:v1" | "bulbul:v2" | (string & {});
type SarvamSpeechVoices = z.infer<typeof SpeakerSchema>;
declare const SpeakerSchema: z.ZodDefault<z.ZodEnum<["meera", "pavithra", "maitreyi", "arvind", "amol", "amartya", "diya", "neel", "misha", "vian", "arjun", "maya", "anushka", "abhilash", "manisha", "vidya", "arya", "karun", "hitesh"]>>;
/**
* Configuration settings for Sarvam Text-to-Speech API.
*
* This type defines the customizable options for generating speech audio
* using the Sarvam Text-to-Speech API. Each property corresponds to a specific
* feature or parameter supported by the API.
*/
type SarvamSpeechSettings = {
/**
* The speaker voice to be used for the output audio.
*
* @default "meera"
* @example "meera" (Default female voice for bulbul:v1)
* @example "arvind" (Male voice for bulbul:v1)
* @example "anushka" (Female voice for bulbul:v2)
*/
speaker?: SarvamSpeechVoices;
/**
* Controls the pitch of the audio.
*
* @default 0.0
* @example -0.5 (Deeper voice)
* @example 0.5 (Sharper voice)
*/
pitch?: number;
/**
* Controls the speed of the audio.
*
* @default 1.0
* @example 0.5 (Slower speech)
* @example 2.0 (Faster speech)
*/
pace?: number;
/**
* Controls the loudness of the audio.
*
* @default 1.0
* @example 0.3 (Quieter audio)
* @example 2.5 (Louder audio)
*/
loudness?: number;
/**
* Specifies the sample rate of the output audio.
*
* @default 22050
* @example 8000 (Low-quality audio)
* @example 24000 (High-quality audio)
*/
speech_sample_rate?: 8000 | 16000 | 22050 | 24000;
/**
* Enables preprocessing for normalization of English words and numeric entities
* (e.g., numbers, dates) in the input text.
*
* @default false
* @example true (Enable preprocessing)
* @example false (Disable preprocessing)
*/
enable_preprocessing?: boolean;
};
type SarvamTranscriptionModelId = "saarika:v2" | "saarika:v1" | "saarika:flash" | (string & {});
type SarvamSpeechTranslationModelId = "saaras:v1" | "saaras:v2" | "saaras:turbo" | "saaras:flash" | (string & {});
type SarvamTranscriptionCallOptions = {
with_timestamps?: boolean;
/**
* 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?: boolean;
/**
* Number of speakers to be detected in the audio.
* This is used when with_diarization is set to true.
* Can be null.
*/
num_speakers?: number;
};
interface SarvamTranslationSettings {
/**
* The language code of the input text. This specifies the source language for transliteration.
*
* @defualt "auto"
*/
from?: SarvamLanguageCode | "auto";
/**
* The language code of the transliteration text. This specifies the target language for transliteration.
*/
to: SarvamLanguageCode;
/**
* 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" | "modern-colloquial" | "classic-colloquial" | "code-mixed";
/**
* 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
*
* @default mayura:v1
*/
model?: "mayura:v1" | "sarvam-translate:v1";
/**
* 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";
}
interface SarvamTransliterateSettings {
/**
* The language code of the input text. This specifies the source language for transliteration.
*
* @defualt "auto"
*/
from?: SarvamLanguageCode | "auto";
/**
* The language code of the transliteration text. This specifies the target language for transliteration.
*/
to: SarvamLanguageCode;
/**
* 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";
}
interface SarvamProvider {
/**
* Creates a model for text generation.
*/
(modelId: SarvamChatModelId, settings?: SarvamChatSettings): LanguageModelV1;
/**
* Creates an Sarvam chat model for text generation.
*/
languageModel(modelId: SarvamChatModelId, settings?: SarvamChatSettings): LanguageModelV1;
/**
* Creates a Sarvam model for transcription.
*/
transcription(modelId: SarvamTranscriptionModelId,
/**
* Audio source language code
*
* @default unknown
*/
languageCode?: SarvamLanguageCode | "unknown", settings?: SarvamTranscriptionCallOptions): TranscriptionModelV1;
/**
* Creates a Sarvam model for Speech translation.
*/
speechTranslation(modelId: SarvamSpeechTranslationModelId): TranscriptionModelV1;
/**
* Creates a Sarvam model for speech.
*/
speech(modelId: SarvamSpeechModelId, languageCode: SarvamLanguageCode, settings?: SarvamSpeechSettings): SpeechModelV1;
/**
* Creates an Sarvam model for transliterate.
*/
transliterate(settings: SarvamTransliterateSettings): LanguageModelV1;
/**
* Creates an Sarvam model for translation.
*/
translation(settings: SarvamTranslationSettings): LanguageModelV1;
/**
* Creates an Sarvam model for language identification.
*/
languageIdentification(): LanguageModelV1;
}
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;
}
/**
* Create an Sarvam provider instance.
*/
declare function createSarvam(options?: SarvamProviderSettings): SarvamProvider;
/**
* Default Sarvam provider instance.
*/
declare const sarvam: SarvamProvider;
export { type SarvamProvider, type SarvamProviderSettings, createSarvam, sarvam };