forked from Azure-Samples/python-openai-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_azure_client.py
More file actions
33 lines (27 loc) · 1022 Bytes
/
test_azure_client.py
File metadata and controls
33 lines (27 loc) · 1022 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os
import azure.identity
import openai
from dotenv import load_dotenv
load_dotenv(override=True)
# Test with AzureOpenAI client (proper way)
token_provider = azure.identity.get_bearer_token_provider(
azure.identity.DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default"
)
# The endpoint should NOT include /openai/v1 when using it as azure_endpoint
endpoint = os.getenv("AZURE_OPENAI_ENDPOINT", "").replace("/openai/v1", "")
print(f"Using endpoint: {endpoint}")
try:
client = openai.AzureOpenAI(
azure_endpoint=endpoint,
azure_ad_token_provider=token_provider,
api_version="2024-08-01-preview"
)
response = client.chat.completions.create(
model=os.getenv("AZURE_OPENAI_CHAT_DEPLOYMENT"),
messages=[{"role": "user", "content": "Say 'Hello, Azure!'"}],
max_tokens=10
)
print(f"\nSuccess! Response: {response.choices[0].message.content}")
except Exception as e:
print(f"\nError: {type(e).__name__}: {e}")