Skip to content

Commit fa9a638

Browse files
authored
Upgrade Azure OpenAI API version and use AZURE_OPENAI_API_VERSION consistently (#2105)
* Base api version on env var * Improve app.py
1 parent f3a4c1e commit fa9a638

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

app/backend/app.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ async def setup_clients():
420420
)
421421
AZURE_OPENAI_EMB_DEPLOYMENT = os.getenv("AZURE_OPENAI_EMB_DEPLOYMENT") if OPENAI_HOST.startswith("azure") else None
422422
AZURE_OPENAI_CUSTOM_URL = os.getenv("AZURE_OPENAI_CUSTOM_URL")
423+
# https://learn.microsoft.com/azure/ai-services/openai/api-version-deprecation#latest-ga-api-release
424+
AZURE_OPENAI_API_VERSION = os.getenv("AZURE_OPENAI_API_VERSION") or "2024-06-01"
423425
AZURE_VISION_ENDPOINT = os.getenv("AZURE_VISION_ENDPOINT", "")
424426
# Used only with non-Azure OpenAI deployments
425427
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
@@ -547,6 +549,7 @@ async def setup_clients():
547549
openai_custom_url=AZURE_OPENAI_CUSTOM_URL,
548550
openai_deployment=AZURE_OPENAI_EMB_DEPLOYMENT,
549551
openai_dimensions=OPENAI_EMB_DIMENSIONS,
552+
openai_api_version=AZURE_OPENAI_API_VERSION,
550553
openai_key=clean_key_if_exists(OPENAI_API_KEY),
551554
openai_org=OPENAI_ORGANIZATION,
552555
disable_vectors=os.getenv("USE_VECTORS", "").lower() == "false",
@@ -573,7 +576,6 @@ async def setup_clients():
573576
current_app.config[CONFIG_CREDENTIAL] = azure_credential
574577

575578
if OPENAI_HOST.startswith("azure"):
576-
api_version = os.getenv("AZURE_OPENAI_API_VERSION") or "2024-03-01-preview"
577579
if OPENAI_HOST == "azure_custom":
578580
current_app.logger.info("OPENAI_HOST is azure_custom, setting up Azure OpenAI custom client")
579581
if not AZURE_OPENAI_CUSTOM_URL:
@@ -586,12 +588,14 @@ async def setup_clients():
586588
endpoint = f"https://{AZURE_OPENAI_SERVICE}.openai.azure.com"
587589
if api_key := os.getenv("AZURE_OPENAI_API_KEY_OVERRIDE"):
588590
current_app.logger.info("AZURE_OPENAI_API_KEY_OVERRIDE found, using as api_key for Azure OpenAI client")
589-
openai_client = AsyncAzureOpenAI(api_version=api_version, azure_endpoint=endpoint, api_key=api_key)
591+
openai_client = AsyncAzureOpenAI(
592+
api_version=AZURE_OPENAI_API_VERSION, azure_endpoint=endpoint, api_key=api_key
593+
)
590594
else:
591595
current_app.logger.info("Using Azure credential (passwordless authentication) for Azure OpenAI client")
592596
token_provider = get_bearer_token_provider(azure_credential, "https://cognitiveservices.azure.com/.default")
593597
openai_client = AsyncAzureOpenAI(
594-
api_version=api_version,
598+
api_version=AZURE_OPENAI_API_VERSION,
595599
azure_endpoint=endpoint,
596600
azure_ad_token_provider=token_provider,
597601
)

