Skip to content

azure_openai

langroid/language_models/azure_openai.py

AzureConfig(**kwargs)

Bases: OpenAIGPTConfig

Configuration for Azure OpenAI GPT.

Attributes:

Name Type Description
type str

should be azure.

api_version str

can be set in the .env file as AZURE_OPENAI_API_VERSION.

deployment_name str

can be set in the .env file as AZURE_OPENAI_DEPLOYMENT_NAME and should be based the custom name you chose for your deployment when you deployed a model.

model_name str

can be set in the .env file as AZURE_GPT_MODEL_NAME and should be based on the model name chosen during setup.

model_version str

can be set in the .env file as AZURE_OPENAI_MODEL_VERSION and should be based on the model name chosen during setup.

Source code in langroid/language_models/openai_gpt.py
def __init__(self, **kwargs) -> None:  # type: ignore
    local_model = "api_base" in kwargs and kwargs["api_base"] is not None

    chat_model = kwargs.get("chat_model", "")
    local_prefixes = ["local/", "litellm/", "ollama/", "vllm/", "llamacpp/"]
    if any(chat_model.startswith(prefix) for prefix in local_prefixes):
        local_model = True

    warn_gpt_3_5 = (
        "chat_model" not in kwargs.keys()
        and not local_model
        and defaultOpenAIChatModel == OpenAIChatModel.GPT3_5_TURBO
    )

    if warn_gpt_3_5:
        existing_hook = kwargs.get("run_on_first_use", noop)

        def with_warning() -> None:
            existing_hook()
            gpt_3_5_warning()

        kwargs["run_on_first_use"] = with_warning

    super().__init__(**kwargs)

AzureGPT(config)

Bases: OpenAIGPT

Class to access OpenAI LLMs via Azure. These env variables can be obtained from the file .azure_env. Azure OpenAI doesn't support completion Attributes: config (AzureConfig): AzureConfig object api_key (str): Azure API key api_base (str): Azure API base url api_version (str): Azure API version model_name (str): the name of gpt model in your deployment model_version (str): the version of gpt model in your deployment

Source code in langroid/language_models/azure_openai.py
def __init__(self, config: AzureConfig):
    # This will auto-populate config values from .env file
    load_dotenv()
    super().__init__(config)
    self.config: AzureConfig = config
    if self.config.deployment_name == "":
        raise ValueError(
            """
            AZURE_OPENAI_DEPLOYMENT_NAME not set in .env file,
            please set it to your Azure openai deployment name."""
        )
    self.deployment_name = self.config.deployment_name

    if self.config.model_name == "":
        raise ValueError(
            """
            AZURE_OPENAI_MODEL_NAME not set in .env file,
            please set it to chat model name in your deployment."""
        )

    if (
        self.config.azure_openai_client_provider
        or self.config.azure_openai_async_client_provider
    ):
        if not self.config.azure_openai_client_provider:
            self.client = None
            logger.warning(
                "Using user-provided Azure OpenAI client, but only async "
                "client has been provided. Synchronous calls will fail."
            )
        if not self.config.azure_openai_async_client_provider:
            self.async_client = None
            logger.warning(
                "Using user-provided Azure OpenAI client, but no async "
                "client has been provided. Asynchronous calls will fail."
            )

        if self.config.azure_openai_client_provider:
            self.client = self.config.azure_openai_client_provider()
        if self.config.azure_openai_async_client_provider:
            self.async_client = self.config.azure_openai_async_client_provider()
            self.async_client.timeout = Timeout(self.config.timeout)
    else:
        if self.config.api_key == "":
            raise ValueError(
                """
                AZURE_OPENAI_API_KEY not set in .env file,
                please set it to your Azure API key."""
            )

        if self.config.api_base == "":
            raise ValueError(
                """
                AZURE_OPENAI_API_BASE not set in .env file,
                please set it to your Azure API key."""
            )

        self.client = AzureOpenAI(
            api_key=self.config.api_key,
            azure_endpoint=self.config.api_base,
            api_version=self.config.api_version,
            azure_deployment=self.config.deployment_name,
        )
        self.async_client = AsyncAzureOpenAI(
            api_key=self.config.api_key,
            azure_endpoint=self.config.api_base,
            api_version=self.config.api_version,
            azure_deployment=self.config.deployment_name,
            timeout=Timeout(self.config.timeout),
        )

    # set the chat model to be the same as the model_name
    # This corresponds to the gpt model you chose for your deployment
    # when you deployed a model
    self.set_chat_model()

    self.supports_json_schema = (
        self.config.api_version >= azureStructuredOutputAPIMin
        and self.config.model_version in azureStructuredOutputList
    )

