In this section, we will see how to use the methods of the documents client.

Create Messages

Returns
DocumentMessage Object

A DocumentMessage object with the messages created from the document.

from uiform import UiForm
from openai import OpenAI

uiclient = UiForm()
doc_msg = uiclient.documents.create_messages(
    document = "freight/booking_confirmation.jpg",
    modality = "text",
    image_settings = {
        "correct_image_orientation": True,
        "dpi": 72,
        "image_to_text": "ocr",
        "browser_canvas": "A4"
    }
)
Use doc_msg.items to have a list of [PIL.Image.Image | str] objects

Correct image orientation

Returns
PIL.Image.Image

The orientation-corrected image as a PIL Image object

from uiform import UiForm
from openai import OpenAI

uiclient = UiForm()
image = uiclient.documents.correct_image_orientation(
    document = "freight/booking_confirmation.jpg",
)

Extractions

Returns
ParsedChatCompletion

An OpenAI ParsedChatCompletion object with the extracted data.


from uiform import UiForm

uiclient = UiForm()

doc_msg = uiclient.documents.extractions.parse(
    document = "freight/booking_confirmation.jpg", 
    model="gpt-4o-mini",
    json_schema = {
      'X-SystemPrompt': 'You are a useful assistant.',
      'properties': {
          'name': {
              'X-FieldPrompt': 'Provide a descriptive and concise name for the event.',
              'description': 'The name of the calendar event.',
              'title': 'Name',
              'type': 'string'
          },
          'date': {
              'X-FieldPrompt': 'Specify the event date in YYYY-MM-DD format.',
              'description': 'The date of the calendar event in ISO 8601 format.',
              'title': 'Date',
              'type': 'string'
          }
      },
      'required': ['name', 'date'],
      'title': 'CalendarEvent',
      'type': 'object'
    },
    modality="text"
)

Templates

templates.documentai

The Document AI templates allow you to extract structured data from documents using predefined templates inspired by Google Document AI. These templates are designed to handle common document types like invoices, receipts, IDs and more.

templates.documentai.parse

templates.documentai.parse
function

Extract structured data from a document using a predefined template inspired by Google Document AI

Response
DocumentExtractResponse

The extraction response containing the structured data.


from uiform import UiForm

uiclient = UiForm()

doc_msg = uiclient.documents.templates.documentai.parse(
    document = "freight/booking_confirmation.jpg", 
    model="gpt-4o-mini",
    template="invoice",
    modality="text"
)