sarvam

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2025 License: MIT Imports: 8 Imported by: 0

README ΒΆ

SarvamAI Go SDK

An unofficial Go SDK for the Sarvam AI APIs, providing easy access to Indian language AI services including translation, text-to-speech, chat completions, and language identification.

🌟 API Parity (wip)

  • Text Translation
  • Language Identification
  • Text-to-Speech
  • Chat Completions
  • Transliteration
  • Speech to text
  • Speech to text translation

πŸš€ Quick Start

Installation
go get code.abhai.dev/sarvam

πŸ“– Examples

Check out the examples directory for complete working examples:

🀝 Contributing

We welcome contributions! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Development Setup
  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m 'Add amazing feature'
  4. Push to the branch: git push origin feature/amazing-feature
  5. Open a Pull Request

πŸ“„ License

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

πŸ†˜ Support

If you encounter any issues or have questions:

  1. Check the examples directory
  2. Review the API documentation
  3. Open an issue on GitHub

Disclaimer

This SDK is an unofficial client for the Sarvam API and is not affiliated with, endorsed by, or maintained by Sarvam.

All trademarks, service marks, and copyrights associated with Sarvam belong to their respective owners.

Use this SDK at your own risk. Please review and comply with Sarvam’s terms of service and API usage policies.


Made with ❀️ for the Indian AI community

Documentation ΒΆ

Overview ΒΆ

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

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func Bool ΒΆ

func Bool(b bool) *bool

Bool returns a pointer to the boolean value.

func Float64 ΒΆ

func Float64(f float64) *float64

Float64 returns a pointer to the float64 value.

func Int ΒΆ

func Int(i int) *int

Int returns a pointer to the int value.

func String ΒΆ

func String(s string) *string

String returns a pointer to the string value.

Types ΒΆ

type ChatCompletionChoice ΒΆ

type ChatCompletionChoice struct {
	FinishReason string  `json:"finish_reason"`
	Index        int     `json:"index"`
	Message      Message `json:"message"`
}

ChatCompletionChoice represents a single completion choice.

type ChatCompletionModel ΒΆ

type ChatCompletionModel string

ChatCompletionModel specifies the model to use for chat completions.

var (
	ChatCompletionModelBulbulV2 ChatCompletionModel = "bulbul:v2"
	ChatCompletionModelSarvamM  ChatCompletionModel = "sarvam-m"
)

type ChatCompletionRequest ΒΆ

type ChatCompletionRequest struct {
	Messages         []Message           `json:"messages"`
	Model            ChatCompletionModel `json:"model"`
	Temperature      *float64            `json:"temperature,omitempty"`
	TopP             *float64            `json:"top_p,omitempty"`
	ReasoningEffort  *ReasoningEffort    `json:"reasoning_effort,omitempty"`
	MaxTokens        *int                `json:"max_tokens,omitempty"`
	Stream           *bool               `json:"stream,omitempty"`
	Stop             interface{}         `json:"stop,omitempty"` // string or []string
	N                *int                `json:"n,omitempty"`
	Seed             *int64              `json:"seed,omitempty"`
	FrequencyPenalty *float64            `json:"frequency_penalty,omitempty"`
	PresencePenalty  *float64            `json:"presence_penalty,omitempty"`
	WikiGrounding    *bool               `json:"wiki_grounding,omitempty"`
}

ChatCompletionRequest represents the request payload for chat completions.

type ChatCompletionResponse ΒΆ

type ChatCompletionResponse struct {
	ID      string                 `json:"id"`
	Choices []ChatCompletionChoice `json:"choices"`
	Created int64                  `json:"created"`
	Model   string                 `json:"model"`
	Object  string                 `json:"object"`
	Usage   *Usage                 `json:"usage"`
}

ChatCompletionResponse represents the response from the chat completions API.

func (*ChatCompletionResponse) GetChoiceContent ΒΆ

func (r *ChatCompletionResponse) GetChoiceContent(index int) string

