Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

First interaction with GenAI APIs - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
GenAI API Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding GenAI API Response Structure

When you send a prompt to a GenAI API, what is the typical structure of the response you receive?

AA JSON object containing the generated text, usage details, and metadata about the request
BA binary file that needs special decoding to extract the generated text
CAn XML file with multiple nested tags describing the model internals
DA plain text string with only the generated text and no additional information
Attempts:
2 left
💡 Hint

Think about what extra information might be useful besides just the generated text.

Predict Output
intermediate
2:00remaining
Output of a GenAI API Call with a Simple Prompt

What will be the output of this Python code snippet that calls a GenAI API with a prompt?

Prompt Engineering / GenAI
response = {
    'choices': [{'message': {'content': 'Hello, how can I help you today?'}}],
    'usage': {'prompt_tokens': 5, 'completion_tokens': 7, 'total_tokens': 12}
}
print(response['choices'][0]['message']['content'])
AKeyError
Bresponse
C{'message': {'content': 'Hello, how can I help you today?'}}
DHello, how can I help you today?
Attempts:
2 left
💡 Hint

Look at how the dictionary keys are accessed to get the message content.

Model Choice
advanced
2:00remaining
Choosing the Right GenAI Model for Summarization

You want to use a GenAI API to summarize long articles quickly and accurately. Which model type should you choose?

AA small model optimized for image recognition
BA general-purpose language model trained only on chat conversations
CA large language model fine-tuned specifically for summarization tasks
DA model designed for speech-to-text transcription
Attempts:
2 left
💡 Hint

Think about which model is trained for text summarization.

Hyperparameter
advanced
2:00remaining
Effect of Temperature Parameter in GenAI API Calls

What happens if you increase the temperature parameter in a GenAI API call?

AThe output text becomes shorter and more concise
BThe generated text becomes more random and creative
CThe API call fails with an error
DThe model runs faster but with less accuracy
Attempts:
2 left
💡 Hint

Temperature controls randomness in text generation.

Metrics
expert
2:00remaining
Interpreting Token Usage Metrics from GenAI API

After a GenAI API call, you receive usage metrics: prompt_tokens=50, completion_tokens=150, total_tokens=200. What does this tell you?

AThe model used 50 tokens from your prompt and generated 150 tokens in response, totaling 200 tokens processed
BThe model generated 50 tokens and used 150 tokens from the prompt, totaling 200 tokens
CThe total tokens count is incorrect because 50 + 150 does not equal 200
DThe model only processed 150 tokens in total
Attempts:
2 left
💡 Hint

Think about what prompt and completion tokens represent.

Practice

(1/5)
1. What is the main purpose of a GenAI API when you first interact with it?
easy
A. To train a new AI model from scratch
B. To store large datasets for AI training
C. To manually code AI algorithms
D. To send a prompt and receive a text response from the AI model

Solution

  1. Step 1: Understand what GenAI APIs do

    GenAI APIs let you send a prompt (a question or task) to an AI model.
  2. Step 2: Identify the response from the API

    The API returns a text response generated by the AI based on your prompt.
  3. Final Answer:

    To send a prompt and receive a text response from the AI model -> Option D
  4. Quick Check:

    GenAI API = prompt in, text out [OK]
Hint: GenAI APIs take your question and give text answers [OK]
Common Mistakes:
  • Thinking you train the AI on first use
  • Believing you write AI code manually
  • Confusing API with data storage
2. Which of the following is the correct way to send a prompt to a GenAI API in Python?
easy
A. response = genai.ask(prompt)
B. response = genai.ask('Hello AI!')
C. response = genai.ask(prompt='Hello AI!')
D. response = genai.ask(input='Hello AI!')

Solution

  1. Step 1: Check the correct parameter name for prompt

    The GenAI API expects the prompt to be passed with the keyword 'prompt'.
  2. Step 2: Verify the syntax for calling the API

    Using named argument prompt='Hello AI!' matches the expected syntax.
  3. Final Answer:

    response = genai.ask(prompt='Hello AI!') -> Option C
  4. Quick Check:

    Use prompt= keyword to send text [OK]
Hint: Use prompt='text' when calling genai.ask() [OK]
Common Mistakes:
  • Omitting the prompt= keyword
  • Using wrong parameter name like input=
  • Passing variable without quotes when string needed
3. Given the code below, what will be printed?
response = genai.ask(prompt='What is 2 + 2?')
print(response.text)
medium
A. '4'
B. 'What is 2 + 2?'
C. An error because response has no attribute text
D. '2 + 2 equals 4'

Solution

  1. Step 1: Understand the prompt sent to the AI

    The prompt asks the AI a simple math question: 'What is 2 + 2?'.
  2. Step 2: Predict the AI's text response

    The AI will respond with the answer '4' as text, accessible via response.text.
  3. Final Answer:

    '4' -> Option A
  4. Quick Check:

    Simple math prompt returns answer text [OK]
Hint: AI answers math questions with the result as text [OK]
Common Mistakes:
  • Expecting the prompt text to be printed
  • Assuming response.text does not exist
  • Thinking AI returns full sentence instead of just answer
4. You wrote this code but get an error:
response = genai.ask('Hello AI!')
print(response.text)
What is the likely cause?
medium
A. The print statement is incorrect
B. The prompt argument is missing its keyword name
C. The genai.ask function does not exist
D. response.text is not a valid attribute

Solution

  1. Step 1: Check how the prompt is passed to genai.ask()

    The code passes 'Hello AI!' without specifying prompt= keyword.
  2. Step 2: Understand the API expects prompt= keyword

    Without prompt=, the function may raise an error or not recognize the input.
  3. Final Answer:

    The prompt argument is missing its keyword name -> Option B
  4. Quick Check:

    Always use prompt= when calling genai.ask() [OK]
Hint: Always name the prompt argument: prompt='text' [OK]
Common Mistakes:
  • Passing prompt as positional argument
  • Assuming print statement causes error
  • Thinking response.text is invalid
5. You want to use a GenAI API to get a short story about a cat. Which approach is best to get a clear, useful response?
hard
A. Send prompt='Write a short story about a cat in 3 sentences.'
B. Send prompt='cat story'
C. Send prompt='Tell me something interesting.'
D. Send prompt='Write a story'

Solution

  1. Step 1: Identify the prompt that clearly states the task

    Send prompt='Write a short story about a cat in 3 sentences.' specifies the topic (cat), the type (short story), and length (3 sentences).
  2. Step 2: Compare other prompts for clarity

    Options B, C, and D are vague and may produce unrelated or too long responses.
  3. Final Answer:

    Send prompt='Write a short story about a cat in 3 sentences.' -> Option A
  4. Quick Check:

    Clear, detailed prompts get better AI answers [OK]
Hint: Be specific and clear in your prompt for best results [OK]
Common Mistakes:
  • Using too short or vague prompts
  • Not specifying length or topic clearly
  • Expecting AI to guess details