Command Palette

Search for a command to run...

Hectal
Phase 11: Testing and Code Quality
Python · Exercise 3 of 6 PreviousNext
Intermediate

Write a fake AI client for tests

The class Assistant calls client.complete(prompt) on any client you give it. In tests you do not want to call a real model, so write the class FakeLLM.

FakeLLM(replies) receives a dictionary that maps a prompt to a reply. Its method complete(prompt) returns the reply for that prompt, or the text "I don't know" if the prompt is not in the dictionary. It also records every prompt it receives, in order, in a list called calls. Two different fakes must not share the same calls list. (The dictionary keys are the full prompt texts, exactly as Assistant builds them.)

The last line of the starter uses your fake with Assistant; it should print 4.

What Check tests

  1. complete returns the canned reply for a known prompt
  2. complete returns "I don't know" for an unknown prompt
  3. calls records every prompt in order
  4. Two fakes do not share their calls list
  5. Assistant works with the fake and the program prints 4

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.