Member-only story
AI_PARSE_DOCUMENT() — get your PDF invoices into the database
The new ai_parse_document() function enables us to detect text in various document types, including PDFs and images. I decided to test it by converting PDFs with invoices to a database-friendly format. To read PDF documents, we need to read files in binary format and then use the ai_parse_document function, which converts them into a string value and stores it in a table.
If you are not yet a member of Medium, you can access the extended version with video on the SunnyData blog for free.
Here is the basic syntax to achieve it:
WITH all_files AS (
SELECT
path,
content
FROM
READ_FILES("/Volumes/hub/default/invoices", format => 'binaryFile')
)
-- Parse the files using ai_parse document
SELECT
path,
ai_parse_document(content) as parsed
FROM
all_files;The easiest way to create the above syntax is to go to “Agents” and set the PDF reader from our volume. I uploaded 3 example PDFs to the volume. “Use PDFs in Agent Bricks” will create a ready SQL script to read, and the script will be much longer in the above example, as it will also generate “cleaned” source data, which looks like a markdown invoice.
