Generate images with Azure OpenAI Service - Python Code in MS Learn not working

Ziggy Zulueta 490 Reputation points MVP
2024-03-26T08:43:44.8833333+00:00

I am going thru this Microsoft Learn Module: "Quickstart: Generate images with Azure OpenAI Service"

https://learn.microsoft.com/en-us/azure/ai-services/openai/dall-e-quickstart?tabs=dalle3%2Ccommand-line&pivots=programming-language-python

This python code is not working in the module: https://learn.microsoft.com/en-us/azure/ai-services/openai/dall-e-quickstart?tabs=dalle3%2Ccommand-line&pivots=programming-language-python#generate-images-with-dall-e

I get the error below:

Traceback (most recent call last): File "C:\Users\ziggy\azureopenaisample\generative-ai-for-beginners\generative-ai-for-beginners\09-building-image-applications\python\app1.py", line 17, in <module> result = client.images.generate( ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ziggy\AppData\Roaming\Python\Python312\site-packages\openai\resources\images.py", line 251, in generate return self._post( ^^^^^^^^^^^ File "C:\Users\ziggy\AppData\Roaming\Python\Python312\site-packages\openai_base_client.py", line 1055, in post return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ziggy\AppData\Roaming\Python\Python312\site-packages\openai_base_client.py", line 834, in request return self._request( ^^^^^^^^^^^^^^ File "C:\Users\ziggy\AppData\Roaming\Python\Python312\site-packages\openai_base_client.py", line 877, in _request raise self._make_status_error_from_response(err.response) from None openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

My deployment seems to be correct:

User's image

And I am generating images in the playground too:

User's image

Upon clicking View code above, it generates a code as well:

User's image

I input that same code in Visual Studio and I get the same error as above:

Traceback (most recent call last): File "C:\Users\ziggy\azureopenaisample\generative-ai-for-beginners\generative-ai-for-beginners\09-building-image-applications\python\app1.py", line 17, in <module> result = client.images.generate( ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ziggy\AppData\Roaming\Python\Python312\site-packages\openai\resources\images.py", line 251, in generate return self._post( ^^^^^^^^^^^ File "C:\Users\ziggy\AppData\Roaming\Python\Python312\site-packages\openai_base_client.py", line 1055, in post return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ziggy\AppData\Roaming\Python\Python312\site-packages\openai_base_client.py", line 834, in request return self._request( ^^^^^^^^^^^^^^ File "C:\Users\ziggy\AppData\Roaming\Python\Python312\site-packages\openai_base_client.py", line 877, in _request raise self._make_status_error_from_response(err.response) from None openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}} PS C:\Users\ziggy\azureopenaisample\generative-ai-for-beginners\generative-ai-for-beginners\09-building-image-applications\python> python app1.py Traceback (most recent call last): File "C:\Users\ziggy\azureopenaisample\generative-ai-for-beginners\generative-ai-for-beginners\09-building-image-applications\python\app1.py", line 17, in <module> result = client.images.generate( ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ziggy\AppData\Roaming\Python\Python312\site-packages\openai\resources\images.py", line 251, in generate return self._post( ^^^^^^^^^^^ File "C:\Users\ziggy\AppData\Roaming\Python\Python312\site-packages\openai_base_client.py", line 1055, in post return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ziggy\AppData\Roaming\Python\Python312\site-packages\openai_base_client.py", line 834, in request return self._request( ^^^^^^^^^^^^^^ File "C:\Users\ziggy\AppData\Roaming\Python\Python312\site-packages\openai_base_client.py", line 877, in _request raise self._make_status_error_from_response(err.response) from None openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

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

Accepted answer
  1. dupammi 8,465 Reputation points Microsoft Vendor
    2024-03-26T11:07:09.2333333+00:00

    Hi @Ziggy Zulueta

    Thank you for using the Microsoft Q&A forum.

    Based on the error message, it seems that the resource is not found. Please ensure that you have deployed the DALL-E 3 model and that the deployment name is correct. Also, ensure that the API key and endpoint are correct.

    Here's a basic example code that worked at my end:

    import os
    from openai import AzureOpenAI
    import json
    client = AzureOpenAI(
        api_version="2024-02-01",
        azure_endpoint="YOUR_AZURE_END_POINT",
        api_key="YOUR_API_KEY",
    )
    result = client.images.generate(
        model="Dalle3", # the name of your DALL-E 3 deployment
        prompt="a close-up of a bear walking throughthe forest",
        n=1
    )
    image_url = json.loads(result.model_dump_json())['data'][0]['url']
    print(image_url)
    

    Please replace the azure_endpoint, api_key, and prompt with your own values. Also, ensure that the model parameter is set to the correct deployment name.

    Please confirm that you have completed all steps outlined in the Microsoft Learn module accurately, ensuring proper permissions and access rights to Azure resources and subscription details. Additionally, please verify that you have entered the correct API key and endpoint in your code. Ensure that you have installed all necessary Python packages and dependencies essential for running the code successfully.

    Result :
    User's image

    Refer this official documentation on generating images with Azure OpenAI Service using Python

    Hope this helps.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful.


0 additional answers

Sort by: Most helpful

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.