How to Fetch Azure OpenAl api_version and engine Dynamically After Resource Creation via Python?

Sudheer Kumar 0 Reputation points
2025-01-16T15:12:33.5466667+00:00

Hello!

I am using Python to automate the creation of Azure OpenAl resources via the Azure Management API. I am successfully able to create the resource, but I need to dynamically fetch the following details after the resource is created:

  1. API Version (api_version)
  2. Engine (such as text-davinci-003, gpt-3.5-turbo, etc.)

Here are the steps I have taken so far:

  1. Created Resource: I use the Azure Management API to create the OpenAl resource (PUT request).
  2. Fetch API Keys: After the resource is created, I fetch the API keys by calling listKeys.

However, when the resource is created, I am unable to dynamically retrieve the API version and engine directly from the resource.

Problem:

  • The api_version returned is showing as Unknown even though I know there is a specific version associated with the OpenAl resource.
  • I am unable to retrieve the engine types dynamically (like text-davinci-003, gpt-3.5-turbo, etc.) after the resource creation.

What I've Tried:

  • I tried using the exportTemplate API, but it only gives me a template and not the specific api_version and engine I need
  • I also tried checking the resource properties, but the api_version field remains Unknown.
  • I even tried making an API call to https://< resource_name>.openai.azure.com/vl/engines, but | receive DNS resolution issues.

My Goal:

I would like to automate the process of retrieving the API version and engine dynamically after the resource is created, using Python and Azure API.

Is there a way to retrieve this information directly after the resource creation or via a different API endpoint?

Any help or guidance would be greatly appreciated!

Thank you! Hello!

I am using Python to automate the creation of Azure OpenAl resources via the Azure Management API. I am successfully able to create the resource, but I need to dynamically fetch the following details after the resource is created:

  1. API Version (api_version)
  2. Engine (such as text-davinci-003, gpt-3.5-turbo, etc.)

Here are the steps I have taken so far:

  1. Created Resource: I use the Azure Management API to create the OpenAl resource (PUT request).
  2. Fetch API Keys: After the resource is created, I fetch the API keys by calling listKeys.

However, when the resource is created, I am unable to dynamically retrieve the API version and engine directly from the resource.

Problem:

  • The api_version returned is showing as Unknown even though I know there is a specific version associated with the OpenAl resource.
  • I am unable to retrieve the engine types dynamically (like text-davinci-003, gpt-3.5-turbo, etc.) after the resource creation.

What I've Tried:

  • I tried using the exportTemplate API, but it only gives me a template and not the specific api_version and engine I need
  • I also tried checking the resource properties, but the api_version field remains Unknown.
  • I even tried making an API call to https://< resource_name>.openai.azure.com/vl/engines, but | receive DNS resolution issues.

My Goal:

I would like to automate the process of retrieving the API version and engine dynamically after the resource is created, using Python and Azure API.

Is there a way to retrieve this information directly after the resource creation or via a different API endpoint?

Any help or guidance would be greatly appreciated!

Thank you!

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
3,836 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 18,876 Reputation points
    2025-01-17T11:38:25.69+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.