Hello Sudheer Kumar,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
I understand that your goal is to fetch Azure OpenAl api_version and available engines dynamically after resource creation via Python.
NOTE: That the api_version for Azure OpenAI is not dynamically tied to the resource. It must be set explicitly based on the Azure SDK or API being used. For an example, the API version used depends on the Azure OpenAI service version (e.g., 2022-12-01
, 2023-03-15-preview
) and you can explicitly define the desired API version when making calls:
api_version = "2023-03-15-preview" # Example API version for GPT-4 or GPT-3.5 engines.
If multiple versions are available, refer to Azure documentation for supported versions or use the SDK to list supported versions programmatically.
Secondly, to dynamically fetch engines, you will need to:
- Confirm the Azure OpenAI resource is deployed in a publicly accessible region.
- Verify no firewalls or DNS policies block access to https://<resource_name>.openai.azure.com.
- Use the following Python script to retrieve engines:
import requests from azure.identity import DefaultAzureCredential # Parameters resource_name = "your_resource_name" api_version = "2023-03-15-preview" # Replace with desired API version # Auth Token credential = DefaultAzureCredential() token = credential.get_token("https://cognitiveservices.azure.com/.default").token # API Endpoint for engines url = f"https://{resource_name}.openai.azure.com/openai/deployments?api-version={api_version}" headers = { "Authorization": f"Bearer {token}" } # Fetch Engines try: response = requests.get(url, headers=headers) response.raise_for_status() engines = response.json() print("Available Engines:", engines) except requests.exceptions.RequestException as e: print("Error fetching engines:", e)
If there are any DNS issues, verify the Azure resource endpoint is valid, that the <resource_name>
matches the actual Azure OpenAI resource name and you're in a supported region. If DNS issues persist, check your system's DNS settings e.g., use Google's public DNS 8.8.8.8 or use tools like ping or curl to perform testing: curl -v https://<resource_name>.openai.azure.com
Finally, you can use Azure SDK for Python in a programmatic way to interact with OpenAI resources like an example below:
from azure.ai.openai import OpenAIClient
from azure.identity import DefaultAzureCredential
# Parameters
endpoint = f"https://{resource_name}.openai.azure.com"
credential = DefaultAzureCredential()
# Initialize Client
client = OpenAIClient(endpoint=endpoint, credential=credential)
# List Deployments (Engines)
deployments = client.list_deployments()
for deployment in deployments:
print(f"Engine: {deployment['model']} - ID: {deployment['id']}")
I hope this is helpful! Do not hesitate to let me know if you have any other questions.
Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.