GetChoiceContent returns the content of a specific choice by index.

func (*ChatCompletionResponse) GetFirstChoiceContent ΒΆ

func (r *ChatCompletionResponse) GetFirstChoiceContent() string

GetFirstChoiceContent returns the content of the first choice from the response.

type Client ΒΆ

type Client struct {
	// contains filtered or unexported fields
}

Client represents a Sarvam AI API client.

func NewClient ΒΆ

func NewClient(apiKey string) *Client

NewClient creates a new Sarvam AI client with the provided API key.

func (*Client) ChatCompletion ΒΆ

func (c *Client) ChatCompletion(req *ChatCompletionRequest) (*ChatCompletionResponse, error)

ChatCompletion creates a chat completion using the Sarvam AI API.

func (*Client) ChatCompletionWithOptions ΒΆ

func (c *Client) ChatCompletionWithOptions(messages []Message, model ChatCompletionModel, options map[string]interface{}) (*ChatCompletionResponse, error)

ChatCompletionWithOptions creates a chat completion with custom options.

func (*Client) IdentifyLanguage ΒΆ

func (c *Client) IdentifyLanguage(input string) (*LanguageIdentification, error)

IdentifyLanguage identifies the language (e.g., en-IN, hi-IN) and script (e.g., Latin, Devanagari) of the input text, supporting multiple languages.

func (*Client) SetBaseURL ΒΆ

func (c *Client) SetBaseURL(baseURL string)

SetBaseURL allows customization of the API endpoint URL.

func (*Client) SimpleChatCompletion ΒΆ

func (c *Client) SimpleChatCompletion(messages []Message, model ChatCompletionModel) (*ChatCompletionResponse, error)

SimpleChatCompletion is a convenience function for simple chat completions.

func (*Client) SpeechToText ΒΆ

func (c *Client) SpeechToText(params SpeechToTextParams) (*SpeechToText, error)

SpeechToText converts speech from an audio file to text.

func (*Client) SpeechToTextTranslate ΒΆ

func (c *Client) SpeechToTextTranslate(params SpeechToTextTranslateParams) (*SpeechToTextTranslate, error)

SpeechToTextTranslate automatically detects the input language, transcribes the speech, and translates the text to English.

func (*Client) TextToSpeech ΒΆ

func (c *Client) TextToSpeech(params TextToSpeechParams) (*TextToSpeech, error)

TextToSpeech converts text to speech in the specified language.

func (*Client) Translate ΒΆ

func (c *Client) Translate(input string, sourceLanguageCode, targetLanguageCode Language) (*Translation, error)

Translate converts text from one language to another while preserving its meaning. This is the simple version that uses default options.

func (*Client) TranslateWithOptions ΒΆ

func (c *Client) TranslateWithOptions(params *TranslateParams) (*Translation, error)

TranslateWithOptions converts text from one language to another with custom options.

func (*Client) Transliterate ΒΆ

func (c *Client) Transliterate(input string, sourceLanguage Language, targetLanguage Language) (*Transliteration, error)

Transliterate converts text from one script to another while preserving the original pronunciation.

type DiarizedEntry ΒΆ

type DiarizedEntry struct {
	Transcript       string  `json:"transcript"`
	StartTimeSeconds float64 `json:"start_time_seconds"`
	EndTimeSeconds   float64 `json:"end_time_seconds"`
	SpeakerID        string  `json:"speaker_id"`
}

DiarizedEntry represents a single speaker's transcript segment.

type DiarizedTranscript ΒΆ

type DiarizedTranscript struct {
	Entries []DiarizedEntry `json:"entries"`
}

DiarizedTranscript represents the complete diarized transcript.

type ErrInputTooLong ΒΆ

type ErrInputTooLong struct {
	InputLength int
	MaxLength   int
}

ErrInputTooLong is returned when the input length is greater than or equal to 1000 characters.

func (*ErrInputTooLong) Error ΒΆ

