Start learning free

LESSON 33 OF 129 · AI ENGINEERING: STRUCTURED OUTPUT

Many Messages at Once

Practice

Each call to the model takes a moment, and for most of that moment your program only waits for the answer. A for loop over eight messages asks, waits, asks, waits: eight waits in a row.

A thread pool keeps a few workers. Each worker makes its own call, so the waits overlap. pool.map takes a function (without parentheses) and a list, and calls the function once for every item. Here is a small pool on another job, measuring words:

with ThreadPoolExecutor(max_workers=2) as pool:
    lengths = list(pool.map(len, ["tea", "lamp", "mug"]))
print(lengths)   # [3, 4, 3]

Two details matter. max_workers is a limit: an API accepts only so many requests at once, and four hundred sent in the same moment would mostly come back as 429 errors. And the answers don't arrive in the order the messages went out, but pool.map still gives the results in the order of the inputs.

Your finished program must print each label next to its own message. The last three lines come from the check and show what happened behind the scenes:

refund: I want my money back for the lamp. It arrived broken.
shipping: Where is my package? I ordered ten days ago.
product: Does the teapot work on an induction stove?
refund: Please refund order 3. I bought it by mistake.
shipping: Can you ship to Canada?
other: Hi! Do you have a job for a student this summer?
product: Is the notebook lined or blank?
shipping: My plant arrived two weeks late.
Model calls: 8
Most calls running at the same time: 4
The answers arrived in the inbox's order: no

Your task: finish classify so it returns one message's category. Then open a pool of four workers, map classify over inbox, turn the results into a list named categories, and print each label with its message.


Assignment
Follow the Coding Tutorial to classify the eight messages with a thread pool of four workers and print the labels in the inbox's order.


Hint
Pass the function itself to pool.map: classify, not classify(). The results come in the inputs' order, so categories[i] is the label of inbox[i].


STEP 1 OF 7
    Ctrl + Enter to check