Start learning free

LESSON 11 OF 129 · AI ENGINEERING: CALLING A MODEL

Hand It a PDF

Practice

A supplier sent the shop an invoice as a PDF, and somebody has to type its numbers into the system. A model can read the file for you. But a request is text and a PDF is bytes, so the file travels as base64: text made from the file's bytes.

Bytes to text: open a file in mode "rb" to read its bytes. Then base64.b64encode rewrites the bytes with letters and digits, and .decode("utf-8") makes an ordinary string. Here it is on two bytes instead of a file:

import base64

text = base64.b64encode(b"Hi").decode("utf-8")
print(text)   # SGk=

The model must know what that long string is. A message's content can be a list of blocks, and each block says what it is in "type". A PDF goes in a document block. Its source says how the file is written ("base64"), what kind of file it is ("application/pdf") and holds the data. A text block with your question comes after it.

The reply is plain text, as always. Reading a file costs input tokens: every page counts. Your program must print:

The order number is 1042 and the total is $64.
Input tokens: 1514

Your task: the client, MODEL and question are ready. Read invoice.pdf as bytes and turn it into base64 text. Build the source, the document block, the content list and one user message. Send it, print the reply's text, then print the input tokens.


Assignment
Follow the Coding Tutorial to send the invoice PDF to the model and print the order number, the total and the input tokens.


Hint
The document block comes first in the content list, the text block second. The message's "content" is that list, not a plain string.


STEP 1 OF 9
    Ctrl + Enter to check