Start learning free

LESSON 12 OF 129 · AI ENGINEERING: CALLING A MODEL

Let It Think in Python

Practice

A model writes its answer word by word. On a question with a catch, it can settle on an answer before it has worked the numbers out. Thinking gives it room: the model first writes working notes for itself, then the answer.

A question with a catch: a ticket costs $8, and groups of 10 or more get $1 off each ticket. Nine friends pay 9 × $8 = $72, not $63, because the rule doesn't apply to them. A quick answer easily misses that. This lesson's question hides the same kind of catch in a shipping rule.

Thinking is a setting of the call. With it on, the reply has two blocks: a thinking block with the notes, then a text block with the answer. So the first block is no longer the answer: you look at each block's type.

Thinking has a price. Thinking tokens are output tokens: you pay for each one, and you wait while they are written. The starting code already asks the quick way. Your finished program must print:

Quick answer: They pay $46, and shipping is free.
Output tokens: 9
Block: thinking
Thinking: 2 Teapots at $22 each are $44. The Pen adds $2, so the products cost $46. Shipping is free only for orders over $50, and $46 is not over $50, so shipping costs $5. $46 + $5 = $51.
Block: text
Answer: They pay $51: $46 for the products plus $5 for shipping, because the order is not over $50.
Output tokens: 68

Your task: make a thinking setting with the type "adaptive" and ask the same question again with it and max_tokens=1000. Loop over the reply's blocks: print each block's type, the notes of the thinking block and the answer of the text block. Then print the output tokens.


Assignment
Follow the Coding Tutorial to ask the question again with thinking on and print each block and the output tokens.


Hint
The thinking block's notes are in block.thinking, and the answer is in block.text. The last print goes after the loop, not inside it.


STEP 1 OF 9
    Ctrl + Enter to check

    Ai Thinking in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free