Skip to content

Instantly share code, notes, and snippets.

@izikeros
Last active July 11, 2024 11:47
Show Gist options
  • Save izikeros/4ce385fae3cdc732dd8df57617ecfb38 to your computer and use it in GitHub Desktop.
Save izikeros/4ce385fae3cdc732dd8df57617ecfb38 to your computer and use it in GitHub Desktop.
[Azure OpenAI Chat Completion boilerplate] The small example that can be a test if openai is configured properly and works #openai
"""Smoke test for Azure OpenAI API.
Check if the API is working as expected and all the required environment variables are set.
Requires the following environment variables (you can use it as .env_template):
AZURE_OPENAI_API_KEY=
AZURE_OPENAI_API_VERSION=
AZURE_OPENAI_ENDPOINT=
COMPLETION_DEPLOYMENT_NAME=
"""
import os
from dotenv import load_dotenv
from openai import AzureOpenAI
load_dotenv()
client = AzureOpenAI(
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
)
deployment_name = os.getenv("COMPLETION_DEPLOYMENT_NAME")
response = client.chat.completions.create(
model=deployment_name,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Does Azure OpenAI support customer managed keys?"},
{
"role": "assistant",
"content": "Yes, customer managed keys are supported by Azure OpenAI.",
},
{"role": "user", "content": "Do other Azure AI services support this too?"},
],
)
print(response.choices[0].message.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment