Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/.sync-sha
Original file line number Diff line number Diff line change
@@ -1 +1 @@
47eb7873d1c3fe6155df55eb39d8bc107f47db46
154cef9a84b8bdd6966acebaced70f9a5a0b3d46
4 changes: 4 additions & 0 deletions samples/python/foundry-models/model-router/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Azure OpenAI resource endpoint (used by Chat Completions and Responses examples)
AZURE_OPENAI_ENDPOINT=https://your-resource-name.openai.azure.com/
AZURE_OPENAI_API_KEY=your-api-key-here
MODEL_DEPLOYMENT_NAME=model-router
87 changes: 87 additions & 0 deletions samples/python/foundry-models/model-router/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Foundry Model Router — API & SDK Samples

Simple "Hello World" Python examples showing how to use [Foundry Model Router](https://learn.microsoft.com/azure/foundry/openai/how-to/model-router) across different Azure OpenAI APIs and SDKs.

Model Router is a deployable AI chat model in Azure AI Foundry that **automatically selects the best underlying LLM** for each prompt in real time. It delivers high performance and cost savings from a single deployment — you use it just like any other chat model.

## Examples

| Folder | API | Auth | Description |
|--------|-----|------|-------------|
| [`chat-completions/`](chat-completions/) | Chat Completions | API Key | Basic single-prompt chat completion via `AzureOpenAI` client |
| [`foundry-responses-sdk/`](foundry-responses-sdk/) | Foundry SDK | Entra ID | Uses `AIProjectClient` → `get_openai_client()` → Responses API |

## Prerequisites

- **Python 3.9+**
- **Azure subscription** with an Azure OpenAI resource
- **Model Router deployment** — deploy `model-router` from the model catalog in [Microsoft Foundry](https://ai.azure.com/)
- For the Foundry SDK example only: **Azure CLI** installed and logged in (`az login`)

## Setup

1. **Create a virtual environment** (recommended)

```bash
python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS/Linux
source .venv/bin/activate
```

2. **Install dependencies**

```bash
pip install -r requirements.txt
```

3. **Create your `.env` file**

```bash
cp .env.sample .env
```

Edit `.env` with your values:

```
AZURE_OPENAI_ENDPOINT=https://your-resource-name.openai.azure.com/
AZURE_OPENAI_API_KEY=your-api-key-here
MODEL_DEPLOYMENT_NAME=model-router
AZURE_AI_PROJECT_ENDPOINT=https://your-ai-services-account-name.services.ai.azure.com/api/projects/your-project-name
```

## Run the Examples

### Chat Completions API

```bash
python model-router-chat-completions.py
```

### Foundry Responses SDK (Entra ID)

```bash
az login
python model-router-foundry-responses-sdk.py
```

## What to Expect

Each example prints:
- **Which underlying model** was selected by the router (e.g. `gpt-4.1-mini-2025-04-14`)
- **The model's response** to the prompt
- Token usage

The `model` field in the response reveals which LLM the router chose. You control routing behavior (Balanced / Quality / Cost) at deployment time in the Foundry portal — not in code.

## Resources

- [Model Router documentation](https://learn.microsoft.com/azure/foundry/openai/how-to/model-router)
- [Model Router concepts](https://learn.microsoft.com/azure/foundry/openai/concepts/model-router)
- [Azure OpenAI Chat Completions quickstart](https://learn.microsoft.com/azure/ai-foundry/openai/how-to/chatgpt)
- [Azure AI Projects SDK (PyPI)](https://pypi.org/project/azure-ai-projects/)

## License

MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Foundry Model Router - Chat Completions API Example

This example demonstrates how to use Azure OpenAI's Chat Completions API
with a Foundry Model Router deployment. Model Router automatically selects
the best underlying LLM for each prompt based on your routing mode
(Balanced, Quality, or Cost).

Prerequisites:
- An Azure OpenAI resource with a "model-router" deployment
- A .env file in the repo root with AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_API_KEY,
and MODEL_DEPLOYMENT_NAME

Usage:
pip install -r ../requirements.txt
python model-router-chat-completions.py
"""

import os
from pathlib import Path

from dotenv import load_dotenv
from openai import AzureOpenAI

# Load environment variables from .env in the repo root
load_dotenv(Path(__file__).resolve().parent.parent / ".env", override=True)

endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
api_key = os.environ["AZURE_OPENAI_API_KEY"]
deployment = os.environ["MODEL_DEPLOYMENT_NAME"]

client = AzureOpenAI(
azure_endpoint=endpoint,
api_key=api_key,
api_version="2024-10-21",
)

response = client.chat.completions.create(
model=deployment,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "user",
"content": "In one sentence, name the most popular tourist destination in Seattle.",
},
],
)

print("--- Chat Completions Response ---")
print(f"Routed to model: {response.model}")
print(f"Response:\n{response.choices[0].message.content}")
print(
f"\nUsage: {response.usage.prompt_tokens} prompt + {response.usage.completion_tokens} completion = {response.usage.total_tokens} total tokens"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,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"
)
4 changes: 4 additions & 0 deletions samples/python/foundry-models/model-router/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
openai>=1.75.0
python-dotenv
azure-ai-projects>=2.0.0
azure-identity
5 changes: 5 additions & 0 deletions samples/python/foundry-models/model-router/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Model Router Quickstart with Chat Completions & Foundry Responses SDK
description: Basic quickstart sample demonstrating the use of Model Router with Foundry APIs and SDKs.

build: "pip install -r requirements.txt"
validate: "python -m py_compile *.py"
Loading