-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(examples/azure): add async snippet (#1787)
- Loading branch information
Showing
1 changed file
with
58 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,67 @@ | ||
from azure.identity import DefaultAzureCredential, get_bearer_token_provider | ||
import asyncio | ||
|
||
from openai import AzureOpenAI | ||
from openai.lib.azure import AzureOpenAI, AsyncAzureOpenAI, AzureADTokenProvider, AsyncAzureADTokenProvider | ||
|
||
token_provider = get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default") | ||
scopes = "https://cognitiveservices.azure.com/.default" | ||
|
||
|
||
# may change in the future | ||
# May change in the future | ||
# https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning | ||
api_version = "2023-07-01-preview" | ||
|
||
# https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource | ||
endpoint = "https://my-resource.openai.azure.com" | ||
|
||
client = AzureOpenAI( | ||
api_version=api_version, | ||
azure_endpoint=endpoint, | ||
azure_ad_token_provider=token_provider, | ||
) | ||
|
||
completion = client.chat.completions.create( | ||
model="deployment-name", # e.g. gpt-35-instant | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "How do I output all files in a directory using Python?", | ||
}, | ||
], | ||
) | ||
print(completion.to_json()) | ||
deployment_name = "deployment-name" # e.g. gpt-35-instant | ||
|
||
|
||
def sync_main() -> None: | ||
from azure.identity import DefaultAzureCredential, get_bearer_token_provider | ||
|
||
token_provider: AzureADTokenProvider = get_bearer_token_provider(DefaultAzureCredential(), scopes) | ||
|
||
client = AzureOpenAI( | ||
api_version=api_version, | ||
azure_endpoint=endpoint, | ||
azure_ad_token_provider=token_provider, | ||
) | ||
|
||
completion = client.chat.completions.create( | ||
model=deployment_name, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "How do I output all files in a directory using Python?", | ||
} | ||
], | ||
) | ||
|
||
print(completion.to_json()) | ||
|
||
|
||
async def async_main() -> None: | ||
from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider | ||
|
||
token_provider: AsyncAzureADTokenProvider = get_bearer_token_provider(DefaultAzureCredential(), scopes) | ||
|
||
client = AsyncAzureOpenAI( | ||
api_version=api_version, | ||
azure_endpoint=endpoint, | ||
azure_ad_token_provider=token_provider, | ||
) | ||
|
||
completion = await client.chat.completions.create( | ||
model=deployment_name, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "How do I output all files in a directory using Python?", | ||
} | ||
], | ||
) | ||
|
||
print(completion.to_json()) | ||
|
||
|
||
sync_main() | ||
|
||
asyncio.run(async_main()) |