openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
examples/azure_ad.py
30lines · modecode
| 1 | from azure.identity import DefaultAzureCredential, get_bearer_token_provider |
| 2 | |
| 3 | from openai import AzureOpenAI |
| 4 | |
| 5 | token_provider = get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default") |
| 6 | |
| 7 | |
| 8 | # may change in the future |
| 9 | # https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning |
| 10 | api_version = "2023-07-01-preview" |
| 11 | |
| 12 | # https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource |
| 13 | endpoint = "https://my-resource.openai.azure.com" |
| 14 | |
| 15 | client = AzureOpenAI( |
| 16 | api_version=api_version, |
| 17 | azure_endpoint=endpoint, |
| 18 | azure_ad_token_provider=token_provider, |
| 19 | ) |
| 20 | |
| 21 | completion = client.chat.completions.create( |
| 22 | model="deployment-name", # e.g. gpt-35-instant |
| 23 | messages=[ |
| 24 | { |
| 25 | "role": "user", |
| 26 | "content": "How do I output all files in a directory using Python?", |
| 27 | }, |
| 28 | ], |
| 29 | ) |
| 30 | print(completion.to_json()) |
| 31 | |