### Install SDK (Node.js) Source: https://docs.sarvam.ai/api-reference-docs/getting-started/quickstart Install the Sarvam AI SDK for Node.js. ```bash npm install sarvamai@latest ``` -------------------------------- ### Install SDK (Python) Source: https://docs.sarvam.ai/api-reference-docs/getting-started/quickstart Install the Sarvam AI SDK for Python. ```bash pip install -U sarvamai ``` -------------------------------- ### Start Speech-to-Text Job (Swift) Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text/stt/job/start Example of starting a speech-to-text job using Swift with direct HTTP requests. ```APIDOC ```swift import Foundation let headers = [ "api-subscription-key": "", "Content-Type": "application/json" ] let parameters = [] as [String : Any] let postData = JSONSerialization.data(withJSONObject: parameters, options: []) let request = NSMutableURLRequest(url: NSURL(string: "https://api.sarvam.ai/speech-to-text/job/v1/job_id/start")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData as Data let session = URLSession.shared let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in if (error != nil) { print(error as Any) } else { let httpResponse = response as? HTTPURLResponse print(httpResponse) } }) dataTask.resume() ``` ``` -------------------------------- ### Start Speech-to-Text Job (Go) Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text/stt/job/start Example of starting a speech-to-text job using Go with direct HTTP requests. ```APIDOC ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.sarvam.ai/speech-to-text/job/v1/job_id/start" payload := strings.NewReader("{}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("api-subscription-key", "") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) } ``` ``` -------------------------------- ### Start Speech-to-Text Job (PHP) Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text/stt/job/start Example of starting a speech-to-text job using PHP with direct HTTP requests. ```APIDOC ```php request('POST', 'https://api.sarvam.ai/speech-to-text/job/v1/job_id/start', [ 'body' => '{}', 'headers' => [ 'Content-Type' => 'application/json', 'api-subscription-key' => '', ], ]); echo $response->getBody(); ``` ``` -------------------------------- ### Start Speech-to-Text Job with Go HTTP Request Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text/stt/job/start This example demonstrates how to start a speech-to-text job by making a direct HTTP POST request using Go. Replace '' with your actual API key. ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.sarvam.ai/speech-to-text/job/v1/job_id/start" payload := strings.NewReader("{}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("api-subscription-key", "") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) } ``` -------------------------------- ### Start Speech-to-Text Job (C#) Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text/stt/job/start Example of starting a speech-to-text job using C# with direct HTTP requests. ```APIDOC ```csharp using RestSharp; var client = new RestClient("https://api.sarvam.ai/speech-to-text/job/v1/job_id/start"); var request = new RestRequest(Method.POST); request.AddHeader("api-subscription-key", ""); request.AddHeader("Content-Type", "application/json"); request.AddParameter("application/json", "{{}}", ParameterType.RequestBody); IRestResponse response = client.Execute(request); ``` ``` -------------------------------- ### Install the SDK Source: https://docs.sarvam.ai/api-reference-docs/api-guides-tutorials/chat-completion/how-to/get-repeatable-results Installs the necessary Python SDK for interacting with the SarvamAI API. ```bash pip install -Uqq sarvamai ``` -------------------------------- ### Start Document Intelligence Job via HTTP POST (Go) Source: https://docs.sarvam.ai/api-reference-docs/document-intelligence/start This Go example demonstrates how to start a document intelligence job by making a direct HTTP POST request to the Sarvam AI API. Replace '' with your key. ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.sarvam.ai/doc-digitization/job/v1/job_id/start" payload := strings.NewReader("{}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("api-subscription-key", "") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) } ``` -------------------------------- ### Start Speech-to-Text Job (Java) Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text/stt/job/start Example of starting a speech-to-text job using Java with direct HTTP requests. ```APIDOC ```java import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.Unirest; HttpResponse response = Unirest.post("https://api.sarvam.ai/speech-to-text/job/v1/job_id/start") .header("api-subscription-key", "") .header("Content-Type", "application/json") .body("{}") .asString(); ``` ``` -------------------------------- ### Start Speech-to-Text Job (Python SDK) Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text/stt/job/start Example of starting a speech-to-text job using the Sarvam AI Python SDK. ```APIDOC ```python from sarvamai import SarvamAI client = SarvamAI( api_subscription_key="YOUR_API_KEY_HERE", ) client.speech_to_text_job.start( job_id="job_id", ) ``` ``` -------------------------------- ### Start Speech-to-Text Job (Ruby) Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text/stt/job/start Example of starting a speech-to-text job using Ruby with direct HTTP requests. ```APIDOC ```ruby require 'uri' require 'net/http' url = URI("https://api.sarvam.ai/speech-to-text/job/v1/job_id/start") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request["api-subscription-key"] = '' request["Content-Type"] = 'application/json' request.body = "{}" response = http.request(request) puts response.read_body ``` ``` -------------------------------- ### Start Document Intelligence Job via HTTP POST (Swift) Source: https://docs.sarvam.ai/api-reference-docs/document-intelligence/start This Swift example shows how to start a document intelligence job using URLSession for an HTTP POST request. Replace '' with your key. ```swift import Foundation let headers = [ "api-subscription-key": "", "Content-Type": "application/json" ] let parameters = [] as [String : Any] let postData = JSONSerialization.data(withJSONObject: parameters, options: []) let request = NSMutableURLRequest(url: NSURL(string: "https://api.sarvam.ai/doc-digitization/job/v1/job_id/start")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData as Data let session = URLSession.shared let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in if (error != nil) { print(error as Any) } else { let httpResponse = response as? HTTPURLResponse print(httpResponse) } }) dataTask.resume() ``` -------------------------------- ### Start Speech-to-Text Job (TypeScript SDK) Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text/stt/job/start Example of starting a speech-to-text job using the Sarvam AI TypeScript SDK. ```APIDOC ```typescript import { SarvamAIClient } from "sarvamai"; async function main() { const client = new SarvamAIClient({ apiSubscriptionKey: "YOUR_API_KEY_HERE", }); await client.speechToTextJob.start("job_id", {{}}); // The second argument is an empty object for this operation } main(); ``` ``` -------------------------------- ### Start Speech-to-Text Translate Job (Go) Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text-translate/stt-translate/job/start Make a POST request to the Sarvam AI API to start a speech-to-text translate job. This example uses Go's standard net/http package. ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.sarvam.ai/speech-to-text-translate/job/v1/job_id/start" payload := strings.NewReader("{}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("api-subscription-key", "") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) } ``` -------------------------------- ### Start Speech-to-Text Translate Job (C#) Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text-translate/stt-translate/job/start This C# example demonstrates starting a speech-to-text translate job using the RestSharp library. Ensure your API subscription key is correctly provided. ```csharp using RestSharp; var client = new RestClient("https://api.sarvam.ai/speech-to-text-translate/job/v1/job_id/start"); var request = new RestRequest(Method.POST); request.AddHeader("api-subscription-key", ""); request.AddHeader("Content-Type", "application/json"); request.AddParameter("application/json", "{}", ParameterType.RequestBody); IRestResponse response = client.Execute(request); ``` -------------------------------- ### Start Speech-to-Text Translate Job (Java) Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text-translate/stt-translate/job/start This Java example uses the Unirest library to send a POST request to start a speech-to-text translate job. Configure your API subscription key. ```java import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.Unirest; HttpResponse response = Unirest.post("https://api.sarvam.ai/speech-to-text-translate/job/v1/job_id/start") .header("api-subscription-key", "") .header("Content-Type", "application/json") .body("{}") .asString(); ``` -------------------------------- ### Start Speech-to-Text Translate Job (Swift) Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text-translate/stt-translate/job/start Initiate a speech-to-text translate job using Swift's URLSession. This example sets up the necessary headers and makes a POST request. ```swift import Foundation let headers = [ "api-subscription-key": "", "Content-Type": "application/json" ] let parameters = [] as [String : Any] let postData = JSONSerialization.data(withJSONObject: parameters, options: []) let request = NSMutableURLRequest(url: NSURL(string: "https://api.sarvam.ai/speech-to-text-translate/job/v1/job_id/start")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData as Data let session = URLSession.shared let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in if (error != nil) { print(error as Any) } else { let httpResponse = response as? HTTPURLResponse print(httpResponse) } }) dataTask.resume() ``` -------------------------------- ### Create Pronunciation Dictionary (PHP) Source: https://docs.sarvam.ai/api-reference-docs/pronunciation-dictionary/create This PHP example uses Guzzle HTTP client to create a pronunciation dictionary. Ensure you have Guzzle installed and replace '' with your key. ```php request('POST', 'https://api.sarvam.ai/text-to-speech/pronunciation-dictionary', [ 'multipart' => [ [ 'name' => 'file', 'filename' => '{ "pronunciations": { "en-US": { "route": "raut", "data": "deɪtə", "schedule": "ˈskɛdʒuːl" }, "es-ES": { "hola": "ˈola", "adiós": "aˈðjos" } } }', 'contents' => null ] ] 'headers' => [ 'api-subscription-key' => '', ], ]); echo $response->getBody(); ``` -------------------------------- ### Get Upload Links via HTTP POST (Go) Source: https://docs.sarvam.ai/api-reference-docs/document-intelligence/get-upload-links Make a direct HTTP POST request to the upload links endpoint. This example demonstrates setting the necessary headers and JSON payload. ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.sarvam.ai/doc-digitization/job/v1/upload-files" payload := strings.NewReader("{\n \"job_id\": \"3fa85f64-5717-4562-b3fc-2c963f66afa6\",\n \"files\": [\n \"invoice_2024.pdf\"\n ]\n}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("api-subscription-key", "") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) } ``` -------------------------------- ### Convert Text to Speech via HTTP POST (Go) Source: https://docs.sarvam.ai/api-reference-docs/text-to-speech/convert This Go example demonstrates making a direct HTTP POST request to the text-to-speech endpoint. Replace '' with your key. ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.sarvam.ai/text-to-speech" payload := strings.NewReader("{\n \"text\": \"Hello, welcome to Sarvam's text-to-speech service. This demo converts your text into natural spoken audio.\",\n \"target_language_code\": \"en-IN\"\n}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("api-subscription-key", "") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) } ``` -------------------------------- ### Full Example: Basic Transcription (Python) Source: https://docs.sarvam.ai/api-reference-docs/api-guides-tutorials/speech-to-text/streaming-api A complete Python example demonstrating how to initialize the client, load audio data, and perform basic transcription using the streaming API. Change the `mode` parameter to switch functionalities. ```python import asyncio import base64 from sarvamai import AsyncSarvamAI # Load your audio file with open("path/to/your/audio.wav", "rb") as f: audio_data = base64.b64encode(f.read()).decode("utf-8") async def basic_transcription(): # Initialize client with your API key client = AsyncSarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY") # Connect and transcribe — change mode as needed async with client.speech_to_text_streaming.connect( model="saaras:v3", mode="transcribe", language_code="en-IN", high_vad_sensitivity=True ) as ws: await ws.transcribe(audio=audio_data) response = await ws.recv() print(f"Result: {response}") asyncio.run(basic_transcription()) ``` -------------------------------- ### Full Example: Basic Transcription Source: https://docs.sarvam.ai/api-reference-docs/api-guides-tutorials/speech-to-text/streaming-api A complete working example demonstrating how to initialize the client, connect to the streaming API, and perform basic transcription. ```APIDOC ## Full Example: Basic Transcription (Python) ### Description This example shows the complete workflow for using the Speech-to-Text Streaming API in Python, including client initialization and audio processing. ### Setup 1. Install the library: `pip install sarvamai` 2. Replace `"path/to/your/audio.wav"` with the actual path to your audio file. 3. Replace `"YOUR_SARVAM_API_KEY"` with your actual API key. ### Code ```python import asyncio import base64 from sarvamai import AsyncSarvamAI # Load your audio file with open("path/to/your/audio.wav", "rb") as f: audio_data = base64.b64encode(f.read()).decode("utf-8") async def basic_transcription(): # Initialize client with your API key client = AsyncSarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY") # Connect and transcribe — change mode as needed async with client.speech_to_text_streaming.connect( model="saaras:v3", mode="transcribe", language_code="en-IN", high_vad_sensitivity=True ) as ws: await ws.transcribe(audio=audio_data) response = await ws.recv() print(f"Result: {response}") asyncio.run(basic_transcription()) ``` ``` ```APIDOC ## Full Example: Basic Transcription (JavaScript) ### Description This example demonstrates how to use the Speech-to-Text Streaming API in JavaScript, including client initialization and audio processing. ### Setup 1. Install the library: `npm install sarvamai` or `yarn add sarvamai` 2. Replace `"path/to/your/audio.wav"` with the actual path to your audio file. 3. Replace `"YOUR_SARVAM_API_KEY"` with your actual API key. ### Code ```javascript import { SarvamAIClient } from "sarvamai"; import * as fs from "fs"; function audioFileToBase64(filePath) { return fs.readFileSync(filePath).toString("base64"); } async function basicTranscription() { const audioData = audioFileToBase64("path/to/your/audio.wav"); const client = new SarvamAIClient({ apiSubscriptionKey: "YOUR_SARVAM_API_KEY" }); // Connect — change mode as needed const socket = await client.speechToTextStreaming.connect({ model: "saaras:v3", mode: "transcribe", "language-code": "en-IN", high_vad_sensitivity: "true" }); socket.on("open", () => { socket.transcribe({ audio: audioData, sample_rate: 16000, encoding: "audio/wav", }); }); socket.on("message", (response) => { console.log("Result:", response); }); await socket.waitForOpen(); await new Promise(resolve => setTimeout(resolve, 5000)); socket.close(); } basicTranscription(); ``` ``` -------------------------------- ### Create Pronunciation Dictionary (Go) Source: https://docs.sarvam.ai/api-reference-docs/pronunciation-dictionary/create This Go example demonstrates how to make a POST request to create a pronunciation dictionary using multipart/form-data. Remember to replace '' with your actual key. ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.sarvam.ai/text-to-speech/pronunciation-dictionary" payload := strings.NewReader("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{\n \"pronunciations\": {\n \"en-US\": {\n \"route\": \"raut\",\n \"data\": \"deɪtə\",\n \"schedule\": \"ˈskɛdʒuːl\"\n },\n \"es-ES\": {\n \"hola\": \"ˈola\",\n \"adiós\": \"aˈðjos\"\n }\n }\n}\"\r\nContent-Type: application/octet-stream\r\n\r\n\r\n-----011000010111000001101001--\r\n") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("api-subscription-key", "") req.Header.Add("Content-Type", "multipart/form-data; boundary=---011000010111000001101001") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) } ``` -------------------------------- ### Full Example: Basic Transcription (JavaScript) Source: https://docs.sarvam.ai/api-reference-docs/api-guides-tutorials/speech-to-text/streaming-api A comprehensive JavaScript example showing how to set up the client, convert audio to base64, and initiate streaming transcription. The `mode` parameter can be adjusted for different use cases. ```javascript import { SarvamAIClient } from "sarvamai"; import * as fs from "fs"; function audioFileToBase64(filePath) { return fs.readFileSync(filePath).toString("base64"); } async function basicTranscription() { const audioData = audioFileToBase64("path/to/your/audio.wav"); const client = new SarvamAIClient({ apiSubscriptionKey: "YOUR_SARVAM_API_KEY" }); // Connect — change mode as needed const socket = await client.speechToTextStreaming.connect({ model: "saaras:v3", mode: "transcribe", "language-code": "en-IN", high_vad_sensitivity: "true" }); socket.on("open", () => { socket.transcribe({ audio: audioData, sample_rate: 16000, encoding: "audio/wav", }); }); socket.on("message", (response) => { console.log("Result:", response); }); await socket.waitForOpen(); await new Promise(resolve => setTimeout(resolve, 5000)); socket.close(); } basicTranscription(); ``` -------------------------------- ### Complete Optimized Voice Agent Example Source: https://docs.sarvam.ai/api-reference-docs/integration/build-voice-agent-with-live-kit An integrated example demonstrating all best practices: STT with `flush_signal`, and `AgentSession` with optimized `turn_detection` and `min_endpointing_delay`. ```python # STT with flush_signal enabled stt=sarvam.STT( language="unknown", model="saaras:v3", mode="transcribe", flush_signal=True ) # AgentSession with optimized settings (no VAD parameter) session = AgentSession( turn_detection="stt", min_endpointing_delay=0.07 ) ``` -------------------------------- ### Translation API REST Example Source: https://docs.sarvam.ai/api-reference-docs/getting-started/quickstart Example of making a translation API call using cURL. ```bash curl -X POST https://api.sarvam.ai/translate \ -H "api-subscription-key: " \ -H "Content-Type: application/json" \ -d '{ "input": "input", "source_language_code": "auto", "target_language_code": "gu-IN" }' ``` -------------------------------- ### Go HTTP Example Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text/stt/job/initiate Initiates a speech-to-text job using a direct HTTP POST request in Go. ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.sarvam.ai/speech-to-text/job/v1" payload := strings.NewReader("{}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("api-subscription-key", "") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) } ``` -------------------------------- ### JavaScript Chat Completion Example Source: https://docs.sarvam.ai/api-reference-docs/api-guides-tutorials/chat-completion/overview A basic example of how to use the SarvamAI client in JavaScript to get a chat completion. ```javascript import { SarvamAIClient } from "sarvamai"; // Initialize the SarvamAI client with your API key const client = new SarvamAIClient({ apiSubscriptionKey: "YOUR_SARVAM_API_KEY", }); async function main() { const response = await client.chat.completions({ model: "sarvam-105b", messages: [ { role: "user", content: "What is the capital of India?", }, ], }); console.log(response.choices[0].message.content); } main(); ``` -------------------------------- ### Run Tutor Agent Source: https://docs.sarvam.ai/api-reference-docs/cookbook/example-voice-agents/tutor-agent Execute the tutor agent script from the command line. This command starts the agent, which will create a Daily room and provide a URL to join. ```bash python tutor_agent.py ``` -------------------------------- ### Python Chat Completion Example Source: https://docs.sarvam.ai/api-reference-docs/api-guides-tutorials/chat-completion/overview A basic example of how to use the SarvamAI client in Python to get a chat completion. ```python from sarvamai import SarvamAI client = SarvamAI( api_subscription_key="YOUR_SARVAM_API_KEY", ) response = client.chat.completions( model="sarvam-105b", messages=[ {"role": "user", "content": "Hey, what is the capital of India?"} ], ) print(response) ``` -------------------------------- ### Translate with All Available Parameters Source: https://docs.sarvam.ai/api-reference-docs/api-guides-tutorials/text-processing/translation Example demonstrating the use of all available parameters for maximum customization in translation. ```python response = client.text.translate( input="Welcome to our service!", source_language_code="en-IN", target_language_code="hi-IN", model="mayura:v1", speaker_gender="Female", mode="modern-colloquial", output_script="fully-native", numerals_format="native" ) ``` ```javascript const response = await client.text.translate({ input: "Welcome to our service!", source_language_code: "en-IN", target_language_code: "hi-IN", model: "mayura:v1", speaker_gender: "Female", mode: "modern-colloquial", output_script: "fully-native", numerals_format: "native" }); ``` ```bash curl -X POST https://api.sarvam.ai/translate \ -H "api-subscription-key: " \ -H "Content-Type: application/json" \ -d '{ "input": "Welcome to our service!", "source_language_code": "en-IN", "target_language_code": "hi-IN", "model": "mayura:v1", "speaker_gender": "Female", "mode": "modern-colloquial", "output_script": "fully-native", "numerals_format": "native" }' ``` -------------------------------- ### Node.js Translation API Example Source: https://docs.sarvam.ai/api-reference-docs/getting-started/quickstart Example of making a translation API call using the Node.js SDK. ```javascript import { SarvamAIClient } from "sarvamai"; const client = new SarvamAIClient({ apiSubscriptionKey: "YOUR_API_KEY" }); const response = await client.text.translate({ input: "Hi, My Name is Vinayak.", source_language_code: "auto", target_language_code: "gu-IN", speaker_gender: "Male" }); console.log(response); ``` -------------------------------- ### Python Translation API Example Source: https://docs.sarvam.ai/api-reference-docs/getting-started/quickstart Example of making a translation API call using the Python SDK. ```python from sarvamai import SarvamAI client = SarvamAI( api_subscription_key="YOUR_API_KEY", ) response = client.text.translate( input="Hi, My Name is Vinayak.", source_language_code="auto", target_language_code="gu-IN", speaker_gender="Male" ) print(response) ``` -------------------------------- ### Start Speech-to-Text Job with C# RestSharp Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text/stt/job/start This C# example utilizes the RestSharp library to send a POST request for starting a speech-to-text job. Replace '' with your API key. ```csharp using RestSharp; var client = new RestClient("https://api.sarvam.ai/speech-to-text/job/v1/job_id/start"); var request = new RestRequest(Method.POST); request.AddHeader("api-subscription-key", ""); request.AddHeader("Content-Type", "application/json"); request.AddParameter("application/json", "{}", ParameterType.RequestBody); IRestResponse response = client.Execute(request); ``` -------------------------------- ### Get Pronunciation Dictionary Entry (C#) Source: https://docs.sarvam.ai/api-reference-docs/pronunciation-dictionary/get This C# example uses RestSharp to perform an HTTP GET request for a pronunciation dictionary entry. Configure your RestClient with the correct URL and headers. ```csharp using RestSharp; var client = new RestClient("https://api.sarvam.ai/text-to-speech/pronunciation-dictionary/dict_id"); var request = new RestRequest(Method.GET); request.AddHeader("api-subscription-key", ""); IRestResponse response = client.Execute(request); ``` -------------------------------- ### Get Pronunciation Dictionary Entry (Go) Source: https://docs.sarvam.ai/api-reference-docs/pronunciation-dictionary/get Make a direct HTTP GET request to retrieve a pronunciation dictionary entry. This example uses Go's standard net/http package. ```go package main import ( "fmt" "net/http" "io" ) func main() { url := "https://api.sarvam.ai/text-to-speech/pronunciation-dictionary/dict_id" req, _ := http.NewRequest("GET", url, nil) req.Header.Add("api-subscription-key", "") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) } ``` -------------------------------- ### Create Pronunciation Dictionary (Swift) Source: https://docs.sarvam.ai/api-reference-docs/pronunciation-dictionary/create This Swift example shows how to set up headers for a POST request to create a pronunciation dictionary. You will need to complete the request body and execution. ```swift import Foundation let headers = [ "api-subscription-key": "", "Content-Type": "multipart/form-data; boundary=---011000010111000001101001" ] let parameters = [ ``` -------------------------------- ### Start Speech-to-Text Job with Java Unirest Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text/stt/job/start This Java example uses the Unirest library to make an HTTP POST request to start a speech-to-text job. Substitute '' with your API key. ```java import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.Unirest; HttpResponse response = Unirest.post("https://api.sarvam.ai/speech-to-text/job/v1/job_id/start") .header("api-subscription-key", "") .header("Content-Type", "application/json") .body("{}") .asString(); ``` -------------------------------- ### Get Pronunciation Dictionary Entry (PHP) Source: https://docs.sarvam.ai/api-reference-docs/pronunciation-dictionary/get Use Guzzle HTTP client in PHP to make a GET request for a pronunciation dictionary entry. Ensure you have the Guzzle library installed via Composer. ```php request('GET', 'https://api.sarvam.ai/text-to-speech/pronunciation-dictionary/dict_id', [ 'headers' => [ 'api-subscription-key' => '', ], ]); echo $response->getBody(); ``` -------------------------------- ### Go HTTP Request Example Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text-translate/stt-translate/job/initiate Initiates a speech-to-text translate job via an HTTP POST request in Go. ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.sarvam.ai/speech-to-text-translate/job/v1" payload := strings.NewReader("{}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("api-subscription-key", "") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) } ``` -------------------------------- ### Run the Agent Source: https://docs.sarvam.ai/api-reference-docs/cookbook/example-voice-agents/loan-advisory-agent This snippet shows how to initialize and run the agent using the PipelineRunner. Ensure the runner is configured correctly before execution. ```python runner = PipelineRunner(handle_sigint=runner_args.handle_sigint) await runner.run(task) ``` ```python if __name__ == "__main__": from pipecat.runner.run import main main() ``` -------------------------------- ### Python SDK Example Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text/stt/job/initiate Initiates a speech-to-text job using the SarvamAI Python SDK. ```python from sarvamai import SarvamAI client = SarvamAI( api_subscription_key="YOUR_API_KEY_HERE", ) client.speech_to_text_job.initialise() ``` -------------------------------- ### Python SDK Example Source: https://docs.sarvam.ai/api-reference-docs/api-guides-tutorials/text-to-speech/streaming-api/http-stream Example of using the SarvamAI Python SDK to stream text-to-speech conversion. ```APIDOC ## Python SDK Example ### Description This snippet demonstrates how to use the `sarvamai` Python library to convert text to speech and receive the audio in chunks. ### Method `client.text_to_speech.convert_stream` ### Parameters - **text** (string) - Required - The text to convert to speech. - **target_language_code** (string) - Required - The language code for the speech (e.g., "en-IN"). - **speaker** (string) - Required - The desired speaker for the voice. - **model** (string) - Required - The specific speech synthesis model to use (e.g., "bulbul:v3"). - **output_audio_codec** (string) - Required - The desired audio codec for the output (e.g., "mp3"). ### Request Example ```python from sarvamai import SarvamAI from sarvamai.core.api_error import ApiError client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY") try: for chunk in client.text_to_speech.convert_stream( text="Hello from Sarvam AI!", target_language_code="en-IN", speaker="shubh", model="bulbul:v3", output_audio_codec="mp3", ): pass # process chunk except ApiError as e: print(f"Error {e.status_code}: {e.body}") ``` ### Error Handling Errors are caught using `ApiError` and provide status code and body details. ``` -------------------------------- ### Basic Translation Source: https://docs.sarvam.ai/api-reference-docs/getting-started/models/mayura Perform a simple translation between languages using default settings. This is ideal for getting started with the Mayura API. ```APIDOC ## Basic Translation ### Description Performs a simple translation between languages with default settings. Perfect for getting started with the Mayura API. ### Method POST ### Endpoint /translate ### Parameters #### Request Body - **input** (string) - Required - The text to translate. - **source_language_code** (string) - Required - The language code of the input text. Set to "auto" for automatic language detection. - **target_language_code** (string) - Required - The language code to translate the text into. ### Request Example ```json { "input": "मैं ऑफिस जा रहा हूँ", "source_language_code": "hi-IN", "target_language_code": "en-IN" } ``` ### Response #### Success Response (200) - **translated_text** (string) - The translated text. #### Response Example ```json { "translated_text": "I am going to the office" } ``` ``` -------------------------------- ### Initialise Document Intelligence Job (Go) Source: https://docs.sarvam.ai/api-reference-docs/document-intelligence/initialise Initiates a Document Intelligence job via a direct HTTP POST request. Replace '' with your actual subscription key. ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.sarvam.ai/doc-digitization/job/v1" payload := strings.NewReader("{}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("api-subscription-key", "") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) } ``` -------------------------------- ### Python SDK Example Source: https://docs.sarvam.ai/api-reference-docs/speech-to-text-translate/stt-translate/job/initiate Initiates a speech-to-text translate job using the SarvamAI client in Python. ```python from sarvamai import SarvamAI client = SarvamAI( api_subscription_key="YOUR_API_KEY_HERE", ) client.speech_to_text_translate_job.initialise() ``` -------------------------------- ### Chat Completions with SarvamAIClient in TypeScript Source: https://docs.sarvam.ai/api-reference-docs/chat/chat-completions Use the SarvamAIClient to send messages and get chat completions. Ensure you have the 'sarvamai' package installed. ```typescript import { SarvamAIClient } from "sarvamai"; async function main() { const client = new SarvamAIClient(); await client.chat.completions({ messages: [ { role: "user", content: "Hello, can you explain the theory of relativity?", }, ], model: "sarvam-105b", }); } main(); ``` -------------------------------- ### Bash Example: Getting a specific pronunciation dictionary Source: https://docs.sarvam.ai/api-reference-docs/api-guides-tutorials/text-to-speech/pronunciation-dictionary This cURL command shows how to retrieve the contents of a specific pronunciation dictionary by its ID. ```bash curl https://api.sarvam.ai/text-to-speech/pronunciation-dictionary/p_5cb7faa6 \ -H "api-subscription-key: YOUR_SARVAM_API_KEY" ``` -------------------------------- ### Tutor Agent Implementation Source: https://docs.sarvam.ai/api-reference-docs/cookbook/example-voice-agents/tutor-agent This Python script sets up a tutor agent. It initializes transport, STT, TTS, and LLM services, configures the conversation context with a system prompt defining the tutor's persona and expertise, and builds a Pipecat pipeline. Event handlers for client connection and disconnection are included to manage the agent's lifecycle. ```python import os from dotenv import load_dotenv from loguru import logger from pipecat.frames.frames import LLMRunFrame from pipecat.pipeline.pipeline import Pipeline from pipecat.pipeline.runner import PipelineRunner from pipecat.pipeline.task import PipelineTask from pipecat.processors.aggregators.llm_context import LLMContext from pipecat.processors.aggregators.llm_response_universal import ( LLMContextAggregatorPair, ) from pipecat.runner.types import RunnerArguments from pipecat.runner.utils import create_transport from pipecat.services.sarvam.stt import SarvamSTTService from pipecat.services.sarvam.tts import SarvamTTSService from pipecat.services.openai.llm import OpenAILLMService from pipecat.transports.base_transport import TransportParams from pipecat.transports.daily.transport import DailyParams load_dotenv(override=True) async def bot(runner_args: RunnerArguments): """Main bot entry point.""" # Create transport (supports both Daily and WebRTC) transport = await create_transport( runner_args, { "daily": lambda: DailyParams(audio_in_enabled=True, audio_out_enabled=True), "webrtc": lambda: TransportParams( audio_in_enabled=True, audio_out_enabled=True ), }, ) # Initialize AI services stt = SarvamSTTService( api_key=os.getenv("SARVAM_API_KEY"), language="unknown", # Auto-detect for multilingual students model="saaras:v3", mode="transcribe" ) tts = SarvamTTSService( api_key=os.getenv("SARVAM_API_KEY"), target_language_code="en-IN", model="bulbul:v3", speaker="ishita" # Clear and articulate voice for teaching ) llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o") # Set up conversation context with tutor personality messages = [ { "role": "system", "content": """You are an expert tutor designed to help students understand and excel in their studies. Your teaching expertise covers multiple subjects: **Mathematics:** - Arithmetic, Algebra, Geometry, Trigonometry - Calculus, Statistics, Probability - Problem-solving techniques **Science:** - Physics: Mechanics, Electricity, Optics, Thermodynamics - Chemistry: Elements, Reactions, Organic Chemistry - Biology: Cell Biology, Human Anatomy, Ecology **Languages:** - English Grammar and Composition - Hindi Grammar and Literature - Reading Comprehension **Social Studies:** - History, Geography, Civics - Economics basics Teaching approach: - Start with the basics and build up to complex concepts - Use real-world examples and analogies to explain abstract concepts - Break down complex problems into smaller, manageable steps - Encourage students and praise their efforts - Ask questions to check understanding - Adapt your explanations based on the student's level - Use simple language and avoid overwhelming with jargon - When solving numerical problems, show each step clearly Communication style: - Be patient, encouraging, and supportive - Speak clearly and at a moderate pace - Celebrate small victories and correct mistakes gently - If a student is struggling, try a different explanation approach - Make learning interesting by connecting it to everyday life Start by greeting the student warmly and asking what subject or topic they'd like to learn or what problem they need help with.""", }, ] context = LLMContext(messages) context_aggregator = LLMContextAggregatorPair(context) # Build pipeline pipeline = Pipeline( [ transport.input(), stt, context_aggregator.user(), llm, tts, transport.output(), context_aggregator.assistant(), ] ) task = PipelineTask(pipeline) @transport.event_handler("on_client_connected") async def on_client_connected(transport, client): logger.info("Student connected") messages.append( {"role": "system", "content": "Greet the student warmly and ask what subject or topic they'd like to learn today."} ) await task.queue_frames([LLMRunFrame()]) @transport.event_handler("on_client_disconnected") async def on_client_disconnected(transport, client): logger.info("Student disconnected") await task.cancel() runner = PipelineRunner(handle_sigint=runner_args.handle_sigint) await runner.run(task) if __name__ == "__main__": from pipecat.runner.run import main main() ``` -------------------------------- ### Initialize LiveKit Agent with Sarvam and OpenAI Plugins Source: https://docs.sarvam.ai/api-reference-docs/cookbook/example-voice-agents/collection-agent Loads environment variables and imports necessary modules for creating a LiveKit agent. This sets up the foundation for the voice agent. ```python import logging from dotenv import load_dotenv from livekit.agents import JobContext, WorkerOptions, cli from livekit.agents.voice import Agent, AgentSession from livekit.plugins import openai, sarvam # Load environment variables load_dotenv() ``` -------------------------------- ### JavaScript Example: Getting a specific pronunciation dictionary Source: https://docs.sarvam.ai/api-reference-docs/api-guides-tutorials/text-to-speech/pronunciation-dictionary This JavaScript code demonstrates how to fetch the pronunciations associated with a given dictionary ID. ```javascript const data = await client.pronunciationDictionary.get("p_5cb7faa6"); console.log(data.pronunciations); ```