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)