func (e *ErrInputTooLong) Error() string

type HTTPError ΒΆ

type HTTPError struct {
	StatusCode int
	Message    string
	Code       string
	RequestID  string
}

HTTPError represents an error response from the Sarvam AI API.

func (*HTTPError) Error ΒΆ

func (e *HTTPError) Error() string

Error implements the error interface for HTTPError.

type Language ΒΆ

type Language string

Language represents a supported language code.

const (
	LanguageAssamese  Language = "as-IN"
	LanguageBengali   Language = "bn-IN"
	LanguageBodo      Language = "brx-IN"
	LanguageDogri     Language = "doi-IN"
	LanguageEnglish   Language = "en-IN"
	LanguageGujarati  Language = "gu-IN"
	LanguageHindi     Language = "hi-IN"
	LanguageKannada   Language = "kn-IN"
	LanguageKashmiri  Language = "ks-IN"
	LanguageKonkani   Language = "kok-IN"
	LanguageMaithili  Language = "mai-IN"
	LanguageMalayalam Language = "ml-IN"
	LanguageManipuri  Language = "mni-IN"
	LanguageMarathi   Language = "mr-IN"
	LanguageNepali    Language = "ne-IN"
	LanguageOdia      Language = "od-IN"
	LanguagePunjabi   Language = "pa-IN"
	LanguageSanskrit  Language = "sa-IN"
	LanguageSantali   Language = "sat-IN"
	LanguageSindhi    Language = "sd-IN"
	LanguageTamil     Language = "ta-IN"
	LanguageTelugu    Language = "te-IN"
	LanguageUrdu      Language = "ur-IN"
)
const LanguageAuto Language = "auto"

func (Language) String ΒΆ

func (l Language) String() string

String returns the human-readable name of the language.

type LanguageIdentification ΒΆ

type LanguageIdentification struct {
	RequestId string
	Language  Language
	Script    Script
}

LanguageIdentification represents the result of language identification.

type Message ΒΆ

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

Message represents a message in the chat conversation.

type NumeralsFormat ΒΆ

type NumeralsFormat string

NumeralsFormat specifies the format for numerals in the translation.

const (
	// NumeralsFormatInternational represents international numeral format.
	NumeralsFormatInternational NumeralsFormat = "international"
	// NumeralsFormatNative represents native numeral format.
	NumeralsFormatNative NumeralsFormat = "native"
)

type OutputScript ΒΆ

type OutputScript string

OutputScript controls the transliteration style applied to the output text.

const (
	// OutputScriptRoman represents Roman script output.
	OutputScriptRoman OutputScript = "roman"
	// OutputScriptFullyNative represents fully native script output.
	OutputScriptFullyNative OutputScript = "fully-native"
	// OutputScriptSpokenFormInNative represents spoken form in native script output.
	OutputScriptSpokenFormInNative OutputScript = "spoken-form-in-native"
)

type ReasoningEffort ΒΆ

type ReasoningEffort string

ReasoningEffort represents the reasoning effort level for chat completions.

const (
	ReasoningEffortLow    ReasoningEffort = "low"
	ReasoningEffortMedium ReasoningEffort = "medium"
	ReasoningEffortHigh   ReasoningEffort = "high"
)

type Script ΒΆ

type Script string

Script represents a writing script.

const (
	ScriptLatin      Script = "Latn"
	ScriptDevanagari Script = "Deva"
	ScriptBengali    Script = "Beng"
	ScriptGujarati   Script = "Gujr"
	ScriptKannada    Script = "Knda"
	ScriptMalayalam  Script = "Mlym"
	ScriptOdia       Script = "Orya"
	ScriptGurmukhi   Script = "Guru"
	ScriptTamil      Script = "Taml"
	ScriptTelugu     Script = "Telu"
)

func (Script) String ΒΆ

func (s Script) String() string

type Speaker ΒΆ

type Speaker string

