openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
examples/azure.py
43lines · modecode
| 1 | from openai import AzureOpenAI |
| 2 | |
| 3 | # may change in the future |
| 4 | # https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning |
| 5 | api_version = "2023-07-01-preview" |
| 6 | |
| 7 | # gets the API Key from environment variable AZURE_OPENAI_API_KEY |
| 8 | client = AzureOpenAI( |
| 9 | api_version=api_version, |
| 10 | # https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource |
| 11 | azure_endpoint="https://example-endpoint.openai.azure.com", |
| 12 | ) |
| 13 | |
| 14 | completion = client.chat.completions.create( |
| 15 | model="deployment-name", # e.g. gpt-35-instant |
| 16 | messages=[ |
| 17 | { |
| 18 | "role": "user", |
| 19 | "content": "How do I output all files in a directory using Python?", |
| 20 | }, |
| 21 | ], |
| 22 | ) |
| 23 | print(completion.to_json()) |
| 24 | |
| 25 | |
| 26 | deployment_client = AzureOpenAI( |
| 27 | api_version=api_version, |
| 28 | # https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource |
| 29 | azure_endpoint="https://example-resource.azure.openai.com/", |
| 30 | # Navigate to the Azure OpenAI Studio to deploy a model. |
| 31 | azure_deployment="deployment-name", # e.g. gpt-35-instant |
| 32 | ) |
| 33 | |
| 34 | completion = deployment_client.chat.completions.create( |
| 35 | model="<ignored>", |
| 36 | messages=[ |
| 37 | { |
| 38 | "role": "user", |
| 39 | "content": "How do I output all files in a directory using Python?", |
| 40 | }, |
| 41 | ], |
| 42 | ) |
| 43 | print(completion.to_json()) |
| 44 | |