Launch price: every exam for $29.99 a year, for the first 300 buyers. See the price

CertStreak
Menu

Create a lightweight chat client application by using the Foundry SDK

A worked example

Put in order

You are extending a single-file Python chat client for a model deployed in Microsoft Foundry. The file already creates an authenticated client object with OpenAI(...) and stores the deployment name in deployment_name.

The client must meet these requirements:

  1. The system instruction You are a computing history tutor. Keep answers brief. must apply to every request.
  2. The second prompt, Which programming language did she help create?, must be answered in the context of the first prompt, Who was Grace Hopper?.
  3. The app manages the conversation history itself, in a Python list named messages that is passed as input on every call. The messages must stay in the order in which the conversation happened.
  4. Both replies must be printed.

Which actions should you add to the client? Place the required actions in the correct order and leave out any actions that are not needed.

Your order

You didn't answer this one.

Correct order
1Create messages with a system message ({"role": "system", "content": "You are a computing history tutor. Keep answers brief."}) followed by the first user message ({"role": "user", "content": "Who was Grace Hopper?"}).
2Call response = client.responses.create(model=deployment_name, input=messages) and print response.output_text.
3Append the reply to the history with messages.append({"role": "assistant", "content": response.output_text}).
4Append the second prompt with messages.append({"role": "user", "content": "Which programming language did she help create?"}).
5Call response = client.responses.create(model=deployment_name, input=messages) again and print response.output_text.
Why

Chat apps keep context by sending the earlier conversation along with each new prompt, so the second request must include the system message, the first question, the model's first reply and the new question together. The list is created first and sent in the first call. The reply can be appended only after that call returns, and the second question is appended after it to preserve conversation order. The second call then sends the complete messages list as input to the deployed model.

  • Step 1: Create messages with a system message ({"role": "system", "content": "You are a computing history tutor. Keep answers brief."}) followed by the first user message ({"role": "user", "content": "Who was Grace Hopper?"}). Every request is sent from this list, so it must exist before any call. Placing the system message first means it is part of every request that sends the list.
  • Step 2: Call response = client.responses.create(model=deployment_name, input=messages) and print response.output_text. The first request sends the list built in the previous step.
  • Step 3: Append the reply to the history with messages.append({"role": "assistant", "content": response.output_text}). The reply text exists only after the first request returns.
  • Step 4: Append the second prompt with messages.append({"role": "user", "content": "Which programming language did she help create?"}). Requirement 3 keeps the list in conversation order, so the second prompt must follow the model's first reply in the list.
  • Step 5: Call response = client.responses.create(model=deployment_name, input=messages) again and print response.output_text. The second request must carry the full history, including the prompt added in the previous step, so that the model can tell that 'she' means Grace Hopper.
  • Not needed: For the second call, pass only the second user message as input instead of messages. This breaks requirement 2. Without the earlier prompt and reply in the input, the model has no conversation context to work out who 'she' is.
  • Not needed: Remove the system message from messages after the first call to reduce the number of tokens sent. This breaks requirement 1, because the system instruction would no longer be part of the second request.

Practise this topic until it's automatic.

Ten questions a day, every answer explained, and misses that come back until you beat them.

Start free