Speaker represents a voice/speaker for text-to-speech conversion.

var (
	SpeakerAnushka  Speaker = "anushka"
	SpeakerAbhilash Speaker = "abhilash"
	SpeakerManisha  Speaker = "manisha"
	SpeakerVidya    Speaker = "vidya"
	SpeakerArya     Speaker = "arya"
	SpeakerKarun    Speaker = "karun"
	SpeakerHitesh   Speaker = "hitesh"
)

type SpeakerGender ΒΆ

type SpeakerGender string

SpeakerGender represents the gender of the speaker for better translations.

const (
	SpeakerGenderMale   SpeakerGender = "Male"
	SpeakerGenderFemale SpeakerGender = "Female"
)

type SpeechSampleRate ΒΆ

type SpeechSampleRate int

SpeechSampleRate represents the audio sample rate for text-to-speech output.

var (
	SpeechSampleRate8000  SpeechSampleRate = 8000
	SpeechSampleRate16000 SpeechSampleRate = 16000
	SpeechSampleRate22050 SpeechSampleRate = 22050
	SpeechSampleRate24000 SpeechSampleRate = 24000
)

type SpeechToText ΒΆ

type SpeechToText struct {
	RequestId          string              `json:"request_id"`
	Transcript         string              `json:"transcript"`
	Timestamps         *Timestamps         `json:"timestamps,omitempty"`
	DiarizedTranscript *DiarizedTranscript `json:"diarized_transcript,omitempty"`
	LanguageCode       Language            `json:"language_code"`
}

SpeechToText represents the result of a speech-to-text operation.

func (*SpeechToText) String ΒΆ

func (s *SpeechToText) String() string

String returns the transcribed text.

type SpeechToTextModel ΒΆ

type SpeechToTextModel string

SpeechToTextModel specifies the model to use for speech-to-text conversion.

var (
	SpeechToTextModelSaarikaV1     SpeechToTextModel = "saarika:v1"
	SpeechToTextModelSaarikaV2     SpeechToTextModel = "saarika:v2"
	SpeechToTextModelSaarikaV2dot5 SpeechToTextModel = "saarika:v2.5"
	SpeechToTextModelSaarikaFlash  SpeechToTextModel = "saarika:flash"
)

type SpeechToTextParams ΒΆ

type SpeechToTextParams struct {
	FilePath       string             // Required: Path to the audio file
	Model          *SpeechToTextModel // Optional: Model to use (default: saarika:v2.5)
	LanguageCode   *Language          // Optional: Language code for the input audio
	WithTimestamps *bool              // Optional: Whether to include timestamps in response
}

SpeechToTextParams contains parameters for speech-to-text conversion.

type SpeechToTextTranslate ΒΆ

type SpeechToTextTranslate struct {
	RequestId          string              `json:"request_id"`
	Transcript         string              `json:"transcript"`
	LanguageCode       Language            `json:"language_code"`
	DiarizedTranscript *DiarizedTranscript `json:"diarized_transcript,omitempty"`
}

SpeechToTextTranslate represents the result of a speech-to-text-translate operation.

func (*SpeechToTextTranslate) String ΒΆ

func (s *SpeechToTextTranslate) String() string

String returns the transcribed and translated text.

type SpeechToTextTranslateModel ΒΆ

type SpeechToTextTranslateModel string

SpeechToTextTranslateModel specifies the model to use for speech-to-text with translation.

var (
	SpeechToTextTranslateModelSaarasV1     SpeechToTextTranslateModel = "saaras:v1"
	SpeechToTextTranslateModelSaarasV2     SpeechToTextTranslateModel = "saaras:v2"
	SpeechToTextTranslateModelSaarasV2dot5 SpeechToTextTranslateModel = "saaras:v2.5"
	SpeechToTextTranslateModelSaarasFlash  SpeechToTextTranslateModel = "saaras:flash"
)

type SpeechToTextTranslateParams ΒΆ