app/backend/prepdocs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def setup_embeddings_service(
115115
openai_custom_url: Union[str, None],
116116
openai_deployment: Union[str, None],
117117
openai_dimensions: int,
118+
openai_api_version: str,
118119
openai_key: Union[str, None],
119120
openai_org: Union[str, None],
120121
disable_vectors: bool = False,
@@ -134,6 +135,7 @@ def setup_embeddings_service(
134135
open_ai_deployment=openai_deployment,
135136
open_ai_model_name=openai_model_name,
136137
open_ai_dimensions=openai_dimensions,
138+
open_ai_api_version=openai_api_version,
137139
credential=azure_open_ai_credential,
138140
disable_batch=disable_batch_vectors,
139141
)
@@ -366,6 +368,8 @@ async def main(strategy: Strategy, setup_index: bool = True):
366368
openai_service=os.getenv("AZURE_OPENAI_SERVICE"),
367369
openai_custom_url=os.getenv("AZURE_OPENAI_CUSTOM_URL"),
368370
openai_deployment=os.getenv("AZURE_OPENAI_EMB_DEPLOYMENT"),
371+
# https://learn.microsoft.com/azure/ai-services/openai/api-version-deprecation#latest-ga-api-release
372+
openai_api_version=os.getenv("AZURE_OPENAI_API_VERSION") or "2024-06-01",
369373
openai_dimensions=openai_dimensions,
370374
openai_key=clean_key_if_exists(openai_key),
371375
openai_org=os.getenv("OPENAI_ORGANIZATION"),

app/backend/prepdocslib/embeddings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def __init__(
163163
open_ai_deployment: Union[str, None],
164164
open_ai_model_name: str,
165165
open_ai_dimensions: int,
166+
open_ai_api_version: str,
166167
credential: Union[AsyncTokenCredential, AzureKeyCredential],
167168
open_ai_custom_url: Union[str, None] = None,
168169
disable_batch: bool = False,
@@ -176,6 +177,7 @@ def __init__(
176177
else:
177178
raise ValueError("Either open_ai_service or open_ai_custom_url must be provided")
178179
self.open_ai_deployment = open_ai_deployment
180+
self.open_ai_api_version = open_ai_api_version
179181
self.credential = credential
180182

181183
async def create_client(self) -> AsyncOpenAI:
@@ -196,7 +198,7 @@ class AuthArgs(TypedDict, total=False):
196198
return AsyncAzureOpenAI(
197199
azure_endpoint=self.open_ai_endpoint,
198200
azure_deployment=self.open_ai_deployment,
199-
api_version="2023-05-15",
201+
api_version=self.open_ai_api_version,
200202
**auth_args,
201203
)
202204

tests/test_prepdocs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ async def mock_create_client(*args, **kwargs):
6262
open_ai_deployment="x",
6363
open_ai_model_name=MOCK_EMBEDDING_MODEL_NAME,
6464
open_ai_dimensions=MOCK_EMBEDDING_DIMENSIONS,
65+
open_ai_api_version="test-api-version",
6566
credential=MockAzureCredential(),
6667
disable_batch=False,
6768
)
@@ -79,6 +80,7 @@ async def mock_create_client(*args, **kwargs):
7980
open_ai_deployment="x",
8081
open_ai_model_name=MOCK_EMBEDDING_MODEL_NAME,
8182
open_ai_dimensions=MOCK_EMBEDDING_DIMENSIONS,
83+
open_ai_api_version="test-api-version",
8284
credential=MockAzureCredential(),
8385
disable_batch=True,
8486
)
@@ -149,6 +151,7 @@ async def test_compute_embedding_ratelimiterror_batch(monkeypatch, caplog):
149151
open_ai_deployment="x",
150152
open_ai_model_name=MOCK_EMBEDDING_MODEL_NAME,
151153
open_ai_dimensions=MOCK_EMBEDDING_DIMENSIONS,
154+
open_ai_api_version="test-api-version",
152155
credential=MockAzureCredential(),
153156
disable_batch=False,
154157
)
@@ -167,6 +170,7 @@ async def test_compute_embedding_ratelimiterror_single(monkeypatch, caplog):
167170
open_ai_deployment="x",
168171
open_ai_model_name=MOCK_EMBEDDING_MODEL_NAME,
169172
open_ai_dimensions=MOCK_EMBEDDING_DIMENSIONS,
173+
open_ai_api_version="test-api-version",
170174
credential=MockAzureCredential(),
171175
disable_batch=True,
172176
)
@@ -193,6 +197,7 @@ async def test_compute_embedding_autherror(monkeypatch, capsys):
193197
open_ai_deployment="x",
194198
open_ai_model_name=MOCK_EMBEDDING_MODEL_NAME,
195199
open_ai_dimensions=MOCK_EMBEDDING_DIMENSIONS,
200+
open_ai_api_version="test-api-version",
196201
credential=MockAzureCredential(),
197202
disable_batch=False,
198203
)
@@ -205,6 +210,7 @@ async def test_compute_embedding_autherror(monkeypatch, capsys):
205210
open_ai_deployment="x",
206211
open_ai_model_name=MOCK_EMBEDDING_MODEL_NAME,
207212
open_ai_dimensions=MOCK_EMBEDDING_DIMENSIONS,
213+
open_ai_api_version="test-api-version",
208214
credential=MockAzureCredential(),
209215
disable_batch=True,
210216
)

tests/test_searchmanager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ async def mock_upload_documents(self, documents):
275275
open_ai_deployment="x",
276276
open_ai_model_name=MOCK_EMBEDDING_MODEL_NAME,
277277
open_ai_dimensions=MOCK_EMBEDDING_DIMENSIONS,
278+
open_ai_api_version="test-api-version",
278279
credential=AzureKeyCredential("test"),
279280
disable_batch=True,
280281
)

0 commit comments

Comments
 (0)