Bird
Raised Fist0
Prompt Engineering / GenAIml~10 mins

Chat completions endpoint in Prompt Engineering / GenAI - Interactive Code Practice

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 send a chat message using the completions endpoint.

Prompt Engineering / GenAI
response = client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": [1]])
Drag options to blanks, or click blank then click option'
A"Hello, how are you?"
BHello, how are you?
C'Hello, how are you?'
DHello how are you
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the message content inside quotes.
Using single quotes instead of double quotes in JSON.
2fill in blank
medium

Complete the code to extract the chat completion text from the response.

Prompt Engineering / GenAI
completion_text = response.choices[0].[1].content
Drag options to blanks, or click blank then click option'
Aresponse
Btext
Ccontent
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' instead of 'message'.
Trying to access 'content' directly from choices[0].
3fill in blank
hard

Fix the error in the code to correctly send multiple messages in the chat completion request.

Prompt Engineering / GenAI
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": [1]
]
response = client.chat.completions.create(model="gpt-4o-mini", messages=messages)
Drag options to blanks, or click blank then click option'
AWhat is the weather today?
B'What is the weather today?'
C"What is the weather today?"
DWhat is the weather today
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting quotes around the message content.
Using single quotes instead of double quotes.
4fill in blank
hard

Fill both blanks to create a chat completion request with a system and user message.

Prompt Engineering / GenAI
messages = [
    {"role": [1], "content": "You are a friendly bot."},
    {"role": [2], "content": "Tell me a joke."}
]
response = client.chat.completions.create(model="gpt-4o-mini", messages=messages)
Drag options to blanks, or click blank then click option'
A"system"
B"assistant"
C"user"
D"bot"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'assistant' or 'bot' as the user role.
Not quoting the role strings.
5fill in blank
hard

Fill all three blanks to extract the chat completion text and print it.

Prompt Engineering / GenAI
response = client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "Hello!"}])
completion = response.[1][0].[2].[3]
print(completion)
Drag options to blanks, or click blank then click option'
Achoices
Bmessage
Ccontent
Dresults
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'results' instead of 'choices'.
Accessing 'content' directly from choices[0].
Missing the 'message' attribute.

Practice

(1/5)
1. What is the main purpose of the chat completions endpoint in GenAI?
easy
A. To send messages and receive AI-generated replies in a conversation format
B. To train a new AI model from scratch
C. To upload datasets for AI training
D. To visualize AI model architecture

Solution

  1. Step 1: Understand the endpoint's function

    The chat completions endpoint is designed to handle conversations by sending messages and getting AI replies.
  2. Step 2: Compare options with the endpoint's purpose

    Only To send messages and receive AI-generated replies in a conversation format describes sending messages and receiving replies, which matches the chat completions endpoint.
  3. Final Answer:

    To send messages and receive AI-generated replies in a conversation format -> Option A
  4. Quick Check:

    Chat completions endpoint = conversation replies [OK]
Hint: Chat completions = chat messages in, AI replies out [OK]
Common Mistakes:
  • Confusing chat completions with model training
  • Thinking it uploads data instead of chatting
  • Assuming it visualizes model details
2. Which of the following is the correct way to format messages sent to the chat completions endpoint?
easy
A. [{"content": "Hello!"}, {"content": "Hi! How can I help?"}]
B. ["Hello!", "Hi! How can I help?"]
C. {"user": "Hello!", "assistant": "Hi! How can I help?"}
D. [{"role": "user", "content": "Hello!"}, {"role": "assistant", "content": "Hi! How can I help?"}]

Solution

  1. Step 1: Recall message format requirements

    The chat completions endpoint expects a list of messages, each with a role and content.
  2. Step 2: Match options to the required format

    [{"role": "user", "content": "Hello!"}, {"role": "assistant", "content": "Hi! How can I help?"}] correctly uses a list of dictionaries with "role" and "content" keys, matching the expected format.
  3. Final Answer:

    [{"role": "user", "content": "Hello!"}, {"role": "assistant", "content": "Hi! How can I help?"}] -> Option D
  4. Quick Check:

    Messages need role and content keys [OK]
Hint: Messages need both role and content keys [OK]
Common Mistakes:
  • Sending messages as plain strings without roles
  • Using incorrect JSON object structure
  • Omitting the role field in messages
3. Given this code snippet using the chat completions endpoint, what will be the output's role and content?
messages = [{"role": "user", "content": "What's the weather?"}]
response = chat_completions(messages=messages, temperature=0.5)
print(response.choices[0].message)
medium
A. {"role": "system", "content": "Weather info not available."}
B. {"role": "user", "content": "What's the weather?"}
C. {"role": "assistant", "content": "I don't have weather data."}
D. An error because temperature is invalid

Solution

  1. Step 1: Understand the response structure

    The chat completions endpoint returns a response with choices, each containing a message with role and content.
  2. Step 2: Identify the role of the returned message

    The returned message role is "assistant" because the AI replies to the user message.
  3. Final Answer:

    {"role": "assistant", "content": "I don't have weather data."} -> Option C
  4. Quick Check:

    Response role = assistant, content = AI reply [OK]
Hint: AI replies have role 'assistant' in response [OK]
Common Mistakes:
  • Confusing user message with AI reply
  • Expecting system role in output
  • Thinking temperature causes error here
4. You wrote this code but get an error:
messages = [{"content": "Hello!"}]
response = chat_completions(messages=messages)
print(response.choices[0].message)
What is the likely cause of the error?
medium
A. The messages list should be a string, not a list
B. Missing the 'role' key in the message dictionary
C. The chat_completions function requires a 'temperature' argument
D. The print statement syntax is incorrect

Solution

  1. Step 1: Check message format requirements

    Each message must have both 'role' and 'content' keys to be valid.
  2. Step 2: Identify missing key in the code

    The message dictionary only has 'content' but lacks the required 'role' key, causing the error.
  3. Final Answer:

    Missing the 'role' key in the message dictionary -> Option B
  4. Quick Check:

    Every message needs role and content keys [OK]
Hint: Always include 'role' in each message dictionary [OK]
Common Mistakes:
  • Assuming temperature is mandatory
  • Thinking messages should be a string
  • Blaming print statement syntax
5. You want the AI to give more creative and varied answers using the chat completions endpoint. Which parameter should you adjust and how?
hard
A. Increase the temperature value closer to 1 to make responses more creative
B. Decrease the max_tokens to limit response length
C. Set temperature to 0 to get random answers
D. Remove the messages parameter to let AI decide context

Solution

  1. Step 1: Understand the role of temperature

    The temperature parameter controls randomness; higher values produce more creative and varied outputs.
  2. Step 2: Choose the correct adjustment for creativity

    Increasing temperature closer to 1 encourages creativity, while 0 makes responses deterministic.
  3. Final Answer:

    Increase the temperature value closer to 1 to make responses more creative -> Option A
  4. Quick Check:

    Higher temperature = more creative answers [OK]
Hint: Higher temperature means more creative AI replies [OK]
Common Mistakes:
  • Setting temperature to 0 expecting creativity
  • Confusing max_tokens with creativity control
  • Removing messages causes loss of context