Skip to main content

VLM Run Hub for various industry-specific schemas

Project description

VLM Run Hub

Structured extraction for VLMs.

This is a repository of schemas for structured ETL on visual domains including:

📄 Documents

  • SEC Filings
  • Earnings tables
  • Other PDFs

🖼️ Images

  • Satellite imagery
  • Product images
  • Sports

🎥 Videos (coming soon!)

💡 Motivation

Frontier LLM APIs like GPT4 and Claude now support structured outputs (usually referred to as "JSON mode") in addition to the more familiar chat interface. This is particularly powerful for visual data (especially documents) because it allows us to extract information in a declarative manner (i.e. we just list the fields we care about and let the VLM do the rest).

We represent schemas as a Pydantic class of typed key-value pairs. Fields can be primitives (str, int, etc.) or nested TypeClasses and include a prompt indicating how that field should be populated from the input. In short, they are a data structure that gets filled out by the VLM of your choosing based on your input and instructions.

Good schemas are a product of 1. a good hierarchy of types/fields and 2. good prompts. This repo is meant to be an authoritative source for both across a variety of input and use-cases, while being agnostic to the VLM provider so it can be used with multiple models.

🚀 Usage

With OpenAI/Instructor

import instructor
from openai import OpenAI
from pydantic import BaseModel
from typing import Type

from vlmrun.hub.utils import encode_image
from vlmrun.hub.schemas.document.invoice import Invoice

instructor_client = instructor.from_openai(
    OpenAI(),
    mode=instructor.Mode.MD_JSON,
)

image_url = 'YOUR_IMAGE_URL'
response_model = Invoice # or any other schema
response = instructor_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": sample.prompt,
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": encode_image(image_url, format="JPEG")
                    },
                    "detail": "auto",
                }
            ],
        },
    ],
    response_model=response_model,
    temperature=0,
)

With VLM Run

import requests

from vlmrun.hub.schemas.document.invoice import Invoice
from vlmrun.hub.utils import encode_image, remote_image

VLM_API_URL = "VLM_API_URL"
VLM_API_KEY = "VLM_API_KEY"

invoice_url = "YOUR_INVOICE_URL"
invoice_image = remote_image(invoice_url)
domain = "document.invoice"

json_data = {
    "file_id": invoice_url,
    "image": encode_image(invoice_image, format="JPEG"),
    "json_schema": Invoice.model_json_schema(),
    "model": "vlm-1",
    "domain": domain,
}

response = requests.post(
    f"{VLM_API_URL}/v1/image/generate",
    json=json_data,
    headers={"Authorization": f"Bearer {VLM_API_KEY}"},
)

Locally with Ollama

from pydantic import BaseModel
from typing import Type
from ollama import chat

from vlmrun.hub.utils import encode_image
from vlmrun.hub.schemas.document.invoice import Invoice

response_model = Invoice
prompt = "YOUR_PROMPT"

chat_response = chat(
    model="llama3.2-vision:11b",
    format=response_model.model_json_schema(),
    messages=[
        {
            "role": "user",
            "content": prompt,
            "images": [
                encode_image(img, format="JPEG").split(",")[1]
                for img in sample.images
            ],
        },
    ],
    options={
        "temperature": 0
    },
)
response: Type[BaseModel] = response_model.model_validate_json(
    chat_response.message.content
)

How is this repo organized?

The hub is organized into a taxonomy of industries and domains, broken down into subcategories for more specific inputs. We hope to expand to more domains in the near future with the help of the developer community.

🤝 How can I contribute?

We welcome and encourage contributions from the developer community, and we have a few guidelines around writing good schemas:

Be as specific as possible without being verbose

Be specific about the name, type and prompt for each field. The clearer the relationship between the input and the field, the better the result tends to be. Prompts should be concise (1-2 sentences) and to the point. Longer prompts can lead to slower inference times.

Use nested schemas where appropriate

If a field is a complex object, use a TypeClass to represent it.

DRY: Don't repeat yourself

Try to place new schemas in the appropriate location and reuse other schemas as subtypes where possible. The goal is to provide a type system for a variety of visual inputs without needing to duplicate information for similar use cases.

📬 Reach out

We'd love to hear from you if you have any questions or feedback!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vlmrun_hub-0.1.1.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vlmrun_hub-0.1.1-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file vlmrun_hub-0.1.1.tar.gz.

File metadata

  • Download URL: vlmrun_hub-0.1.1.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for vlmrun_hub-0.1.1.tar.gz
Algorithm Hash digest
SHA256 17e94ed419d58d493fa9f00faff56b6c3d1cc32494e634c4c596f471c0e70e02
MD5 f115ec72bfca42e83cdde9a7ab5212a2
BLAKE2b-256 76aab629d9958bd66f64ebfce8def39ec467b865e4b8160494ce0477622d7388

See more details on using hashes here.

File details

Details for the file vlmrun_hub-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: vlmrun_hub-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for vlmrun_hub-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8ab4a4c6cd38d9d760e4438b0196d57418fe274360af9c1db2b127ef3997cefa
MD5 82a5a3d5e5764fa53786353160bd5139
BLAKE2b-256 84928c745e6dff470a17782cb04553afe525f98f650d3a8106a38680bef802eb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page