Capstone · practice
Build chat
Chapter 9’s chat_turn(history, question, *, client, budget, sleep) answers one question: it sends the history and the question through the call policy, prints the reply as it streams, and returns a pair, the reply and the new history. The chat command needs a loop around it that reads the person’s questions one at a time, until they are done.
Standard input is a file
Every program starts with three streams. You have used two of them: standard output, where print writes, and standard error, where errors go. The third is standard input, what the program reads. Python gives it to you as sys.stdin, and it behaves like the files you opened in Python in Practice Chapter 5: a for loop over it gives one line at a time, each still ending in "\n", and the loop ends when the input ends.
for line in sys.stdin:
question = line.strip()
In the
chat_command reads from a stdin argument rather than from sys.stdin directly. main passes sys.stdin unless its caller passes another file. A test passes text that it typed in advance, as an io.StringIO, an object from Python’s io module that a for loop reads line by line, exactly like a file. That is how stages.py and the graders type for you.
chat_command
chat_command(*, client, budget, sleep, stdin):
Start with an empty history:
history = [].For each
lineinstdin, takequestion = line.strip().strip()removes the newline and any spaces around the question.If the question is empty, the person typed an empty line: stop the loop with
break. Nothing after that line is read.Otherwise ask it:
result = chat_turn(history, question, client=client, budget=budget, sleep=sleep), then keep the new history withhistory = result[1], so the next question carries the conversation so far.
chat_command returns nothing and prints nothing itself: chat_turn already printed the answer, followed by a newline. It catches nothing either. A broken stream raises StreamBroken after the text that arrived was printed, and a budget that cannot cover the next turn raises BudgetExceeded; Lesson 6 turns both into exit codes. Every turn draws on the same budget dictionary, so a long chat stops when the run’s budget is used up.
If the input ends without an empty line, the for loop ends on its own and the chat is over too.
Run it
When chat_command is written, run python stages.py. Lesson 5’s block calls it with io.StringIO("What does len() return?\nExplain Python lists in detail.\n\n"), two questions and an empty line:
Lesson 5: chat_command, typing two questions and then an empty line
len() returns the number of items in a sequence or collection, such as the characters in a string or the elements in a list.
A list is an ordered collection that can hold any mix of values. You create one with square brackets, such as scores = [90, 72, 85]. Each item has a position, starting at 0, so scores[0] is 90 and scores[-1] is the last item. Slicing takes a range: scores[0:2] gives a new list with the first two items. Lists are mutable, which means you can change them after creating them: scores.append(60) adds an item to the end, scores.remove(72) removes the first matching value, and scores[1] = 75 replaces an item in place. Because a list is changed in place, two names that refer to the same list both see the change. Use list.copy() when you need an independent list. len(scores) tells you how many items it holds.
(tokens used: 345)
Lesson 6: main and its exit codes
--- main(['ask', 'How do I make an independent copy of a list?'])
Run stopped at the next function to build: main is built in Lesson 6.
The questions themselves are not shown, because stages.py typed them; you only see the two streamed answers.
Once Lesson 6 has added the line that runs main, you can chat in the terminal. The program shows no prompt, so after you start it, type a question and press Enter, read the answer as it streams, and end the chat with an empty line. Pressing Ctrl+D at the start of a line ends the input and the chat too.
$ python study_assistant.py chat
What does len() return?
len() returns the number of items in a sequence or collection, such as the characters in a string or the elements in a list.
$
Why does chat_command read from its stdin argument instead of from sys.stdin?
stdin holds "What does len() return?\n\nExplain Python lists.\n". What does chat_command do?
Task
In study_assistant.py, write chat_command(*, client, budget, sleep, stdin):
Start with
history = [].For each
lineinstdin, takequestion = line.strip(). If the question is empty, stop the loop.Otherwise call
result = chat_turn(history, question, client=client, budget=budget, sleep=sleep)and sethistory = result[1].
Return nothing, print nothing yourself and catch nothing: chat_turn prints each answer as it streams. Run python stages.py to see two typed questions answered, then check your work.