classAzureOpenAIEmbedding(OpenAIEmbedding):azure_endpoint:Optional[str]=Field(default=None,description="The Azure endpoint to use.",validate_default=True)azure_deployment:Optional[str]=Field(default=None,description="The Azure deployment to use.",validate_default=True)api_base:str=Field(default="",description="The base URL for Azure deployment.",validate_default=True,)api_version:str=Field(default="",description="The version for Azure OpenAI API.",validate_default=True,)azure_ad_token_provider:Optional[AnnotatedProvider]=Field(default=None,description="Callback function to provide Azure AD token.",exclude=True,)use_azure_ad:bool=Field(description="Indicates if Microsoft Entra ID (former Azure AD) is used for token authentication")_azure_ad_token:Any=PrivateAttr(default=None)_client:AzureOpenAI=PrivateAttr()_aclient:AsyncAzureOpenAI=PrivateAttr()def__init__(self,mode:str=OpenAIEmbeddingMode.TEXT_SEARCH_MODE,model:str=OpenAIEmbeddingModelType.TEXT_EMBED_ADA_002,embed_batch_size:int=DEFAULT_EMBED_BATCH_SIZE,additional_kwargs:Optional[Dict[str,Any]]=None,api_key:Optional[str]=None,api_version:Optional[str]=None,api_base:Optional[str]=None,# azure specificazure_endpoint:Optional[str]=None,azure_deployment:Optional[str]=None,azure_ad_token_provider:Optional[AzureADTokenProvider]=None,use_azure_ad:bool=False,deployment_name:Optional[str]=None,max_retries:int=10,reuse_client:bool=True,callback_manager:Optional[CallbackManager]=None,num_workers:Optional[int]=None,# custom httpx clienthttp_client:Optional[httpx.Client]=None,async_http_client:Optional[httpx.AsyncClient]=None,**kwargs:Any,):azure_endpoint=get_from_param_or_env("azure_endpoint",azure_endpoint,"AZURE_OPENAI_ENDPOINT","")# OpenAI base_url and azure_endpoint are mutually exclusiveifapi_base:azure_endpoint=Noneazure_deployment=resolve_from_aliases(azure_deployment,deployment_name,)super().__init__(mode=mode,model=model,embed_batch_size=embed_batch_size,additional_kwargs=additional_kwargs,api_key=api_key,api_version=api_version,api_base=api_base,azure_endpoint=azure_endpoint,azure_deployment=azure_deployment,azure_ad_token_provider=azure_ad_token_provider,use_azure_ad=use_azure_ad,max_retries=max_retries,reuse_client=reuse_client,callback_manager=callback_manager,http_client=http_client,async_http_client=async_http_client,num_workers=num_workers,**kwargs,)# reset api_base to None if it is the defaultifself.api_base==DEFAULT_OPENAI_API_BASE:self.api_base=None@model_validator(mode="before")@classmethoddefvalidate_env(cls,values:Dict[str,Any])->Dict[str,Any]:"""Validate necessary credentials are set."""if(values.get("api_base")=="https://api.openai.com/v1"andvalues.get("azure_endpoint")isNone):raiseValueError("You must set OPENAI_API_BASE to your Azure endpoint. ""It should look like https://YOUR_RESOURCE_NAME.openai.azure.com/")ifvalues.get("api_version")isNone:raiseValueError("You must set OPENAI_API_VERSION for Azure OpenAI.")returnvaluesdef_get_client(self)->AzureOpenAI:ifnotself.reuse_client:returnAzureOpenAI(**self._get_credential_kwargs())ifself._clientisNone:self._client=AzureOpenAI(**self._get_credential_kwargs())returnself._clientdef_get_aclient(self)->AsyncAzureOpenAI:ifnotself.reuse_client:returnAsyncAzureOpenAI(**self._get_credential_kwargs(is_async=True))ifself._aclientisNone:self._aclient=AsyncAzureOpenAI(**self._get_credential_kwargs(is_async=True))returnself._aclientdef_get_credential_kwargs(self,is_async:bool=False)->Dict[str,Any]:ifself.use_azure_ad:self._azure_ad_token=refresh_openai_azuread_token(self._azure_ad_token)self.api_key=self._azure_ad_token.tokenelse:self.api_key=get_from_param_or_env("api_key",self.api_key,"AZURE_OPENAI_API_KEY")return{"api_key":self.api_key,"azure_ad_token_provider":self.azure_ad_token_provider,"azure_endpoint":self.azure_endpoint,"azure_deployment":self.azure_deployment,"base_url":self.api_base,"api_version":self.api_version,"default_headers":self.default_headers,"http_client":self._async_http_clientifis_asyncelseself._http_client,}@classmethoddefclass_name(cls)->str:return"AzureOpenAIEmbedding"
Source code in llama-index-integrations/embeddings/llama-index-embeddings-azure-openai/llama_index/embeddings/azure_openai/base.py
128129130131132133134135136137138139140141142143
@model_validator(mode="before")@classmethoddefvalidate_env(cls,values:Dict[str,Any])->Dict[str,Any]:"""Validate necessary credentials are set."""if(values.get("api_base")=="https://api.openai.com/v1"andvalues.get("azure_endpoint")isNone):raiseValueError("You must set OPENAI_API_BASE to your Azure endpoint. ""It should look like https://YOUR_RESOURCE_NAME.openai.azure.com/")ifvalues.get("api_version")isNone:raiseValueError("You must set OPENAI_API_VERSION for Azure OpenAI.")returnvalues