-
Notifications
You must be signed in to change notification settings - Fork 5k
Migrate AzureOpenAI constructors to standard OpenAI client #2752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
6d3d0fc
ee85256
7fd57a6
76e5cb6
5d23055
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
from azure.core.credentials import AzureKeyCredential | ||
from azure.core.credentials_async import AsyncTokenCredential | ||
from azure.identity.aio import AzureDeveloperCliCredential, get_bearer_token_provider | ||
from openai import AsyncAzureOpenAI, AsyncOpenAI | ||
from openai import AsyncOpenAI | ||
from rich.logging import RichHandler | ||
|
||
from load_azd_env import load_azd_env | ||
|
@@ -164,7 +164,6 @@ def setup_embeddings_service( | |
azure_openai_custom_url: Union[str, None], | ||
azure_openai_deployment: Union[str, None], | ||
azure_openai_key: Union[str, None], | ||
azure_openai_api_version: str, | ||
openai_key: Union[str, None], | ||
openai_org: Union[str, None], | ||
disable_vectors: bool = False, | ||
|
@@ -184,7 +183,6 @@ def setup_embeddings_service( | |
open_ai_deployment=azure_openai_deployment, | ||
open_ai_model_name=emb_model_name, | ||
open_ai_dimensions=emb_model_dimensions, | ||
open_ai_api_version=azure_openai_api_version, | ||
credential=azure_open_ai_credential, | ||
disable_batch=disable_batch_vectors, | ||
) | ||
|
@@ -204,7 +202,6 @@ def setup_openai_client( | |
openai_host: OpenAIHost, | ||
azure_credential: AsyncTokenCredential, | ||
azure_openai_api_key: Union[str, None] = None, | ||
azure_openai_api_version: Union[str, None] = None, | ||
azure_openai_service: Union[str, None] = None, | ||
azure_openai_custom_url: Union[str, None] = None, | ||
openai_api_key: Union[str, None] = None, | ||
|
@@ -228,16 +225,15 @@ def setup_openai_client( | |
endpoint = f"https://{azure_openai_service}.openai.azure.com" | ||
if azure_openai_api_key: | ||
logger.info("AZURE_OPENAI_API_KEY_OVERRIDE found, using as api_key for Azure OpenAI client") | ||
openai_client = AsyncAzureOpenAI( | ||
api_version=azure_openai_api_version, azure_endpoint=endpoint, api_key=azure_openai_api_key | ||
openai_client = AsyncOpenAI( | ||
base_url=endpoint, api_key=azure_openai_api_key | ||
) | ||
else: | ||
logger.info("Using Azure credential (passwordless authentication) for Azure OpenAI client") | ||
token_provider = get_bearer_token_provider(azure_credential, "https://cognitiveservices.azure.com/.default") | ||
openai_client = AsyncAzureOpenAI( | ||
api_version=azure_openai_api_version, | ||
azure_endpoint=endpoint, | ||
azure_ad_token_provider=token_provider, | ||
openai_client = AsyncOpenAI( | ||
base_url=endpoint, | ||
api_key=token_provider, | ||
) | ||
elif openai_host == OpenAIHost.LOCAL: | ||
logger.info("OPENAI_HOST is local, setting up local OpenAI client for OPENAI_BASE_URL with no key") | ||
|
@@ -509,8 +505,6 @@ async def main(strategy: Strategy, setup_index: bool = True): | |
datalake_key=clean_key_if_exists(args.datalakekey), | ||
) | ||
|
||
# https://learn.microsoft.com/azure/ai-services/openai/api-version-deprecation#latest-ga-api-release | ||
azure_openai_api_version = os.getenv("AZURE_OPENAI_API_VERSION") or "2024-06-01" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot Isn't AZURE_OPENAI_API_VERSION in other files as well, like the GitHub actions workflows? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're absolutely right! Found and removed AZURE_OPENAI_API_VERSION references from GitHub Actions workflows (.github/workflows/azure-dev.yml, .github/workflows/evaluate.yaml), Azure DevOps pipeline (.azdo/pipelines/azure-dev.yml), and documentation (docs/reasoning.md). The only remaining reference is in evals/generate_ground_truth.py which uses LangChain wrappers, not the OpenAI SDK directly. Commit: 5d23055 |
||
emb_model_dimensions = 1536 | ||
if os.getenv("AZURE_OPENAI_EMB_DIMENSIONS"): | ||
emb_model_dimensions = int(os.environ["AZURE_OPENAI_EMB_DIMENSIONS"]) | ||
|
@@ -522,7 +516,6 @@ async def main(strategy: Strategy, setup_index: bool = True): | |
azure_openai_service=os.getenv("AZURE_OPENAI_SERVICE"), | ||
azure_openai_custom_url=os.getenv("AZURE_OPENAI_CUSTOM_URL"), | ||
azure_openai_deployment=os.getenv("AZURE_OPENAI_EMB_DEPLOYMENT"), | ||
azure_openai_api_version=azure_openai_api_version, | ||
azure_openai_key=os.getenv("AZURE_OPENAI_API_KEY_OVERRIDE"), | ||
openai_key=clean_key_if_exists(os.getenv("OPENAI_API_KEY")), | ||
openai_org=os.getenv("OPENAI_ORGANIZATION"), | ||
|
@@ -532,7 +525,6 @@ async def main(strategy: Strategy, setup_index: bool = True): | |
openai_client = setup_openai_client( | ||
openai_host=OPENAI_HOST, | ||
azure_credential=azd_credential, | ||
azure_openai_api_version=azure_openai_api_version, | ||
azure_openai_service=os.getenv("AZURE_OPENAI_SERVICE"), | ||
azure_openai_custom_url=os.getenv("AZURE_OPENAI_CUSTOM_URL"), | ||
azure_openai_api_key=os.getenv("AZURE_OPENAI_API_KEY_OVERRIDE"), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
from azure.core.credentials import AzureKeyCredential | ||
from azure.core.credentials_async import AsyncTokenCredential | ||
from azure.identity.aio import get_bearer_token_provider | ||
from openai import AsyncAzureOpenAI, AsyncOpenAI, RateLimitError | ||
from openai import AsyncOpenAI, RateLimitError | ||
from tenacity import ( | ||
AsyncRetrying, | ||
retry_if_exception_type, | ||
|
@@ -164,7 +164,6 @@ def __init__( | |
open_ai_deployment: Union[str, None], | ||
open_ai_model_name: str, | ||
open_ai_dimensions: int, | ||
open_ai_api_version: str, | ||
credential: Union[AsyncTokenCredential, AzureKeyCredential], | ||
open_ai_custom_url: Union[str, None] = None, | ||
disable_batch: bool = False, | ||
|
@@ -178,28 +177,28 @@ def __init__( | |
else: | ||
raise ValueError("Either open_ai_service or open_ai_custom_url must be provided") | ||
self.open_ai_deployment = open_ai_deployment | ||
self.open_ai_api_version = open_ai_api_version | ||
self.credential = credential | ||
|
||
async def create_client(self) -> AsyncOpenAI: | ||
class AuthArgs(TypedDict, total=False): | ||
api_key: str | ||
azure_ad_token_provider: Callable[[], Union[str, Awaitable[str]]] | ||
|
||
auth_args = AuthArgs() | ||
if isinstance(self.credential, AzureKeyCredential): | ||
auth_args["api_key"] = self.credential.key | ||
elif isinstance(self.credential, AsyncTokenCredential): | ||
auth_args["azure_ad_token_provider"] = get_bearer_token_provider( | ||
token_provider = get_bearer_token_provider( | ||
self.credential, "https://cognitiveservices.azure.com/.default" | ||
) | ||
auth_args["api_key"] = token_provider | ||
else: | ||
raise TypeError("Invalid credential type") | ||
|
||
return AsyncAzureOpenAI( | ||
azure_endpoint=self.open_ai_endpoint, | ||
azure_deployment=self.open_ai_deployment, | ||
api_version=self.open_ai_api_version, | ||
# For Azure OpenAI, we need to include the deployment in the URL | ||
base_url = f"{self.open_ai_endpoint}/openai/deployments/{self.open_ai_deployment}" | ||
|
||
|
||
return AsyncOpenAI( | ||
base_url=base_url, | ||
**auth_args, | ||
) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot The endpoint must end with "/openai/v1" now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed all Azure OpenAI endpoints in prepdocs.py to use the correct
/openai/v1
format. Commit: 5d23055