Skip to content Skip to footer

Getting Started with Azure OpenAI

Preparations

Open a terminal and execute the following commands:

pip3 install openai python-dotenv

Execute your first prompt

.env

AZURE_OPENAI_ENDPOINT = "[your endpoint]"
AZURE_OPENAI_KEY = "[your api key]"
CHAT_COMPLETION_NAME = "[name of your model]"

main.py

import os
from openai import AzureOpenAI
from dotenv import load_dotenv
load_dotenv()

client = AzureOpenAI(
  azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"), 
  api_key=os.getenv("AZURE_OPENAI_KEY"),  
  api_version="2024-08-01-preview"
)

# Select the model
model = os.getenv("CHAT_COMPLETION_NAME")

# Create your first prompt
text_prompt = "How far away is the moon from the earth?"

response = client.chat.completions.create(
  model=model,
  messages = [{"role":"system", "content":"You are a helpful assistant."},
              {"role":"user","content":text_prompt},])

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

Leave a comment

Receive my Python cheatsheet today!

Do you want to become a Python expert? I summarized all my expertise in a 3 pages cheatsheet, so you never have to Google again :)

Socials

Tom’s Tech Academy © 2025. All Rights Reserved.