type SpeechToTextTranslateParams struct {
	FilePath string                      // Required: Path to the audio file
	Prompt   *string                     // Optional: Conversation context to boost model accuracy
	Model    *SpeechToTextTranslateModel // Optional: Model to use for speech-to-text conversion
}

SpeechToTextTranslateParams contains parameters for speech-to-text-translate conversion.

type TextToSpeech ΒΆ

type TextToSpeech struct {
	RequestId string
	Audios    []string
	Data      []byte
}

TextToSpeech represents the result of a text-to-speech operation.

func (*TextToSpeech) Save ΒΆ

func (t *TextToSpeech) Save(filename string) error

Save saves the text-to-speech data as a WAV file.

type TextToSpeechModel ΒΆ

type TextToSpeechModel string

TextToSpeechModel specifies the model to use for text-to-speech conversion.

var (
	TextToSpeechModelBulbulV2 TextToSpeechModel = "bulbul:v2"
)

type TextToSpeechParams ΒΆ

type TextToSpeechParams struct {
	Text                string
	TargetLanguage      Language
	Speaker             *Speaker
	Pitch               *float64
	Pace                *float64
	Loudness            *float64
	SpeechSampleRate    *SpeechSampleRate
	EnablePreprocessing *bool
	Model               *TextToSpeechModel
}

TextToSpeechParams contains all parameters for text-to-speech conversion.

type Timestamps ΒΆ

type Timestamps struct {
	Words            []string  `json:"words"`
	StartTimeSeconds []float64 `json:"start_time_seconds"`
	EndTimeSeconds   []float64 `json:"end_time_seconds"`
}

Timestamps represents word-level timing information for speech-to-text results.

type TranslateParams ΒΆ

type TranslateParams struct {
	Input               string
	SourceLanguage      Language
	TargetLanguage      Language
	SpeakerGender       *SpeakerGender
	Mode                *TranslationMode
	Model               *TranslationModel
	EnablePreprocessing *bool
	OutputScript        *OutputScript
	NumeralsFormat      *NumeralsFormat
}

TranslateParams contains all optional parameters for translation.

type Translation ΒΆ

type Translation struct {
	RequestId      string
	TranslatedText string
	SourceLanguage Language
}

Translation represents the result of a translation operation.

func (*Translation) String ΒΆ

func (t *Translation) String() string

String returns the translated text.

type TranslationMode ΒΆ

type TranslationMode string

TranslationMode specifies the tone or style of the translation.

const (
	// TranslationModeFormal represents formal translation style.
	TranslationModeFormal TranslationMode = "formal"
	// TranslationModeModernColloquial represents modern colloquial translation style.
	TranslationModeModernColloquial TranslationMode = "modern-colloquial"
	// TranslationModeClassicColloquial represents classic colloquial translation style.
	TranslationModeClassicColloquial TranslationMode = "classic-colloquial"
	// TranslationModeCodeMixed represents code-mixed translation style.
	TranslationModeCodeMixed TranslationMode = "code-mixed"
)

type TranslationModel ΒΆ

type TranslationModel string

TranslationModel specifies the translation model to use.

var (
	TranslationModelMayuraV1        TranslationModel = "mayura:v1"
	TranslationModelSarvamTranslate TranslationModel = "sarvam-translate:v1"
)

type Transliteration ΒΆ

type Transliteration struct {
	RequestId          string
	TransliteratedText string
	SourceLanguage     Language
}

Transliteration represents the result of a transliteration operation.

func (*Transliteration) String ΒΆ

func (t *Transliteration) String() string

String returns the transliterated text.

type Usage ΒΆ

type Usage struct {
	CompletionTokens int `json:"completion_tokens"`
	PromptTokens     int `json:"prompt_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

Usage represents token usage information for the API call.

Directories ΒΆ

Path Synopsis
examples
chatcompletions command
speechtotext command
text command
texttospeech command

Jump to

Keyboard shortcuts

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