[docs]@deprecated(since="0.0.10",removal="1.0",alternative_import="langchain_openai.AzureChatOpenAI",)classAzureChatOpenAI(ChatOpenAI):"""`Azure OpenAI` Chat Completion API. To use this class you must have a deployed model on Azure OpenAI. Use `deployment_name` in the constructor to refer to the "Model deployment name" in the Azure portal. In addition, you should have the ``openai`` python package installed, and the following environment variables set or passed in constructor in lower case: - ``AZURE_OPENAI_API_KEY`` - ``AZURE_OPENAI_ENDPOINT`` - ``AZURE_OPENAI_AD_TOKEN`` - ``OPENAI_API_VERSION`` - ``OPENAI_PROXY`` For example, if you have `gpt-35-turbo` deployed, with the deployment name `35-turbo-dev`, the constructor should look like: .. code-block:: python AzureChatOpenAI( azure_deployment="35-turbo-dev", openai_api_version="2023-05-15", ) Be aware the API version may change. You can also specify the version of the model using ``model_version`` constructor parameter, as Azure OpenAI doesn't return model version with the response. Default is empty. When you specify the version, it will be appended to the model name in the response. Setting correct version will help you to calculate the cost properly. Model version is not validated, so make sure you set it correctly to get the correct cost. Any parameters that are valid to be passed to the openai.create call can be passed in, even if not explicitly saved on this class. """azure_endpoint:Union[str,None]=None"""Your Azure endpoint, including the resource. Automatically inferred from env var `AZURE_OPENAI_ENDPOINT` if not provided. Example: `https://example-resource.azure.openai.com/` """deployment_name:Union[str,None]=Field(default=None,alias="azure_deployment")"""A model deployment. If given sets the base client URL to include `/deployments/{azure_deployment}`. Note: this means you won't be able to use non-deployment endpoints. """openai_api_version:str=Field(default="",alias="api_version")"""Automatically inferred from env var `OPENAI_API_VERSION` if not provided."""openai_api_key:Union[str,None]=Field(default=None,alias="api_key")"""Automatically inferred from env var `AZURE_OPENAI_API_KEY` if not provided."""azure_ad_token:Union[str,None]=None"""Your Azure Active Directory token. Automatically inferred from env var `AZURE_OPENAI_AD_TOKEN` if not provided. For more: https://www.microsoft.com/en-us/security/business/identity-access/microsoft-entra-id. """azure_ad_token_provider:Union[Callable[[],str],None]=None"""A function that returns an Azure Active Directory token. Will be invoked on every sync request. For async requests, will be invoked if `azure_ad_async_token_provider` is not provided. """azure_ad_async_token_provider:Union[Callable[[],Awaitable[str]],None]=None"""A function that returns an Azure Active Directory token. Will be invoked on every async request. """model_version:str="""""Legacy, for openai<1.0.0 support."""openai_api_type:str="""""Legacy, for openai<1.0.0 support."""validate_base_url:bool=True"""For backwards compatibility. If legacy val openai_api_base is passed in, try to infer if it is a base_url or azure_endpoint and update accordingly. """@classmethoddefget_lc_namespace(cls)->List[str]:"""Get the namespace of the langchain object."""return["langchain","chat_models","azure_openai"]
[docs]@pre_initdefvalidate_environment(cls,values:Dict)->Dict:"""Validate that api key and python package exists in environment."""ifvalues["n"]<1:raiseValueError("n must be at least 1.")ifvalues["n"]>1andvalues["streaming"]:raiseValueError("n must be 1 when streaming.")# Check OPENAI_KEY for backwards compatibility.# TODO: Remove OPENAI_API_KEY support to avoid possible conflict when using# other forms of azure credentials.values["openai_api_key"]=(values["openai_api_key"]oros.getenv("AZURE_OPENAI_API_KEY")oros.getenv("OPENAI_API_KEY"))values["openai_api_base"]=values["openai_api_base"]oros.getenv("OPENAI_API_BASE")values["openai_api_version"]=values["openai_api_version"]oros.getenv("OPENAI_API_VERSION")# Check OPENAI_ORGANIZATION for backwards compatibility.values["openai_organization"]=(values["openai_organization"]oros.getenv("OPENAI_ORG_ID")oros.getenv("OPENAI_ORGANIZATION"))values["azure_endpoint"]=values["azure_endpoint"]oros.getenv("AZURE_OPENAI_ENDPOINT")values["azure_ad_token"]=values["azure_ad_token"]oros.getenv("AZURE_OPENAI_AD_TOKEN")values["openai_api_type"]=get_from_dict_or_env(values,"openai_api_type","OPENAI_API_TYPE",default="azure")values["openai_proxy"]=get_from_dict_or_env(values,"openai_proxy","OPENAI_PROXY",default="")try:importopenaiexceptImportError:raiseImportError("Could not import openai python package. ""Please install it with `pip install openai`.")ifis_openai_v1():# For backwards compatibility. Before openai v1, no distinction was made# between azure_endpoint and base_url (openai_api_base).openai_api_base=values["openai_api_base"]ifopenai_api_baseandvalues["validate_base_url"]:if"/openai"notinopenai_api_base:values["openai_api_base"]=(values["openai_api_base"].rstrip("/")+"/openai")warnings.warn("As of openai>=1.0.0, Azure endpoints should be specified via "f"the `azure_endpoint` param not `openai_api_base` "f"(or alias `base_url`). Updating `openai_api_base` from "f"{openai_api_base} to {values['openai_api_base']}.")ifvalues["deployment_name"]:warnings.warn("As of openai>=1.0.0, if `deployment_name` (or alias ""`azure_deployment`) is specified then ""`openai_api_base` (or alias `base_url`) should not be. ""Instead use `deployment_name` (or alias `azure_deployment`) ""and `azure_endpoint`.")ifvalues["deployment_name"]notinvalues["openai_api_base"]:warnings.warn("As of openai>=1.0.0, if `openai_api_base` ""(or alias `base_url`) is specified it is expected to be ""of the form ""https://example-resource.azure.openai.com/openai/deployments/example-deployment. "# noqa: E501f"Updating {openai_api_base} to "f"{values['openai_api_base']}.")values["openai_api_base"]+=("/deployments/"+values["deployment_name"])values["deployment_name"]=Noneclient_params={"api_version":values["openai_api_version"],"azure_endpoint":values["azure_endpoint"],"azure_deployment":values["deployment_name"],"api_key":values["openai_api_key"],"azure_ad_token":values["azure_ad_token"],"azure_ad_token_provider":values["azure_ad_token_provider"],"organization":values["openai_organization"],"base_url":values["openai_api_base"],"timeout":values["request_timeout"],"max_retries":values["max_retries"],"default_headers":{**(values["default_headers"]or{}),"User-Agent":"langchain-comm-python-azure-openai",},"default_query":values["default_query"],"http_client":values["http_client"],}values["client"]=openai.AzureOpenAI(**client_params).chat.completionsazure_ad_async_token_provider=values["azure_ad_async_token_provider"]ifazure_ad_async_token_provider:client_params["azure_ad_token_provider"]=azure_ad_async_token_providervalues["async_client"]=openai.AsyncAzureOpenAI(**client_params).chat.completionselse:values["client"]=openai.ChatCompletion# type: ignore[attr-defined]returnvalues
@propertydef_default_params(self)->Dict[str,Any]:"""Get the default parameters for calling OpenAI API."""ifis_openai_v1():returnsuper()._default_paramselse:return{**super()._default_params,"engine":self.deployment_name,}@propertydef_identifying_params(self)->Dict[str,Any]:"""Get the identifying parameters."""return{**self._default_params}@propertydef_client_params(self)->Dict[str,Any]:"""Get the config params used for the openai client."""ifis_openai_v1():returnsuper()._client_paramselse:return{**super()._client_params,"api_type":self.openai_api_type,"api_version":self.openai_api_version,}@propertydef_llm_type(self)->str:return"azure-openai-chat"@propertydeflc_attributes(self)->Dict[str,Any]:return{"openai_api_type":self.openai_api_type,"openai_api_version":self.openai_api_version,}def_create_chat_result(self,response:Union[dict,BaseModel])->ChatResult:ifnotisinstance(response,dict):response=response.dict()forresinresponse["choices"]:ifres.get("finish_reason",None)=="content_filter":raiseValueError("Azure has not provided the response due to a content filter ""being triggered")chat_result=super()._create_chat_result(response)if"model"inresponse:model=response["model"]ifself.model_version:model=f"{model}-{self.model_version}"ifchat_result.llm_outputisnotNoneandisinstance(chat_result.llm_output,dict):chat_result.llm_output["model_name"]=modelreturnchat_result