set_chat_model()

Sets the chat model configuration based on the model name specified in the .env. This function checks the model_name in the configuration and sets the appropriate chat model in the config.chat_model. It supports handling for 'gpt-35-turbo', 'gpt4-turbo', 'gpt-4o' and 'gpt-4o-mini' models. For 'gpt-4', it further delegates the handling to handle_gpt4_model method. If the model name does not match any predefined models, it defaults to OpenAIChatModel.GPT4.

Source code in langroid/language_models/azure_openai.py
def set_chat_model(self) -> None:
    """
    Sets the chat model configuration based on the model name specified in the
    ``.env``. This function checks the `model_name` in the configuration and sets
    the appropriate chat model in the `config.chat_model`. It supports handling for
    'gpt-35-turbo', 'gpt4-turbo', 'gpt-4o' and 'gpt-4o-mini' models. For
    'gpt-4', it further delegates the handling to `handle_gpt4_model` method.
    If the model name does not match any predefined models, it defaults to
    `OpenAIChatModel.GPT4`.
    """
    MODEL_35_TURBO_NAMES = ("gpt-35-turbo", "35-turbo")
    MODEL_GPT4_TURBO_NAME = "gpt-4-turbo"
    MODEL_GPT4o_NAME = "gpt-4o"
    MODEL_GPT4o_MINI_NAME = "gpt-4o-mini"
    MODEL_GPT4_PREFIX = "gpt-4"

    if self.config.model_name in MODEL_35_TURBO_NAMES:
        self.config.chat_model = OpenAIChatModel.GPT3_5_TURBO
    elif self.config.model_name == MODEL_GPT4o_NAME:
        self.config.chat_model = OpenAIChatModel.GPT4o
    elif self.config.model_name == MODEL_GPT4o_MINI_NAME:
        self.config.chat_model = OpenAIChatModel.GPT4o_MINI
    elif self.config.model_name == MODEL_GPT4_TURBO_NAME:
        self.config.chat_model = OpenAIChatModel.GPT4_TURBO
    elif isinstance(
        self.config.model_name, str
    ) and self.config.model_name.startswith(MODEL_GPT4_PREFIX):
        self.handle_gpt4_model()
    else:
        self.config.chat_model = OpenAIChatModel.GPT4

handle_gpt4_model()

Handles the setting of the GPT-4 model in the configuration. This function checks the model_version in the configuration. If the version is not set, it raises a ValueError indicating that the model version needs to be specified in the .env file. It sets OpenAIChatMode.GPT4o if the version is one of those listed below, and OpenAIChatModel.GPT4_TURBO if the version is '1106-Preview', otherwise, it defaults to setting OpenAIChatModel.GPT4.

Source code in langroid/language_models/azure_openai.py
def handle_gpt4_model(self) -> None:
    """
    Handles the setting of the GPT-4 model in the configuration.
    This function checks the `model_version` in the configuration.
    If the version is not set, it raises a ValueError indicating
    that the model version needs to be specified in the ``.env``
    file.  It sets `OpenAIChatMode.GPT4o` if the version is
    one of those listed below, and
    `OpenAIChatModel.GPT4_TURBO` if
    the version is '1106-Preview', otherwise, it defaults to
    setting `OpenAIChatModel.GPT4`.
    """
    VERSIONS_GPT4_TURBO = ("1106-Preview", "2024-04-09")
    VERSIONS_GPT4o = ("2024-05-13", "2024-08-06", "2024-11-20")

    if self.config.model_version == "":
        raise ValueError(
            "AZURE_OPENAI_MODEL_VERSION not set in .env file. "
            "Please set it to the chat model version used in your deployment."
        )

    if self.config.model_version in VERSIONS_GPT4o:
        self.config.chat_model = OpenAIChatModel.GPT4o
    elif self.config.model_version in VERSIONS_GPT4_TURBO:
        self.config.chat_model = OpenAIChatModel.GPT4_TURBO
    else:
        self.config.chat_model = OpenAIChatModel.GPT4