Skip to content

Commit ee85256

Browse files
Copilotpamelafox
andcommitted
Migrate AzureOpenAI constructors to OpenAI client
Co-authored-by: pamelafox <[email protected]>
1 parent 6d3d0fc commit ee85256

File tree

6 files changed

+13
-32
lines changed

6 files changed

+13
-32
lines changed

app/backend/app.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,6 @@ async def setup_clients():
426426
os.getenv("AZURE_OPENAI_EMB_DEPLOYMENT") if OPENAI_HOST in [OpenAIHost.AZURE, OpenAIHost.AZURE_CUSTOM] else None
427427
)
428428
AZURE_OPENAI_CUSTOM_URL = os.getenv("AZURE_OPENAI_CUSTOM_URL")
429-
# https://learn.microsoft.com/azure/ai-services/openai/api-version-deprecation#latest-ga-api-release
430-
AZURE_OPENAI_API_VERSION = os.getenv("AZURE_OPENAI_API_VERSION") or "2024-10-21"
431429
AZURE_VISION_ENDPOINT = os.getenv("AZURE_VISION_ENDPOINT", "")
432430
AZURE_OPENAI_API_KEY_OVERRIDE = os.getenv("AZURE_OPENAI_API_KEY_OVERRIDE")
433431
# Used only with non-Azure OpenAI deployments
@@ -563,7 +561,6 @@ async def setup_clients():
563561
openai_client = setup_openai_client(
564562
openai_host=OPENAI_HOST,
565563
azure_credential=azure_credential,
566-
azure_openai_api_version=AZURE_OPENAI_API_VERSION,
567564
azure_openai_service=AZURE_OPENAI_SERVICE,
568565
azure_openai_custom_url=AZURE_OPENAI_CUSTOM_URL,
569566
azure_openai_api_key=AZURE_OPENAI_API_KEY_OVERRIDE,
@@ -609,7 +606,6 @@ async def setup_clients():
609606
azure_openai_service=AZURE_OPENAI_SERVICE,
610607
azure_openai_custom_url=AZURE_OPENAI_CUSTOM_URL,
611608
azure_openai_deployment=AZURE_OPENAI_EMB_DEPLOYMENT,
612-
azure_openai_api_version=AZURE_OPENAI_API_VERSION,
613609
azure_openai_key=clean_key_if_exists(AZURE_OPENAI_API_KEY_OVERRIDE),
614610
openai_key=clean_key_if_exists(OPENAI_API_KEY),
615611
openai_org=OPENAI_ORGANIZATION,

app/backend/prepdocs.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ def setup_embeddings_service(
164164
azure_openai_custom_url: Union[str, None],
165165
azure_openai_deployment: Union[str, None],
166166
azure_openai_key: Union[str, None],
167-
azure_openai_api_version: str,
168167
openai_key: Union[str, None],
169168
openai_org: Union[str, None],
170169
disable_vectors: bool = False,
@@ -184,7 +183,6 @@ def setup_embeddings_service(
184183
open_ai_deployment=azure_openai_deployment,
185184
open_ai_model_name=emb_model_name,
186185
open_ai_dimensions=emb_model_dimensions,
187-
open_ai_api_version=azure_openai_api_version,
188186
credential=azure_open_ai_credential,
189187
disable_batch=disable_batch_vectors,
190188
)
@@ -204,7 +202,6 @@ def setup_openai_client(
204202
openai_host: OpenAIHost,
205203
azure_credential: AsyncTokenCredential,
206204
azure_openai_api_key: Union[str, None] = None,
207-
azure_openai_api_version: Union[str, None] = None,
208205
azure_openai_service: Union[str, None] = None,
209206
azure_openai_custom_url: Union[str, None] = None,
210207
openai_api_key: Union[str, None] = None,
@@ -228,16 +225,15 @@ def setup_openai_client(
228225
endpoint = f"https://{azure_openai_service}.openai.azure.com"
229226
if azure_openai_api_key:
230227
logger.info("AZURE_OPENAI_API_KEY_OVERRIDE found, using as api_key for Azure OpenAI client")
231-
openai_client = AsyncAzureOpenAI(
232-
api_version=azure_openai_api_version, azure_endpoint=endpoint, api_key=azure_openai_api_key
228+
openai_client = AsyncOpenAI(
229+
base_url=endpoint, api_key=azure_openai_api_key
233230
)
234231
else:
235232
logger.info("Using Azure credential (passwordless authentication) for Azure OpenAI client")
236233
token_provider = get_bearer_token_provider(azure_credential, "https://cognitiveservices.azure.com/.default")
237-
openai_client = AsyncAzureOpenAI(
238-
api_version=azure_openai_api_version,
239-
azure_endpoint=endpoint,
240-
azure_ad_token_provider=token_provider,
234+
openai_client = AsyncOpenAI(
235+
base_url=endpoint,
236+
api_key=token_provider,
241237
)
242238
elif openai_host == OpenAIHost.LOCAL:
243239
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):
509505
datalake_key=clean_key_if_exists(args.datalakekey),
510506
)
511507

512-
# https://learn.microsoft.com/azure/ai-services/openai/api-version-deprecation#latest-ga-api-release
513-
azure_openai_api_version = os.getenv("AZURE_OPENAI_API_VERSION") or "2024-06-01"
514508
emb_model_dimensions = 1536
515509
if os.getenv("AZURE_OPENAI_EMB_DIMENSIONS"):
516510
emb_model_dimensions = int(os.environ["AZURE_OPENAI_EMB_DIMENSIONS"])
@@ -522,7 +516,6 @@ async def main(strategy: Strategy, setup_index: bool = True):
522516
azure_openai_service=os.getenv("AZURE_OPENAI_SERVICE"),
523517
azure_openai_custom_url=os.getenv("AZURE_OPENAI_CUSTOM_URL"),
524518
azure_openai_deployment=os.getenv("AZURE_OPENAI_EMB_DEPLOYMENT"),
525-
azure_openai_api_version=azure_openai_api_version,
526519
azure_openai_key=os.getenv("AZURE_OPENAI_API_KEY_OVERRIDE"),
527520
openai_key=clean_key_if_exists(os.getenv("OPENAI_API_KEY")),
528521
openai_org=os.getenv("OPENAI_ORGANIZATION"),
@@ -532,7 +525,6 @@ async def main(strategy: Strategy, setup_index: bool = True):
532525
openai_client = setup_openai_client(
533526
openai_host=OPENAI_HOST,
534527
azure_credential=azd_credential,
535-
azure_openai_api_version=azure_openai_api_version,
536528
azure_openai_service=os.getenv("AZURE_OPENAI_SERVICE"),
537529
azure_openai_custom_url=os.getenv("AZURE_OPENAI_CUSTOM_URL"),
538530
azure_openai_api_key=os.getenv("AZURE_OPENAI_API_KEY_OVERRIDE"),

app/backend/prepdocslib/embeddings.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ def __init__(
164164
open_ai_deployment: Union[str, None],
165165
open_ai_model_name: str,
166166
open_ai_dimensions: int,
167-
open_ai_api_version: str,
168167
credential: Union[AsyncTokenCredential, AzureKeyCredential],
169168
open_ai_custom_url: Union[str, None] = None,
170169
disable_batch: bool = False,
@@ -178,28 +177,28 @@ def __init__(
178177
else:
179178
raise ValueError("Either open_ai_service or open_ai_custom_url must be provided")
180179
self.open_ai_deployment = open_ai_deployment
181-
self.open_ai_api_version = open_ai_api_version
182180
self.credential = credential
183181

184182
async def create_client(self) -> AsyncOpenAI:
185183
class AuthArgs(TypedDict, total=False):
186184
api_key: str
187-
azure_ad_token_provider: Callable[[], Union[str, Awaitable[str]]]
188185

189186
auth_args = AuthArgs()
190187
if isinstance(self.credential, AzureKeyCredential):
191188
auth_args["api_key"] = self.credential.key
192189
elif isinstance(self.credential, AsyncTokenCredential):
193-
auth_args["azure_ad_token_provider"] = get_bearer_token_provider(
190+
token_provider = get_bearer_token_provider(
194191
self.credential, "https://cognitiveservices.azure.com/.default"
195192
)
193+
auth_args["api_key"] = token_provider
196194
else:
197195
raise TypeError("Invalid credential type")
198196

199-
return AsyncAzureOpenAI(
200-
azure_endpoint=self.open_ai_endpoint,
201-
azure_deployment=self.open_ai_deployment,
202-
api_version=self.open_ai_api_version,
197+
# For Azure OpenAI, we need to include the deployment in the URL
198+
base_url = f"{self.open_ai_endpoint}/openai/deployments/{self.open_ai_deployment}"
199+
200+
return AsyncOpenAI(
201+
base_url=base_url,
203202
**auth_args,
204203
)
205204

app/backend/requirements.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
azure-identity
22
quart
33
quart-cors
4-
openai>=1.3.7
4+
openai>=1.108.1
55
tiktoken
66
tenacity
77
azure-ai-documentintelligence==1.0.0b4

infra/main.bicep

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ param openAiHost string // Set in main.parameters.json
5656
param isAzureOpenAiHost bool = startsWith(openAiHost, 'azure')
5757
param deployAzureOpenAi bool = openAiHost == 'azure'
5858
param azureOpenAiCustomUrl string = ''
59-
param azureOpenAiApiVersion string = ''
6059
@secure()
6160
param azureOpenAiApiKey string = ''
6261
param azureOpenAiDisableKeys bool = true
@@ -442,7 +441,6 @@ var appEnvVariables = {
442441
AZURE_OPENAI_EMB_DEPLOYMENT: embedding.deploymentName
443442
AZURE_OPENAI_SEARCHAGENT_MODEL: searchAgent.modelName
444443
AZURE_OPENAI_SEARCHAGENT_DEPLOYMENT: searchAgent.deploymentName
445-
AZURE_OPENAI_API_VERSION: azureOpenAiApiVersion
446444
AZURE_OPENAI_API_KEY_OVERRIDE: azureOpenAiApiKey
447445
AZURE_OPENAI_CUSTOM_URL: azureOpenAiCustomUrl
448446
// Used only with non-Azure OpenAI deployments
@@ -1346,7 +1344,6 @@ output AZURE_OPENAI_CHATGPT_MODEL string = chatGpt.modelName
13461344
// Specific to Azure OpenAI
13471345
output AZURE_OPENAI_SERVICE string = isAzureOpenAiHost && deployAzureOpenAi ? openAi.outputs.name : ''
13481346
output AZURE_OPENAI_ENDPOINT string = isAzureOpenAiHost && deployAzureOpenAi ? openAi.outputs.endpoint : ''
1349-
output AZURE_OPENAI_API_VERSION string = isAzureOpenAiHost ? azureOpenAiApiVersion : ''
13501347
output AZURE_OPENAI_RESOURCE_GROUP string = isAzureOpenAiHost ? openAiResourceGroup.name : ''
13511348
output AZURE_OPENAI_CHATGPT_DEPLOYMENT string = isAzureOpenAiHost ? chatGpt.deploymentName : ''
13521349
output AZURE_OPENAI_CHATGPT_DEPLOYMENT_VERSION string = isAzureOpenAiHost ? chatGpt.deploymentVersion : ''

infra/main.parameters.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,6 @@
173173
"azureOpenAiCustomUrl": {
174174
"value": "${AZURE_OPENAI_CUSTOM_URL}"
175175
},
176-
"azureOpenAiApiVersion": {
177-
"value": "${AZURE_OPENAI_API_VERSION}"
178-
},
179176
"azureOpenAiApiKey": {
180177
"value": "${AZURE_OPENAI_API_KEY_OVERRIDE}"
181178
},

0 commit comments

Comments
 (0)