Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the OpenAI client from the OpenAI library.
Prompt Engineering / GenAI
from openai import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like 'DALL-E' or 'DalleApi'.
Trying to import a module instead of the client class.
✗ Incorrect
The correct import for the OpenAI client is 'OpenAI' from the OpenAI library.
2fill in blank
mediumComplete the code to create an OpenAI client instance using the API key stored in the environment variable.
Prompt Engineering / GenAI
client = OpenAI(api_key=[1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong environment variable name.
Using os.environ which throws error if variable is missing.
✗ Incorrect
The API key is usually stored in the environment variable 'OPENAI_API_KEY' and accessed with os.getenv for safety.
3fill in blank
hardFix the error in the code to generate an image with DALL-E using a prompt string.
Prompt Engineering / GenAI
response = client.images.generate(model="dall-e-2", prompt=[1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the prompt without quotes causing a NameError.
Passing a list or dictionary instead of a string.
✗ Incorrect
The prompt must be a string literal, so it needs quotes around it.
4fill in blank
hardFill both blanks to set the image size and number of images to generate.
Prompt Engineering / GenAI
response = client.images.generate(model="dall-e-2", prompt='A cat playing piano', size=[1], n=[2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using integers for size instead of string.
Setting n to a string instead of an integer.
✗ Incorrect
The size should be a string like '512x512' and n is the number of images, here 3.
5fill in blank
hardFill all three blanks to extract the image URL from the response dictionary.
Prompt Engineering / GenAI
image_url = response.[1][[2]].[3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keys like 'images' instead of 'data'.
Using string '0' instead of integer 0 for index.
Trying to access 'url' directly from response.
✗ Incorrect
The response contains a 'data' list, we take the first item [0], then get its 'url' field.