|
1 | 1 | # Copyright (c) Microsoft Corporation. |
2 | 2 | # Licensed under the MIT License. |
3 | 3 |
|
4 | | -from azure.identity import get_bearer_token_provider |
| 4 | +from azure.ai.projects import AIProjectClient |
5 | 5 | from helpers.azure_credential_utils import get_azure_credential |
6 | | -from openai import AzureOpenAI |
7 | 6 |
|
8 | 7 |
|
9 | | -def get_openai_client(azure_openai_endpoint: str) -> AzureOpenAI: |
| 8 | +def get_foundry_client(ai_project_endpoint: str): |
| 9 | + """ |
| 10 | + Create an OpenAI-compatible client via Azure AI Foundry (Projects SDK). |
| 11 | +
|
| 12 | + This function uses the Azure AI Foundry approach: |
| 13 | + 1. Create an AIProjectClient with the AI project endpoint |
| 14 | + 2. Call .get_openai_client() to get an OpenAI-compatible client |
| 15 | +
|
| 16 | + Args: |
| 17 | + ai_project_endpoint: The AI Foundry project endpoint URL (e.g., https://aif-xyz.services.ai.azure.com) |
| 18 | +
|
| 19 | + Returns: |
| 20 | + An OpenAI-compatible client from the AI Foundry project |
| 21 | + """ |
10 | 22 | credential = get_azure_credential() |
11 | | - token_provider = get_bearer_token_provider( |
12 | | - credential, "https://cognitiveservices.azure.com/.default" |
13 | | - ) |
14 | | - return AzureOpenAI( |
15 | | - azure_endpoint=azure_openai_endpoint, |
16 | | - azure_ad_token_provider=token_provider, |
17 | | - api_version="2024-10-01-preview", |
| 23 | + |
| 24 | + # Create the AI Foundry Project client |
| 25 | + project_client = AIProjectClient( |
| 26 | + endpoint=ai_project_endpoint, |
| 27 | + credential=credential |
18 | 28 | ) |
| 29 | + |
| 30 | + # Get the OpenAI-compatible client from the project |
| 31 | + # This client supports .beta.chat.completions.parse() and other OpenAI methods |
| 32 | + return project_client.get_openai_client() |
0 commit comments