-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathmodel-router-foundry-responses.py
More file actions
51 lines (41 loc) · 1.79 KB
/
model-router-foundry-responses.py
File metadata and controls
51 lines (41 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Foundry Model Router - Foundry Responses SDK (AIProjectClient) Example
This example demonstrates how to use the Azure AI Projects SDK
(AIProjectClient) to get an authenticated OpenAI client and call the
Responses API with a Foundry Model Router deployment.
NOTE: AIProjectClient requires Entra ID authentication (DefaultAzureCredential),
not API keys. You must be logged in via `az login` before running this.
Prerequisites:
- An Azure AI Foundry project with a "model-router" deployment
- Azure CLI installed and logged in (`az login`)
- A .env file in the repo root with AZURE_AI_PROJECT_ENDPOINT and
MODEL_DEPLOYMENT_NAME
Usage:
pip install -r requirements.txt
az login
python model-router-foundry-responses-sdk.py
"""
import os
from pathlib import Path
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from dotenv import load_dotenv
# Load environment variables from .env in the repo root
load_dotenv(Path(__file__).resolve().parent.parent / ".env", override=True)
project_endpoint = os.environ["AZURE_AI_PROJECT_ENDPOINT"]
deployment = os.environ["MODEL_DEPLOYMENT_NAME"]
with (
DefaultAzureCredential() as credential,
AIProjectClient(endpoint=project_endpoint, credential=credential) as project_client,
project_client.get_openai_client() as openai_client,
):
response = openai_client.responses.create(
model=deployment,
input="In one sentence, name the most popular tourist destination in Seattle.",
)
print("--- Foundry Responses SDK Output ---")
print(f"Routed to model: {response.model}")
print(f"Response:\n{response.output_text}")
print(
f"\nUsage: {response.usage.input_tokens} input + {response.usage.output_tokens} output = {response.usage.total_tokens} total tokens"
)