|
| 1 | +from __future__ import annotations as _annotations |
| 2 | + |
| 3 | +import os |
| 4 | +from typing import overload |
| 5 | + |
| 6 | +import httpx |
| 7 | +from openai import AsyncOpenAI |
| 8 | + |
| 9 | +from pydantic_ai.models import cached_async_http_client |
| 10 | + |
| 11 | +try: |
| 12 | + from openai import AsyncAzureOpenAI |
| 13 | +except ImportError as _import_error: # pragma: no cover |
| 14 | + raise ImportError( |
| 15 | + 'Please install the `openai` package to use the Azure provider, ' |
| 16 | + "you can use the `openai` optional group — `pip install 'pydantic-ai-slim[openai]'`" |
| 17 | + ) from _import_error |
| 18 | + |
| 19 | + |
| 20 | +from . import Provider |
| 21 | + |
| 22 | + |
| 23 | +class AzureProvider(Provider[AsyncOpenAI]): |
| 24 | + """Provider for Azure OpenAI API. |
| 25 | +
|
| 26 | + See <https://azure.microsoft.com/en-us/products/ai-foundry> for more information. |
| 27 | + """ |
| 28 | + |
| 29 | + @property |
| 30 | + def name(self) -> str: |
| 31 | + return 'azure' |
| 32 | + |
| 33 | + @property |
| 34 | + def base_url(self) -> str: |
| 35 | + assert self._base_url is not None |
| 36 | + return self._base_url |
| 37 | + |
| 38 | + @property |
| 39 | + def client(self) -> AsyncOpenAI: |
| 40 | + return self._client |
| 41 | + |
| 42 | + @overload |
| 43 | + def __init__(self, *, openai_client: AsyncAzureOpenAI) -> None: ... |
| 44 | + |
| 45 | + @overload |
| 46 | + def __init__( |
| 47 | + self, |
| 48 | + *, |
| 49 | + azure_endpoint: str | None = None, |
| 50 | + api_version: str | None = None, |
| 51 | + api_key: str | None = None, |
| 52 | + http_client: httpx.AsyncClient | None = None, |
| 53 | + ) -> None: ... |
| 54 | + |
| 55 | + def __init__( |
| 56 | + self, |
| 57 | + *, |
| 58 | + azure_endpoint: str | None = None, |
| 59 | + api_version: str | None = None, |
| 60 | + api_key: str | None = None, |
| 61 | + openai_client: AsyncAzureOpenAI | None = None, |
| 62 | + http_client: httpx.AsyncClient | None = None, |
| 63 | + ) -> None: |
| 64 | + """Create a new Azure provider. |
| 65 | +
|
| 66 | + Args: |
| 67 | + azure_endpoint: The Azure endpoint to use for authentication, if not provided, the `AZURE_OPENAI_ENDPOINT` |
| 68 | + environment variable will be used if available. |
| 69 | + api_version: The API version to use for authentication, if not provided, the `OPENAI_API_VERSION` |
| 70 | + environment variable will be used if available. |
| 71 | + api_key: The API key to use for authentication, if not provided, the `AZURE_OPENAI_API_KEY` environment variable |
| 72 | + will be used if available. |
| 73 | + openai_client: An existing |
| 74 | + [`AsyncAzureOpenAI`](https://github.com/openai/openai-python#microsoft-azure-openai) |
| 75 | + client to use. If provided, `base_url`, `api_key`, and `http_client` must be `None`. |
| 76 | + http_client: An existing `httpx.AsyncClient` to use for making HTTP requests. |
| 77 | + """ |
| 78 | + if openai_client is not None: |
| 79 | + assert azure_endpoint is None, 'Cannot provide both `openai_client` and `azure_endpoint`' |
| 80 | + assert http_client is None, 'Cannot provide both `openai_client` and `http_client`' |
| 81 | + assert api_key is None, 'Cannot provide both `openai_client` and `api_key`' |
| 82 | + self._base_url = str(openai_client.base_url) |
| 83 | + self._client = openai_client |
| 84 | + else: |
| 85 | + azure_endpoint = azure_endpoint or os.getenv('AZURE_OPENAI_ENDPOINT') |
| 86 | + if azure_endpoint is None: # pragma: no cover |
| 87 | + raise ValueError( |
| 88 | + 'Must provide one of the `azure_endpoint` argument or the `AZURE_OPENAI_ENDPOINT` environment variable' |
| 89 | + ) |
| 90 | + |
| 91 | + if api_key is None and 'OPENAI_API_KEY' not in os.environ: # pragma: no cover |
| 92 | + raise ValueError( |
| 93 | + 'Must provide one of the `api_key` argument or the `OPENAI_API_KEY` environment variable' |
| 94 | + ) |
| 95 | + |
| 96 | + if api_version is None and 'OPENAI_API_VERSION' not in os.environ: # pragma: no cover |
| 97 | + raise ValueError( |
| 98 | + 'Must provide one of the `api_version` argument or the `OPENAI_API_VERSION` environment variable' |
| 99 | + ) |
| 100 | + |
| 101 | + http_client = http_client or cached_async_http_client() |
| 102 | + self._client = AsyncAzureOpenAI( |
| 103 | + azure_endpoint=azure_endpoint, |
| 104 | + api_key=api_key, |
| 105 | + api_version=api_version, |
| 106 | + http_client=http_client, |
| 107 | + ) |
| 108 | + self._base_url = str(self._client.base_url) |
0 commit comments