sarvamai

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 12 Imported by: 0

README

Sarvam AI Go SDK (Unofficial)

Go Reference Go Report Card

An idiomatic Go client for the Sarvam AI API.

Note: This is an unofficial, community-maintained SDK. It is not affiliated with or endorsed by Sarvam AI.

Features

  • Chat: Conversational AI optimized for Indic contexts.
  • Speech-to-Text: Support for both REST and WebSocket-based real-time transcription.
  • Text-to-Speech: Natural synthesis with streaming capabilities.
  • Text Processing: Translation, Transliteration, and Language Identification (LID).
  • Document Intelligence: Digitization and extraction for complex documents.
  • Batch Processing: Efficient handling of large audio volumes via Job APIs.
  • Resilience: Automatic retries with exponential backoff and context-aware connection management.

Installation

go get github.com/Shreehari-Acharya/sarvamai-go

Quick Start

Initialization

Initialize the client using your API key from the Sarvam dashboard.

import "github.com/Shreehari-Acharya/sarvamai-go"

client, err := sarvamai.NewClient(sarvamai.Config{
    APIKey: "your-api-key",
})
Chat Completion
import "github.com/Shreehari-Acharya/sarvamai-go/chat"

resp, err := client.Chat.Completions(ctx, chat.ModelSarvamM, []chat.ChatMessage{
    chat.SystemMessage("You are a helpful assistant."),
    chat.UserMessage("What is the capital of India?"),
})
Speech-to-Text
import "github.com/Shreehari-Acharya/sarvamai-go/stt"

file, _ := os.Open("audio.wav")
defer file.Close()

resp, err := client.SpeechToText.Transcribe(ctx, file,
    stt.WithModel(stt.ModelSaaras),
    stt.WithLanguage(stt.LanguageHiIN),
)
Text-to-Speech Streaming
import "github.com/Shreehari-Acharya/sarvamai-go/tts"

stream, err := client.TextToSpeech.StreamConvert(ctx, tts.LanguageHiIN,
    tts.WithStreamSpeaker(tts.SpeakerShubh),
)
defer stream.Close()

stream.SendText("नमस्ते, आप कैसे हैं?")
stream.Flush()

for stream.Next() {
    chunk := stream.Current()
    // chunk.Audio contains base64 encoded data
}

Resilience and Reliability

  • Retries: The SDK automatically retries on 429 Too Many Requests and 5xx errors.
  • Timeout Management: REST requests use a default 30-second timeout, configurable via the client config.
  • Resource Cleanup: WebSocket streams respect context cancellation to ensure underlying connections and goroutines are properly terminated.

Documentation And Examples

Full documentation and detailed examples are available in the examples/ directory.

Contributing

Contributions are welcome. Please refer to CONTRIBUTING.md for guidelines.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Overview

Package sarvamai provides a Go client for the Sarvam AI API.

Usage

Create a client with your API key:

client, err := sarvamai.NewClient(sarvamai.Config{
    APIKey: "your-api-key",
})

API Key

You can obtain an API key from the Sarvam dashboard: https://dashboard.sarvam.ai/key-management

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Text                     *text.TextClient
	SpeechToText             *stt.STTClient
	SpeechToTextTranslate    *translate.TranslateClient
	SpeechToTextJob          *sttjob.SttJobClient
	SpeechToTextTranslateJob *translatejob.TranslateJobClient
	TextToSpeech             *tts.TTSClient
	Chat                     *chat.ChatClient
	DocumentIntelligence     *docintel.DocIntelClient
	// contains filtered or unexported fields
}

Client provides access to Sarvam AI services.

Services

The client exposes the following services:

  • Text: For translation, transliteration, and language detection
  • SpeechToText: For speech-to-text transcription (REST and streaming)
  • TextToSpeech: For text-to-speech conversion
  • Chat: For conversational AI interactions

Example

client, err := sarvamai.NewClient(sarvamai.Config{
    APIKey: "your-api-key",
})
if err != nil {
    log.Fatal(err)
}

// Use text translation
resp, err := client.Text.Translate(ctx, translate.Request{...})

// Use speech-to-text
resp, err := client.SpeechToText.Transcribe(ctx, stt.TranscribeRequest{...})

// Use text-to-speech
resp, err := client.TextToSpeech.Convert(ctx, tts.ConvertRequest{...})

// Use chat
resp, err := client.Chat.Completions(ctx, chat.ChatRequest{...})

func NewClient

func NewClient(cfg Config) (*Client, error)

NewClient creates a new Sarvam AI client with the given configuration.

Parameters

cfg: Configuration containing API key and optional settings. APIKey is required.

Returns

A new Client instance or an error if the API key is missing.

Example

client, err := sarvamai.NewClient(sarvamai.Config{
    APIKey:     "your-api-key",
    BaseURL:    "https://api.sarvam.ai",  // optional, default: https://api.sarvam.ai
    HTTPClient: &http.Client{Timeout: 30 * time.Second}, // optional
})

type Config

type Config struct {
	APIKey     string
	BaseURL    string
	HTTPClient *http.Client
	MaxRetries int
}

Config holds the configuration for the Sarvam AI client.

Fields

  • APIKey: Your Sarvam AI API key. Required. Get one at https://dashboard.sarvam.ai/key-management
  • BaseURL: The base URL for the API. Defaults to "https://api.sarvam.ai" if empty.
  • HTTPClient: The HTTP client to use for requests. Defaults to a 30-second timeout if nil.
  • MaxRetries: Maximum number of retries for transient errors (429, 5xx). Defaults to 3 if <= 0.

Directories

Path Synopsis
Package chat provides types for the Chat Completions API.
Package chat provides types for the Chat Completions API.
Package docintel provides types for Document Intelligence API requests and responses.
Package docintel provides types for Document Intelligence API requests and responses.
examples
chat-completion command
speech-to-text command
stt-translate command
text-to-speech command
translation command
transliteration command
internal
Package languages provides language codes supported by the Sarvam AI API.
Package languages provides language codes supported by the Sarvam AI API.
shared
speech
Contains types used across multiple packages in the speech domain.
Contains types used across multiple packages in the speech domain.
Package translate provides types for the Speech-to-Text Translate API.
Package translate provides types for the Speech-to-Text Translate API.
Package translatejob provides types for the Speech-to-Text Translate Batch Job API.
Package translatejob provides types for the Speech-to-Text Translate Batch Job API.
Package tts provides types for Text-to-Speech API requests and responses.
Package tts provides types for Text-to-Speech API requests and responses.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL