Bird
Raised Fist0
Prompt Engineering / GenAIml~10 mins

Why API access enables integration in Prompt Engineering / GenAI - Test Your Understanding

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call an API endpoint using Python's requests library.

Prompt Engineering / GenAI
import requests
response = requests.[1]('https://api.example.com/data')
print(response.status_code)
Drag options to blanks, or click blank then click option'
Aget
Bpost
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get' when only fetching data.
2fill in blank
medium

Complete the code to extract JSON data from the API response.

Prompt Engineering / GenAI
import requests
response = requests.get('https://api.example.com/data')
data = response.[1]()
print(data)
Drag options to blanks, or click blank then click option'
Atext
Bcontent
Cjson
Draw
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' returns raw string, not parsed JSON.
3fill in blank
hard

Fix the error in the code to send a POST request with JSON data.

Prompt Engineering / GenAI
import requests
payload = {'name': 'Alice', 'age': 30}
response = requests.post('https://api.example.com/users', [1]=payload)
print(response.status_code)
Drag options to blanks, or click blank then click option'
Adata
Bjson
Cparams
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' sends form data, not JSON, causing API errors.
4fill in blank
hard

Fill both blanks to check if the API response was successful and print the JSON content.

Prompt Engineering / GenAI
import requests
response = requests.get('https://api.example.com/data')
if response.[1] == [2]:
    print(response.json())
Drag options to blanks, or click blank then click option'
Astatus_code
B200
Cok
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ok' which is a boolean, not a numeric code.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that filters API data for items with 'active' status.

Prompt Engineering / GenAI
data = [{'id': 1, 'status': 'active'}, {'id': 2, 'status': 'inactive'}, {'id': 3, 'status': 'active'}]
active_items = {item['id']: item for item in data if item[1]'status'] [2] [3]
print(active_items)
Drag options to blanks, or click blank then click option'
A[
B==
C'active'
D['
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of brackets for dictionary keys.

Practice

(1/5)
1. Why does API access make it easier to add AI features to existing software?
easy
A. Because it allows software to talk to AI services without building AI from scratch
B. Because it requires rewriting the entire software code
C. Because it only works with one programming language
D. Because it stores all data locally on the user's device

Solution

  1. Step 1: Understand what API access means

    API access lets software send requests and get responses from AI services easily.
  2. Step 2: Connect API access to software integration

    This means developers can add AI features without building AI themselves, saving time and effort.
  3. Final Answer:

    Because it allows software to talk to AI services without building AI from scratch -> Option A
  4. Quick Check:

    API access enables easy AI integration [OK]
Hint: API means easy connection without rebuilding AI [OK]
Common Mistakes:
  • Thinking API requires rewriting all code
  • Believing API works only with one language
  • Assuming API stores data locally
2. Which of the following is the correct way to call an AI API in Python?
easy
A. response = api.call['generate_text', prompt='Hello']
B. response = api.call generate_text prompt='Hello'
C. response = api.call('generate_text' prompt='Hello')
D. response = api.call('generate_text', prompt='Hello')

Solution

  1. Step 1: Review Python function call syntax

    Functions are called with parentheses and arguments inside, separated by commas.
  2. Step 2: Check each option for correct syntax

    response = api.call('generate_text', prompt='Hello') uses correct parentheses and argument format. Others miss commas, parentheses, or use wrong brackets.
  3. Final Answer:

    response = api.call('generate_text', prompt='Hello') -> Option D
  4. Quick Check:

    Correct Python function call syntax [OK]
Hint: Look for parentheses and commas in function calls [OK]
Common Mistakes:
  • Missing commas between arguments
  • Using square brackets instead of parentheses
  • Omitting parentheses around arguments
3. Given this Python code calling an AI API:
response = api.call('translate', text='Hello', target_lang='es')
print(response)
What is the expected output if the API works correctly?
medium
A. 'Hola'
B. 'Hello'
C. Error: missing target language
D. 'Bonjour'

Solution

  1. Step 1: Understand the API call parameters

    The API is asked to translate 'Hello' into Spanish (target_lang='es').
  2. Step 2: Identify the correct translation output

    'Hola' is the Spanish word for 'Hello', so the API should return 'Hola'.
  3. Final Answer:

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

    Translate 'Hello' to Spanish = 'Hola' [OK]
Hint: Match target language code to correct translation [OK]
Common Mistakes:
  • Confusing language codes
  • Expecting original text as output
  • Assuming error without missing parameters
4. This code tries to call an AI API but causes an error:
response = api.call('summarize', text='Long article')
print(response['summary'])
What is the likely cause of the error?
medium
A. The function call syntax is incorrect
B. The 'text' parameter is missing
C. The API response is not a dictionary with 'summary' key
D. The API call is missing authentication

Solution

  1. Step 1: Analyze the code's access to response

    The code tries to get response['summary'], assuming response is a dictionary with that key.
  2. Step 2: Consider API response format

    If the API returns a string or different structure, accessing ['summary'] causes an error.
  3. Final Answer:

    The API response is not a dictionary with 'summary' key -> Option C
  4. Quick Check:

    Accessing missing key causes error [OK]
Hint: Check if response is dict before accessing keys [OK]
Common Mistakes:
  • Assuming all API responses are dicts
  • Ignoring missing parameters
  • Blaming syntax without checking response type
5. You want to integrate an AI chatbot into your website using API access. Which approach best ensures easy updates and scaling?
hard
A. Download AI software and run it only on one user's device
B. Use a cloud-based AI API service that handles updates and scaling automatically
C. Embed AI code directly into your website without API calls
D. Build your own AI model from scratch and host it on your local server

Solution

  1. Step 1: Understand integration needs for updates and scaling

    Easy updates and scaling require the AI system to be managed externally and accessible via API.
  2. Step 2: Evaluate each option for update and scaling ease

    Cloud-based AI API services automatically update and scale. Other options require manual work or limit access.
  3. Final Answer:

    Use a cloud-based AI API service that handles updates and scaling automatically -> Option B
  4. Quick Check:

    Cloud API services simplify updates and scaling [OK]
Hint: Cloud APIs handle updates and scaling for you [OK]
Common Mistakes:
  • Thinking local hosting is easier to scale
  • Embedding AI code limits flexibility
  • Running AI on one device limits users