All labs · Lab 2 of 5 · 1-2 hours

Model Deployment & Calling

Call the deployed gpt-4o-mini or assigned model from Python and from REST, and observe the token cost.

Prerequisites

  • Foundry Project Setup lab completed
  • Python 3.10+ installed locally OR access to a Codespaces / Cloud Shell

Steps

1

Copy endpoint and deployment name to .env

From the Foundry project page, copy the endpoint URL and the deployment name. Paste them into a local .env file. Commit only .env.example.

bash
# .env (gitignored)
AZURE_OPENAI_ENDPOINT="https://<your-foundry-endpoint>/"
AZURE_OPENAI_API_KEY="<key-from-foundry>"
AZURE_OPENAI_DEPLOYMENT="gpt-4o-mini"
AZURE_OPENAI_API_VERSION="2024-12-01-preview"
2

Install the Azure OpenAI SDK

Use the official openai Python SDK which supports Azure endpoints natively.

bash
pip install openai python-dotenv
3

Send your first model call

Keep the prompt short. Print the response and the token usage.

python
from openai import AzureOpenAI
from dotenv import load_dotenv
import os

load_dotenv()
client = AzureOpenAI(
    api_key=os.environ["AZURE_OPENAI_API_KEY"],
    api_version=os.environ["AZURE_OPENAI_API_VERSION"],
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
)

response = client.chat.completions.create(
    model=os.environ["AZURE_OPENAI_DEPLOYMENT"],
    messages=[
        {"role": "system", "content": "You answer briefly in one sentence."},
        {"role": "user", "content": "Why is the sky blue?"},
    ],
    max_tokens=120,
)

print(response.choices[0].message.content)
print(response.usage)
4

Try the REST equivalent

Foundry also exposes a REST endpoint. Use curl when you need to test from a shell or CI.

bash
curl -sS "$AZURE_OPENAI_ENDPOINT/openai/deployments/$AZURE_OPENAI_DEPLOYMENT/chat/completions?api-version=$AZURE_OPENAI_API_VERSION" \
  -H "Content-Type: application/json" \
  -H "api-key: $AZURE_OPENAI_API_KEY" \
  -d '{"messages":[{"role":"user","content":"Hello"}],"max_tokens":40}'

Verification

When all three (or more) of these are true, you can mark the lab complete and move on.

  • Python script prints a short answer
  • Response usage shows prompt_tokens and completion_tokens
  • REST curl returns a JSON body with the same structure

Azure cost

A 40-token test call costs roughly $0.0003 (less than 1 KRW). Avoid loops while iterating.

Responsible AI

Print the system message in your repo, not the api-key. Never commit .env. Logs should redact prompts that include user data.

Hackathon connection

Sample repo — Hello World call