|
| 1 | +--- |
| 2 | +title: 'Sarvam' |
| 3 | +description: 'Learn how to use the Sarvam AI provider for the AI SDK.' |
| 4 | +--- |
| 5 | + |
| 6 | +# Sarvam Provider |
| 7 | + |
| 8 | +The Sarvam AI Provider is a library developed to integrate with the AI SDK. This library brings Speech to Text (STT) capabilities to your applications, allowing for seamless interaction with audio and text data. |
| 9 | + |
| 10 | +## Setup |
| 11 | + |
| 12 | +The Sarvam provider is available in the `sarvam-ai-provider` module. You can install it with: |
| 13 | + |
| 14 | +<Tabs items={['pnpm', 'npm', 'yarn']}> |
| 15 | + <Tab> |
| 16 | + <Snippet text="pnpm add sarvam-ai-provider" dark /> |
| 17 | + </Tab> |
| 18 | + |
| 19 | + <Tab> |
| 20 | + <Snippet text="npm install sarvam-ai-provider" dark /> |
| 21 | + </Tab> |
| 22 | + |
| 23 | + <Tab> |
| 24 | + <Snippet text="yarn add sarvam-ai-provider" dark /> |
| 25 | + </Tab> |
| 26 | +</Tabs> |
| 27 | + |
| 28 | +## Provider Instance |
| 29 | + |
| 30 | +First, get your **Sarvam API Key** from the [Sarvam Dashboard](https://dashboard.sarvam.ai/auth/signin). |
| 31 | + |
| 32 | +Then initialize `Sarvam` in your application: |
| 33 | + |
| 34 | +```ts |
| 35 | +import { createSarvam } from 'sarvam-ai-provider'; |
| 36 | + |
| 37 | +const sarvam = createSarvam({ |
| 38 | + headers: { |
| 39 | + 'api-subscription-key': 'YOUR_API_KEY', |
| 40 | + }, |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +<Note> |
| 45 | + The `api-subscription-key` needs to be passed in headers. Consider using |
| 46 | + `YOUR_API_KEY` as environment variables for security. |
| 47 | +</Note> |
| 48 | + |
| 49 | +- Transcribe speech to text |
| 50 | + |
| 51 | +```ts |
| 52 | +import { experimental_transcribe as transcribe } from 'ai'; |
| 53 | +import { readFile } from 'fs/promises'; |
| 54 | + |
| 55 | +await transcribe({ |
| 56 | + model: sarvam.transcription('saarika:v2'), |
| 57 | + audio: await readFile('./src/transcript-test.mp3'), |
| 58 | + providerOptions: { |
| 59 | + sarvam: { |
| 60 | + language_code: 'en-IN', |
| 61 | + }, |
| 62 | + }, |
| 63 | +}); |
| 64 | +``` |
| 65 | + |
| 66 | +## Features |
| 67 | + |
| 68 | +### Changing parameters |
| 69 | + |
| 70 | +- Change language_code |
| 71 | + |
| 72 | +```ts |
| 73 | +providerOptions: { |
| 74 | + sarvam: { |
| 75 | + language_code: 'en-IN', |
| 76 | + }, |
| 77 | + }, |
| 78 | +``` |
| 79 | + |
| 80 | +<Note> |
| 81 | + `language_code` specifies the language of the input audio and is required for |
| 82 | + accurate transcription. • It is mandatory for the `saarika:v1` model (this |
| 83 | + model does not support `unknown`). • It is optional for the `saarika:v2` |
| 84 | + model. • Use `unknown` when the language is not known; in that case, the API |
| 85 | + will auto‑detect it. Available options: `unknown`, `hi-IN`, `bn-IN`, `kn-IN`, |
| 86 | + `ml-IN`, `mr-IN`, `od-IN`, `pa-IN`, `ta-IN`, `te-IN`, `en-IN`, `gu-IN`. |
| 87 | +</Note> |
| 88 | + |
| 89 | +- with_timestamps? |
| 90 | + |
| 91 | +```ts |
| 92 | +providerOptions: { |
| 93 | + sarvam: { |
| 94 | + with_timestamps: true, |
| 95 | + }, |
| 96 | +}, |
| 97 | +``` |
| 98 | + |
| 99 | +<Note> |
| 100 | + `with_timestamps` specifies whether to include start/end timestamps for each |
| 101 | + word/token. • Type: boolean • When true, each word/token will include |
| 102 | + start/end timestamps. • Default: false |
| 103 | +</Note> |
| 104 | + |
| 105 | +- with_diarization? |
| 106 | + |
| 107 | +```ts |
| 108 | +providerOptions: { |
| 109 | + sarvam: { |
| 110 | + with_diarization: true, |
| 111 | + }, |
| 112 | +}, |
| 113 | +``` |
| 114 | + |
| 115 | +<Note> |
| 116 | + `with_diarization` enables speaker diarization (Beta). • Type: boolean • When |
| 117 | + true, enables speaker diarization. • Default: false |
| 118 | +</Note> |
| 119 | + |
| 120 | +- num_speakers? |
| 121 | + |
| 122 | +```ts |
| 123 | +providerOptions: { |
| 124 | + sarvam: { |
| 125 | + with_diarization: true, |
| 126 | + num_speakers: 2, |
| 127 | + }, |
| 128 | +}, |
| 129 | +``` |
| 130 | + |
| 131 | +<Note> |
| 132 | + `num_speakers` sets the number of distinct speakers to detect (only when |
| 133 | + `with_diarization` is true). • Type: number | null • Number of distinct |
| 134 | + speakers to detect. • Default: null |
| 135 | +</Note> |
| 136 | + |
| 137 | +## References |
| 138 | + |
| 139 | +- [Sarvam API Docs](https://docs.sarvam.ai/api-reference-docs/endpoints/speech-to-text) |
0 commit comments