Actively maintained. Last release 3 days ago. A safe choice.
pip install sarvamaiSarvam Python Library
The Sarvam Python library provides convenient access to the Sarvam API from Python.
Documentation
API reference documentation is available here.
Installation
pip install sarvamai
Reference
A full reference for this library is available here.
Usage
Instantiate and use the client with the following:
from sarvamai import SarvamAI
client = SarvamAI(
api_subscription_key="YOUR_API_SUBSCRIPTION_KEY",
)
client.text.translate(
input="input",
source_language_code="auto",
target_language_code="bn-IN",
)
Async Client
The SDK also exports an async client so that you can make non-blocking calls to our API.
import asyncio
from sarvamai import AsyncSarvamAI
client = AsyncSarvamAI(
api_subscription_key="YOUR_API_SUBSCRIPTION_KEY",
)
async def main() -> None:
await client.text.translate(
input="input",
source_language_code="auto",
target_language_code="bn-IN",
)
asyncio.run(main())
Exception Handling
When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error will be thrown.
from sarvamai.core.api_error import ApiError
try:
client.text.translate(...)
except ApiError as e:
print(e.status_code)
print(e.body)
Advanced
Retries
The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retryable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).
A request is deemed retryable when any of the following HTTP status codes is returned:
- 408 (Timeout)