mygitphase's picture
Add files using upload-large-folder tool
b5c2964 verified
# This file was auto-generated by Fern from our API Definition.
from __future__ import annotations
import os
import typing
import httpx
from .core.api_error import ApiError
from .core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from .core.logging import LogConfig, Logger
from .environment import SarvamAIEnvironment
if typing.TYPE_CHECKING:
from .chat.client import AsyncChatClient, ChatClient
from .document_intelligence.client import AsyncDocumentIntelligenceClient, DocumentIntelligenceClient
from .pronunciation_dictionary.client import AsyncPronunciationDictionaryClient, PronunciationDictionaryClient
from .speech_to_text.client import AsyncSpeechToTextClient, SpeechToTextClient
from .speech_to_text_job.client import AsyncSpeechToTextJobClient, SpeechToTextJobClient
from .speech_to_text_streaming.client import AsyncSpeechToTextStreamingClient, SpeechToTextStreamingClient
from .speech_to_text_translate_job.client import AsyncSpeechToTextTranslateJobClient, SpeechToTextTranslateJobClient
from .speech_to_text_translate_streaming.client import (
AsyncSpeechToTextTranslateStreamingClient,
SpeechToTextTranslateStreamingClient,
)
from .text.client import AsyncTextClient, TextClient
from .text_to_speech.client import AsyncTextToSpeechClient, TextToSpeechClient
from .text_to_speech_streaming.client import AsyncTextToSpeechStreamingClient, TextToSpeechStreamingClient
class SarvamAI:
"""
Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propagate to these functions.
Parameters
----------
environment : SarvamAIEnvironment
The environment to use for requests from the client. from .environment import SarvamAIEnvironment
Defaults to SarvamAIEnvironment.PRODUCTION
api_subscription_key : typing.Optional[str]
headers : typing.Optional[typing.Dict[str, str]]
Additional headers to send with every request.
timeout : typing.Optional[float]
The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced.
follow_redirects : typing.Optional[bool]
Whether the default httpx client follows redirects or not, this is irrelevant if a custom httpx client is passed in.
httpx_client : typing.Optional[httpx.Client]
The httpx client to use for making requests, a preconfigured client is used by default, however this is useful should you want to pass in any custom httpx configuration.
logging : typing.Optional[typing.Union[LogConfig, Logger]]
Configure logging for the SDK. Accepts a LogConfig dict with 'level' (debug/info/warn/error), 'logger' (custom logger implementation), and 'silent' (boolean, defaults to True) fields. You can also pass a pre-configured Logger instance.
Examples
--------
from sarvamai import SarvamAI
client = SarvamAI(
api_subscription_key="YOUR_API_SUBSCRIPTION_KEY",
)
"""
def __init__(
self,
*,
environment: SarvamAIEnvironment = SarvamAIEnvironment.PRODUCTION,
api_subscription_key: typing.Optional[str] = os.getenv("SARVAM_API_KEY"),
headers: typing.Optional[typing.Dict[str, str]] = None,
timeout: typing.Optional[float] = None,
follow_redirects: typing.Optional[bool] = True,
httpx_client: typing.Optional[httpx.Client] = None,
logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,
):
_defaulted_timeout = (
timeout if timeout is not None else 60 if httpx_client is None else httpx_client.timeout.read
)
if api_subscription_key is None:
raise ApiError(
body="The client must be instantiated be either passing in api_subscription_key or setting SARVAM_API_KEY"
)
self._client_wrapper = SyncClientWrapper(
environment=environment,
api_subscription_key=api_subscription_key,
headers=headers,
httpx_client=httpx_client
if httpx_client is not None
else httpx.Client(timeout=_defaulted_timeout, follow_redirects=follow_redirects)
if follow_redirects is not None
else httpx.Client(timeout=_defaulted_timeout),
timeout=_defaulted_timeout,
logging=logging,
)
self._text: typing.Optional[TextClient] = None
self._speech_to_text: typing.Optional[SpeechToTextClient] = None
self._text_to_speech: typing.Optional[TextToSpeechClient] = None
self._pronunciation_dictionary: typing.Optional[PronunciationDictionaryClient] = None
self._chat: typing.Optional[ChatClient] = None
self._speech_to_text_job: typing.Optional[SpeechToTextJobClient] = None
self._speech_to_text_translate_job: typing.Optional[SpeechToTextTranslateJobClient] = None
self._document_intelligence: typing.Optional[DocumentIntelligenceClient] = None
self._speech_to_text_streaming: typing.Optional[SpeechToTextStreamingClient] = None
self._speech_to_text_translate_streaming: typing.Optional[SpeechToTextTranslateStreamingClient] = None
self._text_to_speech_streaming: typing.Optional[TextToSpeechStreamingClient] = None
@property
def text(self):
if self._text is None:
from .text.client import TextClient # noqa: E402
self._text = TextClient(client_wrapper=self._client_wrapper)
return self._text
@property
def speech_to_text(self):
if self._speech_to_text is None:
from .speech_to_text.client import SpeechToTextClient # noqa: E402
self._speech_to_text = SpeechToTextClient(client_wrapper=self._client_wrapper)
return self._speech_to_text
@property
def text_to_speech(self):
if self._text_to_speech is None:
from .text_to_speech.client import TextToSpeechClient # noqa: E402
self._text_to_speech = TextToSpeechClient(client_wrapper=self._client_wrapper)
return self._text_to_speech
@property
def pronunciation_dictionary(self):
if self._pronunciation_dictionary is None:
from .pronunciation_dictionary.client import PronunciationDictionaryClient # noqa: E402
self._pronunciation_dictionary = PronunciationDictionaryClient(client_wrapper=self._client_wrapper)
return self._pronunciation_dictionary
@property
def chat(self):
if self._chat is None:
from .chat.client import ChatClient # noqa: E402
self._chat = ChatClient(client_wrapper=self._client_wrapper)
return self._chat
@property
def speech_to_text_job(self):
if self._speech_to_text_job is None:
from .speech_to_text_job.client import SpeechToTextJobClient # noqa: E402
self._speech_to_text_job = SpeechToTextJobClient(client_wrapper=self._client_wrapper)
return self._speech_to_text_job
@property
def speech_to_text_translate_job(self):
if self._speech_to_text_translate_job is None:
from .speech_to_text_translate_job.client import SpeechToTextTranslateJobClient # noqa: E402
self._speech_to_text_translate_job = SpeechToTextTranslateJobClient(client_wrapper=self._client_wrapper)
return self._speech_to_text_translate_job
@property
def document_intelligence(self):
if self._document_intelligence is None:
from .document_intelligence.client import DocumentIntelligenceClient # noqa: E402
self._document_intelligence = DocumentIntelligenceClient(client_wrapper=self._client_wrapper)
return self._document_intelligence
@property
def speech_to_text_streaming(self):
if self._speech_to_text_streaming is None:
from .speech_to_text_streaming.client import SpeechToTextStreamingClient # noqa: E402
self._speech_to_text_streaming = SpeechToTextStreamingClient(client_wrapper=self._client_wrapper)
return self._speech_to_text_streaming
@property
def speech_to_text_translate_streaming(self):
if self._speech_to_text_translate_streaming is None:
from .speech_to_text_translate_streaming.client import SpeechToTextTranslateStreamingClient # noqa: E402
self._speech_to_text_translate_streaming = SpeechToTextTranslateStreamingClient(
client_wrapper=self._client_wrapper
)
return self._speech_to_text_translate_streaming
@property
def text_to_speech_streaming(self):
if self._text_to_speech_streaming is None:
from .text_to_speech_streaming.client import TextToSpeechStreamingClient # noqa: E402
self._text_to_speech_streaming = TextToSpeechStreamingClient(client_wrapper=self._client_wrapper)
return self._text_to_speech_streaming
class AsyncSarvamAI:
"""
Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propagate to these functions.
Parameters
----------
environment : SarvamAIEnvironment
The environment to use for requests from the client. from .environment import SarvamAIEnvironment
Defaults to SarvamAIEnvironment.PRODUCTION
api_subscription_key : typing.Optional[str]
headers : typing.Optional[typing.Dict[str, str]]
Additional headers to send with every request.
timeout : typing.Optional[float]
The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced.
follow_redirects : typing.Optional[bool]
Whether the default httpx client follows redirects or not, this is irrelevant if a custom httpx client is passed in.
httpx_client : typing.Optional[httpx.AsyncClient]
The httpx client to use for making requests, a preconfigured client is used by default, however this is useful should you want to pass in any custom httpx configuration.
logging : typing.Optional[typing.Union[LogConfig, Logger]]
Configure logging for the SDK. Accepts a LogConfig dict with 'level' (debug/info/warn/error), 'logger' (custom logger implementation), and 'silent' (boolean, defaults to True) fields. You can also pass a pre-configured Logger instance.
Examples
--------
from sarvamai import AsyncSarvamAI
client = AsyncSarvamAI(
api_subscription_key="YOUR_API_SUBSCRIPTION_KEY",
)
"""
def __init__(
self,
*,
environment: SarvamAIEnvironment = SarvamAIEnvironment.PRODUCTION,
api_subscription_key: typing.Optional[str] = os.getenv("SARVAM_API_KEY"),
headers: typing.Optional[typing.Dict[str, str]] = None,
timeout: typing.Optional[float] = None,
follow_redirects: typing.Optional[bool] = True,
httpx_client: typing.Optional[httpx.AsyncClient] = None,
logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,
):
_defaulted_timeout = (
timeout if timeout is not None else 60 if httpx_client is None else httpx_client.timeout.read
)
if api_subscription_key is None:
raise ApiError(
body="The client must be instantiated be either passing in api_subscription_key or setting SARVAM_API_KEY"
)
self._client_wrapper = AsyncClientWrapper(
environment=environment,
api_subscription_key=api_subscription_key,
headers=headers,
httpx_client=httpx_client
if httpx_client is not None
else httpx.AsyncClient(timeout=_defaulted_timeout, follow_redirects=follow_redirects)
if follow_redirects is not None
else httpx.AsyncClient(timeout=_defaulted_timeout),
timeout=_defaulted_timeout,
logging=logging,
)
self._text: typing.Optional[AsyncTextClient] = None
self._speech_to_text: typing.Optional[AsyncSpeechToTextClient] = None
self._text_to_speech: typing.Optional[AsyncTextToSpeechClient] = None
self._pronunciation_dictionary: typing.Optional[AsyncPronunciationDictionaryClient] = None
self._chat: typing.Optional[AsyncChatClient] = None
self._speech_to_text_job: typing.Optional[AsyncSpeechToTextJobClient] = None
self._speech_to_text_translate_job: typing.Optional[AsyncSpeechToTextTranslateJobClient] = None
self._document_intelligence: typing.Optional[AsyncDocumentIntelligenceClient] = None
self._speech_to_text_streaming: typing.Optional[AsyncSpeechToTextStreamingClient] = None
self._speech_to_text_translate_streaming: typing.Optional[AsyncSpeechToTextTranslateStreamingClient] = None
self._text_to_speech_streaming: typing.Optional[AsyncTextToSpeechStreamingClient] = None
@property
def text(self):
if self._text is None:
from .text.client import AsyncTextClient # noqa: E402
self._text = AsyncTextClient(client_wrapper=self._client_wrapper)
return self._text
@property
def speech_to_text(self):
if self._speech_to_text is None:
from .speech_to_text.client import AsyncSpeechToTextClient # noqa: E402
self._speech_to_text = AsyncSpeechToTextClient(client_wrapper=self._client_wrapper)
return self._speech_to_text
@property
def text_to_speech(self):
if self._text_to_speech is None:
from .text_to_speech.client import AsyncTextToSpeechClient # noqa: E402
self._text_to_speech = AsyncTextToSpeechClient(client_wrapper=self._client_wrapper)
return self._text_to_speech
@property
def pronunciation_dictionary(self):
if self._pronunciation_dictionary is None:
from .pronunciation_dictionary.client import AsyncPronunciationDictionaryClient # noqa: E402
self._pronunciation_dictionary = AsyncPronunciationDictionaryClient(client_wrapper=self._client_wrapper)
return self._pronunciation_dictionary
@property
def chat(self):
if self._chat is None:
from .chat.client import AsyncChatClient # noqa: E402
self._chat = AsyncChatClient(client_wrapper=self._client_wrapper)
return self._chat
@property
def speech_to_text_job(self):
if self._speech_to_text_job is None:
from .speech_to_text_job.client import AsyncSpeechToTextJobClient # noqa: E402
self._speech_to_text_job = AsyncSpeechToTextJobClient(client_wrapper=self._client_wrapper)
return self._speech_to_text_job
@property
def speech_to_text_translate_job(self):
if self._speech_to_text_translate_job is None:
from .speech_to_text_translate_job.client import AsyncSpeechToTextTranslateJobClient # noqa: E402
self._speech_to_text_translate_job = AsyncSpeechToTextTranslateJobClient(
client_wrapper=self._client_wrapper
)
return self._speech_to_text_translate_job
@property
def document_intelligence(self):
if self._document_intelligence is None:
from .document_intelligence.client import AsyncDocumentIntelligenceClient # noqa: E402
self._document_intelligence = AsyncDocumentIntelligenceClient(client_wrapper=self._client_wrapper)
return self._document_intelligence
@property
def speech_to_text_streaming(self):
if self._speech_to_text_streaming is None:
from .speech_to_text_streaming.client import AsyncSpeechToTextStreamingClient # noqa: E402
self._speech_to_text_streaming = AsyncSpeechToTextStreamingClient(client_wrapper=self._client_wrapper)
return self._speech_to_text_streaming
@property
def speech_to_text_translate_streaming(self):
if self._speech_to_text_translate_streaming is None:
from .speech_to_text_translate_streaming.client import (
AsyncSpeechToTextTranslateStreamingClient, # noqa: E402
)
self._speech_to_text_translate_streaming = AsyncSpeechToTextTranslateStreamingClient(
client_wrapper=self._client_wrapper
)
return self._speech_to_text_translate_streaming
@property
def text_to_speech_streaming(self):
if self._text_to_speech_streaming is None:
from .text_to_speech_streaming.client import AsyncTextToSpeechStreamingClient # noqa: E402
self._text_to_speech_streaming = AsyncTextToSpeechStreamingClient(client_wrapper=self._client_wrapper)
return self._text_to_speech_streaming